-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
Bug: Auto-inserted rel=preload
script resources are blocked by nonce-CSP
#26781
Comments
cc @gnoff |
I was testing Just to add onto the use case in the initial post - this also happens for preload links which are generated for I'm happy to take a pass at pushing up a PR for this if there is no disagreement that this should be the expected behavior. |
@ingmarh thanks for reporting. yes we should be using the nonce on the preload if it was present on the underlying script. @liuyenwei if you want to take a pass at putting together a PR that's be great. I'll review. If you don't think you will be able to provide one this week I'll take care of it soon. let me know if you intend to (no pressure either way) |
@gnoff sounds good - I can work on that this week 👍 |
Closing per #26826 (review). |
I can confirm that the issue is fixed with the latest canary, @gnoff Thanks for your additional comment #26826 (review). It makes a lot of sense to me. |
In Chrome, auto-inserted
rel=preload
script resources (viarenderToPipeableStream
with "nonce" option) are blocked/not preloaded when the page has a CSP withscript-src 'nonce-NONCE'
set up.React version: Latest Canary,
18.3.0-canary-aef7ce554-20230503
.As far as I can see, the issue exists since
18.3.0-next-28a574ea8-20221027
.The issue doesn't exist in the latest stable version, 18.2.0, since auto-inserted
rel=preload
link tags were added later.Context: We’re currently in the progress of updating React to v18 in a rather big codebase, and tried to use a Canary release for the updated hydration behavior as described in this comment.
Steps to reproduce
Content-Security-Policy: script-src 'nonce-NONCE'
renderToPipeableStream
with the "nonce" optionThe current behavior
React automatically adds
rel=preload
link tags for resources, including scripts. When using a nonce with ascript-src 'nonce-NONCE'
Content-Security-Policy, and passing the nonce value torenderToPipeableStream
with the "nonce" option, the corresponding auto-inserted<link rel="preload" as="script">
tags don't receive the nonce.The script resources aren’t preloaded in Chrome because they’re blocked by the CSP (Firefox and Safari preload correctly).
React streams something like the following:
The expected behavior
When using a nonce value with the
renderToPipeableStream
"nonce" option, the corresponding auto-inserted<link rel="preload" as="script">
tags include the "nonce" attribute so that the resource can preload in Chrome and is not blocked by ascript-src 'nonce-NONCE'
Content-Security-Policy.Related
prefetch-src
and preloadas
w3c/webappsec-csp#542As far as I understood, the updated behavior doesn’t apply for
<link rel=preload>
, though (only for<link rel=prefetch>
).The text was updated successfully, but these errors were encountered: