Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): dial only the VITE_API_URL origin for the socket

Yudhi Armyndharis95c026660d7c
diff --git a/dashboard/src/utils/urlSecurity.test.ts b/dashboard/src/utils/urlSecurity.test.ts
index e6b794a1c..69edfb12d 100644
--- a/dashboard/src/utils/urlSecurity.test.ts
+++ b/dashboard/src/utils/urlSecurity.test.ts
@@ -65,3 +65,11 @@ test('resolveSocketUrl prefers VITE_WS_URL, then the API origin, then the page o
assert.equal(resolveSocketUrl(undefined, api, page), api);
assert.equal(resolveSocketUrl('https://ws.example.com', api, page), 'https://ws.example.com');
});
+
+test('resolveSocketUrl keeps only the origin of a VITE_API_URL that carries a path', () => {
+ const page = 'https://dashboard.example.com';
+ assert.equal(resolveSocketUrl(undefined, 'https://api.example.com/gw', page), 'https://api.example.com');
+ assert.equal(resolveSocketUrl(undefined, 'https://api.example.com:8443/gw/v1', page), 'https://api.example.com:8443');
+ // A relative value is a path on the page's own host.
+ assert.equal(resolveSocketUrl(undefined, '/gw', page), page);
+});
diff --git a/dashboard/src/utils/urlSecurity.ts b/dashboard/src/utils/urlSecurity.ts
index db180d768..8aff29ab0 100644
--- a/dashboard/src/utils/urlSecurity.ts
+++ b/dashboard/src/utils/urlSecurity.ts
@@ -27,10 +27,17 @@ export function warnIfInsecureHttpUrl(url: string, label: string): string {
}
/**
- * The origin the realtime socket dials: VITE_WS_URL when set, else the API origin VITE_API_URL
- * names (a split-origin build serves the socket from the API, not from the dashboard's host), else
- * the page's own origin.
+ * The origin the realtime socket dials: VITE_WS_URL when set, else the origin 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.
*/
-export function resolveSocketUrl(wsUrl: string | undefined, apiOrigin: string, pageOrigin: string): string {
- return wsUrl || apiOrigin || pageOrigin;
+export function resolveSocketUrl(wsUrl: string | undefined, apiUrl: string, pageOrigin: string): string {
+ if (wsUrl) return wsUrl;
+ if (!apiUrl) return pageOrigin;
+ try {
+ return new URL(apiUrl, pageOrigin).origin;
+ } catch {
+ return pageOrigin;
+ }
}
diff --git a/docs/17-dashboard-design.md b/docs/17-dashboard-design.md
index 43d473052..0c83c4af2 100644
--- a/docs/17-dashboard-design.md
+++ b/docs/17-dashboard-design.md
@@ -548,9 +548,9 @@ export function useStopSessionMutation() {
Real-time updates use **socket.io** (`socket.io-client`), not a raw browser `WebSocket`. The hook is
`src/hooks/useWebSocket.ts`. Key facts:
-- **Namespace `/events`** (not `/ws`). The client connects to
- `${VITE_WS_URL || VITE_API_URL || window.location.origin}/events` — same-origin by default; a split-origin build
- follows the `VITE_API_URL` origin, and `VITE_WS_URL` overrides both.
+- **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`.
- **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`,
@@ -749,8 +749,9 @@ VITE_API_URL=https://api.example.com npm run build # in dashboard/
```
`dashboard/src/services/api.ts` reads `VITE_API_URL` and calls that origin instead of same-origin
-`/api`. The realtime socket follows the same origin unless `VITE_WS_URL` overrides it. Set `SERVE_DASHBOARD=false` on the API so it stops serving its own copy. Remember to add the
-dashboard's origin to `CORS_ORIGINS` on the API.
+`/api`. The realtime socket follows the same origin unless `VITE_WS_URL` overrides it. Set
+`SERVE_DASHBOARD=false` on the API so it stops serving its own copy. Remember to add the dashboard's
+origin to `CORS_ORIGINS` on the API.
For TLS or public exposure of the default single-port setup, terminate at your own reverse proxy
(nginx, Caddy, a cloud load balancer, or a k8s Ingress) in front of the API; see