This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new a4f27cd4c9 RelationshipTransformService: remove the content of signed 
Relationship elements (#1503)
a4f27cd4c9 is described below

commit a4f27cd4c9f35b197048d23f1ed4dfe2ab79051a
Author: PJ Fanning <[email protected]>
AuthorDate: Wed Oct 28 21:32:53 2026 +0000

    RelationshipTransformService: remove the content of signed Relationship 
elements (#1503)
    
    The relationships transform digests Relationship elements without their
    content, as .NET's System.IO.Packaging does by writing them as empty
    elements. A pretty-printed <Relationship ...>\r\n</Relationship> no longer
    breaks the signature - the CR/LF stripping removed in #1498 used to hide
    this case. Also simplify the OOXMLURIDereferencer part stream handling.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../poi/poifs/crypt/dsig/OOXMLURIDereferencer.java |  6 +-----
 .../services/RelationshipTransformService.java     |  6 ++++++
 .../poi/poifs/crypt/dsig/TestSignatureInfo.java    | 24 ++++++++++++++++++++++
 3 files changed, 31 insertions(+), 5 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java
 
b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java
index f48035f8d9..9afd7a42b4 100644
--- 
a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java
+++ 
b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java
@@ -25,7 +25,6 @@
 package org.apache.poi.poifs.crypt.dsig;
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 
@@ -102,14 +101,11 @@ public class OOXMLURIDereferencer implements 
URIDereferencer {
         // .rels parts are not modified: the relationship transform drops the 
whitespace between
         // relationships (e.g. of office 2007 pretty-printed .rels files), and 
removing line breaks
         // inside an attribute would digest another value than the package 
reads
-        InputStream dataStream;
         try {
-            dataStream = part.getInputStream();
+            return new OctetStreamData(part.getInputStream(), uri.toString(), 
null);
         } catch (IOException e) {
             throw new URIReferenceException("I/O error: " + e.getMessage(), e);
         }
-
-        return new OctetStreamData(dataStream, uri.toString(), null);
     }
 
     private PackagePart findPart(URI uri) {
diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/services/RelationshipTransformService.java
 
b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/services/RelationshipTransformService.java
index a05119f8e7..054c2c85b9 100644
--- 
a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/services/RelationshipTransformService.java
+++ 
b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/services/RelationshipTransformService.java
@@ -229,8 +229,14 @@ public class RelationshipTransformService extends 
TransformService {
             root.removeChild(root.getFirstChild());
         }
 
+        // the transform removes any content of the Relationship elements, 
e.g. whitespace of a
+        // pretty-printed <Relationship ...></Relationship> - Office/.NET 
digest them as empty elements.
+        // A nested relationship which is kept is moved to the root, before or 
after its parent is emptied
         rsList.sort(Comparator.comparing(el -> el.getAttribute("Id")));
         for (Element el : rsList) {
+            while (el.hasChildNodes()) {
+                el.removeChild(el.getFirstChild());
+            }
             root.appendChild(el);
         }
 
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java
 
b/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java
index 7aa8d46786..0f8c26acb8 100644
--- 
a/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java
+++ 
b/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java
@@ -527,6 +527,30 @@ class TestSignatureInfo {
         }
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = {
+        // line breaks between the attributes
+        "\r\n    Target=\"word/document.xml\"\r\n/>",
+        // content of the Relationship element is not part of the transform 
output
+        "Target=\"word/document.xml\">\r\n</Relationship>",
+        "Target=\"word/document.xml\"><x:ext 
xmlns:x=\"urn:x\">text</x:ext></Relationship>"
+    })
+    void testSignedRelationshipFormatting(String reformatted) throws Exception 
{
+        // the relationship transform digests the relationships, not how they 
are written
+        InputStream modified = rewriteSignedDocx(entries -> {
+            String target = "Target=\"word/document.xml\"/>";
+            assertThat(new String(entries.get("_rels/.rels"), 
StandardCharsets.UTF_8), containsString(target));
+            entries.put("_rels/.rels", replace(entries.get("_rels/.rels"), 
target, reformatted));
+        });
+
+        try (OPCPackage pkg = OPCPackage.open(modified)) {
+            SignatureInfo si = new SignatureInfo();
+            si.setOpcPackage(pkg);
+            si.setSignatureConfig(new SignatureConfig());
+            assertTrue(si.verifySignature());
+        }
+    }
+
     /**
      * Copies hello-world-signed.docx, adding {@code rel} to _rels/.rels and, 
if {@code addTamperedPart}
      * is set, a word/tampered.xml main part which says "Tampered" instead of 
"Hello world"


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to