-
|
Sorry if I'm missing a obvious solution here. I'm trying to create a per-request nonce for CSP as suggested here: https://content-security-policy.com/nonce/ I figured out that entry-server.tsx export default createHandler(
renderAsync((event) => <StartServer event={event} />, { nonce: crypto.randomUUID() })
)But how can I access this value later when generating eg. the CSP header or other things like fixed header scripts? root.tsx export default function Root() {
const nonce = "???"
return (
<>
<HttpHeader name="Content-Security-Policy" value={csp(nonce)} />
<Html lang="en">
<Head>
<Meta charset="utf-8" />
<CustomHeaderScript nonce={nonce} />
...I tried using a custom middleware to store the nonce in export default createHandler(
({ forward }) => {
return async (event) => {
event.locals.nonce = crypto.randomUUID()
return forward(event)
}
},
renderAsync((event) => <StartServer event={event} />, { nonce: ??? })
)Any advice would be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Right. We didn't work through this as of yet. I guess you could recreate the renderAsync middleware and directly patch the render call. But we need a solution for this. |
Beta Was this translation helpful? Give feedback.
-
|
I'm currently trying to make CSP work with SolidStart 1.0 After adding this extremely simple middleware import { createMiddleware } from "@solidjs/start/middleware";
export default createMiddleware({
onRequest: [
(event) => {
// @ts-ignore
event.nonce = crypto.randomUUID();
}
]
});the random nonce is automatically added to almost every script tag, except:
I'll investigate this further and update this post if I can make it work on my own. Any help would be appreciated 🙏 |
Beta Was this translation helpful? Give feedback.
createHandlernow accepts{ nonce?: string }in 2nd paramoptions | event => options. This also passesnoncetoHydrationScriptthrough hydration context whereasevent.nonceis used for other scripts.can't seem to reproduce modulepreload script though, please provide if possible. but it might be related to this.
solid-start/packages/start/src/server/StartS…