敏感服务器 Cookie 暴露给客户端¶
ID: js/client-exposed-cookie
Kind: problem
Security severity: 5.0
Severity: warning
Precision: high
Tags:
- security
- external/cwe/cwe-1004
Query suites:
- javascript-code-scanning.qls
- javascript-security-extended.qls
- javascript-security-and-quality.qls
如果未设置httpOnly
标志,则服务器存储的身份验证 Cookie 可以被客户端访问。
管理跨站点脚本 (XSS) 攻击的攻击者可以读取 Cookie 并劫持会话。
建议¶
在所有客户端不需要的 Cookie 上设置httpOnly
标志。
示例¶
以下示例在客户端可以查看的 Cookie 中存储身份验证令牌。
const http = require('http');
const server = http.createServer((req, res) => {
res.setHeader("Set-Cookie", `authKey=${makeAuthkey()}`);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h2>Hello world</h2>');
});
要强制 Cookie 使用 SSL 传输,请在 Cookie 上设置secure
属性。
const http = require('http');
const server = http.createServer((req, res) => {
res.setHeader("Set-Cookie", `authKey=${makeAuthkey()}; secure; httpOnly`);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h2>Hello world</h2>');
});
参考文献¶
ExpressJS: 安全使用 Cookie.
OWASP: 正确设置 Cookie 标志.
Mozilla: Set-Cookie.
通用弱点枚举: CWE-1004.