pjfanning commented on code in PR #1581:
URL: https://github.com/apache/poi/pull/1581#discussion_r4647953710
##########
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");
Review Comment:
This rejects any ds:Manifest anywhere in the signature part, including ones
SignedInfo never references (e.g. in unsigned XAdES properties, or literal
content in a transform). A valid signature from a third-party signer with such
a Manifest verified before this PR and now fails. Could the check be limited to
Manifests inside elements that SignedInfo actually references?
##########
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:
Could the warning include which Manifest / parent element was out of place
(e.g. the parent's local name and the Manifest's Id), like the neighbouring
relationship warning names the rel id and part? Otherwise a false rejection of
a third-party signature is hard to diagnose from the log.
##########
poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/SignaturePart.java:
##########
@@ -146,7 +147,7 @@ public boolean validate() {
XMLSignatureFactory xmlSignatureFactory =
signatureInfo.getSignatureFactory();
XMLSignature xmlSignature =
xmlSignatureFactory.unmarshalXMLSignature(domValidateContext);
- boolean valid = xmlSignature.validate(domValidateContext) &&
hasSignedRelationships(doc, xmlSignature);
+ boolean valid = xmlSignature.validate(domValidateContext) &&
hasValidatedManifests(doc) && hasSignedRelationships(doc, xmlSignature);
Review Comment:
A more general alternative to a document-wide scan for Manifest placement:
require each same-document SignedInfo reference of Type `ds#Object` /
`ds#Manifest` to resolve to an `XMLObject` in `xmlSignature.getObjects()` (or a
Manifest inside one). That would also cover any other security-relevant content
we read from a referenced object in future, rather than needing a new ad-hoc
check per structure.
##########
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:
This passes for any reason `verifySignature()` returns false (e.g. the
string edit breaking a SignedInfo digest, or secure validation rejecting the
shape), so it doesn't prove `hasValidatedManifests` is what rejects it. Could
we add a companion case that wraps without tampering `document.xml` and checks
that `xmlSignature.validate` alone passes, or assert the logged warning, so the
new guard itself is pinned?
##########
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:
The splice finds the end with the first `</Object>` after the start and only
matches the unprefixed, double-quoted `<Object Id="idPackageObject"` form. If
the test file is ever re-signed with a `ds:` prefix, single quotes or a nested
Object, this yields malformed or different XML and the test fails (or passes)
for the wrong reason. Doing the edit via DOM would be more robust.
--
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]