Skip to content

Commit d45d962

Browse files
Align behaviour of expect_references with docs
Passing expect_references=True to verify(...) now results in a list of verify_results, irrespective of the number of references in the signature. Fixes #278.
1 parent 9ed6292 commit d45d962

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

signxml/verifier.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,11 @@ def verify(
535535
msg = "Expected to find {} references, but found {}"
536536
raise InvalidSignature(msg.format(self.config.expect_references, len(verify_results)))
537537

538-
return verify_results if self.config.expect_references > 1 else verify_results[0]
538+
return (
539+
verify_results[0]
540+
if type(self.config.expect_references) is int and self.config.expect_references == 1
541+
else verify_results
542+
)
539543

540544
def _verify_reference(
541545
self,

test/test.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ def test_example_multi(self):
112112
expect_references=2,
113113
)
114114

115+
def test_example_multi_unspecified_reference_count(self):
116+
cert, _ = self.load_example_keys()
117+
with open(os.path.join(os.path.dirname(__file__), "example.pem")) as fh:
118+
cert = fh.read()
119+
example_file = os.path.join(os.path.dirname(__file__), "example-125.xml")
120+
res = XMLVerifier().verify(
121+
data=etree.parse(example_file),
122+
x509_cert=cert,
123+
expect_references=True,
124+
)
125+
126+
self.assertIsInstance(res, list)
127+
self.assertEqual(2, len(res))
128+
115129

116130
class TestSignXML(unittest.TestCase, LoadExampleKeys):
117131
def setUp(self):
@@ -490,6 +504,16 @@ def test_elementtree_compat(self):
490504
</samlp:Response>""",
491505
]
492506

507+
def test_verify_results_with_nonspecific_reference_count(self):
508+
crt, key = self.load_example_keys()
509+
data = etree.fromstring(self.saml_test_vectors[0])
510+
reference_uri = "assertionId"
511+
signed_root = XMLSigner().sign(data, reference_uri=reference_uri, key=key, cert=crt)
512+
res = XMLVerifier().verify(etree.tostring(signed_root), x509_cert=crt, expect_references=True)
513+
514+
self.assertIsInstance(res, list)
515+
self.assertEqual(1, len(res))
516+
493517
def test_reference_uris_and_custom_key_info(self):
494518
crt, key = self.load_example_keys()
495519

@@ -499,7 +523,7 @@ def test_reference_uris_and_custom_key_info(self):
499523
reference_uri = ["assertionId", "assertion2"] if "assertion2" in d else "assertionId"
500524
signed_root = XMLSigner().sign(data, reference_uri=reference_uri, key=key, cert=crt)
501525
res = XMLVerifier().verify(etree.tostring(signed_root), x509_cert=crt, expect_references=True)
502-
signed_data_root = res.signed_xml
526+
signed_data_root = res[0].signed_xml
503527
ref = signed_root.xpath(
504528
"/samlp:Response/saml:Assertion/ds:Signature/ds:SignedInfo/ds:Reference",
505529
namespaces={

0 commit comments

Comments
 (0)