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 1924da009d RelationshipTransformService: only treat
relationships-namespace elements as relationships (#1475)
1924da009d is described below
commit 1924da009d04421e11f1804ce52e6d1bcc8d6990
Author: PJ Fanning <[email protected]>
AuthorDate: Sat Oct 24 23:43:17 2026 +0100
RelationshipTransformService: only treat relationships-namespace elements
as relationships (#1475)
Follow-up to #1467. The transform matched Relationship elements in any
namespace, but PackageRelationshipCollection only reads those in the
relationships namespace, as the spec defines them. A foreign element named
Relationship with a signed Id changed the digest, so a valid signature
failed to verify. Also make the comment say which case keeping all elements
covers, and share the signed-docx rewriting between the tests.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../services/RelationshipTransformService.java | 8 +++-
.../poi/poifs/crypt/dsig/TestSignatureInfo.java | 55 ++++++++++++++++------
2 files changed, 47 insertions(+), 16 deletions(-)
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 0307de7878..a05119f8e7 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
@@ -52,6 +52,8 @@ import javax.xml.crypto.dsig.spec.TransformParameterSpec;
import org.apache.jcp.xml.dsig.internal.dom.ApacheNodeSetData;
import org.apache.logging.log4j.Logger;
import org.apache.poi.logging.PoiLogManager;
+import org.apache.poi.openxml4j.opc.PackageNamespaces;
+import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.util.SuppressForbidden;
import org.apache.xml.security.signature.XMLSignatureNodeInput;
@@ -205,9 +207,11 @@ public class RelationshipTransformService extends
TransformService {
// keep only those relationships which id is registered in the
sourceIds - all of them,
// at any depth: the package model also reads nested elements and
resolves a duplicated
- // id to the last one, so dropping one here would leave an unsigned
target out of the digest
+ // id to the last one, so dropping a duplicate of a signed id here
would leave its target
+ // out of the digest. Only elements in the relationships namespace are
relationships,
+ // as in PackageRelationshipCollection
Element root = doc.getDocumentElement();
- NodeList nl = root.getElementsByTagNameNS("*", "Relationship");
+ NodeList nl =
root.getElementsByTagNameNS(PackageNamespaces.RELATIONSHIPS,
PackageRelationship.RELATIONSHIP_TAG_NAME);
List<Element> rsList = new ArrayList<>();
for (int i=0; i<nl.getLength(); i++) {
Element el = (Element)nl.item(i);
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 2ebed5771a..0cc4e24e16 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
@@ -432,6 +432,40 @@ class TestSignatureInfo {
// pointing to an unsigned part - the package model resolves rId1 to
this one
String rel = String.format(Locale.ROOT, wrapper, "<Relationship
Id=\"rId1\" Type=\"" +
PackageRelationshipTypes.CORE_DOCUMENT + "\"
Target=\"word/tampered.xml\"/>");
+
+ try (OPCPackage pkg = OPCPackage.open(modifySignedDocx(rel, true));
+ 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");
+ }
+ }
+
+ @Test
+ void testForeignRelationshipElementIsIgnored() throws Exception {
+ // an element named Relationship in another namespace is not a
relationship,
+ // neither for the package model nor for the relationship transform
+ String rel = "<y:Relationship xmlns:y=\"urn:y\" Id=\"rId1\"
Target=\"word/other.xml\"/>";
+
+ try (OPCPackage pkg = OPCPackage.open(modifySignedDocx(rel, false));
+ XWPFDocument doc = new XWPFDocument(pkg)) {
+ assertEquals("Hello world", doc.getParagraphs().get(0).getText());
+
+ 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"
+ */
+ private static InputStream modifySignedDocx(String rel, boolean
addTamperedPart) throws IOException {
UnsynchronizedByteArrayOutputStream bos =
UnsynchronizedByteArrayOutputStream.builder().get();
try (ZipFile zf =
ZipFile.builder().setFile(testdata.getFile("hello-world-signed.docx")).get();
ZipArchiveOutputStream zos = new ZipArchiveOutputStream(bos)) {
@@ -442,7 +476,7 @@ class TestSignatureInfo {
data = IOUtils.toByteArray(is);
}
String name = ze.getName();
- if ("[Content_Types].xml".equals(name)) {
+ if (addTamperedPart && "[Content_Types].xml".equals(name)) {
data = new String(data,
StandardCharsets.UTF_8).replace("</Types>",
"<Override PartName=\"/word/tampered.xml\"
ContentType=\"" +
XWPFRelation.DOCUMENT.getContentType() +
"\"/></Types>").getBytes(StandardCharsets.UTF_8);
@@ -457,20 +491,13 @@ class TestSignatureInfo {
zos.closeArchiveEntry();
}
assertNotNull(document);
- zos.putArchiveEntry(new ZipArchiveEntry("word/tampered.xml"));
- zos.write(document.replace("Hello world",
"Tampered").getBytes(StandardCharsets.UTF_8));
- zos.closeArchiveEntry();
- }
-
- try (OPCPackage pkg = OPCPackage.open(bos.toInputStream());
- 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");
+ if (addTamperedPart) {
+ zos.putArchiveEntry(new ZipArchiveEntry("word/tampered.xml"));
+ zos.write(document.replace("Hello world",
"Tampered").getBytes(StandardCharsets.UTF_8));
+ zos.closeArchiveEntry();
+ }
}
+ return bos.toInputStream();
}
@Test
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]