From a7c95cab85f1e5702ffb54edc7eba9a38d2df1d3 Mon Sep 17 00:00:00 2001 From: tisachi Date: Wed, 2 Dec 2026 17:25:38 +0100 Subject: [PATCH] =?UTF-8?q?Front:=20propagacja=20X-Forwarded-Proto=20w=20n?= =?UTF-8?q?ginx=20app/admin=20(map=20$http=5Fx=5Fforwarded=5Fproto=20zamia?= =?UTF-8?q?st=20$scheme)=20=E2=80=94=20po=20https=20przez=20TLS-terminator?= =?UTF-8?q?=20Express=20widzi=20wlasciwy=20req.protocol=20i=20same-origin?= =?UTF-8?q?=20sie=20zgadza=20(brak=20falszywego=20403=20CORS);=20+=20test?= =?UTF-8?q?=20sameOrigin=20https+port=2013xxx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/nginx.conf.template | 15 +++++++++++++-- api/test/unit/sameOrigin.test.ts | 8 ++++++++ app/nginx.conf.template | 15 +++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) 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 / {