HTTP 重定向 (302) 在以下 GET 请求中不使用 Cookie

HTTP Redirect (302) Doesn't Use Cookie in Following GET Request

这是我研究了一段时间的问题。

我的重定向似乎不符合 302 重定向中的 Set-Cookie 属性。

下面是我用wireshark获取的请求头和响应头。

HTTP/1.1 302 Moved Temporarily\r\n
Connection: close\r\n
Location: http://192.168.1.1:8888/home/\r\n
Set-Cookie: foo=test_data; Domain=192.168.1.1; Path=/\r\n
\r\n

GET /home/ HTTP/1.1\r\n
Host: 192.168.1.1:8888\r\n
Connection: keep-alive\r\n
Upgrade-Insecure-Requests: 1\r\n
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n
Accept-Encoding: gzip, deflate\r\n
Accept-Language: en-US,en;q=0.8\r\n
DNT: 1\r\n
\r\n

我对内容进行了一些清理,但没有任何重要内容应该被修改。关键是无论我使用什么浏览器,cookie 'foo' 都不会放在 302 之后的 GET 请求中。据我所知,这不是预期的行为。我相信这一点是不正确的吗? 302 有什么我遗漏或做错的吗?

在问题中,Cookie header 没有出现在重定向的 HTTP 请求中 (GET http://192.168.1.1:8888/home)。根本原因是:cookie foo=test_data 从来不存在。当它通过 Set-Cookie 响应 header 从服务器传送时,它会被浏览器拒绝,因为它的 Domain 不包括原始服务器。

根据MDN

A cookie belonging to a domain that does not include the origin server should be rejected by the user agent. The following cookie will be rejected if it was set by a server hosted on originalcompany.com.

Set-Cookie: qwerty=219ffwef9w0f; Domain=somecompany.co.uk; Path=/; Expires=Wed, 30 Aug 2019 00:00:00 GMT

更准确的描述,您可以查看RFC6265 section -4.1.2.3

这是有充分理由的设计。如果所有服务器都可以Set-Cookie所有域,那么清除其他网站的cookie将非常容易,这对互联网来说将是一场灾难。