authentik.lib.tests.test_xml
Test XML utilities
1"""Test XML utilities""" 2 3from django.test import SimpleTestCase 4from lxml.etree import XMLSyntaxError # nosec 5 6from authentik.lib.xml import UnsafeXML, lxml_from_string 7 8 9class TestXML(SimpleTestCase): 10 """Test XML utilities""" 11 12 def test_parse(self): 13 """Test that a document without a document type declaration is parsed""" 14 root = lxml_from_string( 15 '<?xml version="1.0"?>' 16 '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ' 17 'ID="_1" Version="2.0" IssueInstant="2026-05-19T01:01:53.461Z"/>' 18 ) 19 20 self.assertEqual(root.tag, "{urn:oasis:names:tc:SAML:2.0:protocol}AuthnRequest") 21 22 def test_doctype(self): 23 """Test that a document type declaration is refused""" 24 with self.assertRaisesMessage(UnsafeXML, "XML document contains a DOCTYPE declaration"): 25 lxml_from_string( 26 '<?xml version="1.0"?>' 27 "<!DOCTYPE samlp:AuthnRequest>" 28 '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ' 29 'ID="_1" Version="2.0" IssueInstant="2026-05-19T01:01:53.461Z"/>' 30 ) 31 32 def test_doctype_utf16(self): 33 """Test that a document type declaration is refused in a UTF-16 document""" 34 document = ( 35 '<?xml version="1.0" encoding="UTF-16"?>' 36 "<!DOCTYPE samlp:AuthnRequest>" 37 '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ' 38 'ID="_1" Version="2.0" IssueInstant="2026-05-19T01:01:53.461Z"/>' 39 ).encode("utf-16") 40 41 with self.assertRaisesMessage(UnsafeXML, "XML document contains a DOCTYPE declaration"): 42 lxml_from_string(document) 43 44 def test_doctype_internal_subset(self): 45 """Test that a document type declaration with an internal subset is refused""" 46 with self.assertRaisesMessage(UnsafeXML, "XML document contains a DOCTYPE declaration"): 47 lxml_from_string('<?xml version="1.0"?><!DOCTYPE d [<!ENTITY g "h">]><d>&g;</d>') 48 49 def test_doctype_external_identifier(self): 50 """Test that a document type declaration with an external identifier is refused""" 51 with self.assertRaisesMessage(UnsafeXML, "XML document contains a DOCTYPE declaration"): 52 lxml_from_string( 53 '<?xml version="1.0"?>' 54 '<!DOCTYPE d SYSTEM "https://sp.example.invalid/d.dtd">' 55 "<d/>" 56 ) 57 58 def test_declared_encoding_mismatch(self): 59 """Test that a document whose declared encoding does not match its content is refused""" 60 document = ( 61 '<?xml version="1.0" encoding="UTF-8"?>' 62 "<!DOCTYPE samlp:AuthnRequest>" 63 '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ' 64 'ID="_1" Version="2.0" IssueInstant="2026-05-19T01:01:53.461Z"/>' 65 ).encode("utf-16") 66 67 with self.assertRaisesMessage(UnsafeXML, "XML document could not be validated"): 68 lxml_from_string(document) 69 70 def test_declared_encoding_unsupported(self): 71 """Test that a document in an encoding that cannot be validated is refused""" 72 document = '<?xml version="1.0" encoding="UTF-32"?><!DOCTYPE d><d>x</d>'.encode("utf-32") 73 74 with self.assertRaisesMessage(UnsafeXML, "XML document could not be validated"): 75 lxml_from_string(document) 76 77 def test_doctype_in_comment(self): 78 """Test that the text of a document type declaration in a comment is not one""" 79 root = lxml_from_string('<?xml version="1.0"?><!-- <!DOCTYPE d> --><d/>') 80 81 self.assertEqual(root.tag, "d") 82 83 def test_doctype_in_attribute(self): 84 """Test that the text of a document type declaration in an attribute is not one""" 85 root = lxml_from_string('<?xml version="1.0"?><d a="<!DOCTYPE d>"/>') 86 87 self.assertEqual(root.attrib["a"], "<!DOCTYPE d>") 88 89 def test_single_byte_encoding(self): 90 """Test that a document in a single-byte encoding is parsed""" 91 document = '<?xml version="1.0" encoding="windows-1252"?><d>caf\xe9</d>'.encode("cp1252") 92 93 root = lxml_from_string(document) 94 95 self.assertEqual(root.text, "caf\xe9") 96 97 def test_nesting_depth(self): 98 """Test that the parser keeps the default limit on nesting depth""" 99 with self.assertRaises(XMLSyntaxError): 100 lxml_from_string("<a>" * 300 + "x" + "</a>" * 300)
class
TestXML(django.test.testcases.SimpleTestCase):
10class TestXML(SimpleTestCase): 11 """Test XML utilities""" 12 13 def test_parse(self): 14 """Test that a document without a document type declaration is parsed""" 15 root = lxml_from_string( 16 '<?xml version="1.0"?>' 17 '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ' 18 'ID="_1" Version="2.0" IssueInstant="2026-05-19T01:01:53.461Z"/>' 19 ) 20 21 self.assertEqual(root.tag, "{urn:oasis:names:tc:SAML:2.0:protocol}AuthnRequest") 22 23 def test_doctype(self): 24 """Test that a document type declaration is refused""" 25 with self.assertRaisesMessage(UnsafeXML, "XML document contains a DOCTYPE declaration"): 26 lxml_from_string( 27 '<?xml version="1.0"?>' 28 "<!DOCTYPE samlp:AuthnRequest>" 29 '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ' 30 'ID="_1" Version="2.0" IssueInstant="2026-05-19T01:01:53.461Z"/>' 31 ) 32 33 def test_doctype_utf16(self): 34 """Test that a document type declaration is refused in a UTF-16 document""" 35 document = ( 36 '<?xml version="1.0" encoding="UTF-16"?>' 37 "<!DOCTYPE samlp:AuthnRequest>" 38 '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ' 39 'ID="_1" Version="2.0" IssueInstant="2026-05-19T01:01:53.461Z"/>' 40 ).encode("utf-16") 41 42 with self.assertRaisesMessage(UnsafeXML, "XML document contains a DOCTYPE declaration"): 43 lxml_from_string(document) 44 45 def test_doctype_internal_subset(self): 46 """Test that a document type declaration with an internal subset is refused""" 47 with self.assertRaisesMessage(UnsafeXML, "XML document contains a DOCTYPE declaration"): 48 lxml_from_string('<?xml version="1.0"?><!DOCTYPE d [<!ENTITY g "h">]><d>&g;</d>') 49 50 def test_doctype_external_identifier(self): 51 """Test that a document type declaration with an external identifier is refused""" 52 with self.assertRaisesMessage(UnsafeXML, "XML document contains a DOCTYPE declaration"): 53 lxml_from_string( 54 '<?xml version="1.0"?>' 55 '<!DOCTYPE d SYSTEM "https://sp.example.invalid/d.dtd">' 56 "<d/>" 57 ) 58 59 def test_declared_encoding_mismatch(self): 60 """Test that a document whose declared encoding does not match its content is refused""" 61 document = ( 62 '<?xml version="1.0" encoding="UTF-8"?>' 63 "<!DOCTYPE samlp:AuthnRequest>" 64 '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ' 65 'ID="_1" Version="2.0" IssueInstant="2026-05-19T01:01:53.461Z"/>' 66 ).encode("utf-16") 67 68 with self.assertRaisesMessage(UnsafeXML, "XML document could not be validated"): 69 lxml_from_string(document) 70 71 def test_declared_encoding_unsupported(self): 72 """Test that a document in an encoding that cannot be validated is refused""" 73 document = '<?xml version="1.0" encoding="UTF-32"?><!DOCTYPE d><d>x</d>'.encode("utf-32") 74 75 with self.assertRaisesMessage(UnsafeXML, "XML document could not be validated"): 76 lxml_from_string(document) 77 78 def test_doctype_in_comment(self): 79 """Test that the text of a document type declaration in a comment is not one""" 80 root = lxml_from_string('<?xml version="1.0"?><!-- <!DOCTYPE d> --><d/>') 81 82 self.assertEqual(root.tag, "d") 83 84 def test_doctype_in_attribute(self): 85 """Test that the text of a document type declaration in an attribute is not one""" 86 root = lxml_from_string('<?xml version="1.0"?><d a="<!DOCTYPE d>"/>') 87 88 self.assertEqual(root.attrib["a"], "<!DOCTYPE d>") 89 90 def test_single_byte_encoding(self): 91 """Test that a document in a single-byte encoding is parsed""" 92 document = '<?xml version="1.0" encoding="windows-1252"?><d>caf\xe9</d>'.encode("cp1252") 93 94 root = lxml_from_string(document) 95 96 self.assertEqual(root.text, "caf\xe9") 97 98 def test_nesting_depth(self): 99 """Test that the parser keeps the default limit on nesting depth""" 100 with self.assertRaises(XMLSyntaxError): 101 lxml_from_string("<a>" * 300 + "x" + "</a>" * 300)
Test XML utilities
def
test_parse(self):
13 def test_parse(self): 14 """Test that a document without a document type declaration is parsed""" 15 root = lxml_from_string( 16 '<?xml version="1.0"?>' 17 '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ' 18 'ID="_1" Version="2.0" IssueInstant="2026-05-19T01:01:53.461Z"/>' 19 ) 20 21 self.assertEqual(root.tag, "{urn:oasis:names:tc:SAML:2.0:protocol}AuthnRequest")
Test that a document without a document type declaration is parsed
def
test_doctype(self):
23 def test_doctype(self): 24 """Test that a document type declaration is refused""" 25 with self.assertRaisesMessage(UnsafeXML, "XML document contains a DOCTYPE declaration"): 26 lxml_from_string( 27 '<?xml version="1.0"?>' 28 "<!DOCTYPE samlp:AuthnRequest>" 29 '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ' 30 'ID="_1" Version="2.0" IssueInstant="2026-05-19T01:01:53.461Z"/>' 31 )
Test that a document type declaration is refused
def
test_doctype_utf16(self):
33 def test_doctype_utf16(self): 34 """Test that a document type declaration is refused in a UTF-16 document""" 35 document = ( 36 '<?xml version="1.0" encoding="UTF-16"?>' 37 "<!DOCTYPE samlp:AuthnRequest>" 38 '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ' 39 'ID="_1" Version="2.0" IssueInstant="2026-05-19T01:01:53.461Z"/>' 40 ).encode("utf-16") 41 42 with self.assertRaisesMessage(UnsafeXML, "XML document contains a DOCTYPE declaration"): 43 lxml_from_string(document)
Test that a document type declaration is refused in a UTF-16 document
def
test_doctype_internal_subset(self):
45 def test_doctype_internal_subset(self): 46 """Test that a document type declaration with an internal subset is refused""" 47 with self.assertRaisesMessage(UnsafeXML, "XML document contains a DOCTYPE declaration"): 48 lxml_from_string('<?xml version="1.0"?><!DOCTYPE d [<!ENTITY g "h">]><d>&g;</d>')
Test that a document type declaration with an internal subset is refused
def
test_doctype_external_identifier(self):
50 def test_doctype_external_identifier(self): 51 """Test that a document type declaration with an external identifier is refused""" 52 with self.assertRaisesMessage(UnsafeXML, "XML document contains a DOCTYPE declaration"): 53 lxml_from_string( 54 '<?xml version="1.0"?>' 55 '<!DOCTYPE d SYSTEM "https://sp.example.invalid/d.dtd">' 56 "<d/>" 57 )
Test that a document type declaration with an external identifier is refused
def
test_declared_encoding_mismatch(self):
59 def test_declared_encoding_mismatch(self): 60 """Test that a document whose declared encoding does not match its content is refused""" 61 document = ( 62 '<?xml version="1.0" encoding="UTF-8"?>' 63 "<!DOCTYPE samlp:AuthnRequest>" 64 '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ' 65 'ID="_1" Version="2.0" IssueInstant="2026-05-19T01:01:53.461Z"/>' 66 ).encode("utf-16") 67 68 with self.assertRaisesMessage(UnsafeXML, "XML document could not be validated"): 69 lxml_from_string(document)
Test that a document whose declared encoding does not match its content is refused
def
test_declared_encoding_unsupported(self):
71 def test_declared_encoding_unsupported(self): 72 """Test that a document in an encoding that cannot be validated is refused""" 73 document = '<?xml version="1.0" encoding="UTF-32"?><!DOCTYPE d><d>x</d>'.encode("utf-32") 74 75 with self.assertRaisesMessage(UnsafeXML, "XML document could not be validated"): 76 lxml_from_string(document)
Test that a document in an encoding that cannot be validated is refused
def
test_doctype_in_comment(self):
78 def test_doctype_in_comment(self): 79 """Test that the text of a document type declaration in a comment is not one""" 80 root = lxml_from_string('<?xml version="1.0"?><!-- <!DOCTYPE d> --><d/>') 81 82 self.assertEqual(root.tag, "d")
Test that the text of a document type declaration in a comment is not one
def
test_doctype_in_attribute(self):
84 def test_doctype_in_attribute(self): 85 """Test that the text of a document type declaration in an attribute is not one""" 86 root = lxml_from_string('<?xml version="1.0"?><d a="<!DOCTYPE d>"/>') 87 88 self.assertEqual(root.attrib["a"], "<!DOCTYPE d>")
Test that the text of a document type declaration in an attribute is not one
def
test_single_byte_encoding(self):
90 def test_single_byte_encoding(self): 91 """Test that a document in a single-byte encoding is parsed""" 92 document = '<?xml version="1.0" encoding="windows-1252"?><d>caf\xe9</d>'.encode("cp1252") 93 94 root = lxml_from_string(document) 95 96 self.assertEqual(root.text, "caf\xe9")
Test that a document in a single-byte encoding is parsed