CORS(跨域资源共享)来源验证失败
漏洞详情:
CORS(跨域资源共享)定义了一种允许客户端跨域请求的机制。此应用程序正以不安全的方式使用 CORS。
Web 应用程序无法正确验证来源报头(请查看“详细信息”部分以获取更多信息)并返回报头 Access-Control-Allow-Credentials: 。<span class=""bb-dark"">true</span>
在此配置中,任何网站均可发出通过用户凭据提出的请求并读取对这些请求的响应。信任任意来源会有效地禁用同源策略,从而允许第三方网站进行双向交互。
漏洞验证举例:
漏洞构造请求:
GET /api/ HTTP/1.1
Origin: https://www.example.com
Cookie: sync_networkid=undefined; sync_userid=undefined
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Encoding: gzip,deflate,br
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.2210.91
Host: ywzt.tjwatergroup.com:8090
Connection: Keep-alive
漏洞验证详情:
origin:https://www.example.com
msg:Any origin is accepted (arbitrary Origin header values are reflected in Access-Control-Allow-Origin response headers).
解决方案:
在 Access-Control-Allow-Origin 报头中仅允许选定的受信任域。
在Nginx上修复CORS安全漏洞的配置方法
要在Nginx上正确处理CORS并修复这个安全漏洞,您需要配置服务器只允许特定的可信来源,并正确设置相关CORS头。以下是详细的配置方案:
基础安全配置
1. 仅允许特定来源(推荐方案)
server {
# ...其他配置...
# 定义允许的源列表
map $http_origin $cors_origin {
default "";
"~^https://(trusted-domain1\.com|trusted-domain2\.com)$" $http_origin;
}
location / {
# 只设置匹配的源
if ($cors_origin) {
add_header 'Access-Control-Allow-Origin' $cors_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Vary' 'Origin';
}
# 允许的方法
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
# 允许的头部
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
# 预检请求缓存时间
add_header 'Access-Control-Max-Age' 1728000;
# ...其他配置...
}
}
2. 处理OPTIONS预检请求
location / {
# 处理OPTIONS预检请求
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' $cors_origin;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
# ...其他正常请求处理...
}
高级安全配置
1. 针对特定路径的CORS配置
location /api/ {
# 只对API路径启用CORS
if ($http_origin ~* (https://trusted-domain1\.com|https://trusted-domain2\.com)) {
set $cors "1";
}
if ($cors = "1") {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Vary' 'Origin';
}
# ...其他API配置...
}
2. 完全禁用CORS(最严格方案)
如果您的应用不需要跨域访问:
server {
# 禁用所有CORS
add_header 'Access-Control-Allow-Origin' none;
add_header 'Access-Control-Allow-Methods' none;
add_header 'Access-Control-Allow-Headers' none;
add_header 'Access-Control-Allow-Credentials' false;
# ...其他配置...
}
验证配置
配置完成后,使用以下方法验证:
- 使用curl测试:
curl -H "Origin: https://malicious.com" -I https://yourdomain.com/api/endpoint
应该看不到Access-Control-Allow-Origin头
- 使用合法来源测试:
curl -H "Origin: https://trusted-domain1.com" -I https://yourdomain.com/api/endpoint
应该看到正确的CORS头
注意事项
不要使用通配符:避免
Access-Control-Allow-Origin: *,特别是当使用Access-Control-Allow-Credentials: true时Vary头很重要:确保包含
Vary: Origin头,防止缓存污染限制HTTP方法:只允许必要的HTTP方法(GET/POST等)
定期审查:定期检查允许的来源列表,移除不再需要的域名
结合其他安全措施:CORS不是安全功能的替代品,应与其他安全措施(如CSRF保护)结合使用
nginx解决方案
location /api {
# 动态匹配信任的域名和端口
if ($http_origin ~* ^https?://(www\.example\.com(:443|:80)?|bbs\.example\.com:1433)$) {
set $cors "1";
}
if ($cors = "1") {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Vary' 'Origin';
}
# 其他API配置...
}
通过以上配置,您可以有效修复Nginx服务器上的CORS安全漏洞,同时保持必要的跨域功能。