naruto-lgtm commented on code in PR #1595:
URL: https://github.com/apache/poi/pull/1595#discussion_r4731046346
##########
poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/SignatureInfo.java:
##########
@@ -201,14 +201,22 @@ public void setUriDereferencer(URIDereferencer
uriDereferencer) {
}
/**
- * @return true, if first signature part is valid
+ * @return true, if the package has at least one signature and all of its
signatures are valid
*/
public boolean verifySignature() {
initXmlProvider();
//
http://www.oracle.com/technetwork/articles/javase/dig-signature-api-140772.html
- // only validate first part
- Iterator<SignaturePart> iter = getSignatureParts().iterator();
- return iter.hasNext() && iter.next().validate();
+ // a package can hold several signatures, e.g. when a document is
co-signed, and which one
+ // comes first follows from the ids of the signature origin's
relationships, so validating
+ // one of them says nothing about the state of the others
+ boolean signed = false;
+ for (SignaturePart sp : getSignatureParts()) {
Review Comment:
You're right on both counts. `getRelatedPart` throws
`IllegalArgumentException` when the target part is missing and the iterator
only caught `InvalidFormatException`, so that escaped out of
`verifySignature()`; and a bad relationship in last position turned into
`NoSuchElementException`, because `next()` called `hasNext()` again after
skipping it.
The iterator now resolves the next part in `hasNext()` and records when a
signature relationship didn't resolve to one, and `verifySignature()` returns
false if anything was recorded, so the position no longer decides whether it's
noticed. `getSignatureParts()` still just skips those entries, as before, minus
those two exceptions.
`testUnresolvableSignatureIsNotValid` covers a missing sig2.xml part listed
after the valid signature, the same listed before it (rId0, so it sorts first),
and an external target.
##########
poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/SignatureInfo.java:
##########
@@ -201,14 +201,22 @@ public void setUriDereferencer(URIDereferencer
uriDereferencer) {
}
/**
- * @return true, if first signature part is valid
+ * @return true, if the package has at least one signature and all of its
signatures are valid
*/
public boolean verifySignature() {
initXmlProvider();
//
http://www.oracle.com/technetwork/articles/javase/dig-signature-api-140772.html
- // only validate first part
- Iterator<SignaturePart> iter = getSignatureParts().iterator();
- return iter.hasNext() && iter.next().validate();
+ // a package can hold several signatures, e.g. when a document is
co-signed, and which one
+ // comes first follows from the ids of the signature origin's
relationships, so validating
+ // one of them says nothing about the state of the others
+ boolean signed = false;
+ for (SignaturePart sp : getSignatureParts()) {
+ if (!sp.validate()) {
Review Comment:
1. Kept the exception. A signature POI can't process shouldn't come back as
valid, and returning false instead would report a parser or algorithm gap as
"this document was tampered with", which is worse for a caller than a thrown
exception. It's now an `@throws EncryptedDocumentException` on the javadoc and
covered by `testSignatureWhichCannotBeProcessed` (a second part that isn't
well-formed XML).
2. Documented rather than changed. Making `extractConfig` fire only once
would break the per-signature path, where callers iterate `getSignatureParts()`
and read the config after each `validate()`. The javadoc now says the config
ends up describing the last valid signature rather than the first and can still
hold an earlier signer's details when the method returns false, and points at
`getSignatureParts()`/`SignaturePart.getSigner()` for the signer of a
particular signature.
--
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]