-
Notifications
You must be signed in to change notification settings - Fork 107
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
Allow reading nonce if it was included in header #269
Conversation
If the nonce was generated before the CSP headers were set, then allow reading it with request.csp_nonce. If the CSP headers were set with no nonce, then continue raising CSPNonceError when reading it as a string. If read as a boolean, then return False. This will allow other middleware like django-debug-toolbar to alter the response after the CSP middleware runs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @robhudson, I'll add some docs. And maybe another middleware that always generates the nonce...
This variant middleware always generates the nonce. This is useful when a process that runs after the middleware needs the nonce. One example is the middleware used by django-debug-toolbar (DDT). It needs to be defined early in the MIDDLEWARE list that it can inject HTML, CSS, and JavaScript after the response has been generated. DDT users could use this middleware to ensure the CSP nonce is always available for its asset.
I added a commit:
I meant to do this over two commits, |
The I like these changes. |
If the nonce was generated before the CSP headers were set, then allow reading it with
request.csp_nonce
.If the CSP headers were set with no nonce, then continue raising
CSPNonceError
when reading it as a string. If read as a boolean (if request.csp_nonce
), then returnFalse
.This will allow other middleware like django-debug-toolbar to alter the response after the CSP middleware runs, fixing #268.