From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../corsalloworiginnotmatchingorigin/index.html | 35 +++++++++++ .../http/cors/errors/corsdidnotsucceed/index.html | 30 +++++++++ .../web/http/cors/errors/corsdisabled/index.html | 26 ++++++++ .../corsexternalredirectnotallowed/index.html | 28 +++++++++ .../http/cors/errors/corsmethodnotfound/index.html | 33 ++++++++++ .../cors/errors/corsmissingalloworigin/index.html | 51 +++++++++++++++ .../corsmultiplealloworiginnotallowed/index.html | 25 ++++++++ .../errors/corsnotsupportingcredentials/index.html | 34 ++++++++++ .../errors/corsoriginheadernotadded/index.html | 26 ++++++++ .../errors/corspreflightdidnotsucceed/index.html | 27 ++++++++ .../http/cors/errors/corsrequestnothttp/index.html | 36 +++++++++++ .../index.html" | 32 ++++++++++ files/zh-cn/web/http/cors/errors/index.html | 72 ++++++++++++++++++++++ 13 files changed, 455 insertions(+) create mode 100644 files/zh-cn/web/http/cors/errors/corsalloworiginnotmatchingorigin/index.html create mode 100644 files/zh-cn/web/http/cors/errors/corsdidnotsucceed/index.html create mode 100644 files/zh-cn/web/http/cors/errors/corsdisabled/index.html create mode 100644 files/zh-cn/web/http/cors/errors/corsexternalredirectnotallowed/index.html create mode 100644 files/zh-cn/web/http/cors/errors/corsmethodnotfound/index.html create mode 100644 files/zh-cn/web/http/cors/errors/corsmissingalloworigin/index.html create mode 100644 files/zh-cn/web/http/cors/errors/corsmultiplealloworiginnotallowed/index.html create mode 100644 files/zh-cn/web/http/cors/errors/corsnotsupportingcredentials/index.html create mode 100644 files/zh-cn/web/http/cors/errors/corsoriginheadernotadded/index.html create mode 100644 files/zh-cn/web/http/cors/errors/corspreflightdidnotsucceed/index.html create mode 100644 files/zh-cn/web/http/cors/errors/corsrequestnothttp/index.html create mode 100644 "files/zh-cn/web/http/cors/errors/cors\351\224\231\350\257\257\345\205\201\350\256\270\345\207\255\350\257\201/index.html" create mode 100644 files/zh-cn/web/http/cors/errors/index.html (limited to 'files/zh-cn/web/http/cors') diff --git a/files/zh-cn/web/http/cors/errors/corsalloworiginnotmatchingorigin/index.html b/files/zh-cn/web/http/cors/errors/corsalloworiginnotmatchingorigin/index.html new file mode 100644 index 0000000000..ce85096ae3 --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/corsalloworiginnotmatchingorigin/index.html @@ -0,0 +1,35 @@ +--- +title: 原因:CORS 头 'Access-Control-Allow-Origin'与'xyz'不匹配 +slug: Web/HTTP/CORS/Errors/CORSAllowOriginNotMatchingOrigin +translation_of: Web/HTTP/CORS/Errors/CORSAllowOriginNotMatchingOrigin +--- +
{{HTTPSidebar}}
+ +

原因

+ +
原因:CORS 头 'Access-Control-Allow-Origin'与'xyz'不匹配
+ +

出了什么问题?

+ +

简而言之,发出请求的原点与 {{HTTPHeader("Access-Control-Allow-Origin")}} 标题允许的任何来源都不匹配。

+ +

如果响应包含多个 Access-Control-Allow-Origin 标头,也会发生此错误。

+ +

如果您的代码使用CORS请求访问的服务在您的控制之下,请确保将其配置为将其来源包含在其 Access-Control-Allow-Origin 标头中,并且响应中仅包含一个此类标头。 标题本身接受逗号描述的起源列表,因此添加新的起源并不困难。

+ +

例如,在Apache中,将以下行添加到服务器的配置中(在相应的 <Directory><Location><Files><VirtualHost> 部分中)。 配置通常位于 .conf 文件中(httpd.confapache.conf 是这些文件的通用名称),或者位于 .htaccess 文件中。

+ +
Header set Access-Control-Allow-Origin 'origin-list'
+ +

在 Nginx 中,对应的配置为:

+ +
add_header 'Access-Control-Allow-Origin' 'origin-list'
+ +

查看更多

+ + diff --git a/files/zh-cn/web/http/cors/errors/corsdidnotsucceed/index.html b/files/zh-cn/web/http/cors/errors/corsdidnotsucceed/index.html new file mode 100644 index 0000000000..fa3844fa44 --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/corsdidnotsucceed/index.html @@ -0,0 +1,30 @@ +--- +title: 'Reason: CORS request did not succeed' +slug: Web/HTTP/CORS/Errors/CORSDidNotSucceed +tags: + - CORS + - HTTP + - 原因 + - 安全 + - 错误 +translation_of: Web/HTTP/CORS/Errors/CORSDidNotSucceed +--- +
{{HTTPSidebar}}
+ +

原因

+ +
原因: CORS 请求未成功
+ +

哪里错了?

+ +

使用CORS的{{Glossary("HTTP")}} 请求失败,因为HTTP连接在网络或协议级别失败。该错误与CORS没有直接关系,而是某种基本的网络错误。很多情况下,它是某个浏览器插件(比如广告拦截或隐私保护插件)阻止了请求而引起的。

+ +

参见

+ + diff --git a/files/zh-cn/web/http/cors/errors/corsdisabled/index.html b/files/zh-cn/web/http/cors/errors/corsdisabled/index.html new file mode 100644 index 0000000000..7d62602cb9 --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/corsdisabled/index.html @@ -0,0 +1,26 @@ +--- +title: 'Reason: CORS disabled' +slug: Web/HTTP/CORS/Errors/CORSDisabled +translation_of: Web/HTTP/CORS/Errors/CORSDisabled +--- +
{{HTTPSidebar}}
+ +

原因

+ +
原因:CROS被禁用
+ +

哪里错了?

+ +

发送了一个需要使用{{Glossary("CORS")}}的请求,但在用户的浏览器中禁用了CORS。发生这种情况时,用户需要在浏览器中重新打开CORS。

+ +

在Firefox中,禁用CORS的首选项是content.cors.disable。将此设置为true将禁用CORS,因此在这种情况下,CORS请求将始终因此错误而失败。

+ +

参见

+ + diff --git a/files/zh-cn/web/http/cors/errors/corsexternalredirectnotallowed/index.html b/files/zh-cn/web/http/cors/errors/corsexternalredirectnotallowed/index.html new file mode 100644 index 0000000000..cacd2b8ce9 --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/corsexternalredirectnotallowed/index.html @@ -0,0 +1,28 @@ +--- +title: 'Reason: CORS request external redirect not allowed' +slug: Web/HTTP/CORS/Errors/CORSExternalRedirectNotAllowed +translation_of: Web/HTTP/CORS/Errors/CORSExternalRedirectNotAllowed +--- +
{{HTTPSidebar}}
+ +

原因

+ +
原因: CORS不允许请求外部重定向
+ +

哪里错了?

+ +

{{Glossary("CORS")}} 请求被服务器响应了一个HTTP重定向到与原始请求的Origin不同的URL上,这在CORS请求中是不允许的。

+ +

例如,如果请求 https://service.tld/fetchdata ,但服务器的HTTP响应是 "301 永久移动", "307 暂时重定向",或 "308 永久重定向" 并且 Location 是https://anotherservice.net/getdata,那么CORS请求将会失败。

+ +

要解决此问题,请更新代码以使用重定向报告的新URL,从而避免重定向。

+ +

参见

+ + diff --git a/files/zh-cn/web/http/cors/errors/corsmethodnotfound/index.html b/files/zh-cn/web/http/cors/errors/corsmethodnotfound/index.html new file mode 100644 index 0000000000..b91a4ca628 --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/corsmethodnotfound/index.html @@ -0,0 +1,33 @@ +--- +title: 'Reason: Did not find method in CORS header ‘Access-Control-Allow-Methods’' +slug: Web/HTTP/CORS/Errors/CORSMethodNotFound +translation_of: Web/HTTP/CORS/Errors/CORSMethodNotFound +--- +
{{HTTPSidebar}}
+ +

原因

+ +
原因:在 CORS 头 ‘Access-Control-Allow-Methods’ 中没有找到方法
+ +

哪里错了?

+ +

{{Glossary("CORS")}} 请求使用的 HTTP 方法不包含在响应的 {{HTTPHeader("Access-Control-Allow-Methods")}} 头指定的方法列表中。此头指定了一个使用逗号分隔的 HTTP 方法列表,当使用 CORS 访问请求中指定的 URL 时,可以使用这些方法;如果请求使用任何其他方法,则会发生此错误。

+ +

例如,如果响应中包含:

+ +
Access-Control-Allow-Methods: GET,HEAD,POST
+ +

尝试使用 {{HTTPMethod("PUT")}} 请求会因为这个错误失败。

+ +

请确保当访问服务时你的代码只使用了允许的 HTTP 方法。

+ +

说明:如果服务器的 Access-Control-Allow-methods 头中包含任何未识别的或未定义的方法名称,会出现不同的错误:Reason: invalid token 'xyz' in CORS header 'Access-Control-Allow-Methods'.

+ +

相关内容

+ + diff --git a/files/zh-cn/web/http/cors/errors/corsmissingalloworigin/index.html b/files/zh-cn/web/http/cors/errors/corsmissingalloworigin/index.html new file mode 100644 index 0000000000..9be945f1f4 --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/corsmissingalloworigin/index.html @@ -0,0 +1,51 @@ +--- +title: 'Reason: CORS header ''Access-Control-Allow-Origin'' missing' +slug: Web/HTTP/CORS/Errors/CORSMissingAllowOrigin +tags: + - CORS + - Cross-Origin + - HTTP +translation_of: Web/HTTP/CORS/Errors/CORSMissingAllowOrigin +--- +
{{HTTPSidebar}}
+ +

原因

+ +
原因:缺少CORS “Access-Control-Allow-Origin”
+ +

哪里出错了?

+ +

对 {{Glossary("CORS")}} 请求的响应缺少必需的{{HTTPHeader("Access-Control-Allow-Origin")}}头,其用于确定在当前源内操作的资源是否可以访问。

+ +

如果服务器在您的控制之下,请将请求站点的源添加到允许访问的域集,方法是将其添加到Access-Control-Allow-Origin头的值。

+ +

例如,要允许https://amazing.site上的站点使用CORS访问资源,这个http头应该为:

+ +
Access-Control-Allow-Origin: https://amazing.site
+
+ +

您还可以使用"*"通配符配置站点使得允许任何站点访问它。您应该只将它用于公共的API。私有API永远不应使用"*",而应设置特定的一个域或一些域。此外,通配符仅适用于将{{htmlattrxref("crossorigin")}} 属性设置为"c"的请求。

+ +
Access-Control-Allow-Origin: *
+ +
+

警告: 使用通配符允许所有站点访问私有API是显而易见的坏主意。

+
+ +

例如,在Apache服务器中,将下面一行添加到服务器的配置中(在相应的<Directory>,<Location>,<Files>或<VirtualHost>部分中)。 配置通常位于.conf文件中(httpd.conf和apache.conf是这些文件的通用名称),或者位于.htaccess文件中。

+ +
Header set Access-Control-Allow-Origin 'origin-list'
+
+ +

对于Nginx,设置此http头的命令是:

+ +
add_header 'Access-Control-Allow-Origin' 'origin-list'
+
+ +

See also

+ + diff --git a/files/zh-cn/web/http/cors/errors/corsmultiplealloworiginnotallowed/index.html b/files/zh-cn/web/http/cors/errors/corsmultiplealloworiginnotallowed/index.html new file mode 100644 index 0000000000..596e98877d --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/corsmultiplealloworiginnotallowed/index.html @@ -0,0 +1,25 @@ +--- +title: 'Reason: Multiple CORS header ''Access-Control-Allow-Origin'' not allowed' +slug: Web/HTTP/CORS/Errors/CORSMultipleAllowOriginNotAllowed +translation_of: Web/HTTP/CORS/Errors/CORSMultipleAllowOriginNotAllowed +--- +
{{HTTPSidebar}}
+ +

原因

+ +
原因: 不允许出现多个 ‘Access-Control-Allow-Origin’ CORS 头信息。
+ +

为什么会出现该问题?

+ +

服务器返回了超过一个 {{HTTPHeader("Access-Control-Allow-Origin")}} 标头信息,因此被认为是错误的。

+ +

如果您正在管理该服务,请改正只返回一个 Access-Control-Allow-Origin 标头信息。同时需要注意,服务器返回的 Access-Control-Allow-Origin 值不应该是一个列表,因为浏览器只会接受一个值,并且不能为空。

+ +

查看更多

+ + diff --git a/files/zh-cn/web/http/cors/errors/corsnotsupportingcredentials/index.html b/files/zh-cn/web/http/cors/errors/corsnotsupportingcredentials/index.html new file mode 100644 index 0000000000..4ace9a4ac3 --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/corsnotsupportingcredentials/index.html @@ -0,0 +1,34 @@ +--- +title: >- + Reason: Credential is not supported if the CORS header + ‘Access-Control-Allow-Origin’ is ‘*’ +slug: Web/HTTP/CORS/Errors/CORSNotSupportingCredentials +translation_of: Web/HTTP/CORS/Errors/CORSNotSupportingCredentials +--- +
{{HTTPSidebar}}
+ +

原因

+ +
在CORS中,Credential不接受http响应首部中的‘Access-Control-Allow-Origin’设置为通配符‘*’
+ +

出了什么问题?

+ +

{{Glossary("CORS")}} 请求发出时,已经设定了credentials,但服务端配置了http响应首部 {{HTTPHeader("Access-Control-Allow-Origin")}} 的值为通配符 ("*") ,而这与使用credentials相悖。

+ +

要在客户端改正这个问题,只需要确保发出 CORS 请求时将credential设置为false。

+ + + +

如果还不成功,则需要修改服务端,可能需要修改 Access-Control-Allow-Origin 的值,来为客户端所能够加载资源的源予以授权。

+ +

另请参见

+ + diff --git a/files/zh-cn/web/http/cors/errors/corsoriginheadernotadded/index.html b/files/zh-cn/web/http/cors/errors/corsoriginheadernotadded/index.html new file mode 100644 index 0000000000..e3e3587c0f --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/corsoriginheadernotadded/index.html @@ -0,0 +1,26 @@ +--- +title: 'Reason: CORS header ‘Origin’ cannot be added' +slug: Web/HTTP/CORS/Errors/CORSOriginHeaderNotAdded +translation_of: Web/HTTP/CORS/Errors/CORSOriginHeaderNotAdded +--- +
{{HTTPSidebar}}
+ +

原因

+ +
原因:没有添加头字段‘Origin’
+ +

哪里错了?

+ +

 {{Glossary("user agent")}} 不能把 {{HTTPHeader("Origin")}} 头字段添加到 {{Glossary("HTTP")}} 请求中。 所有的 CORS 请求必须有 Origin 头字段。

+ +

例如,如果JavaScript代码以增强的权限运行,允许它访问多个域名的内容,则会发生这种情况。

+ +

参见

+ + diff --git a/files/zh-cn/web/http/cors/errors/corspreflightdidnotsucceed/index.html b/files/zh-cn/web/http/cors/errors/corspreflightdidnotsucceed/index.html new file mode 100644 index 0000000000..760c5a0199 --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/corspreflightdidnotsucceed/index.html @@ -0,0 +1,27 @@ +--- +title: 'Reason: CORS preflight channel did not succeed' +slug: Web/HTTP/CORS/Errors/CORSPreflightDidNotSucceed +translation_of: Web/HTTP/CORS/Errors/CORSPreflightDidNotSucceed +--- +
{{HTTPSidebar}}
+ +

Reason

+ +
Reason: CORS preflight channel did not succeed
+ +

What went wrong?

+ +

{{Glossary("CORS")}} 请求需要预校验,但是预校验请求失败。可能是由于下列几种原因导致:

+ + + +

See also

+ + diff --git a/files/zh-cn/web/http/cors/errors/corsrequestnothttp/index.html b/files/zh-cn/web/http/cors/errors/corsrequestnothttp/index.html new file mode 100644 index 0000000000..9618d6e0e0 --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/corsrequestnothttp/index.html @@ -0,0 +1,36 @@ +--- +title: 原因:CORS 请求不是 HTTP +slug: Web/HTTP/CORS/Errors/CORSRequestNotHttp +tags: + - CORS + - 同源规则 + - 安全 + - 错误 +translation_of: Web/HTTP/CORS/Errors/CORSRequestNotHttp +--- +

{{HTTPSidebar}}

+ +

原因

+ +
原因: CORS 请求不是 HTTP
+ +

哪里出错了?

+ +

{{Glossary("CORS")}} 请求只能使用 HTTPS URL 方案,但请求指定的 URL 可能是不同类型。这种情况经常发生在 URL 指定本地文件,例如使用了 file:/// 的 URL。

