敏感 Cookie 的明文传输¶
ID: js/clear-text-cookie
Kind: problem
Security severity: 5.0
Severity: warning
Precision: high
Tags:
- security
- external/cwe/cwe-614
- external/cwe/cwe-311
- external/cwe/cwe-312
- external/cwe/cwe-319
Query suites:
- javascript-code-scanning.qls
- javascript-security-extended.qls
- javascript-security-and-quality.qls
以明文形式传输的 Cookie 可能被攻击者拦截。如果敏感 Cookie 被拦截,攻击者可以读取 Cookie 并使用它代表用户执行操作。
建议¶
始终使用 SSL 传输敏感 Cookie,方法是在 Cookie 上设置“secure” 属性。
示例¶
以下示例将身份验证令牌存储在 Cookie 中,该 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-614.
通用弱点枚举: CWE-311.
通用弱点枚举: CWE-312.
通用弱点枚举: CWE-319.