UNPKG

2.13 kBMarkdownView Raw
1---
2title: Security
3---
4
5# Security
6
7[MODES: framework, data]
8
9<br/>
10<br/>
11
12This is by no means a comprehensive guide, but React Router provides features to help address a few aspects under the _very large_ umbrella that is _Security_.
13
14## `Content-Security-Policy`
15
16### Framework Mode without RSC
17
18If you are implementing a [Content-Security-Policy (CSP)][csp] in your application, specifically one using the `unsafe-inline` directive, you will need to specify a [`nonce`][nonce] attribute on the inline `<script>` elements rendered in your HTML.
19
20Add a nonce to these two spots in [`entry.server.tsx`][entryserver]:
21
22- The [`<ServerRouter nonce>`][serverrouter] prop
23 - This will be proxied along through React Context and used for other Framework Mode components that output `nonce`-aware elements, including [`<Scripts>`][scripts], [`<ScrollRestoration>`][scrollrestoration]
24 - If those components specify their own `nonce` prop, it will override the `ServerRouter` value
25- The `nonce` options of [`renderToPipeableStream`][renderToPipeableStream]/[`renderToReadableStream`][renderToReadableStream]
26
27### RSC Framework and RSC Data Mode
28
29For RSC Framework and RSC Data Mode, generate the nonce in `entry.ssr.tsx` and pass it to `routeRSCServerRequest`, `RSCStaticRouter`, and the CSP response header. See the [RSC Content Security Policy nonce guide][rsc-csp]. The nonce is only needed while generating the HTML document; it should not be included in the RSC payload or passed to `matchRSCServerRequest`.
30
31[csp]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP
32[entryserver]: ../api/framework-conventions/entry.server.tsx
33[nonce]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce
34[renderToPipeableStream]: https://react.dev/reference/react-dom/server/renderToPipeableStream
35[renderToReadableStream]: https://react.dev/reference/react-dom/server/renderToReadableStream
36[scripts]: ../api/components/Scripts
37[scrollrestoration]: ../api/components/ScrollRestoration
38[serverrouter]: ../api/framework-routers/ServerRouter
39[rsc-csp]: ./react-server-components#content-security-policy-nonces