diff --git a/admin/nginx.conf.template b/admin/nginx.conf.template index 641462e..e8ce205 100644 --- a/admin/nginx.conf.template +++ b/admin/nginx.conf.template @@ -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 / { diff --git a/api/test/unit/sameOrigin.test.ts b/api/test/unit/sameOrigin.test.ts index 4209aee..615edc7 100644 --- a/api/test/unit/sameOrigin.test.ts +++ b/api/test/unit/sameOrigin.test.ts @@ -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); diff --git a/app/nginx.conf.template b/app/nginx.conf.template index 9360f4e..264a9d2 100644 --- a/app/nginx.conf.template +++ b/app/nginx.conf.template @@ -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 / {