这绝不是一份全面的指南,但 React Router 提供了一些功能,帮助解决“安全性”这个庞大主题下的几个方面。
Content-Security-Policy
如果您在应用程序中实施内容安全策略 (CSP),特别是使用了 unsafe-inline
指令的策略,您将需要为 HTML 中渲染的内联 <script>
元素指定一个 nonce
属性。这必须在任何生成内联脚本的 API 上指定,包括:
<Scripts nonce>
(root.tsx
)<ScrollRestoration nonce>
(root.tsx
)<ServerRouter nonce>
(entry.server.tsx
)renderToPipeableStream(..., { nonce })
(entry.server.tsx
)renderToReadableStream(..., { nonce })
(entry.server.tsx
)