IdentityPython / pysaml2

Python implementation of SAML2
Apache License 2.0
555 stars 422 forks source link

Zlib error not being catch on inner exception try-catch of entity.py #962

Closed wbtan7 closed 4 months ago

wbtan7 commented 4 months ago

Description

The feature or problem addressed by this PR

For some reason, zlib.error is not being catch on the inner try-catch on entity.py and resolve to UnravelError on the outer exception (supposedly it has to run base64.b64decode silently after error?), if I import zlib from s_utils.py then the try-catch works properly. Not sure if is just me, or is a bug ? I run this in python 3.11.9, pysaml2 7.5.0

# entity.py - def unravel
...
try:
    if binding == BINDING_HTTP_REDIRECT:
        xmlstr = decode_base64_and_inflate(txt)
    elif binding == BINDING_HTTP_POST:
        try:
            xmlstr = decode_base64_and_inflate(txt)
        except zlib.error: # supposedly to error here? but is not for some reason, i tried type(e) clearly shows zlib.error
            xmlstr = base64.b64decode(txt)
    elif binding == BINDING_SOAP:
        func = getattr(soap, f"parse_soap_enveloped_saml_{msgtype}")
        xmlstr = func(txt)
    elif binding == BINDING_HTTP_ARTIFACT:
        xmlstr = base64.b64decode(txt)
    else:
        xmlstr = txt
except Exception: # being caught here instead
    raise UnravelError(f"Unravelling binding '{binding}' failed")
...
What your changes do and why you chose this solution

Could replicate as below, where we raise the error directly, and is supposed to catch by the inner try-catch in entity.py and run base64.b64decode.

# s_utils.py
def decode_base64_and_inflate(string):
    """base64 decodes and then inflates according to RFC1951

    :param string: a deflated and encoded string
    :return: the string after decoding and inflating
    """
    raise zlib.error('test')
    return zlib.decompress(base64.b64decode(string), -15)

Checklist