naruto-lgtm commented on code in PR #1581:
URL: https://github.com/apache/poi/pull/1581#discussion_r4651347812
##########
poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/SignaturePart.java:
##########
@@ -184,6 +185,24 @@ public boolean validate() {
}
}
+ /**
+ * Checks that each ds:Manifest is the content of a ds:Object of the
signature, as only those
+ * manifests are validated - a signed object moved into another element
keeps its digest,
+ * but the references of its manifest, i.e. of the package parts, would
not be checked
+ */
+ private static boolean hasValidatedManifests(Document doc) {
+ Element signature = doc.getDocumentElement();
+ NodeList manifests = doc.getElementsByTagNameNS(XML_DIGSIG_NS,
"Manifest");
+ for (int i=0; i<manifests.getLength(); i++) {
+ Node obj = manifests.item(i).getParentNode();
+ if (obj.getParentNode() != signature ||
!XML_DIGSIG_NS.equals(obj.getNamespaceURI()) ||
!"Object".equals(obj.getLocalName())) {
Review Comment:
Done. The warning now names the local name of the referenced element and its
Id, e.g. "the manifest in the referenced Object with the Id idPackageObject is
not validated, as it isn't the content of a ds:Object of the signature".
##########
poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java:
##########
@@ -642,6 +642,31 @@ protected void addManifestReferences(SignatureInfo
signatureInfo, List<Reference
}
}
+ @Test
+ void testWrappedPackageObject() throws Exception {
+ // the signed package object is moved into another ds:Object, where it
still has the digest
+ // the SignedInfo references, but its manifest isn't a ds:Manifest of
the signature anymore
+ InputStream modified = rewriteSignedDocx(entries -> {
+ String sig = new String(entries.get("_xmlsignatures/sig1.xml"),
StandardCharsets.UTF_8);
+ int start = sig.indexOf("<Object Id=\"idPackageObject\"");
+ int end = sig.indexOf("</Object>", start) + "</Object>".length();
Review Comment:
Changed to a DOM edit. `rewriteSignature` parses sig1.xml, hands the
ds:Signature element to the test and serializes it back with
`XMLHelper.newTransformer()` (INDENT is off there), so the canonical form of
the subtrees is unchanged. The object is found by Id over
`getElementsByTagNameNS`, and the wrapper takes the prefix of the element it
wraps, so a `ds:` prefixed or re-signed file exercises the same thing instead
of silently matching nothing.
##########
poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java:
##########
@@ -642,6 +642,31 @@ protected void addManifestReferences(SignatureInfo
signatureInfo, List<Reference
}
}
+ @Test
+ void testWrappedPackageObject() throws Exception {
+ // the signed package object is moved into another ds:Object, where it
still has the digest
+ // the SignedInfo references, but its manifest isn't a ds:Manifest of
the signature anymore
+ InputStream modified = rewriteSignedDocx(entries -> {
+ String sig = new String(entries.get("_xmlsignatures/sig1.xml"),
StandardCharsets.UTF_8);
+ int start = sig.indexOf("<Object Id=\"idPackageObject\"");
+ int end = sig.indexOf("</Object>", start) + "</Object>".length();
+ assertTrue(start > 0);
+ sig = sig.substring(0, start) + "<Object>" + sig.substring(start,
end) + "</Object>" + sig.substring(end);
+ entries.put("_xmlsignatures/sig1.xml",
sig.getBytes(StandardCharsets.UTF_8));
+ entries.put("word/document.xml",
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(), "the manifest of the signed
package object must be validated");
Review Comment:
Added `testWrappedPackageObjectKeepsTheSignedDigests`: same wrapping,
word/document.xml untouched, and it asserts that unmarshalling and validating
the signature on its own (validateManifests and secure validation on, same
URIDereferencer) still returns true, before asserting `verifySignature()` is
false. So the digests and the signature value are demonstrably intact and the
new check is what rejects the document. It fails on the previous commit with
expected <false> but was <true>.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]