我很难找到我的问题的答案,毫无疑问,因为我不知道要搜索什么,但我希望这里的人可以提供帮助:)
我在托管反应应用程序的节点应用程序中使用随机数实现了头盔。
节点应用中的头盔实现:
app.use((req, res, next) => {
res.locals.cspNonce = crypto.randomBytes(16).toString("hex");
next();
});
app.use(
helmet.contentSecurityPolicy({
useDefaults: true,
directives: {
scriptSrc: ["'self'", (req, res) => `'nonce-${res.locals.cspNonce}'`],
styleSrc: ["'self'", (req, res) => `'nonce-${res.locals.cspNonce}'`],
},
})
);
在我的整个 react 应用程序中,我导入脚本和样式表的唯一位置是我的 index.html 页面的头部。所以我的问题是,我需要在下面的脚本和链接标签中添加什么才能正确使用带有 nonce 的 CSP?
反应应用程序 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
nonce="What to put here, if it even goes here?"
/>
<script
type="text/javascript"
src="https://code.jquery.com/jquery.min.js"
nonce="How do I implement the res.locals.cspNonce here?"
>
</script>
<title>Some title</title>