+ +

要解决此问题,请确保在发出涉及 CORS 的请求时使用 HTTPS URL,例如 {{domxref("XMLHttpRequest")}},Fetch API,或 Web Fonts(@font-face),或 WebGL 纹理, 或 XSL 样式表。

+ +

自 Firefox 68 本地文件安全性的改变

+ +

当用户在 Firefox 67 或更早版本中使用 file:/// URI 打开页面时,页面来源被定义为打开页面的目录。同一目录及其子目录中的资源均被视为具有相同的来源,符合 CORS 同源规则。

+ +

为响应 CVE-2019-11730,Firefox 68 及更高版本中定义,使用 file:/// URL 打开页面的来源唯一。 因此,同一目录或其子目录中的其他资源不再满足 CORS 同源规则。这个新的表现通过 privacy.file_unique_origin 这一首选项控制,默认启用。

+ +

参考

+ + diff --git "a/files/zh-cn/web/http/cors/errors/cors\351\224\231\350\257\257\345\205\201\350\256\270\345\207\255\350\257\201/index.html" "b/files/zh-cn/web/http/cors/errors/cors\351\224\231\350\257\257\345\205\201\350\256\270\345\207\255\350\257\201/index.html" new file mode 100644 index 0000000000..9ba1f6b468 --- /dev/null +++ "b/files/zh-cn/web/http/cors/errors/cors\351\224\231\350\257\257\345\205\201\350\256\270\345\207\255\350\257\201/index.html" @@ -0,0 +1,32 @@ +--- +title: 故:在CORS头Access-Control-Allow-Credentials中预期为true +slug: Web/HTTP/CORS/Errors/CORS错误允许凭证 +translation_of: Web/HTTP/CORS/Errors/CORSMIssingAllowCredentials +--- +
+ +

理由

+ +
故:在CORS头Access-Control-Allow-Credentials中预期设为true
+ +

错在了哪儿?

+ +

     CORS请求要求服务器允许使用凭据,但是服务器的HTTPHeader:Access-Control-Allow-Credentials标头的值并没有设置为true 。

+ +

想要在客户端解决此问题,请修改代码以不请求使用凭据:

+ + + +

想要通过更改服务器的配置来消除此错误,请调整服务器的配置以将Access-Control-Allow-Credentials标头的值设置为true。

+ +

更多

+ + diff --git a/files/zh-cn/web/http/cors/errors/index.html b/files/zh-cn/web/http/cors/errors/index.html new file mode 100644 index 0000000000..67710ad510 --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/index.html @@ -0,0 +1,72 @@ +--- +title: CORS errors +slug: Web/HTTP/CORS/Errors +translation_of: Web/HTTP/CORS/Errors +--- +
+

 

+
+ +
 
+ + + +

如果未正确设置CORS配置,浏览器控制台将显示错误,例如"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at $somesite"(跨源请求已阻止:同源策略禁止在某些站点上读取远程资源”),表示请求因违反CORS安全性而被阻止规则。 但这可能不一定是设置错误。 因为用户的Web应用程序和远程外部服务可能故意禁止该请求。如果要使端点可用,则需要进行一些调试才能成功。

+ +

确定问题

+ +

要了解CORS配置的基本问题,您需要找出哪个请求有问题以及原因。步骤如下:

+ +

 

+ +
    +
  1.  打开有问题的网站并打开开发者工具(Developer Tools)。
  2. +
  3.  尝试重现失败的事务并检查控制台( console)是否看到CORS违规错误消息。它可能看起来像这样:
  4. +
+ +

Firefox console showing CORS error

+ +

错误消息的文本将类似于以下内容:

+ +
Cross-Origin Request Blocked: The Same Origin Policy disallows
+reading the remote resource at https://some-url-here. (Reason:
+additional information here).
+ +
+

注意:出于安全原因,JavaScript代码无法给出有关CORS请求出错的详细信息。 所有代码都知道发生了错误。 确定具体问题的唯一方法是查看浏览器的控制台以获取详细信息。

+
+ +

CORS错误信息

+ +

当请求因CORS失败时,Firefox的控制台会在其控制台中显示消息。 错误文本的一部分是“原因”消息,它提供了对出错的更深入的了解。 消息的原因如下: 

+ + + +

参见

+ + -- cgit v1.2.3-54-g00ecf