Nginx 重定向到 Phoenix 失去授权 header
Nginx redirection to Phoenix loses authorization header
我有一个使用 Cloud 运行 部署的 Phoenix Framework 应用程序,有多个客户端在使用它。
由于 Cloud 运行 后台任务限制,我想将部署移动到 Computer Engine,但我不想强迫客户更新他们的应用程序(即后端 url)。
所以我虽然可以在云中部署一个 Ngnix 实例 运行 将所有内容重定向到 Compute Engine。
问题是我使用的授权请求 header 没有到达 Phoenix 连接。我已经在不同的服务器(Python 简单服务器)上尝试过,它正确地接收了每个 header。
那么,问题来了。 Phoenix Framework 是否过滤掉一些来自重定向的请求 header?
header 是:
{"authorization", "Bearer XXX"}
ngixn.conf 就是:
server {
listen 80;
return 301 http://0.0.0.0:4000$request_uri;
}
对 conn.req_headers
的检查:
$curl -L --location --request GET 'http://localhost:80' \
--header 'Content-Type: application/json' --header 'other: whatever' \
--header 'Authorization: Bearer XXXX' \
--data-raw ''`
检查给出:
[
{"accept", "*/*"},
{"content-type", "application/json"},
{"host", "0.0.0.0:4000"},
{"other", "whatever"},
{"user-agent", "curl/7.54.0"}
]
如果 curl 直接到 Phoenix 服务器应用程序:
curl -L --location --request GET 'http://localhost:4000' \
--header 'Content-Type: application/json' --header 'other: whatever' \
--header 'Authorization: Bearer XXXX' \
--data-raw ''`
我们得到了:
[
{"accept", "*/*"},
{"authorization", "Bearer XXXX"},
{"content-length", "0"},
{"content-type", "application/json"},
{"host", "localhost:4000"},
{"other", "whatever"},
{"user-agent", "curl/7.54.0"}
]
已编辑
Python 服务器也未收到授权 header。
直接请求服务器:
127.0.0.1 - - [03/Nov/2020 07:22:30] "GET / HTTP/1.1" 200 -
Host: localhost:4000
User-Agent: curl/7.54.0
Accept: */*
Content-Type: application/json
other: whatever
Authorization: Bearer XXXX
Content-Length: 0
通过 Nginx 重定向的请求:
127.0.0.1 - - [03/Nov/2020 07:22:41] "GET / HTTP/1.1" 200 -
Host: 0.0.0.0:4000
User-Agent: curl/7.54.0
Accept: */*
Content-Type: application/json
other: whatever
所以我猜 Nginx 是 'capturing' auth header 并且不让它通过
这是 curl 中的一项功能。如果一个请求被重定向到不同的主机名,那么任何 Authorization
header 都将在第二个请求中被删除,以免将凭据泄露给不相关的服务器。 (您正在向 localhost:80
发出请求,但重定向位置是 0.0.0.0:4000
,因此这算作不同的主机名。)
您可以通过使用 --location-trusted
选项而不是 -L
来让 curl 转发 Authorization
header。
(虽然您在 curl 7.54.0 中看到这个很奇怪 - 根据 this security advisory,curl 7.54.0 应该像您期望的那样运行,并且只有 7.58.0 和更高版本有这个保护功能。)
我有一个使用 Cloud 运行 部署的 Phoenix Framework 应用程序,有多个客户端在使用它。
由于 Cloud 运行 后台任务限制,我想将部署移动到 Computer Engine,但我不想强迫客户更新他们的应用程序(即后端 url)。
所以我虽然可以在云中部署一个 Ngnix 实例 运行 将所有内容重定向到 Compute Engine。
问题是我使用的授权请求 header 没有到达 Phoenix 连接。我已经在不同的服务器(Python 简单服务器)上尝试过,它正确地接收了每个 header。
那么,问题来了。 Phoenix Framework 是否过滤掉一些来自重定向的请求 header?
header 是:
{"authorization", "Bearer XXX"}
ngixn.conf 就是:
server {
listen 80;
return 301 http://0.0.0.0:4000$request_uri;
}
对 conn.req_headers
的检查:
$curl -L --location --request GET 'http://localhost:80' \
--header 'Content-Type: application/json' --header 'other: whatever' \
--header 'Authorization: Bearer XXXX' \
--data-raw ''`
检查给出:
[
{"accept", "*/*"},
{"content-type", "application/json"},
{"host", "0.0.0.0:4000"},
{"other", "whatever"},
{"user-agent", "curl/7.54.0"}
]
如果 curl 直接到 Phoenix 服务器应用程序:
curl -L --location --request GET 'http://localhost:4000' \
--header 'Content-Type: application/json' --header 'other: whatever' \
--header 'Authorization: Bearer XXXX' \
--data-raw ''`
我们得到了:
[
{"accept", "*/*"},
{"authorization", "Bearer XXXX"},
{"content-length", "0"},
{"content-type", "application/json"},
{"host", "localhost:4000"},
{"other", "whatever"},
{"user-agent", "curl/7.54.0"}
]
已编辑
Python 服务器也未收到授权 header。
直接请求服务器:
127.0.0.1 - - [03/Nov/2020 07:22:30] "GET / HTTP/1.1" 200 -
Host: localhost:4000
User-Agent: curl/7.54.0
Accept: */*
Content-Type: application/json
other: whatever
Authorization: Bearer XXXX
Content-Length: 0
通过 Nginx 重定向的请求:
127.0.0.1 - - [03/Nov/2020 07:22:41] "GET / HTTP/1.1" 200 -
Host: 0.0.0.0:4000
User-Agent: curl/7.54.0
Accept: */*
Content-Type: application/json
other: whatever
所以我猜 Nginx 是 'capturing' auth header 并且不让它通过
这是 curl 中的一项功能。如果一个请求被重定向到不同的主机名,那么任何 Authorization
header 都将在第二个请求中被删除,以免将凭据泄露给不相关的服务器。 (您正在向 localhost:80
发出请求,但重定向位置是 0.0.0.0:4000
,因此这算作不同的主机名。)
您可以通过使用 --location-trusted
选项而不是 -L
来让 curl 转发 Authorization
header。
(虽然您在 curl 7.54.0 中看到这个很奇怪 - 根据 this security advisory,curl 7.54.0 应该像您期望的那样运行,并且只有 7.58.0 和更高版本有这个保护功能。)