手动注入请求Origin导致返回Access-Control-Allow-Origin:*使用nginx漏洞修复

http {
  map $http_origin $allow_cors {
    default 1; # 默认不允许跨域
    "~^(http://(dz.xxx.com)?)$" 1; # 允许特定域名跨域
    "~http://localhost$" 1; # 允许localhost跨域
    "~http://10.132.233.224$" 1; # 允许特定IP跨域,同时修正了正则表达式的错误
    "~*" 0;
  }

  server {
    listen 80;

    location /testss/ {
        if ($allow_cors != 1) {
            return 403; # 如果不允许跨域,则返回403状态码
        }

        # 如果允许跨域,则设置相应的Access-Control-Allow-Origin头
        if ($http_origin ~* (http://(dz.xxx.com|localhost|10.132.233.224))) {
            add_header 'Access-Control-Allow-Origin' "$http_origin";
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }

        # 处理OPTIONS请求,某些浏览器在发送跨域请求前会发送OPTIONS请求进行预检
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' "$http_origin";
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            return 204; # 返回204状态码,表示不需要发送实体内容
        }

        # 其他location配置...
    }

    # 其他server配置...
  }

}

在本次处理中,我们直接将手动注入请求Origin报头直接屏蔽,可修复相关注入漏洞

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值