From bd38e8ccf9d086693bdde824d37ddcf56d44e7d9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 5 Jul 2026 20:41:15 +0000 Subject: [PATCH] fix: prevent timing attack in Bearer token validation Replaced short-circuit length checks and raw buffer comparisons with SHA-256 hashes to ensure constant-time token comparison via crypto.timingSafeEqual. --- .jules/sentinel.md | 4 ++++ src/cli/commands/serve.ts | 9 +++------ tests/integration/prompt_templates.test.ts | 4 +++- 3 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 .jules/sentinel.md diff --git a/.jules/sentinel.md b/.jules/sentinel.md new file mode 100644 index 00000000..2bbb8e7b --- /dev/null +++ b/.jules/sentinel.md @@ -0,0 +1,4 @@ +## 2024-05-24 - Fix timing attack vulnerability in Bearer token validation +**Vulnerability:** A length check before calling `crypto.timingSafeEqual` in `src/cli/commands/serve.ts` leaks the expected token's length via timing differences. +**Learning:** Checking buffer lengths before constant-time comparison creates a short-circuit fast path that defeats the purpose of constant-time evaluation. +**Prevention:** Always hash both secrets to a fixed length (e.g., using `crypto.createHash('sha256')`) before comparison to ensure true constant-time evaluation. diff --git a/src/cli/commands/serve.ts b/src/cli/commands/serve.ts index 2c37293e..e20ec168 100644 --- a/src/cli/commands/serve.ts +++ b/src/cli/commands/serve.ts @@ -346,13 +346,10 @@ export async function handleServeCommand(_options: unknown, command: Command) { let isAuthenticated = false; if (scheme?.toLowerCase() === 'bearer' && token) { - const tokenBuffer = Buffer.from(token); + const tokenHash = crypto.createHash('sha256').update(token).digest(); for (const authToken of authTokens) { - const authTokenBuffer = Buffer.from(authToken); - if ( - tokenBuffer.length === authTokenBuffer.length && - crypto.timingSafeEqual(tokenBuffer, authTokenBuffer) - ) { + const authTokenHash = crypto.createHash('sha256').update(authToken).digest(); + if (crypto.timingSafeEqual(tokenHash, authTokenHash)) { isAuthenticated = true; break; } diff --git a/tests/integration/prompt_templates.test.ts b/tests/integration/prompt_templates.test.ts index 21905259..f1360d78 100644 --- a/tests/integration/prompt_templates.test.ts +++ b/tests/integration/prompt_templates.test.ts @@ -36,7 +36,9 @@ describe('Prompt templates', () => { expect(planSystem).toContain('You are SalmonLoop.'); expect(patchSystem).toContain('You are PATCH, a phase-native diff compiler.'); - expect(autopilotSystem).toContain('You are a senior software engineer running in "autopilot" mode.'); + expect(autopilotSystem).toContain( + 'You are a senior software engineer running in "autopilot" mode.', + ); expect(answerSystem).toContain('You are a coding assistant in "answer" mode.'); expect(researchSystem).toContain('You are a research assistant.'); });