dart-xml icon indicating copy to clipboard operation
dart-xml copied to clipboard

XmlPrefixName.namespaceUri is null in copied XmlNode

Open scribetw opened this issue 2 years ago • 3 comments

I'm using xml to process SAML responses.

dependencies:
  xml: ^5.3.1
import 'package:test/test.dart';
import 'package:xml/xml.dart';

void main() {
  test('clone namespaceUri', () {
    final xml = '<samlp:Response xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol">'
        '<saml:Assertion></saml:Assertion>'
        '</samlp:Response>';
    final doc = XmlDocument.parse(xml);
    final assertion = doc.rootElement.firstElementChild;
    final assertion2 = assertion!.copy();

    expect(assertion.name.namespaceUri, 'urn:oasis:names:tc:SAML:2.0:assertion');
    expect(assertion2.name.namespaceUri, 'urn:oasis:names:tc:SAML:2.0:assertion'); // failed
  });
}

https://github.com/renggli/dart-xml/blob/3c587e18c3b507eb737a2aec864f0bc8413f8aed/lib/src/xml/utils/prefix_name.dart#L18 The resolving of a namespace URI is depending on the ancestors.

Consider storing the namespace URI property directly.

scribetw avatar Feb 14 '22 07:02 scribetw

I don't know how other libraries handle this situation?

Resolving the property early might be possible, but it breaks the general mutability of the DOM (or require complicated invalidation logic).

Storing the property on copy might be possible, but changes the DOM in unexpected ways.

renggli avatar Feb 14 '22 08:02 renggli

In xmldom (a Node.js library) it is stored while parsing and creating a new element or attribute node. https://github.com/xmldom/xmldom/blob/e31e25d9b0ce79e8545d23771181a79d96b80c73/lib/dom.js#L894

https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-NodeNSname

namespaceURI of type DOMString, readonly, introduced in DOM Level 2 The namespace URI of this node, or null if it is unspecified. This is not a computed value that is the result of a namespace lookup based on an examination of the namespace declarations in scope. It is merely the namespace URI given at creation time.

I think it's safe to store it at creation time.

scribetw avatar Feb 14 '22 08:02 scribetw

Interesting: https://www.w3.org/TR/DOM-Level-2-Core/core.html#Namespaces-Considerations. This basically breaks mutability and serialization of the DOM by design :-/

renggli avatar Feb 14 '22 09:02 renggli