Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): dial only the origin of VITE_WS_URL

Yudhi Armyndharis93071d66dd1f
diff --git a/dashboard/src/hooks/useWebSocket.ts b/dashboard/src/hooks/useWebSocket.ts
index 767ca2c25..dad123f20 100644
--- a/dashboard/src/hooks/useWebSocket.ts
+++ b/dashboard/src/hooks/useWebSocket.ts
@@ -138,7 +138,7 @@ interface ServerErrorFrame {
message?: string;
}
-// VITE_WS_URL when set, else the API origin (a split-origin build names it in VITE_API_URL), else the
+// The origin of VITE_WS_URL when set, else of VITE_API_URL (a split-origin build names the API there), else the
// page's own origin (the single-container setup, or nginx proxying /socket.io in Docker).
const SOCKET_URL = resolveSocketUrl(import.meta.env.VITE_WS_URL, API_ORIGIN, window.location.origin);
// Warn when VITE_WS_URL is an insecure http:// URL on a non-localhost host; api.ts already warns
diff --git a/dashboard/src/utils/urlSecurity.test.ts b/dashboard/src/utils/urlSecurity.test.ts
index 69edfb12d..396f414b9 100644
--- a/dashboard/src/utils/urlSecurity.test.ts
+++ b/dashboard/src/utils/urlSecurity.test.ts
@@ -73,3 +73,21 @@ test('resolveSocketUrl keeps only the origin of a VITE_API_URL that carries a pa
// A relative value is a path on the page's own host.
assert.equal(resolveSocketUrl(undefined, '/gw', page), page);
});
+
+test('resolveSocketUrl keeps only the origin of VITE_WS_URL', () => {
+ const page = 'https://dashboard.example.com';
+ const api = 'https://api.example.com';
+ // A trailing slash or a path would otherwise turn '/events' into an unknown namespace.
+ assert.equal(resolveSocketUrl('https://ws.example.com/', api, page), 'https://ws.example.com');
+ assert.equal(resolveSocketUrl('https://ws.example.com:8443/rt/', api, page), 'https://ws.example.com:8443');
+});
+
+test('resolveSocketUrl dials a scheme-less VITE_WS_URL on the page protocol, as socket.io did', () => {
+ const page = 'https://dashboard.example.com';
+ const api = 'https://api.example.com';
+ assert.equal(resolveSocketUrl('localhost:2785', api, page), 'https://localhost:2785');
+ assert.equal(resolveSocketUrl('ws.example.com:8443/', api, page), 'https://ws.example.com:8443');
+ assert.equal(resolveSocketUrl('ws.example.com', api, page), 'https://ws.example.com');
+ assert.equal(resolveSocketUrl('//ws.example.com/rt', api, page), 'https://ws.example.com');
+ assert.equal(resolveSocketUrl('localhost:2785', api, 'http://localhost:5173'), 'http://localhost:2785');
+});
diff --git a/dashboard/src/utils/urlSecurity.ts b/dashboard/src/utils/urlSecurity.ts
index 8aff29ab0..137620abd 100644
--- a/dashboard/src/utils/urlSecurity.ts
+++ b/dashboard/src/utils/urlSecurity.ts
@@ -27,16 +27,20 @@ export function warnIfInsecureHttpUrl(url: string, label: string): string {
}
/**
- * The origin the realtime socket dials: VITE_WS_URL when set, else the origin of VITE_API_URL (a
+ * The origin the realtime socket dials: that of VITE_WS_URL when set, else of VITE_API_URL (a
* split-origin build serves the socket from the API, not from the dashboard's host), else the page's
- * own origin. Only the origin is taken from VITE_API_URL: socket.io reads a URL path as the namespace,
- * so a path there would turn '/events' into an unknown namespace the gateway rejects.
+ * own origin. Only the origin is taken from either value: socket.io reads a URL path as the namespace,
+ * so a path (even a trailing slash) would turn '/events' into an unknown namespace the gateway rejects.
*/
export function resolveSocketUrl(wsUrl: string | undefined, apiUrl: string, pageOrigin: string): string {
- if (wsUrl) return wsUrl;
- if (!apiUrl) return pageOrigin;
+ const raw = wsUrl || apiUrl;
+ if (!raw) return pageOrigin;
+ // socket.io dials a value with no scheme ('host:port') on the page's protocol; URL would read
+ // 'host:' as a scheme (opaque 'null' origin) or a bare host as a path on the page's own host.
+ const source = /^[a-z][a-z\d+.-]*:\/\//i.test(raw) || raw.startsWith('/') ? raw : `//${raw}`;
try {
- return new URL(apiUrl, pageOrigin).origin;
+ const origin = new URL(source, pageOrigin).origin;
+ return origin === 'null' ? pageOrigin : origin;
} catch {
return pageOrigin;
}
diff --git a/docs/17-dashboard-design.md b/docs/17-dashboard-design.md
index fbad8da2d..93493fb09 100644
--- a/docs/17-dashboard-design.md
+++ b/docs/17-dashboard-design.md
@@ -550,8 +550,9 @@ Real-time updates use **socket.io** (`socket.io-client`), not a raw browser `Web
`src/hooks/useWebSocket.ts`. Key facts:
- **Namespace `/events`** (not `/ws`). The client connects to `<origin>/events`, where the origin is
- `VITE_WS_URL` when set, else the origin of `VITE_API_URL` (so a split-origin build reaches the
- API), else `window.location.origin`.
+ that of `VITE_WS_URL` when set, else that of `VITE_API_URL` (so a split-origin build reaches the
+ API), else `window.location.origin`. A path or trailing slash on either variable is dropped, and a
+ value with no scheme (`host:port`) is dialled on the page's protocol.
- **API key via the socket.io `auth` payload (and an `X-API-Key` header for proxies), deliberately
_not_ in the query string** — a key in the handshake URL would leak into access logs / `Referer`.
- **Reconnection is socket.io's built-in mechanism** — `reconnectionAttempts: 5`,
@@ -570,7 +571,7 @@ import { io, Socket } from 'socket.io-client';
import { API_ORIGIN } from '../services/api';
import { resolveSocketUrl } from '../utils/urlSecurity';
-// VITE_WS_URL, else the VITE_API_URL origin, else the page origin.
+// The origin of VITE_WS_URL, else of VITE_API_URL, else the page origin.
const SOCKET_URL = resolveSocketUrl(import.meta.env.VITE_WS_URL, API_ORIGIN, window.location.origin);
interface ServerEventEnvelope {