Skip to content

Use constant-time comparison for API key check#34

Merged
LarsLaskowski merged 1 commit into
mainfrom
claude/issue-18-vavxlu
Jul 13, 2026
Merged

Use constant-time comparison for API key check#34
LarsLaskowski merged 1 commit into
mainfrom
claude/issue-18-vavxlu

Conversation

@LarsLaskowski

@LarsLaskowski LarsLaskowski commented Jul 13, 2026

Copy link
Copy Markdown
Owner

Summary

The withAPIKey middleware compared the client-supplied API key against the configured key with plain string ==, which short-circuits at the first differing byte. An attacker able to time many requests could recover the key byte by byte (timing side channel), which is practical on a LAN — the primary deployment target.

Both keys are now hashed with SHA-256 and the digests compared with crypto/subtle.ConstantTimeCompare, so the comparison runs in constant time over a fixed length and neither key bytes nor the configured key's length leak through response timing. The early return for an empty configured key (auth disabled) is unchanged.

Related Issue

Closes #18

Checklist

  • Tests added/updated for the change (go test ./... passes locally) — existing TestAPIKey_RequiredWhenConfigured and TestAPIKey_NotRequiredByDefault cover the acceptance criteria and pass unchanged
  • go vet ./... and golangci-lint run are clean
  • Documentation updated if this changes the REST API (docs/API.md), configuration (README.md, packaging/pimonitor.example.yaml), or installation/packaging (packaging/install.sh, systemd units) — no user-facing behavior change, no docs needed
  • No breaking change to /api/v1/... response shapes, or a new API version was introduced instead

Go's == on strings short-circuits at the first differing byte, so an
attacker who can time many requests could recover the configured API
key byte by byte. Hash both the provided and configured keys with
SHA-256 and compare the digests with subtle.ConstantTimeCompare, which
also hides the configured key's length.

Closes #18

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017mkTn2GYT2E8mCtD5gVYy5
@LarsLaskowski
LarsLaskowski merged commit 7a220f5 into main Jul 13, 2026
3 checks passed
@LarsLaskowski
LarsLaskowski deleted the claude/issue-18-vavxlu branch July 13, 2026 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Security: API key comparison is not constant-time (timing side channel)

2 participants