Skip to content

Commit a0539a2

Browse files
Merge pull request #779 from peppelinux/metadata_exp_handler
Raise SAMLError on failure to parse a metadata file
2 parents 745c592 + 072f814 commit a0539a2

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/saml2/mdstore.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import sys
88
from itertools import chain
99
from warnings import warn as _warn
10-
1110
from hashlib import sha1
1211
from os.path import isfile
1312
from os.path import join
1413

1514
import requests
15+
1616
import six
1717

1818
from saml2 import md
@@ -24,7 +24,6 @@
2424
from saml2 import BINDING_HTTP_REDIRECT
2525
from saml2 import BINDING_HTTP_POST
2626
from saml2 import BINDING_SOAP
27-
2827
from saml2.httpbase import HTTPBase
2928
from saml2.extension.idpdisc import BINDING_DISCO
3029
from saml2.extension.idpdisc import DiscoveryResponse
@@ -612,7 +611,10 @@ def do_entity_descriptor(self, entity_descr):
612611
self.entity[entity_descr.entity_id] = _ent
613612

614613
def parse(self, xmlstr):
615-
self.entities_descr = md.entities_descriptor_from_string(xmlstr)
614+
try:
615+
self.entities_descr = md.entities_descriptor_from_string(xmlstr)
616+
except Exception as e:
617+
raise SAMLError(f'Failed to parse metadata file: {self.filename}') from e
616618

617619
if not self.entities_descr:
618620
self.entity_descr = md.entity_descriptor_from_string(xmlstr)

tests/invalid_metadata_file.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this content is invalid

tests/test_30_mdstore.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from unittest.mock import Mock
88
from unittest.mock import patch
99

10+
from pytest import raises
11+
1012
import responses
1113

1214
from six.moves.urllib import parse
@@ -19,6 +21,7 @@
1921
from saml2.mdstore import name
2022
from saml2 import sigver
2123
from saml2.httpbase import HTTPBase
24+
from saml2 import SAMLError
2225
from saml2 import BINDING_SOAP
2326
from saml2 import BINDING_HTTP_REDIRECT
2427
from saml2 import BINDING_HTTP_POST
@@ -156,6 +159,10 @@
156159
"class": "saml2.mdstore.MetaDataFile",
157160
"metadata": [(full_path("swamid-2.0.xml"),)],
158161
}],
162+
"14": [{
163+
"class": "saml2.mdstore.MetaDataFile",
164+
"metadata": [(full_path("invalid_metadata_file.xml"),)],
165+
}],
159166
}
160167

161168

@@ -170,6 +177,12 @@ def _fix_valid_until(xmlstring):
170177
xmlstring)
171178

172179

180+
def test_invalid_metadata():
181+
mds = MetadataStore(ATTRCONV, sec_config, disable_ssl_certificate_validation=True)
182+
with raises(SAMLError):
183+
mds.imp(METADATACONF["14"])
184+
185+
173186
def test_swami_1():
174187
UMU_IDP = 'https://idp.umu.se/saml2/idp/metadata.php'
175188
mds = MetadataStore(ATTRCONV, sec_config,

0 commit comments

Comments
 (0)