[英]Self submitting form in a node application
嗨,我正在使用node-oidc-provider
實現 SSO 客戶端
背景:
node-oidc-provider
有一個內置的表單提交,要求用戶通過單擊提交隱藏表單的按鈕“確認他想退出”,該按鈕將撤銷他的 OAuth 令牌。
我想通過在頁面加載時自行提交表單來跳過確認步驟,就像 package 作者在這里建議的那樣
問題:
我在腳本和元標記中添加了隨機數,但瀏覽器仍然拒絕加載我的腳本
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>`;
查看 browsers.network 選項卡中的請求,我看到了 nonce
然而,當瀏覽器呈現響應時,nonce 被剝離引用 CSP 違規,我猜元頭有問題但是在閱讀 CSP 文檔后我無法找出錯誤
Firefox: Content Security Policy: The page's settings blocked the loading of a resource at inline (“script-src”).
看起來您同時發布了兩個 CSP - 第一個是 HTTP header,第二個是通過元標記發布的。
在這種情況下,所有來源都應該通過兩個 CSP unscratched 才能被允許,但第一個 CSP 沒有nonce
。
據推測,第一個 CSP 是Helmet中間件發布的默認 CSP,它在 NodeJS 的依賴項中。
查看HTTP 回復header,說明書在這里。
如果存在Content-Security-Policy
HTTP header,您有 2 個選擇:
nonce
添加到 HTTP header 並刪除元標記 CSP。如果這是 Helmet 的技巧,那么可以使用以下方法關閉 CSP:
// This disables the `contentSecurityPolicy` middleware but keeps the rest.
app.use(
helmet({
contentSecurityPolicy: false,
})
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.