From 7c123f1b8e935d011c0416afbf6568ce7ec274c0 Mon Sep 17 00:00:00 2001 From: Bahri Yilmaz Date: Thu, 12 Nov 2026 21:41:31 +0300 Subject: [PATCH] Fix CodeQL ReDoS and log-injection alerts - `bearerToken` matches `\S+` instead of `.+` after `\s+`, so a header like `Bearer` followed by thousands of spaces no longer backtracks polynomially (tokens never contain whitespace). - The optional request log JSON-encodes the URL so control characters cannot forge log lines. 27/27 tests pass locally. Co-Authored-By: Claude Opus 5.5 --- package-lock.json | 16 ++++++++++++++++ src/app.ts | 3 ++- src/auth.ts | 3 ++- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..bdf09da --- /dev/null +++ b/package-lock.json @@ -0,0 +1,16 @@ +{ + "name": "ecommerce-api", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "ecommerce-api", + "version": "1.0.0", + "license": "MIT", + "engines": { + "node": ">=22.18" + } + } + } +} diff --git a/src/app.ts b/src/app.ts index 1a0233e..fceebc9 100644 --- a/src/app.ts +++ b/src/app.ts @@ -67,7 +67,8 @@ export function createApp(deps: AppDeps) { } } finally { if (process.env.LOG_REQUESTS === '1') { - console.log(`${req.method} ${req.url} ${res.statusCode} ${(performance.now() - started).toFixed(1)}ms`); + // JSON.stringify escapes control characters so a crafted URL cannot forge log lines. + console.log(`${req.method} ${JSON.stringify(req.url ?? '')} ${res.statusCode} ${(performance.now() - started).toFixed(1)}ms`); } } }; diff --git a/src/auth.ts b/src/auth.ts index 75b83e8..972af4b 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -52,7 +52,8 @@ export function verifyToken(token: string, secret: string): TokenPayload | null } export function bearerToken(header: string | undefined): string | null { - const m = /^Bearer\s+(.+)$/i.exec(header ?? ''); + // Tokens never contain whitespace; \S+ keeps the match linear on long headers. + const m = /^Bearer\s+(\S+)\s*$/i.exec(header ?? ''); return m ? m[1] : null; }