简体   繁体   English

无法解决的CORS问题! 如何在MacOS上禁用Chrome的相同来源策略?

[英]Unresolvable CORS issue! How to disable the same origin policy of Chrome on MacOS?

For a while, I keep facing a problem about CORS. 一段时间以来,我一直面临有关CORS的问题。 I'm running an ExtJS app with localhost and during REST process on Delete process, it keeps giving this error: 我正在使用localhost运行ExtJS应用程序,并且在Delete进程上的REST过程中,它不断出现此错误:

Response for preflight has invalid HTTP status code 403.

I've already reached these topics; 我已经达到了这些主题。


I did several things but none of them worked for me! 我做了几件事,但没有一件对我有用!

Using extension for CORS: Allow-Control-Allow-Origin: * and here is a screenshot of extension's settings: 对CORS使用扩展程序:Allow-Control-Allow-Origin:*,这是扩展程序设置的屏幕截图: 延伸

I've Chrome Canary on MacOS and running it with web-security-disabled . 我已经在MacOS上安装了Chrome Canary,并在web-security-disabledweb-security-disabled运行了它。 The browser is opening with FLAG and notice that this is web-security-disabled mod but somehow it does not behave as expected. 浏览器正在打开FLAG,并注意到这是已禁用Web安全的mod,但是某种程度上它的行为不符合预期。 Here is the terminal command I've used to run it: 这是我用来运行它的终端命令:

open -a /Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary --args --disable-web-security --/ChromeDisabled

But it's not working as well! 但是效果不佳! So what am I doing wrong? 那我在做什么错? How can I solve this problem? 我怎么解决这个问题?

I guess you want to solve the puzzle why it gives Error 403. The problem lies on the server-side, not in the browser nor in ExtJS. 我想您想解决为什么会出现错误403的难题。问题出在服务器端,而不是浏览器或ExtJS中。

Error 403 means "Unauthorized". 错误403表示“未经授权”。 So, why are you unauthorized? 那么,为什么要未经授权? And what is a "preflight"? 什么是“预检”?

A preflight request is a special request sent to the backend by the browser using the OPTIONS HTTP method. 预检请求是浏览器使用OPTIONS HTTP方法发送到后端的特殊请求。 It is sent before the actual request, and it is sent without headers, cookies or other authentication data. 它在实际请求之前发送,并且不包含标头,cookie或其他身份验证数据而发送。 It should not return the data, only a few headers indicating from which domains CORS requests are allowed to access the URL, and which methods and headers they may send. 它不应返回数据,而仅返回几个标头,这些标头指示允许CORS请求从哪个域访问URL,以及它们可以发送的方法和标头。 If the browser finds that the response information allows it to send the actual request, it will send the actual request and process the returned data. 如果浏览器发现响应信息允许它发送实际的请求,它将发送实际的请求并处理返回的数据。

So, to support CORS, OPTIONS requests against the backend have to always go through unauthenticated, since no authentication information can be sent. 因此,为了支持CORS,针对后端的OPTIONS请求必须始终经过未经身份验证,因为无法发送身份验证信息。 Your backend, however, does not allow OPTIONS requests to go through unauthenticated. 但是,您的后端不允许OPTIONS请求通过未经身份验证的请求。

You may want to check which authentication code you use and try to get OPTIONS requests around authentication (of course, they shouldn't return any data then). 您可能需要检查使用的身份验证代码,并尝试获取围绕身份验证的OPTIONS请求(当然,它们那时不应返回任何数据)。 I have no knowledge about your backend technology, you may want to ask how to solve this in another question with the correct tags; 我对您的后端技术一无所知,您可能想问一下如何使用正确的标签解决另一个问题。 in C# it would be like this (I guess you have similar functions at your disposal somehow): 在C#中会是这样(我想您可以使用类似的功能):

[HttpOptions]
[AllowAnonymous]
public HttpResponseMessage GenerateDemoKey() {
    var response = Request.CreateResponse(HttpStatusCode.OK);
    response.Headers.Add("Access-Control-Allow-Origin", "*");
    response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
    response.Headers.Add("Access-Control-Allow-Headers", "Origin, Content-Type, X-Auth-Token, X-Requested-With, Authorization");
    return response;
}

So, if it's an OPTIONS request (line 1), to the URL .../GenerateDemoKey (line 3) it may go through unauthorized (line 2), and the response with Status 200: OK (line 4) has headers added that tell the browser that sites from any domain are allowed to access the real URL (line 5), as long as they use one of the six named methods (line 6) and send only the five named headers (line 7). 因此,如果它是一个OPTIONS请求(第1行),则可能通过URL .../GenerateDemoKey (第3行),未经授权(第2行),并且状态为200:确定的响应(第4行)添加了标头,告诉浏览器,允许任何域的网站访问真实URL(第5行),只要它们使用六个命名方法之一(第6行)并且仅发送五个命名标头(第7行)即可。

In PHP, on the other hand, you would add an if block to the start of your script, before you process the authentication : 另一方面,在PHP中,您将在处理身份验证之前将if块添加到脚本的开头:

<?php
if($_SERVER['REQUEST_METHOD']=="OPTIONS") {
    header("Access-Control-Allow-Origin: *")
    header("Access-Control-Allow-Methods: GET, POST*")
    header("Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token, X-Requested-With, Authorization")
    exit(0);
}

If you use htaccess to authenticate, you may have to add a rule to the htaccess file to allow OPTIONS requests to pass through. 如果使用htaccess进行身份验证,则可能必须向htaccess文件中添加一条规则,以允许通过OPTIONS请求。 With the combination of htaccess and PHP, there's a huge security risk there - make double sure to only whitelist those URLs that you have checked won't return private information when called with the OPTIONS method. 结合使用htaccess和PHP,存在巨大的安全风险-双重确保仅将那些您检查过的URL列入白名单,这些URL在使用OPTIONS方法调用时不会返回私有信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM