RFC Discussion: Content Security Policies #1244
Replies: 2 comments 2 replies
-
I'm torn on this one. I've seen from another open-source project (single-spa) that setting a CSP by default turns into a fairly large maintenance burden due to all the devs that are either just trying out the project or have never used or understood CSPs. It's nice that we can give good security by default, but we also turn into the people that have to educate what a CSP is, how to change it, and manage all the questions and issues that come from that. |
Beta Was this translation helpful? Give feedback.
-
Not sure if this is possible with streaming. However, it's possible to pass a // App.server.jsx
// ...
const handleRequest = renderHydrogen(App);
function generateNonce (numberOfBytes) {
const bytes = new Uint8Array(numberOfBytes);
crypto.getRandomValues(bytes);
return btoa(String.fromCharCode.apply(null, bytes));
}
export default async function(request, options) {
const nonce = import.meta.env.PROD ? generateNonce(18) : undefined;
const response = await handleRequest(request, {...options, nonce});
if (nonce) {
response.headers.set(
'Content-Security-Policy',
`default-src 'self' data: 'unsafe-inline'; script-src 'nonce-${nonce}' 'self' https://cdn.shopify.com; img-src 'self' https://cdn.shopify.com; style-src 'unsafe-inline' 'self' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com`,
);
}
return response;
}
I think with the nonce we don't need |
Beta Was this translation helpful? Give feedback.
-
Hydrogen doesn't provide a default content security policy. Instead it needs to be manually added. This can be done multiple ways:
1. Headers within
App.server.jsx
:Pros:
Cons:
unsafe-inline
directive because streaming relies on inline scripts. This makes a hydrogen app more susceptible to XSS attacks.2.
meta
tags withinApp.server.jsx
:Pros:
unsafe-inline
directive can be used, because by the time the meta tag is added, most likely all streamed inline scripts have executed from the server. This makes a hydrogen app more secure.Cons:
Questions
<meta>
tag with anunsafe-inline
CSP where we know no other content is going to stream and be broken by the CSP?default-src 'self' data: cdn.shopify.com fonts.googleapis.com fonts.gstatic.com
Beta Was this translation helpful? Give feedback.
All reactions