gpt4 book ai didi

safari - Safari 中的跨源资源共享策略拒绝跨源重定向

转载 作者:行者123 更新时间:2023-12-02 00:40:38 27 4
gpt4 key购买 nike

我们有一个重定向到另一台服务器的 API 端点。它是通过 XHR 调用的,似乎在大多数浏览器中都能正常工作,除了 Safari(特别是在 iOS 上)。

我在控制台中遇到的错误是:跨源重定向被跨源资源共享策略拒绝

我们在执行重定向的页面和其他服务器上都有 CORS。重定向页面设置:

Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: false

其他服务器有:

Access-Control-Allow-Origin: *

如何在 CORS 策略中允许重定向?

最佳答案

W3C 规范和其他权威来源直接禁止通配符 Access-Control-Allow-OriginAccess-Control-Allow-Credentials: true 一起使用。

Note: The string "*" cannot be used for a resource that supports credentials.

https://www.w3.org/TR/cors/#resource-requests

Important note: when responding to a credentialed request, server must specify a domain, and cannot use wild carding.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials

If credentials mode is "include", then Access-Control-Allow-Origin cannot be *.

https://fetch.spec.whatwg.org/#cors-protocol-and-credentials

进一步步骤

由于您的问题缺乏细节,让我们做一些定义:

  • domain-a 是您的客户端代码的域
  • domain-b 是您向其发出请求的端点的域
  • domain-c是请求最终重定向到的域

首先,我认为您想要找到一个解决方法。只要您告诉所有端点都在您的控制之下,那么您可以:

  • 域-a域-c发出直接请求(或者甚至发出条件请求,如果重定向取决于参数)
  • 在后端公开另一个端点,将请求包装到domain-b

WebKit tracker 报告错误也很重要如果它确实违反了规范。为了更轻松地重现该案例,我制作了一个 CherryPy 应用程序,您可以将其附加到报告中。运行步骤:

  1. 将“127.0.0.1 域-a 域-b 域-c”添加到您的 /etc/hosts
  2. 将以下代码放入corsredirect.py
  3. 在终端中运行这些命令

     virtualenv -p python3 venv
    . venv/bin/activate
    pip install cherrypy
    python corsredirect.py
  4. 将浏览器指向 http://domain-a:8080然后按下按钮

这就是应用程序。

#!/usr/bin/env python3
'''
Add localhost aliases in /etc/hosts for "domain-a", "domain-b", "domain-c".

The flow is: [domain-a] --CORS-GET--> [domain-b] --redirect--> [domain-c].
Open as http://domain-a:8080/
'''


import cherrypy


def cors():
cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'

cherrypy.tools.cors = cherrypy._cptools.HandlerTool(cors)


class App:

@cherrypy.expose
def index(self):
return '''<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=utf-8' http-equiv='content-type'>
<title>CORS redirect test</title>
</head>
<body>
<button>make request</button>
<script type='text/javascript'>
document.querySelector('button').onclick = function()
{
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://domain-b:8080/redirect', true);
xhr.onload = function()
{
var text = xhr.responseText;
console.log('success', text);
};
xhr.onerror = function()
{
console.error('failure');
};
xhr.send();
};
</script>
</body>
</html>
'''

@cherrypy.expose
@cherrypy.config(**{'tools.cors.on': True})
def redirect(self):
raise cherrypy.HTTPRedirect('http://domain-c:8080/endpoint')

@cherrypy.expose
@cherrypy.config(**{'tools.cors.on': True})
@cherrypy.tools.json_out()
def endpoint(self):
return {'answer': 42}


if __name__ == '__main__':
config = {
'global' : {
'server.socket_host' : '127.0.0.1',
'server.socket_port' : 8080,
'server.thread_pool' : 8
}
}
cherrypy.quickstart(App(), '/', config)

关于safari - Safari 中的跨源资源共享策略拒绝跨源重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40711452/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com