我什么时候应该在我的回复 headers 中真正将 "Access-Control-Allow-Credentials" 设置为 "true"?

When should I really set "Access-Control-Allow-Credentials" to "true" in my response headers?

MDN 表示,当必须在站点 Access-Control-Allow-Crendentials 之间交换 cookie、授权 header 或 TLS 客户端证书等凭证时,必须将其设置为 true

考虑两个站点 A - https://example1.xyz.com and another one is B- https://example2.xyz.com。现在我必须从 A 向 B 发出 http Get 请求。当我从 A 请求 B 时,我得到

"No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example1.xyz.com' is therefore not allowed access."

所以,我在 B

中添加以下响应 headers
response.setHeader("Access-Control-Allow-Origin", request.getHeader("origin"));

这解决了同源错误,我可以向 B 请求。何时以及为什么我应该设置

response.setHeader("Access-Control-Allow-Credentials", "true");

当我用谷歌搜索解决这个 same-origin 错误时,大多数人都建议同时使用这两个 header。我不清楚使用第二个Access-Control-Allow-Credentials

  1. 什么时候应该同时使用两者?
  2. 为什么我应该将 Access-Control-Allow-Origin 设置为从请求 header 获得的 origin 而不是通配符 *

请举个例子让我更好地理解。

如果您希望请求也能够发送 cookie,则需要 Allow-Credentials。如果您需要授权传入请求,基于会话 ID cookie 将是一个常见的原因。

设置通配符允许任何站点向您的端点发出请求。如果请求与您定义的白名单相匹配,则设置 allow to origin 是很常见的。一些浏览器会缓存允许响应,如果您也从另一个域请求相同的内容,这可能会导致请求被拒绝。

设置Access-Control-Allow-Credentials: true实际上有两个作用:

这些效果与设置 XMLHttpRequest.withCredentials or credentials: 'include' (Fetch API) have of causing credentials (HTTP cookies, TLS client certificates, and authentication entries) 实际包含在请求中的效果相结合。

https://fetch.spec.whatwg.org/#example-cors-with-credentials 在 Fetch 规范中有一个很好的例子

Why should I set Access-Control-Allow-Origin to origin obtained from request header rather than wildcard *?

你不应该,除非你非常确定你在做什么。

在以下情况下实际上是安全的:

  1. 您要为其设置响应 header 的资源是一个 public 站点或 API 端点,旨在供所有人访问,并且
  2. 您只是没有设置可能使攻击者能够访问敏感信息或机密数据的 cookie。

例如,如果您的服务器代码设置 cookie 只是为了保存应用程序状态或 session 状态以方便您的用户,那么采用 Origin 请求 header 和 reflecting/echoing 它返回 Access-Control-Allow-Origin 值,同时还发送 Access-Control-Allow-Credentials: true 响应 header.

另一方面,如果您设置的 cookie 暴露了敏感信息或机密数据,那么除非您真的确定您已经锁定了东西(不知何故……),否则您真的想避免反映 Origin 回到 Access-Control-Allow-Origin 值(不在服务器端检查它)同时发送 Access-Control-Allow-Credentials: true.

如果这样做,您可能会以允许恶意攻击者获取的方式暴露敏感信息或机密数据。有关风险的解释,请阅读以下内容:

如果您发送 CORS header 的资源是 而不是 一个 public 站点或 API 端点每个人都可以访问但位于 Intranet 内或在某些 IP-address-restricted 防火墙后面,那么您绝对真的想避免组合 Access-Control-Allow-Origin-reflects-OriginAccess-Control-Allow-Credentials: true。 (在 Intranet 情况下,您几乎总是希望只允许特定的 hardcoded/whitelisted 来源。)