Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 2024-07-21 - Fix timing attack vulnerability in token validation
**Vulnerability:** The Bearer token validation used a length check (`tokenBuffer.length === authTokenBuffer.length`) before calling `crypto.timingSafeEqual`. This short-circuit fast path leaked the length of valid tokens via timing differences.
**Learning:** `crypto.timingSafeEqual` requires buffers of equal length. Checking length first defeats the purpose of a constant-time comparison because an attacker can brute-force the length.
**Prevention:** Always hash both secrets to a fixed length (e.g., using `crypto.createHash('sha256')`) before comparison to ensure true constant-time evaluation, regardless of the input lengths.
8 changes: 3 additions & 5 deletions src/cli/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,9 @@ export async function handleServeCommand(_options: unknown, command: Command) {
if (scheme?.toLowerCase() === 'bearer' && token) {
const tokenBuffer = Buffer.from(token);
for (const authToken of authTokens) {
const authTokenBuffer = Buffer.from(authToken);
if (
tokenBuffer.length === authTokenBuffer.length &&
crypto.timingSafeEqual(tokenBuffer, authTokenBuffer)
) {
const tokenHash = crypto.createHash('sha256').update(tokenBuffer).digest();
const authHash = crypto.createHash('sha256').update(Buffer.from(authToken)).digest();
if (crypto.timingSafeEqual(tokenHash, authHash)) {
isAuthenticated = true;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/bun-test-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function restoreConsoleOutputs() {

export function clearMockState() {
mock.restore();
mock.clearAllMocks();

auditTrail.clearAuditTrail();
}

Expand Down
4 changes: 3 additions & 1 deletion tests/integration/prompt_templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});
Expand Down
Loading