google-chrome - 最新 Chrome 85 更新后的 CORS 问题

标签 google-chrome nginx cors passenger-nginx

我是这里的新用户,如果我违反任何规则,请提前道歉。这是我面临的问题,需要建议。
我有一个 Chrome 扩展程序,它可以与 Gmail 一起使用,并通过 Rails 应用程序的 Phusion 乘客服务器使用我在 nginx 上运行的 Web 服务器中的 API。
我的 Nginx 版本是 nginx 版本:nginx/1.15.8 和 Phusion Passenger 版本是 Phusion Passenger Enterprise 6.0.1
我在 nginx 中有 CORS 设置如下:

####### CORS Management ##########

add_header 'Access-Control-Allow-Origin' 'https://mail.google.com,https://*.gmail.com';

add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, HEAD';

add_header Referrer-Policy "no-referrer";

add_header Pragma "no-cache";

##################################

这在 Chrome 84 之前一直有效,但是,随着 Chrome 85 的最新更新,它开始抛出 CORS 错误,如下所示:
########## Chrome 85 中开始出现错误 ############
从源“https://mail.google.com”访问“https://my-site.com/”已被 CORS 政策阻止:没有“Access-Control-Allow-Origin” header 请求的资源。
########################################
在此之后,我根据来自各种来源和博客的建议/引用将 CORS 设置更新为完全开放,现在更新后的 CORS 设置如下所示:
更新 Nginx 中的 CORS 设置

 location / {

     if ($request_method = 'OPTIONS') {

        add_header 'Access-Control-Allow-Origin' $http_origin always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        
        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #

        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type' always;
        
        #
        # Tell client that this pre-flight info is valid for 20 days
        #

        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8' always;
        add_header 'Content-Length' 0 always;

        return 204;
     }

     if ($request_method = 'POST') {

        add_header 'Access-Control-Allow-Origin' $http_origin always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type' always;

     }

     if ($request_method = 'GET') {

        add_header 'Access-Control-Allow-Origin' $http_origin always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type' always;

     }

}
##########################################
在 Nginx 中更新此设置后,CORS 错误消失了,但现在当扩展程序进行 API 调用时,我从服务器收到 401 Unauthorized 错误。
我尝试调整所有方法,但无法修复它。有什么我遗漏或做的不同的事情吗?
请帮忙!

最佳答案

这不是这个规范变化的影响吗?
Chrome 扩展内容脚本中跨域请求的更改
https://www.chromium.org/Home/chromium-security/extension-content-script-fetches

关于google-chrome - 最新 Chrome 85 更新后的 CORS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63666195/

相关文章:

nginx:阻止目录但允许访问子目录

nginx - CentOS 6.6 与 Nginx 1.6.2 - 突然无法重启 nginx - nginx : [emerg] open() “/usr/share/nginx/on” failed (13: Permission denied)

html - 创建 Google Chrome 扩展以将新选项卡设置为本地 HTML 文件和样式表

google-chrome - 检测另一个浏览器选项卡是否正在使用语音识别

javascript - 难以使用 JavaScript 突出显示插件

nginx - 如何配置Chef Solo在新的Vagrant盒子上安装Nginx?

AngularJS + Nginx CORS

http - Cors Post 请求上的 Axios 网络错误,状态代码为 200

Azure CDN 不尊重 Blob 的 CORS

javascript - Chrome 中的 CKEditor 光标在聚焦时不会闪烁