Skip to content

Commit 4af86fd

Browse files
committed
Add XSLT namespace regression tests
In last commit I mentioned the wrong library. It was actually pyuppsala that had the issue not pybergshamra. Make sure we have the minium required version that contains the fix.
1 parent e786cc8 commit 4af86fd

6 files changed

Lines changed: 172 additions & 3 deletions

File tree

‎README.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Dependencies
4242
============
4343

4444
* pybergshamra >= 0.11.1 (XML signatures and PKCS#11 support)
45-
* pyuppsala >= 0.11.0 (XML processing)
45+
* pyuppsala >= 0.13.1 (XML processing)
4646
* pygments
4747
* gunicorn (for the standalone pyffd server)
4848
* ... cf pyproject.toml

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dependencies = [
2626
"pydantic>=2.8",
2727
"pyramid>=2.1",
2828
"pytz>=2025.2",
29-
"pyuppsala>=0.13.0",
29+
"pyuppsala>=0.13.1",
3030
"pybergshamra>=0.11.1",
3131
"pyyaml>=3.10",
3232
"redis>=5.3.0",
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata" xmlns:alg="urn:oasis:names:tc:SAML:metadata:algsupport" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:eidas="http://eidas.europa.eu/saml-extensions" xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ID="_eumiddleware" entityID="https://test.demw.eidas.swedenconnect.se/eidas-middleware/Metadata" validUntil="2026-12-09T09:46:59.594Z">
3+
<ds:Signature>
4+
<ds:SignedInfo>
5+
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
6+
<ds:SignatureMethod Algorithm="http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1"/>
7+
<ds:Reference URI="#_eumiddleware">
8+
<ds:Transforms>
9+
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
10+
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
11+
<ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="xsd"/>
12+
</ds:Transform>
13+
</ds:Transforms>
14+
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
15+
<ds:DigestValue>gVjWffnS2gYWlNyqxkCbQynU425D1pqZiHJvNQr1xtU=</ds:DigestValue>
16+
</ds:Reference>
17+
</ds:SignedInfo>
18+
<ds:SignatureValue>MgE/0IKAPG2+TkzbVOj6NZpcMMMJVN+1pqdqF6nKPNCE0Lfy4vLdap9qxtzDpbQpl7Ece0zYQnHYn6J/x9FjRm6N/jBth+YNxwmNtyE5GyxsVmNIAhSvMkerKqgHBxKmkTrp17au7bQzxHexvGEnkA0OzmuHstO0cKiCZ7B6ykCCOnh8ydSXkUeUAFfSzpoBraIz0FqcSnoK5LfxzZfSiA3Q1JzaPkmdLf5tIzw1OxwR1ggyIXLsh3q8buWV4otEFRS6cPF9iRvwlIIEqLn8klIlzFTNC5mVcoVUNuShdEsU03xkTATfnJMKdbFa4P3ArIMDtEV3KEoi5SY5mnpXSPtfstYyVn0Y0nDnBhWpDCdy/2xEq/0B6JxMlqwuWlI8XP+ayn5c7pw7Y2m/3tBZIr8IQftQBYGBrpGQk1eYJmKH9SeT6iX5mXi9ElqGee5UPIb/ogAMdSBWpp2wQwcPC+Xe3vNlt8o1P4XAehElzBhj7loMvqbC+BUVMCLDHJ58Zvy8bE5d54mb4M+tlpZnYU+sgqqm3DHmCZJw4sQ0odXoYQFsQeQOlMXFxN+mMZvDNi0eOoirN8euwLWy/sSbhpP0zp6ELT/kurmqxx//QGfGWHANu8Ab+qh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AgQwDQYJKoZIhvcNAQELBQADggIBAEXwkfny+YEBO9ALqTrTUK/1baIKi0CLQ/CZ0We/5BFjIp6KQphTGy5lzqtBG3Y68FO6JjOHbcDPZJfyjfniYjuRWh4bHgaBdu7NSdHZW9t6PeT14by6r9/pSjx9llDKchG2gSObuMuH5uI4t3c/hNhW17gv8m5/MzxxBJrR5lULPvMC3+v0Uy60MnycxcPDAi4HQfNkdHf+t3MLwH/HF60OAZ+pXHkahEJn3/F4nKQR8j8rdrn2ZT5mbYwjG27MZ7bhmFLfBtV6jXfQZqDfcxvOP8waEyxyMNah/a5LVQz+PJT+RHF4wqsigiMa248z8YCiz3oj6irpW4Ln/7YnJ8UTatRIOP7xK8hR2gUGtYxHxuGHASqn7tOGRlIdIZFROd28y2HSqgfApg5KUh2eupWMAnXlkcpxiLPz4rx+FX3kditH1z24HzcH2g3ytvL90j/7Gh1cp7BD3e3lf76wzLHUlIH+O573V8XPe1fAyutxsaHIY5S2VHv8fmFODqAS7uPyuZ1pc8gVAJEERGzbLL5WwZETXONrYpRoVIX8ojAU4sOCN8RRnrF5k1obgYj0B72ziXXZ8D/or9AP1BE7npmURLZ2qKM+FeTInZxKcYyLrvpkqVgyX6YM6cV+/XlG3LwS5D8gpKvD5+DIcQMwKXVEXNg3xncodVl2tdPeM8/D</ds:X509Certificate>
22+
</ds:X509Data>
23+
</ds:KeyInfo>
24+
</ds:Signature>
25+
<md:Extensions>
26+
<mdattr:EntityAttributes xmlns:mdattr="urn:oasis:names:tc:SAML:metadata:attribute">
27+
<saml2:Attribute Name="urn:oasis:names:tc:SAML:attribute:assurance-certification" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">
28+
<saml2:AttributeValue>http://eidas.europa.eu/LoA/high</saml2:AttributeValue>
29+
</saml2:Attribute>
30+
<saml2:Attribute Name="http://eidas.europa.eu/entity-attributes/protocol-version" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">
31+
<saml2:AttributeValue xsi:type="xsd:string">1.4.1</saml2:AttributeValue>
32+
</saml2:Attribute>
33+
<saml2:Attribute Name="http://eidas.europa.eu/entity-attributes/application-identifier" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">
34+
<saml2:AttributeValue xsi:type="xsd:string">German eIDAS Middleware version: 4.1.0</saml2:AttributeValue>
35+
</saml2:Attribute>
36+
<saml2:Attribute Name="http://macedir.org/entity-category" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">
37+
<saml2:AttributeValue>http://eidas.europa.eu/entity-attributes/termsofaccess/requesterid</saml2:AttributeValue>
38+
</saml2:Attribute>
39+
</mdattr:EntityAttributes>
40+
<alg:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
41+
<alg:SigningMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256" MinKeySize="256"/>
42+
<alg:SigningMethod Algorithm="http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1" MaxKeySize="4096" MinKeySize="3072"/>
43+
</md:Extensions>
44+
<md:IDPSSODescriptor WantAuthnRequestsSigned="true" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
45+
<md:Extensions>
46+
<eidas:NodeCountry>DE</eidas:NodeCountry>
47+
</md:Extensions>
48+
<md:KeyDescriptor use="signing">
49+
<ds:KeyInfo>
50+
<ds:X509Data>
51+
<ds:X509Certificate>MIIE5TCCAs2gAwIBAgIBfTANBgkqhkiG9w0BAQsFADAsMQ8wDQYDVQQKEwZTQ1RFU1QxGTAXBgNVBAMTEFNBTUwgU2lnbmVyIFRlc3QwHhcNMjMxMjEzMDAwMDAwWhcNMjcxMjEyMDAwMDAwWjAsMQ8wDQYDVQQKEwZTQ1RFU1QxGTAXBgNVBAMTEFNBTUwgU2lnbmVyIFRlc3QwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQCpRC2dXoyg26aWMvWshKF6LEGBgti6frNnBDehY/xPtSsaJgfA3Ba413GgGbFS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KUh2eupWMAnXlkcpxiLPz4rx+FX3kditH1z24HzcH2g3ytvL90j/7Gh1cp7BD3e3lf76wzLHUlIH+O573V8XPe1fAyutxsaHIY5S2VHv8fmFODqAS7uPyuZ1pc8gVAJEERGzbLL5WwZETXONrYpRoVIX8ojAU4sOCN8RRnrF5k1obgYj0B72ziXXZ8D/or9AP1BE7npmURLZ2qKM+FeTInZxKcYyLrvpkqVgyX6YM6cV+/XlG3LwS5D8gpKvD5+DIcQMwKXVEXNg3xncodVl2tdPeM8/D</ds:X509Certificate>
52+
</ds:X509Data>
53+
</ds:KeyInfo>
54+
</md:KeyDescriptor>
55+
<md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified</md:NameIDFormat>
56+
<md:SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="https://test.demw.eidas.swedenconnect.se/eidas-middleware/RequestReceiver"/>
57+
<md:SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="https://test.demw.eidas.swedenconnect.se/eidas-middleware/RequestReceiver"/>
58+
<saml2:Attribute FriendlyName="FamilyName" Name="http://eidas.europa.eu/attributes/naturalperson/CurrentFamilyName"/>
59+
<saml2:Attribute FriendlyName="FirstName" Name="http://eidas.europa.eu/attributes/naturalperson/CurrentGivenName"/>
60+
<saml2:Attribute FriendlyName="CurrentAddress" Name="http://eidas.europa.eu/attributes/naturalperson/CurrentAddress"/>
61+
<saml2:Attribute FriendlyName="PersonIdentifier" Name="http://eidas.europa.eu/attributes/naturalperson/PersonIdentifier"/>
62+
<saml2:Attribute FriendlyName="BirthName" Name="http://eidas.europa.eu/attributes/naturalperson/BirthName"/>
63+
<saml2:Attribute FriendlyName="PlaceOfBirth" Name="http://eidas.europa.eu/attributes/naturalperson/PlaceOfBirth"/>
64+
<saml2:Attribute FriendlyName="DateOfBirth" Name="http://eidas.europa.eu/attributes/naturalperson/DateOfBirth"/>
65+
<saml2:Attribute FriendlyName="Nationality" Name="http://eidas.europa.eu/attributes/naturalperson/Nationality"/>
66+
</md:IDPSSODescriptor>
67+
<md:Organization>
68+
<md:OrganizationName xml:lang="sv">Sweden Connect</md:OrganizationName>
69+
<md:OrganizationDisplayName xml:lang="sv">Sweden Connect</md:OrganizationDisplayName>
70+
<md:OrganizationURL xml:lang="sv">https://swedenconnect.se</md:OrganizationURL>
71+
</md:Organization>
72+
<md:ContactPerson contactType="technical">
73+
<md:Company>Sweden Connect</md:Company>
74+
<md:GivenName>Sweden Connect</md:GivenName>
75+
<md:SurName>Operations</md:SurName>
76+
<md:EmailAddress>operations@swedenconnect.se</md:EmailAddress>
77+
<md:TelephoneNumber>+46105742100</md:TelephoneNumber>
78+
</md:ContactPerson>
79+
<md:ContactPerson contactType="support">
80+
<md:Company>Sweden Connect</md:Company>
81+
<md:GivenName>Sweden Connect</md:GivenName>
82+
<md:SurName>Operations</md:SurName>
83+
<md:EmailAddress>operations@swedenconnect.se</md:EmailAddress>
84+
<md:TelephoneNumber>+46105742100</md:TelephoneNumber>
85+
</md:ContactPerson>
86+
</md:EntityDescriptor>

‎src/pyff/test/test_pipeline.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,44 @@ def test_publish_prunes_genuinely_unused_namespace(self):
13051305
except OSError:
13061306
pass
13071307

1308+
def test_xslt_cleanup_keeps_xsi_type_prefixes(self):
1309+
"""XSLT-rewritten metadata must stay well-formed when attributes use xsi:/xsd: prefixes.
1310+
1311+
eIDAS middleware metadata (test07-xsi-type-middleware.xml) uses
1312+
xsi:type="xsd:string". Running it through an xslt step must not produce output
1313+
with an undeclared prefix ("Undeclared namespace prefix: xsi"), which broke
1314+
loading in pyuppsala (https://github.com/kushaldas/pyuppsala/issues/36).
1315+
"""
1316+
entity = 'https://test.demw.eidas.swedenconnect.se/eidas-middleware/Metadata'
1317+
xsi_type = '{http://www.w3.org/2001/XMLSchema-instance}type'
1318+
tmpfile = tempfile.NamedTemporaryFile('w').name
1319+
try:
1320+
self.exec_pipeline(
1321+
f"""
1322+
- when clean:
1323+
- xslt:
1324+
stylesheet: tidy.xsl
1325+
- check_xml_namespaces
1326+
- break
1327+
- load:
1328+
- file://{self.datadir}/metadata/test07-xsi-type-middleware.xml cleanup clean
1329+
- select
1330+
- publish: {tmpfile}
1331+
"""
1332+
)
1333+
t2 = parse_xml(tmpfile)
1334+
assert t2 is not None
1335+
entity_elt = t2.find(".//{{{}}}EntityDescriptor[@entityID='{}']".format(NS['md'], entity))
1336+
assert entity_elt is not None, "entity was dropped after the xslt step"
1337+
typed = entity_elt.findall(".//*[@{http://www.w3.org/2001/XMLSchema-instance}type]")
1338+
assert typed, "xsi:type attributes were lost or lost their namespace"
1339+
assert all(e.get(xsi_type) == 'xsd:string' for e in typed)
1340+
finally:
1341+
try:
1342+
os.unlink(tmpfile)
1343+
except OSError:
1344+
pass
1345+
13081346
def test_wsfed_type_prefix_survives(self):
13091347
"""A complex-type xsi:type prefix (as opposed to a datatype prefix like xs/xsd)
13101348
needs no special-casing: real WS-Fed metadata also carries elements in that
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from unittest import TestCase
2+
3+
from pyuppsala import etree
4+
5+
XSI = "http://www.w3.org/2001/XMLSchema-instance"
6+
XSL_HEAD = b'<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">'
7+
8+
9+
def _transform(stylesheet: bytes, source: bytes):
10+
result = etree.XSLT(etree.fromstring(stylesheet))(etree.fromstring(source))
11+
# getroot() reparses the serialized output, so an undeclared prefix raises here
12+
return result.getroot()
13+
14+
15+
class TestXSLTNamespaceDeclarations(TestCase):
16+
"""Created attributes must carry the namespace declaration they need (XSLT 1.0 7.1.3).
17+
18+
Regression tests for https://github.com/kushaldas/pyuppsala/issues/36.
19+
"""
20+
21+
def test_attribute_copied_into_literal_result_element(self):
22+
xsl = (
23+
XSL_HEAD
24+
+ b"""
25+
<xsl:template match="/root"><out><xsl:apply-templates select="node()|@*"/></out></xsl:template>
26+
<xsl:template match="*"><xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy></xsl:template>
27+
<xsl:template match="text()|@*"><xsl:copy/></xsl:template>
28+
</xsl:stylesheet>"""
29+
)
30+
src = b'<root xmlns:xsi="%s" xsi:type="xsd:string"><a xsi:type="x"/></root>' % XSI.encode()
31+
out = _transform(xsl, src)
32+
assert out.get('{%s}type' % XSI) == 'xsd:string'
33+
assert out[0].get('{%s}type' % XSI) == 'x'
34+
35+
def test_prefix_declared_only_on_dropped_element(self):
36+
xsl = (
37+
XSL_HEAD
38+
+ b"""
39+
<xsl:template match="/"><out><xsl:copy-of select="//a/@*"/><b><xsl:copy-of select="//a/@*"/></b></out></xsl:template>
40+
</xsl:stylesheet>"""
41+
)
42+
src = b'<root><a xmlns:xsi="%s" xsi:type="t"/></root>' % XSI.encode()
43+
out = _transform(xsl, src)
44+
assert out.get('{%s}type' % XSI) == 't'
45+
assert out[0].get('{%s}type' % XSI) == 't'

‎uv.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)