为什么Chrome在错误的子域上设置Cookie? - python

我有以下子域:

api.example.com
app1.example.com
app2.example.com

我正在使用nginx作为Web服务器来服务所有这些服务器。 api.example.com是我正在开发的python烧瓶应用程序。 app1.example.comapp2.example.com是静态js内容,不是我开发的。

Nginx配置为允许跨域资源共享:

    if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' $http_origin always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
            add_header 'Access-Control-Allow-Credentials' 'true' always;
            add_header 'Access-Control-Max-Age' 1728000 always;
            add_header 'Content-Type' 'text/plain; charset=utf-8' always;
            add_header 'Content-Length' 0 always;
            return 204;
    }
    if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' $http_origin always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
            add_header 'Access-Control-Allow-Credentials' 'true' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
    }
    if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' $http_origin always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
            add_header 'Access-Control-Allow-Credentials' 'true' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
    }

location块的上方,将控制权传递给uwsgi套接字。复制和复制api1api2相同。

app1app2api发出请求,并且api在响应其中一些请求时设置cookie。

Cookie设置如下:

response.set_cookie(
        key='some_cookie_name',
        value=some_token,
        max_age=current_app.config["JWT_EXP"],
        secure=True,
        httponly=True)

path默认情况下设置为'/'
domain属性不存在,
samesite属性也不存在,
以上根据this documentation。

如果我错了,请纠正我,但是据我了解,当app1请求api资源(正在设置cookie)时,此cookie绑定到app1.example.com,并且不应出现在app2.example.com上(反之亦然)反之亦然。

我在chrome上观察到的行为是不同的。

app1请求上设置cookie,然后在app2请求上设置cookie(具有相同的名称),将覆盖app1上的cookie。 app2子域没有cookie(使用chrome开发人员工具检查)。

现在,当我更改第二个子域(app2)的cookie的名称时,app2上仍将不存在cookie,但是,现在在app1子域上可以看到具有不同名称的第二个cookie。

看起来无论我做什么,所有cookie都设置在app1子域上。检查example.com时,这些cookie也存在。

此外,当app2.example.comapi发出请求时,它会发回在app1上可见的所有cookie。

这种行为正确吗?是否由于服务器端启用了CORS?如何在app1app2之间分隔cookie?

编辑:添加了cors配置代码

参考方案

set-cookie标头中的cookie域需要匹配从其请求的主机。同时需要CORS和Cookie标头。如果未在Set-Cookie上设置域,则浏览器将采用所请求主机的FQDN。如果您确实设置了域,则可以使用domain = example.com或类似名称在同一域中的站点之间共享cookie。

可能会在FQDN主机和该主机所在的域上设置相同的cookie名称,尽管会造成混淆。一个主机将发送到该域中的其他主机,而两个主机都将发送到对同一FQDN的后续调用。

我希望您希望在您的域而不是FQDN上使用cookie。您应该能够重写apt,app1和app2返回的Set-Cookie标头,以便它们位于域中。如果可能,最好将Cookie固定在应用程序本身上。

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…

TypeError:'str'对象不支持项目分配,带有json文件的python - python

以下是我的代码import json with open('johns.json', 'r') as q: l = q.read() data = json.loads(l) data['john'] = '{}' data['john']['use…