Skip to content

Commit

Permalink
fix: handle possible xml schema exceptions in Test.__init__ - v0.9.21
Browse files Browse the repository at this point in the history
  • Loading branch information
peppelinux committed Sep 28, 2021
1 parent 7dbe18a commit ac12ec3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
15 changes: 14 additions & 1 deletion src/spid_sp_test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import logging
import re
import sys

from pathlib import Path
from .constants import HTTP_NO_PORT_REGEX


BASE_DIR = Path(__file__).resolve().parent
__version__ = "0.9.20"
__version__ = "0.9.21"
__name__ = "spid_sp_test"
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -128,6 +129,18 @@ def _assertHttpUrlWithoutPort(self, check, *args, **kwargs):
def _assertIsValidHttpUrl(self, check, *args, **kwargs):
self._assert(re.match("https?://", check if check else ""), *args, **kwargs)

def handle_init_errors(self, method, description, traceback=""):
self._assertTrue(
False,
method,
description=description,
traceback=traceback,
method=method,
test_id=[],
)
self.is_ok(method)
sys.exit(1)

# maybe useful .. one day ?!
# idp_server = self.idp()
# pysaml2 auth req object with signature check
Expand Down
17 changes: 12 additions & 5 deletions src/spid_sp_test/authn_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,19 @@ def __init__(
self.xsds_files = xsds_files or self.xsds_files
self.xsds_files_path = xsds_files_path or f"{BASE_DIR}/xsd"

self.md = etree.fromstring(self.metadata)
del_ns(self.md)
try:
self.md = etree.fromstring(self.metadata)
del_ns(self.md)

self.doc = etree.fromstring(self.authn_request_decoded)
# clean up namespace (otherwise xpath doesn't work ...)
del_ns(self.doc)
self.doc = etree.fromstring(self.authn_request_decoded)
# clean up namespace (otherwise xpath doesn't work ...)
del_ns(self.doc)
except Exception as e:
_method = f"Error parsing AuthnRequest: {self.authn_request_decoded}"
self.handle_init_errors(
method = _method,
description = f"{e}"
)

# binding detection
self.IS_HTTP_REDIRECT = self.authn_request.get("Signature")
Expand Down
11 changes: 9 additions & 2 deletions src/spid_sp_test/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ def __init__(
self.metadata = self.get(metadata_url)
self.xsds_files_path = xsds_files_path or f"{BASE_DIR}/xsd"

self.doc = etree.fromstring(self.metadata)
try:
self.doc = etree.fromstring(self.metadata)
except Exception as e:
_method = f"Error parsing Metadata: {self.metadata_url}"
self.handle_init_errors(
method = _method,
description = f"{e}"
)
# clean up namespace (otherwise xpath doesn't work ...)
del_ns(self.doc)

Expand Down Expand Up @@ -94,7 +101,7 @@ def xsd_check(self, xsds_files: list = ["saml-schema-metadata-2.0.xsd"]):
self._assertTrue(
False,
msg,
description="xsd test failed",
description=f"{e}",
traceback=f"{e}",
method=_method,
test_id=test_id,
Expand Down

0 comments on commit ac12ec3

Please sign in to comment.