diff --git a/.jules/sentinel.md b/.jules/sentinel.md new file mode 100644 index 00000000..51ebe15b --- /dev/null +++ b/.jules/sentinel.md @@ -0,0 +1,4 @@ +## 2025-02-12 - Fix Timing Attack Vulnerability in Bearer Token Validation +**Vulnerability:** A timing attack vulnerability existed where short-circuiting on buffer length differences in `crypto.timingSafeEqual` leaked the length of the secret. +**Learning:** Developers might check buffer lengths before `timingSafeEqual` because `timingSafeEqual` throws an error on unequal lengths. However, this re-introduces a timing leak (length). +**Prevention:** Always hash both inputs to a fixed-length digest (e.g. SHA-256) before using `crypto.timingSafeEqual`. 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.'); });