跨域资源共享 (CORS) 身份验证传输配置错误¶
ID: js/cors-misconfiguration-for-credentials
Kind: path-problem
Security severity: 7.5
Severity: error
Precision: high
Tags:
- security
- external/cwe/cwe-346
- external/cwe/cwe-639
- external/cwe/cwe-942
Query suites:
- javascript-code-scanning.qls
- javascript-security-extended.qls
- javascript-security-and-quality.qls
服务器可以发送 "Access-Control-Allow-Credentials"
CORS 标头,以控制浏览器何时可以在跨域 HTTP 请求中发送用户凭据。
当 Access-Control-Allow-Credentials
标头为 "true"
时,Access-Control-Allow-Origin
标头必须具有与 "*"
不同的值,才能使浏览器接受该标头。因此,要允许跨域请求(包括凭据)使用多个来源,服务器必须动态计算 "Access-Control-Allow-Origin"
标头的值。因此,从请求到服务器的信息中计算此标头值可能允许攻击者控制浏览器发送凭据的来源。
建议¶
当 Access-Control-Allow-Credentials
标头值为 "true"
时,如果 Access-Control-Allow-Origin
标头的动态计算依赖于用户控制的输入,则必须包含清理步骤。
由于攻击者很容易获得 “null” 来源,因此在 Access-Control-Allow-Credentials
标头值为 "true"
时,永远不要使用 “null” 作为 Access-Control-Allow-Origin
标头的值。
示例¶
在下面的示例中,服务器允许浏览器在跨域请求中发送用户凭据。请求标头 origins
控制此类跨域请求的允许来源。
var https = require('https'),
url = require('url');
var server = https.createServer(function(){});
server.on('request', function(req, res) {
let origin = url.parse(req.url, true).query.origin;
// BAD: attacker can choose the value of origin
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader("Access-Control-Allow-Credentials", true);
// ...
});
这不是安全的,因为攻击者可以选择 origin
请求标头的值,使浏览器将其凭据发送到他们自己的服务器。使用包含跨域请求允许来源的白名单可以解决此问题。
var https = require('https'),
url = require('url');
var server = https.createServer(function(){});
server.on('request', function(req, res) {
let origin = url.parse(req.url, true).query.origin,
whitelist = {
"https://example.com": true,
"https://subdomain.example.com": true,
"https://example.com:1337": true
};
if (origin in whitelist) {
// GOOD: the origin is in the whitelist
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader("Access-Control-Allow-Credentials", true);
}
// ...
});
参考¶
Mozilla 开发者网络:CORS, Access-Control-Allow-Origin.
Mozilla 开发者网络:CORS, Access-Control-Allow-Credentials.
PortSwigger:利用 CORS 配置错误获取比特币和奖励
通用弱点枚举:CWE-346.
通用弱点枚举:CWE-639.
通用弱点枚举:CWE-942.