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 9cc871645f OOXMLURIDereferencer: don't strip line breaks from signed 
.rels parts (#1498)
9cc871645f is described below

commit 9cc871645fd834a8725cbf385d7b12e176d628a0
Author: Nayyar <[email protected]>
AuthorDate: Thu Oct 29 01:13:17 2026 +0530

    OOXMLURIDereferencer: don't strip line breaks from signed .rels parts 
(#1498)
    
    don't strip line breaks from signed .rels parts
    
    OOXMLURIDereferencer removed every CR and LF byte from .rels parts
    before the relationship transform digested them, while the package
    reads a line break inside an attribute value as a space. A line feed
    appended to a signed relationship's Target therefore left the digest
    unchanged but made the package resolve another part, e.g.
    /word/document.xml%20, with the signature still valid.
    
    The relationship transform already drops the whitespace between
    relationships, which the workaround was for, so pass .rels parts
    unchanged.
---
 .../poi/poifs/crypt/dsig/OOXMLURIDereferencer.java | 21 ++++--------------
 .../poi/poifs/crypt/dsig/TestSignatureInfo.java    | 25 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 17 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 f11ed57e21..f48035f8d9 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
@@ -36,8 +36,6 @@ import javax.xml.crypto.URIReference;
 import javax.xml.crypto.URIReferenceException;
 import javax.xml.crypto.XMLCryptoContext;
 
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
 import org.apache.logging.log4j.Logger;
 import org.apache.poi.logging.PoiLogManager;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@@ -101,24 +99,13 @@ public class OOXMLURIDereferencer implements 
URIDereferencer {
             }
         }
 
-        InputStream dataStream = null;
+        // .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();
-
-            // workaround for office 2007 pretty-printed .rels files
-            if (part.getPartName().toString().endsWith(".rels")) {
-                // although xmlsec has an option to ignore line breaks, 
currently this
-                // only affects .rels files, so we only modify these
-                try (UnsynchronizedByteArrayOutputStream bos = 
UnsynchronizedByteArrayOutputStream.builder().get()) {
-                    for (int ch; (ch = dataStream.read()) != -1; ) {
-                        if (ch == 10 || ch == 13) continue;
-                        bos.write(ch);
-                    }
-                    dataStream = bos.toInputStream();
-                }
-            }
         } catch (IOException e) {
-            IOUtils.closeQuietly(dataStream);
             throw new URIReferenceException("I/O error: " + e.getMessage(), e);
         }
 
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 9cb911ef5c..7aa8d46786 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
@@ -502,6 +502,31 @@ class TestSignatureInfo {
         }
     }
 
+    @Test
+    void testLineBreakInSignedRelationshipTarget() throws Exception {
+        // a line break in an attribute is read as a space, so the target 
becomes "word/document.xml "
+        // (stored as /word/document.xml%20) - another part than the signed one
+        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, "Target=\"word/document.xml\n\""));
+            entries.put("[Content_Types].xml", 
replace(entries.get("[Content_Types].xml"), "</Types>",
+                "<Override PartName=\"/word/document.xml%20\" ContentType=\"" +
+                XWPFRelation.DOCUMENT.getContentType() + "\"/></Types>"));
+            entries.put("word/document.xml%20", 
replace(entries.get("word/document.xml"), "Hello world", "Tampered"));
+        });
+
+        try (OPCPackage pkg = OPCPackage.open(modified);
+             XWPFDocument doc = new XWPFDocument(pkg)) {
+            assertEquals("Tampered", doc.getParagraphs().get(0).getText());
+
+            SignatureInfo si = new SignatureInfo();
+            si.setOpcPackage(pkg);
+            si.setSignatureConfig(new SignatureConfig());
+            assertFalse(si.verifySignature(), "signature must not cover the 
substituted main part");
+        }
+    }
+
     /**
      * 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