Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test: pin the stall reaper's own rule and the bracketed IPv6 SOCKS proxy

Yudhi Armyndharis2248a73414fe
diff --git a/src/common/security/proxy-dispatcher.spec.ts b/src/common/security/proxy-dispatcher.spec.ts
index bc660a566..b6ddfde68 100644
--- a/src/common/security/proxy-dispatcher.spec.ts
+++ b/src/common/security/proxy-dispatcher.spec.ts
@@ -1,5 +1,6 @@
import * as http from 'http';
import * as net from 'net';
+import { networkInterfaces } from 'os';
import type { AddressInfo } from 'net';
import { Dispatcher1Wrapper, fetch as undiciFetch } from 'undici';
import { createProxyDispatcher, hasUnauthenticatableSocks4Credentials, urlFetchProxy } from './proxy-dispatcher';
@@ -169,6 +170,17 @@ describe('createProxyDispatcher over real SOCKS proxies', () => {
expect(proxy.destinations).toEqual(['2606:2800:220:1:248:1893:25c8:1946:80', '127.0.0.1:80']);
});
+ // `URL` keeps an IPv6 literal bracketed, and the brackets would go to the proxy's address lookup as part of a
+ // name. The IPv4-mapped form reaches the IPv4 loopback listener of the helper on a host with IPv6.
+ const ipv6Available = Object.values(networkInterfaces()).some(list => list?.some(a => a.family === 'IPv6'));
+ (ipv6Available ? it : it.skip)('reaches a proxy given as a bracketed IPv6 literal', async () => {
+ proxy = await startSocks5Proxy({ relayTo: portOf(origin) });
+ const bracketed = proxy.url.replace('127.0.0.1', '[::ffff:127.0.0.1]');
+
+ expect(await originAnswers(bracketed, 'http://203.0.113.7/file')).toBe('origin saw /file');
+ expect(proxy.destinations).toHaveLength(1);
+ });
+
it('authenticates a socks5 proxy with the decoded credentials', async () => {
// URL credentials stay percent-encoded until they are decoded for the handshake.
proxy = await startSocks5Proxy({ relayTo: portOf(origin), credentials: { username: 'us@er', password: 'p@ss:1' } });
diff --git a/src/config/inflight-body-budget.spec.ts b/src/config/inflight-body-budget.spec.ts
index 0c39a37ef..934bbc798 100644
--- a/src/config/inflight-body-budget.spec.ts
+++ b/src/config/inflight-body-budget.spec.ts
@@ -479,6 +479,26 @@ describe('stall reaper', () => {
expect(budget.currentBytes()).toBe(0);
});
+ it('reaps a body that stops after part of it arrived, long before the pace check would', () => {
+ // 10 MiB of a 20 MiB body lands in the first seconds and then nothing does. The pace the request
+ // timeout allows is still far below what has arrived, so only the stall rule can release it.
+ const budget = createInflightBodyBudget(100 * MB, { perClientShare: 1 });
+ const req = makeReq({ 'content-length': String(20 * MB) });
+ run(budget, req);
+ const socket = (req as unknown as { socket: { bytesRead: number } }).socket;
+
+ jest.advanceTimersByTime(1_000);
+ socket.bytesRead = 10 * MB;
+ jest.advanceTimersByTime(5_000);
+ expect(destroyMock(req)).not.toHaveBeenCalled();
+ expect(budget.currentBytes()).toBe(20 * MB);
+
+ // 15 s of silence since the poll that saw the last byte, plus one poll to notice it.
+ jest.advanceTimersByTime(25_000);
+ expect(destroyMock(req)).toHaveBeenCalledTimes(1);
+ expect(budget.currentBytes()).toBe(0);
+ });
+
it('does not reap a connection that keeps making progress, and keeps its declared size', () => {
const budget = createInflightBodyBudget(1000, { perClientShare: 1 });
const req = makeReq({ 'content-length': '400' });