Front: propagacja X-Forwarded-Proto w nginx app/admin (map $http_x_forwarded_proto zamiast $scheme) — po https przez TLS-terminator Express widzi wlasciwy req.protocol i same-origin sie zgadza (brak falszywego 403 CORS); + test sameOrigin https+port 13xxx

This commit is contained in:
tisachi
2026-12-02 17:25:38 +01:00
parent 63c8c1cebf
commit a7c95cab85
3 changed files with 34 additions and 4 deletions

View File

@@ -3,6 +3,17 @@
# do /etc/nginx/conf.d/default.conf. Zmienne nginx ($uri, $host, ...) sa bezpieczne.
# API_UPSTREAM przykladowo: fitos-api:9002 (dev, wspolna siec fitos-net) lub api:9002
# (stos deploy — ta sama siec compose).
# Protokol liczy sie po X-Forwarded-Proto od kontenera TLS (TisachiSSL, jedyny
# terminator https na 1300x), nie po $scheme — do nginx frontu ruch zawsze dochodzi
# plainem. Bez tego mapowania po https Express (trust proxy 1) zobaczylby
# proto=http przy Origin: https://... -> fałszywe cross-origin (403 CORS).
# Bez naglowka (LAN http, smoke z hosta na 127.0.0.1) = wlasny $scheme.
map $http_x_forwarded_proto $fwd_proto {
default $scheme;
https https;
}
server {
listen 9000;
server_name _;
@@ -32,14 +43,14 @@ server {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Proto $fwd_proto;
}
location = /health {
proxy_pass http://$api_upstream;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Proto $fwd_proto;
}
location / {

View File

@@ -7,6 +7,14 @@ describe('isSameOrigin — origin requesta vs nagłówek Origin', () => {
expect(isSameOrigin('https://tisachi.ddns.net', 'https', 'tisachi.ddns.net')).toBe(true);
});
it('https z niestandardowym portem (staging 13xxx przez TisachiSSL) to same-origin', () => {
// X-Forwarded-Proto=https + Host z portem: req.protocol=https, host zostaje z portem
expect(isSameOrigin('https://tisachi.ddns.net:13001', 'https', 'tisachi.ddns.net:13001')).toBe(true);
expect(isSameOrigin('https://192.168.50.127:13002', 'https', '192.168.50.127:13002')).toBe(true);
// ten sam host+port, ale klient po http — dalej cross-origin (inny scheme)
expect(isSameOrigin('http://tisachi.ddns.net:13001', 'https', 'tisachi.ddns.net:13001')).toBe(false);
});
it('domyślny port jest niewidoczny po jednej stronie', () => {
expect(isSameOrigin('http://api.example.test:80', 'http', 'api.example.test')).toBe(true);
expect(isSameOrigin('http://api.example.test', 'http', 'api.example.test:80')).toBe(true);

View File

@@ -3,6 +3,17 @@
# do /etc/nginx/conf.d/default.conf. Zmienne nginx ($uri, $host, ...) sa bezpieczne.
# API_UPSTREAM przykladowo: fitos-api:9002 (dev, wspolna siec fitos-net) lub api:9002
# (stos deploy — ta sama siec compose).
# Protokol liczy sie po X-Forwarded-Proto od kontenera TLS (TisachiSSL, jedyny
# terminator https na 1300x), nie po $scheme — do nginx frontu ruch zawsze dochodzi
# plainem. Bez tego mapowania po https Express (trust proxy 1) zobaczylby
# proto=http przy Origin: https://... -> fałszywe cross-origin (403 CORS).
# Bez naglowka (LAN http, smoke z hosta na 127.0.0.1) = wlasny $scheme.
map $http_x_forwarded_proto $fwd_proto {
default $scheme;
https https;
}
server {
listen 9001;
server_name _;
@@ -38,14 +49,14 @@ server {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Proto $fwd_proto;
}
location = /health {
proxy_pass http://$api_upstream;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Proto $fwd_proto;
}
location / {