Skip to content

fix(server): compare the server password in constant time - #59167

Open
benjaminkincses wants to merge 1 commit into
anomalyco:devfrom
benjaminkincses:constant-time-password-check
Open

benjaminkincses wants to merge 1 commit into
anomalyco:devfrom
benjaminkincses:constant-time-password-check

Conversation

@benjaminkincses

Copy link
Copy Markdown

Issue for this PR

Closes #58563

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

the basic auth password check in authorized() used ===, so response timing leaks how much of a guessed password is right. verifySession in the same file already uses timingSafeEqual. this makes the password check do the same (length check first, since timingSafeEqual throws on different lengths).

How did you verify your code works?

added a case to packages/server/test/auth.test.ts covering wrong passwords of the same and different lengths. bun test test/auth.test.ts passes (3 tests), tsc -b clean.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I reproduced the timing leak from #58563 on the base with the bot's script: rejecting a 1 MiB guess that is wrong only in its last character cost ~23 µs vs ~4.5 µs for one wrong in its first character. With this change both cost the same. Passwords with multibyte characters (UTF-8 inputs of different byte lengths) don't throw, and a real opencode serve with Basic auth still accepts the right password and returns 401 for wrong ones. bun test test/auth.test.ts passes and tsc -b in packages/server is clean.

Public API: unchanged.

No blocking concerns. The equal-length check still reveals the password's length, which verifySession() already does. Hashing both sides before comparing would hide it, but that's optional.

This branch has not been deployed

No deployments
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.

server basic auth password check is not constant time

1 participant