Skip to content

Commit 7398c26

Browse files
committed
Fix csp version detection
1 parent 60b0bc7 commit 7398c26

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

djangosaml2/utils.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import zlib
1919
from functools import lru_cache, wraps
2020
from typing import Optional
21+
from importlib.metadata import version, PackageNotFoundError
2122

2223
from django.conf import settings
2324
from django.core.exceptions import ImproperlyConfigured
@@ -255,22 +256,19 @@ def _django_csp_update_decorator():
255256
else:
256257
# autosubmit of forms uses nonce per default
257258
# form-action https: to send data to IdPs
258-
259259
# Check django-csp version to determine the appropriate format
260260
try:
261-
version = getattr(csp, "__version__", "0.0")
262-
major_version = int(version.split(".")[0])
263-
261+
csp_version = version('django-csp')
262+
major_version = int(csp_version.split('.')[0])
263+
264264
# Version detection successful
265265
if major_version >= 4:
266266
# django-csp 4.0+ uses dict format with named 'config' parameter
267267
return csp_update(config={"form-action": ["https:"]})
268-
else:
269-
# django-csp < 4.0 uses kwargs format
270-
return csp_update(FORM_ACTION=["https:"])
271-
except (AttributeError, ValueError, IndexError):
268+
# django-csp < 4.0 uses kwargs format
269+
return csp_update(FORM_ACTION=["https:"])
270+
except (PackageNotFoundError, ValueError, RuntimeError, AttributeError, IndexError):
272271
# Version detection failed, we need to try both formats
273-
274272
# Try v4.0+ style first because:
275273
# 1. It has better error handling with clear messages
276274
# 2. Newer versions are more likely to be supported in the future

0 commit comments

Comments
 (0)