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

Add Django CSP nonce support #989

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

iamareebjamal
Copy link

@iamareebjamal iamareebjamal commented Feb 17, 2020

  • Add template changes
  • Add branches for non-csp users
  • Check offline compression for every case

Fixes #946

@iamareebjamal iamareebjamal changed the title WIP: Add Django CSP nonce support Add Django CSP nonce support Feb 18, 2020
@iamareebjamal iamareebjamal changed the title Add Django CSP nonce support WIP: Add Django CSP nonce support Feb 18, 2020
@iamareebjamal
Copy link
Author

@diox Added tests and confirmed working in online mode. Just working on somethings for offline mode. Can you please check if you agree with the approach.

About offline mode, there are 2 options:

  1. Add settings.COMPRESS_CSP_NONCE_TAG - this is the easiest one and more configurable, just need to change 1 setting to turn on or off
  2. Add {% compress js csp_nonce %} - this is more involved

@iamareebjamal iamareebjamal changed the title WIP: Add Django CSP nonce support Add Django CSP nonce support Feb 18, 2020
@iamareebjamal
Copy link
Author

Confirmed to be working in offline mode

@iamareebjamal
Copy link
Author

@diox Ping?

@diox
Copy link
Member

diox commented Mar 2, 2020

Sorry for the delay, been busy with other stuff. I haven't tried this yet but it looks good on the surface.

Regarding offline, I am not sure why you'd want a specific tag or setting. Actually I'm not even sure you need COMPRESS_CSP_NONCE setting at all. Is there a scenario where making it work automatically when there is a nonce in the original content would be a bad thing ?

One improvement I think you should do is have a method in the CompressorMixin to get the nonce from a context (that would, in the default implementation, return context['request'].csp_nonce). That way it's possible to override the behavior by using a custom templatetag.

@iamareebjamal
Copy link
Author

I remember that it was needed in offline compression because at the time, context does not have a nonce. If there is an alternative, I'll implement it.

About the method in CompressorMixin, I'll add that

@diox
Copy link
Member

diox commented Mar 2, 2020

I think the setting can be replaced by detecting if there is a nonce in the original content or not.

@iamareebjamal
Copy link
Author

Hmm, how will we detect that? If I match nonce=, it'll create false posiitves because it can be in the content as well

@iamareebjamal
Copy link
Author

Also, the setting gives you the ability to not write nonce in each and every javascript/CSS and get nonce in the output as evident in the test

@diox
Copy link
Member

diox commented Mar 2, 2020

We already have an HTML parser involved when compressing, so we can extract the necessary info in a reliable way and pass it down from there.

@9mido
Copy link

9mido commented May 6, 2020

Any ideas when this will be PR change will take effect? I am looking forward to this since my site uses django-csp.

@iamareebjamal
Copy link
Author

I haven't got enough time to finalize the requested changes after last review, however we are already using it in a production app without any issues. I will still see if I can get some time to change the implementation
However, note that only implementation detail is needed to change, everything else works fine

@manickamx2
Copy link

@iamareebjamal @diox Wanted to bump this, any ideas if/when this PR could be merged? Thanks.

@diox
Copy link
Member

diox commented May 2, 2022

The conflicts would need to be solved and my suggestion from #989 (comment) implemented before we could consider merging this.

@uniuuu
Copy link

uniuuu commented Oct 10, 2022

Hi @diox
Any quick workaround available before the PR will finalized?

@diox
Copy link
Member

diox commented Oct 10, 2022

The logic from that PR could be re-used in a custom class and templates inheriting from compressor. Besides that I don't know. You're welcome to try to pick that PR up again and re-submit it with my comments addressed (crediting original submitter).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Usage when also using django-csp
5 participants