Skip to content

fix: sort namespace declarations and attributes by code point in canonicalization - #665

Open
bardrop wants to merge 1 commit into
node-saml:masterfrom
bardrop-oss:fix/c14n-code-point-order
Open

bardrop wants to merge 1 commit into
node-saml:masterfrom
bardrop-oss:fix/c14n-code-point-order

Conversation

@bardrop

@bardrop bardrop commented Nov 12, 2026 •

Copy link
Copy Markdown

Fixes #664.

C14N document order (§2.2) sorts namespace nodes by prefix, and attributes by namespace URI then local name, comparing by code point. Both C14nCanonicalization and ExclusiveCanonicalization got this wrong in two ways:

  • nsCompare used localeCompare, so ds sorted before SOAP-ENV.
  • attrCompare compared the joined string namespaceURI + localName with <. So urn:a + zb sorted after urn:az + a, and UTF-16 code units put U+10000 before U+F900.

Both now use one compareByCodePoint() helper in utils.ts: namespace URI first, then local name, with no-namespace attributes first (empty URI). attrCompare / nsCompare keep their signatures.

Tests: test/c14n-document-order.spec.ts runs each case through all four canonicalizers. On master 12 of them fail, for the three reasons in the issue. Two guard cases (default namespace first, no-namespace attributes first) pass before and after. npm run build && npm test && npm run lint is green (590 passing).

I couldn't test non-BMP prefixes or attribute names: the pinned @xmldom/xmldom 0.8 rejects them at parse time. Non-BMP namespace URIs do parse, so the code point case is tested through those.

Cross-check with libxml2: I compared the output with xmllint --c14n and xmllint --exc-c14n on 800 random documents (mixed-case prefixes, - _ ., digits, non-ASCII and non-BMP URIs, shuffled declarations and attributes). Master: 76 C14N and 42 exclusive mismatches. This branch: 0.

Compatibility: this changes canonical bytes whenever the old order was wrong. So a signature made by an older xml-crypto over such a document will stop verifying here. The common case is inclusive C14N over a SOAP envelope that declares both SOAP-ENV and ds. Signatures from implementations that follow the spec (I checked libxml2, which xmlsec uses) over the same documents start verifying. If you'd rather ship this in 7.0 or put it behind a flag, I can change it.

Summary by CodeRabbit

  • Bug Fixes
    • XML canonicalization now orders namespace prefixes and attributes consistently by Unicode code point, rather than relying on locale-dependent comparison. Attributes are ordered by namespace URI and then local name, with unnamespaced attributes handled consistently.
  • Tests
    • Added coverage across all canonicalization methods for namespace and attribute ordering, including comparisons involving characters outside the Basic Multilingual Plane.

…nicalization

C14N document order compares namespace prefixes, and attribute namespace
URIs then local names, by code point. nsCompare used localeCompare, so
`ds` sorted before `SOAP-ENV`. attrCompare compared the joined
`namespaceURI + localName` with `<`, so `urn:a`+`zb` sorted after
`urn:az`+`a`, and UTF-16 code units put U+10000 before U+F900.

Both canonicalizers now use a shared code point comparison.

Fixes node-saml#664
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The canonicalization implementations now compare namespace prefixes by code point and sort attributes by namespace URI, then local name. A shared comparator supports these comparisons. New tests check ordering across all four canonicalization implementations.

Changes: Canonicalization ordering

Layer / File(s) Summary
Code-point ordering across canonicalization
src/utils.ts, src/c14n-canonicalization.ts, src/exclusive-canonicalization.ts, test/c14n-document-order.spec.ts
A shared comparator now compares strings by Unicode code point. Both canonicalization implementations use it to order namespace prefixes and attributes. Tests cover prefix ordering, default namespaces, attribute ordering, and a namespace URI with a character outside the BMP.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: cjbarth

Merge Risk: Minimal · up to 99731

The change makes namespace and attribute ordering follow the canonicalization spec, which should fix digest mismatches with other implementations. Signatures created by older versions over affected documents may stop verifying; the PR states this tradeoff. No merge-blocking risk was found.

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 names the main change: namespace declarations and attributes in canonicalization are sorted by code point. The raw summary and objectives confirm that C14nCanonicalization and ExclusiveCanon…
Linked Issues check Passed Issue #664 (direct) requires code-point ordering of namespace nodes by prefix and of attributes by namespace URI, then local name, in C14nCanonicalization and ExclusiveCanonicalization. At the reviewe…
Out of Scope Changes check Passed The changes in scope are the sort comparators in src/c14n-canonicalization.ts and src/exclusive-canonicalization.ts, the new compareByCodePoint helper in src/utils.ts, and the new test file. All of th…
Full details: Title check

Explanation

The title names the main change: namespace declarations and attributes in canonicalization are sorted by code point. The raw summary and objectives confirm that C14nCanonicalization and ExclusiveCanonicalization now compare namespace prefixes and attributes by code point, with attribute ordering by namespace URI then local name. The title is specific, concise, and uses a conventional prefix. It omits the attribute ordering rule and the shared helper, which is acceptable for a title.

Full details: Linked Issues check

Explanation

Issue #664 (direct) requires code-point ordering of namespace nodes by prefix and of attributes by namespace URI, then local name, in C14nCanonicalization and ExclusiveCanonicalization. At the reviewed head, both classes define nsCompare as utils.compareByCodePoint(a.prefix || "", b.prefix || "") and attrCompare as compareByCodePoint on namespaceURI || "" followed by compareByCodePoint on localName. This removes localeCompare and the concatenated-string comparison. utils.compareByCodePoint steps by code point, advancing 2 UTF-16 units for supplementary code points, so the comparison is by code point. Tracing the issue's examples: SOAP-ENV < ds < wsse (0x53 < 0x64); B < a; p:zb (urn:a) before q:a (urn:az); U+F900 (豈) before U+10000, matching the Expected output. The signatures are unchanged, as the PR states. Test file test/c14n-document-order.spec.ts is reported in the PR and summary as covering all four canonicalizers; I did not read its contents, so the test evidence rests on the PR description and summary. No non-coding requirement applies.

Full details: Out of Scope Changes check

Explanation

The changes in scope are the sort comparators in src/c14n-canonicalization.ts and src/exclusive-canonicalization.ts, the new compareByCodePoint helper in src/utils.ts, and the new test file. All of them serve the code-point ordering required by #664. The reviewed head shows no unrelated logic in the comparators or helper. The whole-PR diff could not be read directly (changed blobs unavailable to the diff operation), and the surrounding namespace-rendering code at head was not compared to the merge base. The raw summary reports the c14n file change as +7/-23 and attributes it only to the comparators, which supports this verdict.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.

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: f803a7bc-ff87-4a59-b5cc-631702b7b8a9
Commits

Reviewing files that changed from the base of the PR and between 84df780 and 9973105.

Files selected for processing: 4

  • src/c14n-canonicalization.ts
  • src/exclusive-canonicalization.ts
  • src/utils.ts
  • test/c14n-document-order.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.

@bardrop

bardrop commented Nov 20, 2026

Copy link
Copy Markdown
Author

Friendly ping when you have time. Is there anything you'd like changed here?

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.

C14N sorts namespace prefixes with localeCompare and attributes by namespaceURI + localName

1 participant