Skip to content

Use timingSafeEqual for ADMIN_API_KEY comparison #4209

Closed

Description

@bokelley

Context

server/src/middleware/auth.ts:644-652 (hasValidAdminApiKey) compares the request-supplied admin API key to process.env.ADMIN_API_KEY using ===. Surfaced by security-reviewer in PR #4207.

Risk

Low. The key is high-entropy and remote timing-attack signal across the public internet is very weak. But:

  • It's a one-line hardening.
  • Removes a category of question for any future security review.

Proposal

Swap the === for crypto.timingSafeEqual over equal-length Buffers. Length-mismatch handling needs to compare a dummy buffer of the correct length to keep the comparison constant-time.

```ts
import { timingSafeEqual } from 'node:crypto';

function constantTimeEqual(a: string, b: string): boolean {
const aBuf = Buffer.from(a);
const bBuf = Buffer.from(b);
if (aBuf.length !== bBuf.length) {
// Compare against a same-length dummy to keep the timing constant.
timingSafeEqual(aBuf, Buffer.alloc(aBuf.length));
return false;
}
return timingSafeEqual(aBuf, bBuf);
}
```

Files to touch

  • `server/src/middleware/auth.ts` — `hasValidAdminApiKey` only.
  • Add a unit test that asserts both equal-length-mismatch and length-mismatch paths return false.

Out of scope

Other API key comparisons in the codebase (registry signing keys, agent credentials, etc.). Audit those separately.

Activity

  1. added
    claude-triaging
    on May 8, 2026
  2. bokelley commented on May 8, 2026

    @bokelley
    ContributorAuthor

    Triage

    Classification: Bug (security hardening)
    Bucket(s): security-sensitive
    Status: ready-for-human

    What the experts said:

    • security-reviewer: Implementation correct — constantTimeEqual with dummy-call-on-length-mismatch matches the existing verifyDevSession pattern (line 547); timingSafeEqual is already imported at line 2 so the diff is minimal. One suggestion: use Buffer.from(a, 'latin1') instead of the default UTF-8 to make the ASCII-only assumption on ADMIN_API_KEY explicit rather than implicit.

    My take: Security-sensitive bucket means no auto-PR. The proposal is clean and self-contained; @bokelley, your call to greenlight — proceed as-is or fold in the 'latin1' encoding suggestion.


    Triaged by Claude Code. Session: https://claude.ai/code/cse_01VsmRH2QwA5denNndQGKHAy


    Generated by Claude Code

  3. added
    claude-triaged
    and removed
    claude-triaging
    on May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    claude-triaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions