Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(http): let bodyless requests skip the in-flight body budget

Yudhi Armyndharis9a619b122738
diff --git a/src/config/inflight-body-budget.spec.ts b/src/config/inflight-body-budget.spec.ts
index 55423e79a..fc3cff197 100644
--- a/src/config/inflight-body-budget.spec.ts
+++ b/src/config/inflight-body-budget.spec.ts
@@ -241,6 +241,44 @@ describe('createInflightBodyBudget middleware', () => {
expect(req.listenerCount('data')).toBe(0);
});
+ describe('with the client map at capacity', () => {
+ const fromIp = (ip: string, headers: Record<string, string> = {}): Request & EventEmitter => {
+ const req = makeReq(headers);
+ (req as unknown as { socket: { remoteAddress: string } }).socket.remoteAddress = ip;
+ return req;
+ };
+ const saturated = (): InflightBodyBudget => {
+ const budget = createInflightBodyBudget(1_000_000, { perClientShare: 1 });
+ for (let i = 0; i < 10_000; i++) {
+ const ip = `10.${Math.floor(i / 65536)}.${Math.floor(i / 256) % 256}.${i % 256}`;
+ expect(run(budget, fromIp(ip, { 'content-length': '10' })).next).toHaveBeenCalledTimes(1);
+ }
+ expect(budget.currentBytes()).toBe(100_000);
+ return budget;
+ };
+
+ it('still admits bodyless requests from a new client, without tracking them', () => {
+ const budget = saturated();
+ for (const headers of [{}, { 'content-length': '0' }] as Record<string, string>[]) {
+ const req = fromIp('192.0.2.200', headers);
+ const { next, state, res } = run(budget, req);
+ expect(next).toHaveBeenCalledTimes(1);
+ expect(state.code).toBe(0);
+ expect(budget.clientBytes(req)).toBe(0);
+ expect(req.listenerCount('close')).toBe(0);
+ expect(res.listenerCount('finish')).toBe(0);
+ }
+ expect(budget.currentBytes()).toBe(100_000);
+ });
+
+ it('still refuses a body-carrying request from a new client', () => {
+ const budget = saturated();
+ const { next, state } = run(budget, fromIp('192.0.2.200', { 'content-length': '10' }));
+ expect(next).not.toHaveBeenCalled();
+ expect(state.code).toBe(503);
+ });
+ });
+
it('drops the socket instead of writing a 503 when the response is already flushing', () => {
const budget = createInflightBodyBudget(1000, { perClientShare: 1 });
run(budget, makeReq({ 'content-length': '800' }));
diff --git a/src/config/inflight-body-budget.ts b/src/config/inflight-body-budget.ts
index 0b5c96864..29e6c130e 100644
--- a/src/config/inflight-body-budget.ts
+++ b/src/config/inflight-body-budget.ts
@@ -136,10 +136,11 @@ export function createInflightBodyBudget(budgetBytes: number, options?: Inflight
const perClientCap = Math.max(1, Math.floor(budgetBytes * share));
// Per-client in-flight bytes, keyed on the resolved client IP (an IPv6 client on its /64).
// Entries are created lazily and deleted by the same exactly-once release that decrements the
- // aggregate, so the map cannot
- // leak a client that finished. A cap on the MAP itself guards the pathological many-spoofed-IPs
- // case: past it, a NEW client key is treated as busiest (refused) rather than evicting a live
- // one - refusing beats corrupting another client's accounting.
+ // aggregate, so the map cannot leak a client that finished. Only body-carrying requests are
+ // tracked. A cap on the MAP itself guards the pathological many-spoofed-IPs case: past it, a NEW
+ // client key sending a body is treated as busiest (refused) rather than evicting a live one -
+ // refusing beats corrupting another client's accounting. Bodyless requests (GETs, health
+ // probes) never touch the map, so a full map cannot refuse them.
const clientInFlight = new Map<string, number>();
const MAX_TRACKED_CLIENTS = 10_000;
// Opening reservation for a body with no declared length (chunked). It is only a placeholder:
@@ -185,6 +186,7 @@ export function createInflightBodyBudget(budgetBytes: number, options?: Inflight
// A body with no declared length is expected only when the request is chunk-encoded (Node
// ignores close-delimited request bodies on keep-alive HTTP/1.1). Anything else — GETs,
// health checks, Content-Length: 0 — reserves nothing and is never reaped.
+ // It is never refused or tracked either: see the early return below the encoding guard.
let reserved = declared ?? (req.headers['transfer-encoding'] !== undefined ? undeclaredReservation : 0);
// Every quantity this budget works with — the declared length, and socket.bytesRead in the
@@ -212,6 +214,13 @@ export function createInflightBodyBudget(budgetBytes: number, options?: Inflight
return;
}
+ // Nothing to reserve, so nothing to refuse, track or reap. This must stay BELOW the encoding
+ // guard: a compressed body with a zero or unusable Content-Length reserves 0 yet still gets 415.
+ if (reserved === 0) {
+ next();
+ return;
+ }
+
// Admission control on the RESERVED size: a request that would push the aggregate OR ITS
// CLIENT'S SHARE past the budget is refused before a single byte of its body is buffered.
// The per-client check is what makes the DoS bounded per attacker rather than per deployment: