diff --git a/.env.example b/.env.example index ac7f2cf2..0434bf0d 100644 --- a/.env.example +++ b/.env.example @@ -115,6 +115,7 @@ INITIAL_SETUP_TOKEN=your-random-setup-token-min-32-chars BREAK_GLASS_TOKEN= # Figées au build Docker (prediqo-web) — mode HTTP VPS : +# Security/Support/Candle WS prefer same-origin /ws/ via nginx (9080/9443). Never publish :4001. VITE_API_BASE_URL=http://prediqo.redirectme.net:9080 VITE_WS_ENABLED=true VITE_WS_URL=ws://prediqo.redirectme.net:9080/ws/v1/games/candle-predict diff --git a/deploy/PRODUCTION_SENTRY_CLAMAV.md b/deploy/PRODUCTION_SENTRY_CLAMAV.md index cf70408c..948dcad6 100644 --- a/deploy/PRODUCTION_SENTRY_CLAMAV.md +++ b/deploy/PRODUCTION_SENTRY_CLAMAV.md @@ -174,7 +174,9 @@ Recommandé en **dev local** ; en **production**, activez ClamAV dès que des up ## 3. WebSocket sécurité (rappel) -Les notifications admin temps réel utilisent `/ws/v1/security` sur le processus `backend-ws`. Nginx route déjà tout le préfixe `/ws/` vers `backend-ws:4001`. +Les notifications admin temps réel utilisent `/ws/v1/security` sur le processus `backend-ws`. Nginx route déjà tout le préfixe `/ws/` vers `backend-ws:4001` (interne Docker). + +Le frontend résout l’URL en **same-origin** (`ws(s)://:9080|9443/ws/v1/security`) — **ne pas** exposer ni bakee `ws://…:4001` en production (timeout navigateur). Vérifiez dans `.env` : diff --git a/prediqo_web/.env.example b/prediqo_web/.env.example index 0ac463ea..b7367666 100644 --- a/prediqo_web/.env.example +++ b/prediqo_web/.env.example @@ -1,13 +1,19 @@ # Local dev (npm run dev — Vite sur :5173) +# Security / Support / Candle WS use same-origin in production builds. +# In Vite DEV only, explicit VITE_WS_* may point at backend-ws :4001. VITE_API_BASE_URL=http://localhost:4000 VITE_WS_ENABLED=true VITE_WS_URL=ws://localhost:4001/ws/v1/games/candle-predict +# Optional overrides (same host as the page in prod; :4001 only for local DEV): +# VITE_WS_SECURITY_URL=ws://localhost:4001/ws/v1/security +# VITE_WS_SUPPORT_URL=ws://localhost:4001/ws/v1/support # Observability (optional) # VITE_SENTRY_DSN=https://...@sentry.io/... # VITE_SENTRY_TRACES_SAMPLE_RATE=0.1 # VPS HTTP (port 9080 — figé au build Docker via .env racine) +# Do NOT use public :4001 — nginx proxies /ws/ on 9080/9443. # VITE_API_BASE_URL=http://prediqo.redirectme.net:9080 # VITE_WS_URL=ws://prediqo.redirectme.net:9080/ws/v1/games/candle-predict diff --git a/prediqo_web/src/features/candle-predict/ws/protocol.ts b/prediqo_web/src/features/candle-predict/ws/protocol.ts index b6a98325..0fdd893b 100644 --- a/prediqo_web/src/features/candle-predict/ws/protocol.ts +++ b/prediqo_web/src/features/candle-predict/ws/protocol.ts @@ -1,4 +1,5 @@ import type { GameState } from '../types'; +import { isViteWsEnabled, resolveSameOriginWsUrl } from '../../../shared/ws/resolveSameOriginWsUrl'; export type WsClientMessage = | { type: 'subscribe'; intervalMinutes: number } @@ -46,38 +47,12 @@ export type WsServerMessage = | { type: 'error'; code: string; message: string }; export function resolveWsBaseUrl(): string { - const explicit = (import.meta.env.VITE_WS_URL as string | undefined)?.replace(/\/$/, ''); - - if (typeof window !== 'undefined') { - const pageHost = window.location.host; - if (explicit) { - try { - const configured = new URL(explicit); - // Use baked URL only when it targets the same host the user is browsing. - if (configured.host === pageHost) return explicit; - } catch { - /* fall through to same-origin */ - } - } - // Prefer same-origin /ws/ (nginx on :9080 or :9443) so a stale build URL cannot break live updates. - const u = new URL(window.location.origin); - u.protocol = u.protocol === 'https:' ? 'wss:' : 'ws:'; - u.pathname = '/ws/v1/games/candle-predict'; - u.search = ''; - return u.toString().replace(/\/$/, ''); - } - - if (explicit) return explicit; - - const api = import.meta.env.VITE_API_BASE_URL || 'http://localhost:4000'; - const url = new URL(api); - url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'; - url.port = import.meta.env.VITE_WS_PORT || '4001'; - url.pathname = '/ws/v1/games/candle-predict'; - url.search = ''; - return url.toString().replace(/\/$/, ''); + return resolveSameOriginWsUrl( + '/ws/v1/games/candle-predict', + import.meta.env.VITE_WS_URL as string | undefined + ); } export function isWsEnabled(): boolean { - return import.meta.env.VITE_WS_ENABLED === 'true'; + return isViteWsEnabled(); } diff --git a/prediqo_web/src/features/security/ws/protocol.ts b/prediqo_web/src/features/security/ws/protocol.ts index 4953e016..e0477ead 100644 --- a/prediqo_web/src/features/security/ws/protocol.ts +++ b/prediqo_web/src/features/security/ws/protocol.ts @@ -1,3 +1,5 @@ +import { isViteWsEnabled, resolveSameOriginWsUrl } from '../../../shared/ws/resolveSameOriginWsUrl'; + export type SecurityWsClientMessage = { type: 'ping' } | { type: 'subscribe_admin' }; export type SecurityWsServerMessage = @@ -25,18 +27,12 @@ export type SecurityWsServerMessage = | { type: 'error'; code: string; message: string }; export function resolveSecurityWsBaseUrl(): string { - const explicit = import.meta.env.VITE_WS_SECURITY_URL as string | undefined; - if (explicit) return explicit.replace(/\/$/, ''); - - const api = import.meta.env.VITE_API_BASE_URL || 'http://localhost:4000'; - const url = new URL(api); - url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'; - url.port = import.meta.env.VITE_WS_PORT || '4001'; - url.pathname = '/ws/v1/security'; - url.search = ''; - return url.toString().replace(/\/$/, ''); + return resolveSameOriginWsUrl( + '/ws/v1/security', + import.meta.env.VITE_WS_SECURITY_URL as string | undefined + ); } export function isSecurityWsEnabled(): boolean { - return import.meta.env.VITE_WS_ENABLED === 'true'; + return isViteWsEnabled(); } diff --git a/prediqo_web/src/features/support/ws/protocol.ts b/prediqo_web/src/features/support/ws/protocol.ts index 95484446..75d543be 100644 --- a/prediqo_web/src/features/support/ws/protocol.ts +++ b/prediqo_web/src/features/support/ws/protocol.ts @@ -1,4 +1,5 @@ import type { SupportMessage } from '../types'; +import { isViteWsEnabled, resolveSameOriginWsUrl } from '../../../shared/ws/resolveSameOriginWsUrl'; export type SupportWsClientMessage = | { type: 'ping' } @@ -31,18 +32,12 @@ export type SupportWsServerMessage = | { type: 'error'; code: string; message: string }; export function resolveSupportWsBaseUrl(): string { - const explicit = import.meta.env.VITE_WS_SUPPORT_URL as string | undefined; - if (explicit) return explicit.replace(/\/$/, ''); - - const api = import.meta.env.VITE_API_BASE_URL || 'http://localhost:4000'; - const url = new URL(api); - url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'; - url.port = import.meta.env.VITE_WS_PORT || '4001'; - url.pathname = '/ws/v1/support'; - url.search = ''; - return url.toString().replace(/\/$/, ''); + return resolveSameOriginWsUrl( + '/ws/v1/support', + import.meta.env.VITE_WS_SUPPORT_URL as string | undefined + ); } export function isSupportWsEnabled(): boolean { - return import.meta.env.VITE_WS_ENABLED === 'true'; + return isViteWsEnabled(); } diff --git a/prediqo_web/src/shared/ws/resolveSameOriginWsUrl.ts b/prediqo_web/src/shared/ws/resolveSameOriginWsUrl.ts new file mode 100644 index 00000000..bb3a7174 --- /dev/null +++ b/prediqo_web/src/shared/ws/resolveSameOriginWsUrl.ts @@ -0,0 +1,45 @@ +/** + * Resolve a WebSocket base URL that prefers same-origin `/ws/...` (nginx :9080 / :9443). + * Avoids baking a public `:4001` host that browsers cannot reach on the VPS. + */ + +export function resolveSameOriginWsUrl(pathname: string, explicitUrl?: string): string { + const explicit = explicitUrl?.replace(/\/$/, '') || undefined; + const normalizedPath = pathname.startsWith('/') ? pathname : `/${pathname}`; + + if (typeof window !== 'undefined') { + const pageHost = window.location.host; + if (explicit) { + try { + const configured = new URL(explicit); + // Same host as the page (typical VPS nginx :9080 / :9443). + if (configured.host === pageHost) return explicit; + // Vite dev has no /ws/ proxy — keep explicit ws://localhost:4001/... + if (import.meta.env.DEV) return explicit; + } catch { + /* fall through to same-origin */ + } + } + // Production / Docker web: same-origin /ws/ via nginx (never public :4001). + const u = new URL(window.location.origin); + u.protocol = u.protocol === 'https:' ? 'wss:' : 'ws:'; + u.pathname = normalizedPath; + u.search = ''; + return u.toString().replace(/\/$/, ''); + } + + if (explicit) return explicit; + + // Non-browser / SSR fallback for local tooling only. + const api = import.meta.env.VITE_API_BASE_URL || 'http://localhost:4000'; + const url = new URL(api); + url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'; + url.port = import.meta.env.VITE_WS_PORT || '4001'; + url.pathname = normalizedPath; + url.search = ''; + return url.toString().replace(/\/$/, ''); +} + +export function isViteWsEnabled(): boolean { + return import.meta.env.VITE_WS_ENABLED === 'true'; +}