You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
refactor: select with the DOM instead of xpath for the library's own … - #666
xml-crypto evaluates a few fixed XPath expressions internally: finding signatures, the KeyInfo certificate, Algorithm attributes, SignedInfo, comments to strip, the enveloped signature, and the element a reference's ID points to during verification. This change does those lookups with DOM traversal instead. XPath the caller supplies is still evaluated with xpath, exactly as before.
It also adds optional ways to select nodes without writing an XPath string, for callers who already have them from the DOM:
addReference({ select }): a function given the parsed document that returns the elements to sign. Use it instead of xpath; passing both throws.
computeSignature(xml, { location: { reference } }): reference may also be a function that returns the node to place the signature relative to.
findAncestorNs(doc, element): accepts an Element as well as an XPath string.
Compatibility
No public API is removed or deprecated, and existing XPath inputs behave the same.
The DOM lookups return the same results as the expressions they replace, including:
document order;
matching names with local-name();
an ID attribute in any namespace, still counted toward the duplicate-ID check;
case-insensitive matching of the Algorithm attribute name;
searching from a detached node.
Descendants are walked iteratively, so deeply nested input doesn't recurse.
Testing
Ran npm run build, npm test and npm run lint.
Added test/dom-selection.spec.ts. It checks the new lookups against the XPath expressions they replace, the ID and duplicate-ID cases, and the new select and location.reference options, sync and async.
Ran the passport-wsfed-saml2 test suite against this branch. Its results match the current release.
Summary by CodeRabbit
New Features
Select XML elements to sign with a callback instead of an XPath expression. If the callback initially finds no input elements, it can be called again after signature insertion to find content in signature objects.
Choose signature placement with a callback that receives the parsed document; XPath-based placement remains supported.
Improvements
XML signature processing now uses document traversal to find signatures, certificates, and algorithms, and to resolve same-document references. Duplicate ID matches continue to be rejected.
No actionable comments were generated in the recent review.
Walkthrough
The change replaces several XPath searches with DOM traversal and adds callback options for reference selection and signature placement. XPath options remain available. Tests cover DOM searches, verification, callback-based signing, and invalid selections.
Changes: XML signature processing
Layer / File(s)
Summary
DOM traversal and verification src/utils.ts, src/enveloped-signature.ts, src/signed-xml.ts, test/dom-selection.spec.ts
DOM traversal helpers support signature discovery, certificate and algorithm lookup, ID resolution, canonicalization, and enveloped-signature processing. Tests compare DOM-based results with XPath behavior and cover ID cases.
Reference adds a select callback as an alternative to xpath. Signing uses the callback to find reference targets and rejects calls that provide both options. Documentation and tests cover callback selection, including a second evaluation after signature insertion when the initial selection finds no input elements.
location.reference accepts a callback that receives the parsed document and returns a node or null. Signature placement invokes the callback and reports when it returns no node.
sequenceDiagram
participant Caller
participant SignedXml
participant SelectCallback
Caller->>SignedXml: Call computeSignature
SignedXml->>SelectCallback: Pass parsed document to select
SelectCallback-->>SignedXml: Return selected elements
alt Initial selection finds no input elements
SignedXml->>SelectCallback: Call select again after signature insertion
SelectCallback-->>SignedXml: Return selected elements
end
Loading
Suggested reviewers:cjbarth
Merge Risk: Minimal · up to 30fe3
The DOM-selection refactor and the new select and location callbacks show no remaining concrete merge-blocking issues. The earlier leaf-node concern is fixed.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title "refactor: select with the DOM instead of xpath for the library's own …" is cut off, but it names the main change. The PR replaces the library's internal XPath lookups (signatures, KeyInfo c…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title "refactor: select with the DOM instead of xpath for the library's own …" is cut off, but it names the main change. The PR replaces the library's internal XPath lookups (signatures, KeyInfo certificates, Algorithm attributes, SignedInfo, comments, enveloped signatures, referenced elements) with DOM traversal. The change also adds a select option to addReference(). Caller-supplied XPath still works. The title is a refactor description, so it fits the changeset. The truncation and the ellipsis reduce clarity, but the primary change is still identifiable.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: defaults
Review profile: CHILL
Plan: Advanced
Run ID: 66db31c1-9b7d-4752-8ce7-44ca26f91535
Commits
Reviewing files that changed from the base of the PR and between 17474a0 and 30fe3b1.
Files selected for processing: 2
src/enveloped-signature.ts
test/dom-selection.spec.ts
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Additional comments: 1
src/enveloped-signature.ts-18-18: 1
18-18: Minor · Stability & Availability · Quick win
Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.
Handle nodes without a child list.
With @xmldom/xmldom 0.8.15, text nodes inherit childNodes = null. Thus, process(doc.createTextNode("x"), {}) now throws at Array.from(null). The previous direct-child XPath found no signature and returned the node unchanged. (raw.githubusercontent.com)
Use Array.from(node.childNodes ?? []), or iterate through sibling links. This preserves leaf-node handling and direct-child signature removal.
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: defaults
Review profile: CHILL
Plan: Advanced
Run ID: c46e0a8b-a662-4726-a5cb-2da7c9bcdae5
Commits
Reviewing files that changed from the base of the PR and between 84df780 and 17474a0.
Files selected for processing: 6
README.md
src/enveloped-signature.ts
src/signed-xml.ts
src/types.ts
src/utils.ts
test/dom-selection.spec.ts
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
xml-crypto evaluates a few fixed XPath expressions internally: finding signatures, the KeyInfo certificate, Algorithm attributes, SignedInfo, comments to strip, the enveloped signature, and the element a reference's ID points to during verification. This change does those lookups with DOM traversal instead. XPath the caller supplies is still evaluated with xpath, exactly as before.
It also adds optional ways to select nodes without writing an XPath string, for callers who already have them from the DOM:
Compatibility
Testing
Summary by CodeRabbit