-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Mark client-side scripts as safe to use for browsers (#20087) #20107
Mark client-side scripts as safe to use for browsers (#20087) #20107
Conversation
PR Summary
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #20107 +/- ##
============================================
- Coverage 63.64% 63.42% -0.22%
- Complexity 11376 11378 +2
============================================
Files 429 429
Lines 37073 37077 +4
============================================
- Hits 23594 23515 -79
- Misses 13479 13562 +83 ☔ View full report in Codecov by Sentry. |
Are you sure about that? AFAIK if header is added by webserver, it is done after generating response by PHP, so PHP does not have access to this header. |
You are right, that was an incorrect assumption. I will revise the request. This will make the comparison easier. |
The initial solution of setting only the CSP nonce via the Response property in the application configuration has become less attractive after review. What do you think about the new version? |
5778147
to
676a6ec
Compare
It looks OK. |
It seems the
I will add the missing variable shortly. |
28dc476
to
838dd65
Compare
361dd46
to
3071e20
Compare
Thanks! |
Glad to contribute :) |
The important choices made and the reasons why are summarized below. I'm open to suggestions for improvements.
What attribute should be added to the script tag?
Only the nonce
Custom attributes
Solution 1 -- Only the nonce
This section assumes only the nonce is added.
Where should the nonce script attribute be added?
In the BaseHtml helper
Set the nonce attribute in all script tags via
yii\helpers\BaseHtml::script()
when it exists. This is similar to how the hidden CSRF token input is added to all forms viayii\helpers\BaseHtml::beginForm()
.Adding it at a low level ensures all script tags are whitelisted and makes it easier to maintain.
How should the nonce be called?
From a function in the Response component
Where should the nonce be set?
In the application configuration
Solution 2 -- Custom attributes
Set custom script attributes centrally in web.php:
Updates required to make this work:
jsOptions
toyii\web\View
.jsOptions
insideyii\helpers\Html::script()
.