Skip to content

refactor: select with the DOM instead of xpath for the library's own … - #666

Open
elbuo8 wants to merge 2 commits into
node-saml:masterfrom
elbuo8:refactor/dom-instead-of-xpath
Open

elbuo8 wants to merge 2 commits into
node-saml:masterfrom
elbuo8:refactor/dom-instead-of-xpath

Conversation

@elbuo8

@elbuo8 elbuo8 commented Nov 24, 2026 •

Copy link
Copy Markdown

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.

…lookups

Co-Authored-By: Claude <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

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.
Callback-based reference selection
src/types.ts, src/signed-xml.ts, README.md, test/dom-selection.spec.ts
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.
Callback-based signature placement
src/types.ts, src/signed-xml.ts, README.md
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.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Sequence Diagram(s)

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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

Recent 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: 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant