fix: Support nested enveloped signature location (#525) - #526
Conversation
Update XPath query to find Signature elements at any depth within the document, not just direct children. This fixes an issue where signatures nested within other elements were not properly detected and removed.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (4)
Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review. 📝 WalkthroughWalkthroughThis change fixes nested enveloped-signature processing. It updates signature-node state handling, maps nested signatures into cloned XML during canonicalization, restricts ChangesNested enveloped-signature handling
Estimated code review effort: 3 (Moderate) | ~25 minutes Merge Risk: ⚪ Minimal · up to Nested enveloped signatures can now be removed from signed content at their actual location while preserving deeper signatures and restoring signing state after failures. The covered signing, verification, and tamper scenarios indicate no current merge-blocking risk. Sequence Diagram(s)sequenceDiagram
participant Client
participant SignedXml
participant EnvelopedSignature
participant Verifier
Client->>SignedXml: computeSignature with nested location
SignedXml->>EnvelopedSignature: canonicalize referenced node
EnvelopedSignature->>EnvelopedSignature: remove nested Signature node
SignedXml-->>Client: return signed XML
Client->>Verifier: checkSignature
Verifier-->>Client: return verification result
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Out of Scope Changes checkExplanation The rollback behavior in
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
) The `enveloped-signature` transform requires the signature element to be removed from the content being signed. Previously, when a signature was placed within a parent node that was subsequently signed, the transform might fail to identify or remove the correct signature node within the canonicalization context. This update ensures the correct signature node within the cloned document is identified and excluded, allowing valid enveloped signatures even when they are nested within the signed content.
The enveloped signature transform's XPath query is updated to target only direct child Signature elements. This ensures that when signing content containing nested signatures, only the immediate enveloping signature is removed, preserving any deeply nested signatures. A rollback mechanism is added to `SignedXml.computeSignature` to restore the `signatureNode` property if `addAllReferences` fails. This prevents the `SignedXml` instance from being left in an inconsistent state after an unsuccessful signing attempt.
|
@shunkica , I've fixed a few regressions here. I think this is ready to merge. What do you think? |
|
LGTM. Updated PR body. |
Description
This PR fixes an issue where the enveloped signature transformation was unable to find and remove Signature elements that were nested within other elements in the document.
Changes
Updated the XPath query in from to to search for Signature elements at any depth, not just direct childrenAdded a test case demonstrating the issue with nested signaturescomputeSignaturesetssignatureNodebefore processing references, and restores it if reference processing throwsgetCanonXmlmaps the signature element into the cloned subtree so the enveloped-signature transform removes exactly the signature being created, at any depthEnvelopedSignatureremoves the mapped node by identity during signing; verification still matches bySignatureValue/rootand/root/container, tampering, and state recovery after a failed signRelated Issue
Fixes #525
Testing
Added a test case that verifies signatures can be validated when the Signature element is nested within other elements in the document structure.
Summary by CodeRabbit
Bug Fixes
Tests