Skip to content

Commit 59caa21

Browse files
committed
Don't declare the xml prefix on aggregate roots
entitiesdescriptor() copied the whole NS map onto the EntitiesDescriptor root, which emitted an explicit xmlns:xml declaration. The xml prefix is implicitly bound and must never be declared. libxml2 ignores the declaration when canonicalizing, but .NET's SignedXml keeps it, so a signed aggregate failed to verify in ADFSToolkit although xmlsec1 and other libxml2-based verifiers accepted it. Drop the prefix from the nsmap before building the root, and add a regression test.
1 parent 4af86fd commit 59caa21

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎src/pyff/samlmd.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,12 @@ def entitiesdescriptor(
510510
for entity in entities:
511511
nsmap.update(entity.nsmap)
512512

513+
# The xml prefix is bound by definition and must never be declared. An explicit
514+
# xmlns:xml is legal XML, but .NET's exclusive c14n keeps it in the canonical
515+
# form, so consumers such as ADFSToolkit compute a different digest and reject
516+
# the signature.
517+
nsmap.pop('xml', None)
518+
513519
log.debug("selecting %d entities before validation" % len(entities))
514520

515521
attrs = dict(Name=name, nsmap=nsmap)

‎src/pyff/test/test_utils.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,26 @@ def test_entitiesdescriptor_copies_shared_entities(self):
280280
[e.get('entityID') for e in first],
281281
)
282282

283+
def test_entitiesdescriptor_does_not_declare_xml_prefix(self):
284+
"""The aggregate root must not carry an explicit xmlns:xml declaration.
285+
286+
.NET's SignedXml keeps it in the exclusive c14n output, so a signature
287+
over such a root fails to verify in ADFSToolkit although libxml2 based
288+
verifiers accept it.
289+
"""
290+
from pyuppsala import etree
291+
292+
from pyff.samlmd import entitiesdescriptor
293+
294+
t = entitiesdescriptor(self._entities(), 'https://md.example.org/first', lookup_fn=None, validate=False)
295+
296+
self.assertNotIn('xml', t.nsmap)
297+
serialized = etree.tostring(t, encoding='unicode')
298+
self.assertNotIn('xmlns:xml=', serialized)
299+
# the other pyff prefixes are still declared on the root
300+
self.assertEqual(NS['md'], t.nsmap.get('md'))
301+
self.assertEqual(NS['ds'], t.nsmap.get('ds'))
302+
283303
def test_entitiesdescriptor_copy_false_moves(self):
284304
"""copy=False keeps the historical (lxml) move behaviour for callers that want it."""
285305
from pyff.samlmd import entitiesdescriptor

0 commit comments

Comments
 (0)