From bfb9b51073c6dea01c19cb6cfafbfec1975f8f67 Mon Sep 17 00:00:00 2001 From: Carlos Valente Date: Fri, 6 Dec 2024 21:15:12 +0100 Subject: [PATCH] fix: secure protocol resolution --- apps/client/src/externals.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/apps/client/src/externals.ts b/apps/client/src/externals.ts index e2e4b082e..963ae663c 100644 --- a/apps/client/src/externals.ts +++ b/apps/client/src/externals.ts @@ -34,9 +34,12 @@ export const websocketUrl = resolveUrl('ws', 'ws'); function resolveUrl(protocol: 'http' | 'ws', path: string) { const url = new URL(window.location.origin); - // check if protocol URL is secure - url.protocol = protocol === 'http' ? 'http' : 'ws'; - url.protocol += window.location.protocol === 'https:' ? 's' : ''; + // generate ws url + if (protocol === 'ws') { + // ensure we remain in a secure context + const isSecure = window.location.protocol === 'https:'; + url.protocol = isSecure ? 'wss' : 'ws'; + } // make path name relative to the base URI url.pathname = baseURI ? `${baseURI}/${path}` : path;