Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(http): drop request bodies that fall behind the request timeout
Yudhi Armyndharis554534f29e4f
diff --git a/.env.example b/.env.exampleindex f1878c5d6..8fb23aef7 100644--- a/.env.example+++ b/.env.example@@ -906,8 +906,8 @@ PLUGINS_DIR=./data/plugins # Plugin directory (default: ./data/plugins)# get 503 + Retry-After without their body being read (protects against slow-body memory pinning).# Keep it >= BODY_SIZE_LIMIT. Default: 4 x BODY_SIZE_LIMIT (100 MiB with the default 25mb cap).# One client IP may hold at most HALF of this budget in flight and gets the same 503 + Retry-After-# past that, so a single heavy uploader cannot pin the whole gateway. Behind a reverse proxy set-# TRUSTED_PROXIES, or every caller resolves to the proxy address and shares that one half.+# past that. Behind a reverse proxy set TRUSTED_PROXIES, or every caller resolves to the proxy+# address and shares that one half. A request holds its declared size (a chunked body 1 MiB, or+# what has arrived past that) until it finishes; a body that sends nothing for 15 s, or that falls+# behind the pace needed to deliver what it holds within REQUEST_TIMEOUT_MS, is dropped.# INFLIGHT_BODY_BUDGET_BYTES=104857600-# # Each client IP gets half the budget as its own share (no one-# # uploader/attacker can pin more), keyed via TRUSTED_PROXIES.diff --git a/docs/04-security-design.md b/docs/04-security-design.mdindex f97f566e3..06541638f 100644--- a/docs/04-security-design.md+++ b/docs/04-security-design.md@@ -299,6 +299,15 @@ Every response the per-IP window admits carries `X-RateLimit-*-ingress-ip`, an aThe API exposes the rate-limit headers via CORS (`exposedHeaders`) so browser clients can read them, the plain `Retry-After` included. The simplest backpressure signal remains the `429` status itself, with `Retry-After` as the retry delay.+### In-flight request bodies++The windows above run at the routing layer, after a request body has been buffered, so they cannot bound memory held by slow uploads. A pre-routing middleware does: it caps the request-body bytes in flight across all connections at `INFLIGHT_BODY_BUDGET_BYTES` (default 4 x `BODY_SIZE_LIMIT`) and answers a request that would cross it with `503` and `Retry-After`, before reading its body.++- One client (IPv6 on its /64) may hold at most half of the budget.+- A request holds its declared `Content-Length` until it finishes. After a 15-second grace it must keep up the pace that lands the whole body within `REQUEST_TIMEOUT_MS` (by default 25 MiB in 300 seconds, about 85 KiB/s); a request that falls behind is dropped and its reservation released. A chunked body holds a 1 MiB placeholder, or the bytes that have arrived once they pass it, and must deliver the placeholder at the same pace (about 3.4 KiB/s by default). If that growth crosses the budget or its client's half, that request is aborted.+- A body that sends nothing for 15 seconds is dropped and its reservation released.+- Requests without a body are never refused for lack of budget.+### WebSocket (`/events`) limitsSocket.IO frames never pass through the Nest enhancer pipeline, so the HTTP windows above do **not** apply to the WebSocket surface. `EventsGateway` enforces its own in-process limits instead (all keyed in-memory per process; any blank/non-positive/non-numeric env value falls back to the default):diff --git a/src/config/configuration.ts b/src/config/configuration.tsindex b0732702d..35b5ac79c 100644--- a/src/config/configuration.ts+++ b/src/config/configuration.ts@@ -2,6 +2,7 @@ import * as path from 'path';import { computeFeatureFlags } from './feature-flags';import { computeSendPacingConfig } from '../modules/message/send-pacing.config';import { resolveInflightBodyBudgetBytes } from './inflight-body-budget';+import { resolveRequestTimeoutMs } from './http-timeouts';import { readWsRateLimitConfig } from '../modules/events/ws-rate-limit';/**@@ -111,7 +112,7 @@ export default () => ({// requires headers > keepAlive, and main.ts normalizes it anyway). Set any to 0 at your own risk;// env.validation rejects 0 so a bound is never silently disabled.http: {- requestTimeoutMs: parseInt(process.env.REQUEST_TIMEOUT_MS || '300000', 10),+ requestTimeoutMs: resolveRequestTimeoutMs(process.env.REQUEST_TIMEOUT_MS),headersTimeoutMs: parseInt(process.env.HEADERS_TIMEOUT_MS || '65000', 10),keepAliveTimeoutMs: parseInt(process.env.KEEPALIVE_TIMEOUT_MS || '5000', 10),// Aggregate cap on request-body bytes buffered across ALL connections (the pre-body-parserdiff --git a/src/config/http-timeouts.spec.ts b/src/config/http-timeouts.spec.tsindex c390bdded..6f3ffef30 100644--- a/src/config/http-timeouts.spec.ts+++ b/src/config/http-timeouts.spec.ts@@ -1,4 +1,12 @@-import { applyHttpTimeouts, HttpTimeoutSink } from './http-timeouts';+import { applyHttpTimeouts, HttpTimeoutSink, resolveRequestTimeoutMs } from './http-timeouts';++describe('resolveRequestTimeoutMs', () => {+ it("defaults to Node's 300 s and honours REQUEST_TIMEOUT_MS", () => {+ expect(resolveRequestTimeoutMs(undefined)).toBe(300000);+ expect(resolveRequestTimeoutMs('')).toBe(300000);+ expect(resolveRequestTimeoutMs('120000')).toBe(120000);+ });+});describe('applyHttpTimeouts', () => {const sink = (): HttpTimeoutSink => ({ requestTimeout: 0, headersTimeout: 0, keepAliveTimeout: 0 });diff --git a/src/config/http-timeouts.ts b/src/config/http-timeouts.tsindex b328139a1..0d9169a2a 100644--- a/src/config/http-timeouts.ts+++ b/src/config/http-timeouts.ts@@ -14,6 +14,14 @@ export interface HttpTimeoutConfig {keepAliveTimeoutMs: number;}+/**+ * REQUEST_TIMEOUT_MS as configured, defaulting to Node's 300 s. Shared by the server config and the+ * in-flight body budget, which paces declared bodies against the same timeout.+ */+export function resolveRequestTimeoutMs(raw: string | undefined): number {+ return parseInt(raw || '300000', 10);+}+/** The values actually written, after any normalization. */export type HttpTimeoutReport = HttpTimeoutConfig;diff --git a/src/config/inflight-body-budget.spec.ts b/src/config/inflight-body-budget.spec.tsindex fc3cff197..b83511a89 100644--- a/src/config/inflight-body-budget.spec.ts+++ b/src/config/inflight-body-budget.spec.ts@@ -341,7 +341,7 @@ describe('stall reaper', () => {expect(budget.currentBytes()).toBe(0);});- it('does not reap a connection that keeps making progress, however slow', () => {+ 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' });run(budget, req);@@ -354,6 +354,148 @@ describe('stall reaper', () => {}expect(destroyMock(req)).not.toHaveBeenCalled();expect(budget.currentBytes()).toBe(400);+ expect(budget.clientBytes(req)).toBe(400);+ });++ describe('reservation of a slow body', () => {+ const socketOf = (req: Request): { bytesRead: number; remoteAddress: string } =>+ (req as unknown as { socket: { bytesRead: number; remoteAddress: string } }).socket;+ const trickle = (reqs: Request[], bytes: number, ms: number): void => {+ for (let t = 0; t < ms; t += 1_000) {+ jest.advanceTimersByTime(1_000);+ if ((t + 1_000) % 4_000 === 0) for (const r of reqs) socketOf(r).bytesRead += bytes;+ }+ };++ it('drops a declared body that falls behind the pace the request timeout allows, releasing it once', () => {+ const budget = createInflightBodyBudget(1000, { perClientShare: 1 });+ const req = makeReq({ 'content-length': '400' });+ const { res } = run(budget, req);++ // One byte every 4 s: never idle long enough for the stall reaper, far too slow to finish.+ trickle([req], 1, 20_000);+ expect(destroyMock(req)).toHaveBeenCalledTimes(1);+ expect(budget.currentBytes()).toBe(0);+ expect(budget.clientBytes(req)).toBe(0);++ // The terminal events the destroy triggers do not release it a second time.+ const other = makeReq({ 'content-length': '1000' });+ expect(run(budget, other).next).toHaveBeenCalledTimes(1);+ req.emit('close');+ res.emit('close');+ expect(budget.currentBytes()).toBe(1000);+ });++ it('keeps a declared body that arrives fast enough to finish within the request timeout', () => {+ const budget = createInflightBodyBudget(2000, { perClientShare: 1, requestTimeoutMs: 100_000 });+ const req = makeReq({ 'content-length': '1000' });+ run(budget, req);++ // 10 bytes/s finishes 1000 bytes in exactly the 100 s timeout.+ trickle([req], 40, 96_000);+ expect(destroyMock(req)).not.toHaveBeenCalled();+ expect(budget.currentBytes()).toBe(1000);+ });++ it('measures the pace against the configured request timeout', () => {+ const budget = createInflightBodyBudget(2000, { perClientShare: 1, requestTimeoutMs: 100_000 });+ const req = makeReq({ 'content-length': '1000' });+ run(budget, req);++ // Half the pace a 100 s timeout needs; the default 300 s timeout would still allow it.+ trickle([req], 20, 40_000);+ expect(destroyMock(req)).toHaveBeenCalledTimes(1);+ expect(budget.currentBytes()).toBe(0);+ });++ it('drops a chunked body that trickles below the pace its placeholder needs, releasing it once', () => {+ const budget = createInflightBodyBudget(2 * MB, { perClientShare: 1 });+ const req = makeReq({ 'transfer-encoding': 'chunked' });+ const { res } = run(budget, req);++ trickle([req], 1, 20_000);+ expect(destroyMock(req)).toHaveBeenCalledTimes(1);+ expect(budget.currentBytes()).toBe(0);+ expect(budget.clientBytes(req)).toBe(0);++ const other = makeReq({ 'content-length': String(2 * MB) });+ expect(run(budget, other).next).toHaveBeenCalledTimes(1);+ req.emit('close');+ res.emit('close');+ expect(budget.currentBytes()).toBe(2 * MB);+ });++ it('keeps a chunked body that stays on pace, and stops pacing it once it passes the placeholder', () => {+ const budget = createInflightBodyBudget(64 * MB, { perClientShare: 1, requestTimeoutMs: 100_000 });+ const req = makeReq({ 'transfer-encoding': 'chunked' });+ run(budget, req);++ // 10.5 KB/s delivers the 1 MiB placeholder inside the 100 s timeout, then keeps going past it.+ trickle([req], 42_000, 200_000);+ expect(destroyMock(req)).not.toHaveBeenCalled();+ expect(budget.currentBytes()).toBeGreaterThan(MB);+ });++ it('keeps a client share held by trickling requests', () => {+ const budget = createInflightBodyBudget(1000); // share 0.5 -> cap 500+ const a = makeReq({ 'content-length': '250' });+ const b = makeReq({ 'content-length': '250' });+ run(budget, a);+ run(budget, b);++ trickle([a, b], 1, 16_000);+ expect(budget.clientBytes(a)).toBe(500);+ expect(run(budget, makeReq({ 'content-length': '200' })).state.code).toBe(503);++ // Once they fall behind the pace the request timeout allows, both are dropped and the share frees.+ trickle([a, b], 1, 10_000);+ expect(destroyMock(a)).toHaveBeenCalledTimes(1);+ expect(destroyMock(b)).toHaveBeenCalledTimes(1);+ expect(budget.clientBytes(a)).toBe(0);+ expect(run(budget, makeReq({ 'content-length': '200' })).next).toHaveBeenCalledTimes(1);+ });++ it('never lets trickling bodies that complete together exceed the budget', () => {+ const budget = createInflightBodyBudget(1000, { perClientShare: 1 });+ const live: Request[] = [];+ // A new 200-byte body every second for two minutes, each trickling one byte every 4 s.+ for (let s = 1; s <= 120; s++) {+ const req = makeReq({ 'content-length': '200' });+ socketOf(req).remoteAddress = `198.51.100.${s}`;+ if (run(budget, req).next.mock.calls.length > 0) live.push(req);+ jest.advanceTimersByTime(1_000);+ if (s % 4 === 0) for (const r of live) socketOf(r).bytesRead += 4;+ }++ // Every live body then lands before the next poll.+ for (const r of live) {+ socketOf(r).bytesRead = 200;+ (r as unknown as { complete: boolean }).complete = true;+ }+ jest.advanceTimersByTime(5_000);+ expect(budget.currentBytes()).toBeLessThanOrEqual(1000);+ expect(live).toHaveLength(5);+ for (const r of live) expect(destroyMock(r)).not.toHaveBeenCalled();+ });++ it('refuses a new body while a trickling one holds its declaration, and settles it at its size', () => {+ const budget = createInflightBodyBudget(1000, { perClientShare: 1 });+ const req = makeReq({ 'content-length': '400' });+ const { res } = run(budget, req);+ trickle([req], 5, 16_000);+ expect(budget.currentBytes()).toBe(400);+ expect(run(budget, makeReq({ 'content-length': '900' })).state.code).toBe(503);++ // The rest of the body lands between polls and Node marks the message complete.+ socketOf(req).bytesRead = 400;+ (req as unknown as { complete: boolean }).complete = true;+ jest.advanceTimersByTime(5_000);+ expect(destroyMock(req)).not.toHaveBeenCalled();+ expect(budget.currentBytes()).toBe(400);++ res.emit('finish');+ expect(budget.currentBytes()).toBe(0);+ });});it('stops reaping once the declared body has fully arrived', () => {@@ -375,10 +517,12 @@ describe('stall reaper', () => {const req = makeReq({ 'transfer-encoding': 'chunked' });const { res } = run(budget, req);+ // A finished chunked body is priced at what arrived, not the opening placeholder.+ (req as unknown as { socket: { bytesRead: number } }).socket.bytesRead = 300;req.emit('end');jest.advanceTimersByTime(120_000);expect(destroyMock(req)).not.toHaveBeenCalled();- expect(budget.currentBytes()).toBe(1000);+ expect(budget.currentBytes()).toBe(300);res.emit('finish');expect(budget.currentBytes()).toBe(0);diff --git a/src/config/inflight-body-budget.ts b/src/config/inflight-body-budget.tsindex 29e6c130e..218be8048 100644--- a/src/config/inflight-body-budget.ts+++ b/src/config/inflight-body-budget.ts@@ -23,9 +23,12 @@* multiple of its wire size once parsed (the raw Buffer pinned on rawBody, plus the decoded string* and the parsed object) — the budget bounds the input, not the parse.*- * A stalled sender (headers, then silence) holds its- * reservation only until the stall reaper drops the socket after STALL_TIMEOUT_MS without any- * new body bytes, so a handful of silent connections cannot pin the whole budget. The request+ * A stalled sender (headers, then silence) holds its reservation only until the stall reaper drops+ * the socket after STALL_TIMEOUT_MS without any new body bytes. A body that keeps trickling is+ * dropped once it falls behind the pace needed to deliver its reservation (the declared size, or a+ * chunked body's placeholder) within Node's request timeout, after the same STALL_TIMEOUT_MS grace. The reservation is never lowered mid-stream,+ * only released with the request, so declared bodies that complete together stay within the+ * budget, and the per-client share bounds what one slow source can hold. The request* stream itself is never tapped — no 'data' listener — so downstream consumers (the body* parser, busboy) see every chunk exactly as it arrives, even when they attach late.*@@ -62,11 +65,14 @@ const FALLBACK_LIMIT_BYTES = 25 * UNIT_BYTES.mb;* such connections at the per-request cap would pin the entire default budget, renewable forever.* Any admitted request expecting a body is therefore polled: if no new body bytes arrive for* STALL_TIMEOUT_MS the socket is destroyed and the reservation released. Polling (rather than one- * fixed deadline) lets any progress reset the clock, so slow-but-moving uploads are untouched.+ * fixed deadline) lets any progress reset the clock, so slow-but-moving uploads are not dropped.*/const STALL_TIMEOUT_MS = 15_000;const STALL_POLL_MS = 5_000;+/** Node's default server.requestTimeout, used when the caller does not pass REQUEST_TIMEOUT_MS. */+const DEFAULT_REQUEST_TIMEOUT_MS = 300_000;+/*** What a chunked (undeclared-length) body reserves before any of it has arrived. The same poll that* watches for a stall also reconciles this against `socket.bytesRead`, so the reservation converges@@ -118,6 +124,13 @@ export interface InflightBodyBudgetOptions {* a legitimate bulk uploader above the share gets 503 + Retry-After, not a hang.*/perClientShare?: number;+ /**+ * Node's request timeout (REQUEST_TIMEOUT_MS) in milliseconds. A body that falls behind the pace+ * needed to deliver its reservation within it (the declared size, or a chunked body's+ * placeholder) is dropped. Default 300 000, Node's own default; 0 or less+ * turns the pace check off.+ */+ requestTimeoutMs?: number;}export interface InflightBodyBudget {@@ -134,6 +147,7 @@ export function createInflightBodyBudget(budgetBytes: number, options?: Inflightconst trustedProxies = options?.trustedProxies ?? [];const share = Math.min(1, Math.max(Number.EPSILON, options?.perClientShare ?? 0.5));const perClientCap = Math.max(1, Math.floor(budgetBytes * share));+ const requestTimeoutMs = options?.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;// 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. Only body-carrying requests are@@ -223,9 +237,7 @@ export function createInflightBodyBudget(budgetBytes: number, options?: Inflight// 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:- // four trickle connections from one source exhaust only that source's share, and every other- // client's uploads still land.+ // The per-client share bounds what one source can hold.const clientKey = limiterKeyForIp(resolveClientIp(req, trustedProxies));const clientBusy = clientInFlight.get(clientKey) ?? 0;const mapAtCapacity = clientInFlight.size >= MAX_TRACKED_CLIENTS && !clientInFlight.has(clientKey);@@ -235,7 +247,7 @@ export function createInflightBodyBudget(budgetBytes: number, options?: Inflight}inFlightBytes += reserved;- if (!mapAtCapacity) clientInFlight.set(clientKey, clientBusy + reserved);+ clientInFlight.set(clientKey, clientBusy + reserved);let released = false;let stallTimer: ReturnType<typeof setInterval> | undefined;@@ -245,7 +257,7 @@ export function createInflightBodyBudget(budgetBytes: number, options?: InflightstallTimer = undefined;};- // Exactly-once release: the first terminal event wins — normal completion (res 'finish'),+ // Exactly-once release: the first terminal event wins: normal completion (res 'finish'),// client/socket abort (req/res 'close'), stream failure (req/res 'error'). An aborted upload// typically fires several of these; the flag guarantees the aggregate is decremented once.const release = (): void => {@@ -253,11 +265,9 @@ export function createInflightBodyBudget(budgetBytes: number, options?: Inflightreleased = true;disarmStallReaper();inFlightBytes -= reserved;- if (!mapAtCapacity) {- const busy = (clientInFlight.get(clientKey) ?? reserved) - reserved;- if (busy > 0) clientInFlight.set(clientKey, busy);- else clientInFlight.delete(clientKey);- }+ const busy = (clientInFlight.get(clientKey) ?? reserved) - reserved;+ if (busy > 0) clientInFlight.set(clientKey, busy);+ else clientInFlight.delete(clientKey);};res.on('finish', release);res.on('close', release);@@ -265,71 +275,87 @@ export function createInflightBodyBudget(budgetBytes: number, options?: Inflightreq.on('close', release);req.on('error', release);- if (reserved > 0) {- // Stall reaper (see STALL_TIMEOUT_MS above). Progress is measured on the SOCKET byte- // counter, never on the request stream: attaching a 'data' listener would switch the- // stream to flowing mode and eat chunks before a late consumer (the async guards run- // before busboy/body-parser attach) ever sees them. 'end' is safe to observe — it does- // not start the flow — and disarms the reaper once the real consumer finished reading.- const socket = req.socket;- const startBytes = socket.bytesRead;- let lastBytes = startBytes;- let lastProgress = Date.now();+ // Stall reaper (see STALL_TIMEOUT_MS above). Progress is measured on the SOCKET byte counter,+ // never on the request stream: attaching a 'data' listener would switch the stream to flowing+ // mode and eat chunks before a late consumer (the async guards run before busboy/body-parser+ // attach) ever sees them. 'end' is safe to observe (it does not start the flow) and disarms+ // the reaper once the real consumer finished reading.+ const socket = req.socket;+ const startBytes = socket.bytesRead;+ let lastBytes = startBytes;+ const admittedAt = Date.now();+ let lastProgress = admittedAt;- // Undeclared length: replace the opening placeholder with what has actually arrived, so a- // small chunked upload stops holding a big reservation and a large one is accounted honestly.- // Crossing the budget mid-stream aborts the request — the same bound a declared length gets- // at admission, applied to a sender that declined to declare one.- const reconcileUndeclared = (readNow: number): void => {- const actual = Math.max(undeclaredReservation, readNow - startBytes);- if (actual === reserved) return;- const delta = actual - reserved;- inFlightBytes += delta;- reserved = actual;- if (!mapAtCapacity) {- // Crossing the per-client cap mid-stream aborts too: the share bound applies to what is- // actually arriving, not only to what was declared. reserved is updated BEFORE release()- // so the exactly-once decrement subtracts the reconciled size, not the placeholder.- const busy = (clientInFlight.get(clientKey) ?? 0) + delta;- clientInFlight.set(clientKey, busy);- if (busy > perClientCap) {- release();- req.destroy();- return;- }- }- if (inFlightBytes > budgetBytes) {- release();- req.destroy();- }- };+ // A declared body keeps its declared size until it is released. A chunked body is re-priced at+ // the bytes that have arrived, never below its opening placeholder until it is complete: budget+ // handed back mid-stream would be taken back, unchecked, by a body that then completes between+ // polls. Growth that crosses the aggregate or the client share aborts the+ // request mid-stream, the same bound a declared length gets at admission. A complete body is+ // re-priced at its real size but never aborted: it is already buffered. reserved is updated+ // BEFORE release() so the exactly-once decrement subtracts the reconciled size.+ const reconcile = (complete: boolean): void => {+ if (released || declared !== undefined) return;+ const floor = complete ? 0 : undeclaredReservation;+ const actual = Math.max(floor, socket.bytesRead - startBytes);+ const delta = actual - reserved;+ if (delta === 0) return;+ inFlightBytes += delta;+ reserved = actual;+ const busy = (clientInFlight.get(clientKey) ?? 0) + delta;+ clientInFlight.set(clientKey, busy);+ if (!complete && delta > 0 && (busy > perClientCap || inFlightBytes > budgetBytes)) {+ release();+ req.destroy();+ }+ };+ const settle = (): void => {+ reconcile(true);+ disarmStallReaper();+ };- stallTimer = setInterval(() => {- // The whole message is in (Node parsed it to the end) — there is nothing left to stall on,- // whether or not any consumer has read it. Without this a body that arrived in one segment- // before this middleware ran would keep the reaper armed on a byte counter that can no- // longer move, and a handler slower than STALL_TIMEOUT_MS would be killed mid-work.- if (req.complete) {- disarmStallReaper();- return;- }- const readNow = socket.bytesRead;- if (readNow !== lastBytes) {- if (declared === undefined) reconcileUndeclared(readNow);- lastBytes = readNow;- lastProgress = Date.now();- // The whole declared body has arrived; nothing left to stall on.- if (declared !== undefined && readNow - startBytes >= declared) disarmStallReaper();- return;- }- if (Date.now() - lastProgress >= STALL_TIMEOUT_MS) {+ stallTimer = setInterval(() => {+ // The whole message is in (Node parsed it to the end), so there is nothing left to stall on,+ // whether or not any consumer has read it. Without this a body that arrived in one segment+ // before this middleware ran would keep the reaper armed on a byte counter that can no+ // longer move, and a handler slower than STALL_TIMEOUT_MS would be killed mid-work.+ if (req.complete) {+ settle();+ return;+ }+ reconcile(false);+ if (released) return;+ const readNow = socket.bytesRead;+ // Pace reap. A body must keep up with the rate that lands what it holds inside the request+ // timeout, measured from admission after a STALL_TIMEOUT_MS grace: its declared size, or for+ // a chunked body its current reservation (the placeholder until more than that has arrived,+ // then the arrived bytes, which always meet the target). A steady sender that falls behind+ // would be cut off by the timeout anyway. One that starts slow and speeds up can be dropped+ // even though it would have finished in time; that is the accepted cost of not letting a+ // trickle hold its reservation for the whole timeout. A fully-arrived body always meets the+ // target.+ if (requestTimeoutMs > 0) {+ const target = declared ?? reserved;+ const paced = (target * (Date.now() - admittedAt - STALL_TIMEOUT_MS)) / requestTimeoutMs;+ if (readNow - startBytes < Math.min(target, paced)) {release();req.destroy();+ return;}- }, STALL_POLL_MS);- stallTimer.unref();- req.on('end', disarmStallReaper);- }+ }+ if (readNow !== lastBytes) {+ lastBytes = readNow;+ lastProgress = Date.now();+ // The whole declared body has arrived; nothing left to stall on.+ if (declared !== undefined && readNow - startBytes >= declared) disarmStallReaper();+ return;+ }+ if (Date.now() - lastProgress >= STALL_TIMEOUT_MS) {+ release();+ req.destroy();+ }+ }, STALL_POLL_MS);+ stallTimer.unref();+ req.on('end', settle);next();};diff --git a/src/configure-app.ts b/src/configure-app.tsindex 26e69da8a..e66642116 100644--- a/src/configure-app.ts+++ b/src/configure-app.ts@@ -9,6 +9,7 @@ import { createInflightBodyBudget, resolveInflightBodyBudgetBytes } from './confimport { requestContextMiddleware } from './common/middleware/request-context.middleware';import { injectDashboardCspNonce } from './config/dashboard-csp';import { resolveCorsPolicy, isUpgradeInsecureRequestsEnabled, resolveBodyLimit } from './config/bootstrap-security';+import { resolveRequestTimeoutMs } from './config/http-timeouts';/** Where the bundled dashboard documents come from, and whether to serve them at all. */export interface DashboardSource {@@ -65,6 +66,7 @@ export function configureApp(app: INestApplication, options: ConfigureAppOptions.split(',').map(p => p.trim()).filter(Boolean),+ requestTimeoutMs: resolveRequestTimeoutMs(process.env.REQUEST_TIMEOUT_MS),}).middleware,);