pysaml2 icon indicating copy to clipboard operation
pysaml2 copied to clipboard

Python 3.13 Compatibility

Open ephes opened this issue 10 months ago • 1 comments

Hi there,

First off, thank you for this amazing library! I recently wrote a blog post demonstrating how to use Django as a service provider, with pysaml2 serving as an example IdP. A reader has since pointed out that it doesn’t work with Python 3.13, and after some investigation, I suspect the issue stems from the removal of the cgi module in Python 3.13.

I noticed that pysaml2 still imports cgi in a couple of places. For example:

In src/saml2/httputil.py:

# The `cgi` module is used here:
formdata = cgi.parse(environ["wsgi.input"], environ, empty, err)

# Since `parse_qs` is already imported, this could potentially be replaced with:
input_stream = environ["wsgi.input"]
content_length = int(environ.get("CONTENT_LENGTH", 0))
form_data_bytes = input_stream.read(content_length)
form_data = parse_qs(form_data_bytes.decode('utf-8'))

In src/saml2/pack.py and src/saml2/httputil.py:

# The fallback to `cgi` for HTML escaping could be removed, as the `html` module 
# (introduced in Python 3.4) provides the necessary functionality:
try:
    import html
except Exception:
    import cgi as html  # type: ignore[no-redef]

Would it make sense to update these references to make pysaml2 compatible with Python 3.13? I’d be happy to help test any changes.

Best regards, Jochen

ephes avatar Jan 19 '25 07:01 ephes

Would you like to make a PR with this change? I'd be happy to accept it.

c00kiemon5ter avatar Feb 10 '25 21:02 c00kiemon5ter