使用node-oidc-provider实现单点登录客户端
背景:
node-oidc-provider有一个内置表单提交,它要求用户通过单击一个提交隐藏表单的按钮来“确认他想要注销”,该按钮将撤销他的OAuth令牌。
我想跳过确认步骤,在页面加载时自动提交表单,就像包作者建议的那样,这里。
问题:
我已经在脚本和元标记中添加了一个nonce,但是浏览器仍然拒绝加载我的脚本
async function getNonce () {
const crypto = require("crypto");
return crypto.randomBytes(16).toString("base64");
}
async function logoutSource (ctx, form) {
// @param ctx - koa request context
const nonce = await getNonce();
ctx.body = `<!DOCTYPE HTML>
<head>
<title>Logout</title>
<meta http-equiv="content-security-policy"
content="
script-src 'nonce-${nonce}' strict-dynamic 'unsafe-inline';
default-src 'self';
">
</head>
<body>
${form}
<script nonce="${nonce}">
var form = document.forms[0];
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'logout';
input.value = 'yes';
form.appendChild(input);
form.submit();
</script>
</body>
</html>`;查看浏览器网络选项卡中的请求,我看到了

但是,当浏览器呈现响应时,以CSP违规为由将当前的响应删除,我猜想元头可能有问题,但是在阅读CSP文档之后,我无法发现错误。
更新1 Chrome显示此错误消息

火狐:Content Security Policy: The page’s settings blocked the loading of a resource at inline (“script-src”).
发布于 2021-10-03 14:14:04
看起来您同时发布了两个CSPs -第一个是ia HTTP头,第二个是通过meta标记。
在这种情况下,所有的源都应该传递两个CSP,这两个CSP都是不被抓取的,但第一个CSP没有nonce。
据推测,第一个CSP是由头盔中间件发布的默认CSP,它位于NodeJS的依赖项中。
检查header,手册是这里。
如果存在Content-Security-Policy header,则有两个选项:
nonce添加到header中,并删除元标记CSP。如果这是头盔的窍门,则可以使用以下方法关闭CSP:
// This disables the `contentSecurityPolicy` middleware but keeps the rest.
app.use(
helmet({
contentSecurityPolicy: false,
})
);https://stackoverflow.com/questions/69392065
复制相似问题