Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any thoughts on content security policy / django-csp? #34

Open
gunnar-rydberg opened this issue Apr 9, 2025 · 3 comments
Open

Any thoughts on content security policy / django-csp? #34

gunnar-rydberg opened this issue Apr 9, 2025 · 3 comments
Labels
question Further information is requested

Comments

@gunnar-rydberg
Copy link

gunnar-rydberg commented Apr 9, 2025

When I migrated from 0.11a11 to .11b4 I had to mess around with content security policy hashes for the importmap added the django admin page. (Was this added in the refactoring?...)

We are using the django-cps package for reference. (version 3.8).

Of course to just get things working, we just add the required CPS hash to the headers of each response. (Handled by django-cps configuration)

However since you are working on django_js_assets: Maybe one could make it interact with django-cps and add the correct nounce to the script-tag containing importmap.

@matthiask
Copy link
Owner

Yep, that's definitely something I'm interested in pursuing. Do you have a working solution you could share, or at the very least some snippets showing how the output would/should look like?

@gunnar-rydberg
Copy link
Author

I'm afraid not.

Looks like django-csp adds cps_nonce to djangos request object.

And then for any instance of (in this case) <script> you want to do something like this:

<script type="importmap" nonce="{{ request.csp_nonce }}">
...
</script>

However the request context is not automatically propagated down to the widget initialization.

@matthiask
Copy link
Owner

(Dumping links)

I think django-csp-helpers has an interesting idea on how to implement something like this:
https://github.com/dmptrluke/django-csp-helpers/blob/master/csp_helpers/classes.py

I have looked at overriding media classes to support additional attributes but this hasn't gone too far yet, but here's the pull request which would enable us to add our own Media class in any arbitrary position during the forms.Media merging and injecting the CSP attribute everywhere:
django/django#19058

Also, here's a DEP draft which I haven't found the time to work on a lot yet:
django/deps#101

I have generated some code yesterday for django-js-asset:
matthiask/django-js-asset#15

@matthiask matthiask added the question Further information is requested label Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants