Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(wwebjs): fail the session on an unusable stored proxy
Yudhi Armyndharisf7b7a0e777bf
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.tsindex ac6b1d4e2..ba497c798 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts@@ -443,6 +443,52 @@ describe('WhatsAppWebJsAdapter initialize() retry on a navigation-killed first i});});+// A stored proxy that bypassed DTO validation used to be dropped with a warning, so Chromium+// launched and the session egressed from the host's own address. It must fail like Baileys (#859).+describe('WhatsAppWebJsAdapter initialize() with an unusable stored proxy', () => {+ let rmSpy: jest.SpyInstance;+ let clientInitSpy: jest.SpyInstance;+ let savedWebVersion: string | undefined;++ beforeEach(() => {+ savedWebVersion = process.env.WWEBJS_WEB_VERSION;+ process.env.WWEBJS_WEB_VERSION = 'off';+ rmSpy = jest.spyOn(fs.promises, 'rm').mockResolvedValue(undefined);+ clientInitSpy = jest+ .spyOn(Client.prototype as unknown as { initialize: () => Promise<void> }, 'initialize')+ .mockResolvedValue(undefined);+ });++ afterEach(() => {+ rmSpy.mockRestore();+ clientInitSpy.mockRestore();+ if (savedWebVersion === undefined) {+ delete process.env.WWEBJS_WEB_VERSION;+ } else {+ process.env.WWEBJS_WEB_VERSION = savedWebVersion;+ }+ });++ it.each([+ ['proxy.example.com:8080', /not a supported http\(s\)\/socks4\/socks5 URL/],+ ['http://u:p%zz@h:1', /malformed percent-encoded credentials/],+ ])('fails the session without launching for %s', async (url, reason) => {+ const adapter = new WhatsAppWebJsAdapter({+ sessionId: 'sess-bad-proxy',+ sessionDataPath: './data/sessions',+ puppeteer: {},+ proxy: { url, type: 'http' },+ });+ const onError = jest.fn();++ await expect(adapter.initialize({ onError })).rejects.toThrow(reason);++ expect(adapter.getStatus()).toBe(EngineStatus.FAILED);+ expect(onError).toHaveBeenCalledWith(expect.stringMatching(reason));+ expect(clientInitSpy).not.toHaveBeenCalled();+ });+});+describe('buildProxyLaunchConfig (#628 — proxy credentials must not go into --proxy-server)', () => {it('strips credentials from an HTTP proxy and returns them as proxyAuthentication', () => {expect(buildProxyLaunchConfig('http://user:[email protected]:8080')).toEqual({diff --git a/src/engine/adapters/wwebjs-lifecycle.ts b/src/engine/adapters/wwebjs-lifecycle.tsindex 804a55b29..9614066c1 100644--- a/src/engine/adapters/wwebjs-lifecycle.ts+++ b/src/engine/adapters/wwebjs-lifecycle.ts@@ -273,8 +273,9 @@ export class WwebjsLifecycle {? [...this.host.config.puppeteer.args]: withPinnedBrowserLocale(DEFAULT_PUPPETEER_ARGS);- // Add proxy configuration if provided — but only when the URL parses to a supported scheme, so- // a malformed/stored proxy value can't break the Chromium launch or smuggle a non-proxy scheme.+ // Add proxy configuration if provided. A stored proxy that does not parse to a supported scheme+ // fails the session, as on Baileys (#859): launching without it would send the session's traffic+ // out of the host's own address instead of the egress the operator chose.let proxyAuthentication: { username: string; password: string } | undefined;if (this.host.config.proxy) {if (isSupportedProxyUrl(this.host.config.proxy.url)) {@@ -291,7 +292,10 @@ export class WwebjsLifecycle {}this.host.logger.log(`Using proxy: ${proxyLaunch.serverArg}`);} else {- this.host.logger.warn(`Ignoring invalid proxy URL for session ${this.host.config.sessionId}`);+ throw new Error(+ `The session proxy URL is not a supported http(s)/socks4/socks5 URL; fix or clear it with ` ++ `PATCH /api/sessions/${this.host.config.sessionId}/proxy`,+ );}}