Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): pin forwarded origin against network-path host hijack
Yudhi Armyndharis8f540abed623
diff --git a/src/modules/session/session-proxy.interceptor.spec.ts b/src/modules/session/session-proxy.interceptor.spec.tsindex 425b7b9e3..e2ed31db1 100644--- a/src/modules/session/session-proxy.interceptor.spec.ts+++ b/src/modules/session/session-proxy.interceptor.spec.ts@@ -343,6 +343,17 @@ describe('forwardTarget', () => {expect(target.pathname).toBe('/api/sessions/s1');});+ it.each([+ // A pathname that resolves to a leading `//` is a network-path reference: re-anchoring it with+ // `new URL(path, base)` would read `attacker` as the authority. The setter-based rebuild must not.+ ['double-slash path after an absolute host', 'http://attacker.tld//evil.host/api/x'],+ ['bare network-path in the path', 'http://attacker.tld//evil.host/foo?keep=1'],+ // A control character can split the parse and shift the host the same way.+ ['embedded tab', 'http://attacker.tld/\t/evil.host/x'],+ ])('keeps the owner origin for a %s (network-path / control-char hijack)', (_label, originalUrl) => {+ expect(new URL(forwardTarget(originalUrl, OWNER)).origin).toBe('http://10.0.0.5:2785');+ });+it('carries the owner base path when one is configured', () => {expect(forwardTarget('/api/sessions/s1', 'http://10.0.0.5:2785/')).toBe('http://10.0.0.5:2785/api/sessions/s1');});diff --git a/src/modules/session/session-proxy.interceptor.ts b/src/modules/session/session-proxy.interceptor.tsindex 28f1d9c9e..4c4d4d932 100644--- a/src/modules/session/session-proxy.interceptor.ts+++ b/src/modules/session/session-proxy.interceptor.ts@@ -58,11 +58,23 @@ export const FORWARDED_HEADER = 'x-openwa-forwarded';* any origin they choose and receive the response, with the credentials of the call attached: a* server-side request forgery that also exfiltrates the API key. Rebuilding from the owner's base* makes the origin structurally un-influenceable rather than merely validated.+ *+ * The rebuild carries the path and query only, and it does so through the URL setters rather than+ * `new URL(path, base)`: a `pathname` that begins with `//` (or that a control character re-parses+ * into) is a network-path reference, so `new URL('//elsewhere/x', base)` would resolve its authority+ * from the path and re-open the exact origin hijack this function exists to close. Assigning+ * `target.pathname` / `target.search` on a clone of the owner's origin never reinterprets the value+ * as an authority, so the host stays the owner's for every input. Routing cannot currently deliver+ * such a target (Express does not match a session route whose path starts with `//`), but the origin+ * must be un-influenceable by construction, not by the router's normalization holding.*/export function forwardTarget(originalUrl: string, ownerNodeUrl: string): string {const base = new URL(ownerNodeUrl);const requested = new URL(originalUrl, base);- return new URL(`${requested.pathname}${requested.search}`, base).toString();+ const target = new URL(base.toString());+ target.pathname = requested.pathname;+ target.search = requested.search;+ return target.toString();}/**