Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): keep the NODE_URL path prefix when forwarding

Yudhi Armyndharis5d8b45bc7e81
diff --git a/src/modules/session/session-proxy.interceptor.spec.ts b/src/modules/session/session-proxy.interceptor.spec.ts
index e3c9b9f93..8e38bd5a5 100644
--- a/src/modules/session/session-proxy.interceptor.spec.ts
+++ b/src/modules/session/session-proxy.interceptor.spec.ts
@@ -453,5 +453,18 @@ describe('forwardTarget', () => {
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');
+ // An owner behind a path-prefixed reverse proxy: the prefix is where its API lives.
+ expect(forwardTarget('/api/sessions/s1?x=1', 'https://node-a.example.com/openwa/')).toBe(
+ 'https://node-a.example.com/openwa/api/sessions/s1?x=1',
+ );
+ expect(forwardTarget('/api/sessions/s1', 'https://node-a.example.com/openwa')).toBe(
+ 'https://node-a.example.com/openwa/api/sessions/s1',
+ );
+ });
+
+ it('keeps the owner origin and prefix for a network-path target under a prefixed base', () => {
+ const target = new URL(forwardTarget('http://attacker.tld//evil.host/x', 'https://node-a.example.com/openwa'));
+ expect(target.origin).toBe('https://node-a.example.com');
+ expect(target.pathname.startsWith('/openwa/')).toBe(true);
});
});
diff --git a/src/modules/session/session-proxy.interceptor.ts b/src/modules/session/session-proxy.interceptor.ts
index 1eb67f1b0..ab6f361f5 100644
--- a/src/modules/session/session-proxy.interceptor.ts
+++ b/src/modules/session/session-proxy.interceptor.ts
@@ -65,7 +65,8 @@ const NOT_DISPATCHED_CODES = new Set([
export const FORWARDED_HEADER = 'x-openwa-forwarded';
/**
- * The URL to forward to: ALWAYS the owner's origin, carrying only the request's path and query.
+ * The URL to forward to: ALWAYS the owner's origin (under its NODE_URL path, if it has one),
+ * carrying only the request's path and query.
*
* The request target is caller-controlled. HTTP/1.1 allows the absolute form
* (`GET http://elsewhere/api/sessions/x HTTP/1.1`), Express matches the route for it, and
@@ -88,7 +89,8 @@ export function forwardTarget(originalUrl: string, ownerNodeUrl: string): string
const base = new URL(ownerNodeUrl);
const requested = new URL(originalUrl, base);
const target = new URL(base.toString());
- target.pathname = requested.pathname;
+ // Joined, not replaced: an owner behind a path-prefixed reverse proxy is reachable only under it.
+ target.pathname = base.pathname.replace(/\/$/, '') + requested.pathname;
target.search = requested.search;
return target.toString();
}