Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(http): refuse an encoded NUL in the request URL

Yudhi Armyndharisac39dd66dd28
diff --git a/src/configure-app.ts b/src/configure-app.ts
index 8c9f5deb4..2d65f69dd 100644
--- a/src/configure-app.ts
+++ b/src/configure-app.ts
@@ -42,8 +42,8 @@ export interface AppliedBodyCaps {
/**
* Everything the production HTTP surface installs on the Express app: request context, the CSP
- * nonce, helmet, the SPA document handler, CORS, the in-flight body budget, the body parsers and the
- * trailing-slash DELETE refusal.
+ * nonce, helmet, the SPA document handler, CORS, the encoded-NUL refusal, the in-flight body budget,
+ * the body parsers and the trailing-slash DELETE refusal.
*
* It lives here rather than inside bootstrap() so the e2e lane can run the SAME stack. main.ts
* boots on import, so a suite cannot import it; the whole stack was therefore executed by nothing,
@@ -200,6 +200,14 @@ export function configureApp(app: INestApplication, options: ConfigureAppOptions
maxAge: 86400, // 24 hours
});
+ // Express decodes %00 in a path parameter or the query to U+0000, which PostgreSQL rejects in every text
+ // parameter, so the lookup behind the route failed with 500 (on the public ingress route too, which
+ // resolves its instance before anything else). No route takes one. After CORS, so a browser can read it.
+ app.use((req: Request, res: Response, next: NextFunction) => {
+ if (!req.originalUrl.includes('%00')) return next();
+ res.status(400).json({ statusCode: 400, message: 'URL must not contain an encoded NUL', error: 'Bad Request' });
+ });
+
// Aggregate in-flight body budget (DoS hardening): once too many body bytes are being buffered
// across ALL connections, new requests get 503 + Retry-After without their body being read.
// This is a deliberate PRE-GUARD: the throttler/auth guards run at the Nest routing layer —
diff --git a/src/main.ts b/src/main.ts
index 18b1b6949..873e4efac 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -151,8 +151,8 @@ async function bootstrap() {
app.useWebSocketAdapter(new RedisIoAdapter(app));
// The production HTTP surface: request context, the CSP nonce, helmet, the SPA document handler,
- // CORS, in-flight body budget, body parsers and the trailing-slash DELETE refusal. Extracted so the
- // e2e lane runs the SAME stack instead of a copy of it (src/configure-app.ts).
+ // CORS, the encoded-NUL refusal, in-flight body budget, body parsers and the trailing-slash DELETE
+ // refusal. Extracted so the e2e lane runs the SAME stack instead of a copy of it (src/configure-app.ts).
const { bodyLimit, inflightBudgetBytes } = configureApp(app);
bootstrapLogger.log(`Request body caps: ${bodyLimit} per request, ${inflightBudgetBytes} bytes aggregate in flight`);
diff --git a/test/configure-app.e2e-spec.ts b/test/configure-app.e2e-spec.ts
index 2ae243893..5730ae3ab 100644
--- a/test/configure-app.e2e-spec.ts
+++ b/test/configure-app.e2e-spec.ts
@@ -263,6 +263,19 @@ describe('production HTTP surface (configureApp)', () => {
expect(ingressHits).toEqual(['DELETE /api/ingress/p/i/hook/', 'DELETE /API/ingress/p/i/hook/']);
});
+ it('refuses a path or query that decodes to a NUL character before any route runs', async () => {
+ ingressHits.length = 0;
+ for (const path of ['/api/sessions/abc%00', '/api/sessions/abc?name=%00', '/api/ingress/p%00/i/hook']) {
+ const res = await request(app.getHttpServer()).get(path).set('Origin', 'https://allowed.example').expect(400);
+ expect(res.body).toMatchObject({ statusCode: 400, error: 'Bad Request' });
+ expect(res.headers['x-request-id']).toBeDefined();
+ expect(res.headers['access-control-allow-origin']).toBe('https://allowed.example');
+ }
+ expect(ingressHits).toEqual([]);
+ // %2500 decodes to the text "%00", not to a NUL, so it still reaches the route.
+ await request(app.getHttpServer()).get('/api/sessions/abc%2500').expect(200);
+ });
+
it('answers a compressed body with 415 rather than charging the budget its inflated size', async () => {
const res = await request(app.getHttpServer())
.post('/api/echo')