Skip to content

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

Open

Description

@trackoor

C14nCanonicalization and ExclusiveCanonicalization do not order namespace declarations and attributes as Canonical XML requires: namespace nodes by prefix, attributes by namespace URI then local name, both compared by code point (C14N 1.0 §2.2). Other implementations produce different octets for these inputs, so digests do not match.

const { DOMParser } = require('@xmldom/xmldom');
const { C14nCanonicalization } = require('xml-crypto');
const c14n = s =>
  new C14nCanonicalization().process(
    new DOMParser().parseFromString(s, 'text/xml').documentElement,
    {},
  );

console.log(c14n('<SOAP-ENV:Envelope xmlns:SOAP-ENV="urn:s" xmlns:ds="urn:d" xmlns:wsse="urn:w"/>'));
console.log(c14n('<r xmlns:B="urn:1" xmlns:a="urn:2"/>'));
console.log(c14n('<r xmlns:p="urn:a" xmlns:q="urn:az" p:zb="1" q:a="2"/>'));
console.log(c14n('<r 豈="1" \u{10000}="2"/>'));

Actual:

<SOAP-ENV:Envelope xmlns:ds="urn:d" xmlns:SOAP-ENV="urn:s" xmlns:wsse="urn:w"></SOAP-ENV:Envelope>
<r xmlns:a="urn:2" xmlns:B="urn:1"></r>
<r xmlns:p="urn:a" xmlns:q="urn:az" q:a="2" p:zb="1"></r>
<r 𐀀="2" 豈="1"></r>

Expected:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="urn:s" xmlns:ds="urn:d" xmlns:wsse="urn:w"></SOAP-ENV:Envelope>
<r xmlns:B="urn:1" xmlns:a="urn:2"></r>
<r xmlns:p="urn:a" xmlns:q="urn:az" p:zb="1" q:a="2"></r>
<r 豈="1" 𐀀="2"></r>

ExclusiveCanonicalization gives the same wrong attribute order for the last two inputs.

Tested with xml-crypto 6.4.0 and master (84df780), @xmldom/xmldom 0.9.12, Node.js 26.13.0.

In src/c14n-canonicalization.ts and src/exclusive-canonicalization.ts, nsCompare uses localeCompare (locale collation, so ds sorts before SOAP-ENV), attrCompare compares the concatenation namespaceURI + localName (urn:a + zb sorts after urn:az + a) with <, which compares UTF-16 code units and puts U+10000 before U+F900.

Credit: https://ucsbplse.github.io/

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions