-
Notifications
You must be signed in to change notification settings - Fork 102
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
Adding nonce to script tags rendered by Django Form Assets (Media class) #119
Comments
If this is still an issue, I have created a workaround. It is a template tag you can use ( |
Tested, it works :) It would be cool if it could be included in django-csp |
I've expanded |
i'm indeed also using django-csp-helpers as a workaround. 👍 |
That's cool I wasn't aware of the Media class. Assuming we can make people aware that they might be whitelisting assets from other domains similar to #127 (comment), then this seems like an extension of the template tags. I'm open to landing Media class support in django-csp, but not planning on working on it. I don't think it blocks #135 but django core might want the feature. |
I can look into merging this in. I'm not super familiar with the logistics of the licensing stuff. Do I need to copy the license over from https://github.com/dmptrluke/django-csp-helpers/blob/master/LICENSE to somewhere in django-csp? |
To add a nonce to script tags rendered by Django Form Assets, you can:
After the code is implemented, the CustomMedia class will be used to render script tags for Django form assets (such as JavaScript files specified using the Media class in MyForm). The render_js method sets a nonce attribute on each script tag, using the value from CSP_NONCE specified in the Django CSP settings. Lastly, to add a nonce to other script tags in your Django views, you can use the csp_nonce variable in your HTML template like this:
To add a nonce to script tags rendered by Django Form Assets (Media class) using django-csp, you can create a custom widget that adds the nonce to the script tag. Here is an example:
This custom widget class extends Django's MediaDefiningClass and adds a nonce to any script tags in the widget's media definition. The get_nonce() function is provided by django-csp and generates a random nonce for each request. To use this custom widget class, you can create a new widget that inherits from TextInput (or any other widget) and sets metaclass to NonceMediaDefiningClass:
This will ensure that any script tags rendered by the widget will include a nonce attribute with a random value. If you are copying code over from another open-source project with a different license, you should always make sure that you comply with the terms of the original project's license. In this case, it looks like django-csp-helpers is licensed under the MIT License. If you are copying code from django-csp-helpers to django-csp, you should ensure that you include a copy of the MIT License in the LICENSE file of django-csp or somewhere else in the project directory. It's also a good practice to provide attribution to the original project and its contributors in your project's documentation, README file, or relevant sections of your code. When copying code from django-csp-helpers to django-csp, you should include the MIT License with the copied code. You can do this by adding a copy of the LICENSE file from django-csp-helpers to django-csp, or by including the license text in a comment at the top of the copied code. This ensures that the license terms are clear and that users of the code understand their rights and obligations. |
Hi! While MIT and BSD are largely the same license, I give permission for code from django-csp-helpers to be added to this project under the existing license without including any extra licensing or attribution. |
Django allows to associate different files – like stylesheets and scripts – with the forms and widgets that require those assets, using the Media class described here.
I have looked at the source code for Django's widgets.py, but I can't find an elegant way to add the nonce to the rendered <script> tag.
Could you suggest a workaround ? Or is this something that should be addressed in Django itself ?
Thanks for your help.
The text was updated successfully, but these errors were encountered: