Skip to content

fix(mcp): harden the MCP server auth token - #723

Merged
Quorafind merged 2 commits into
masterfrom
fix/575-timing-safe-token
Nov 29, 2026
Merged

Quorafind merged 2 commits into
masterfrom
fix/575-timing-safe-token

Conversation

@Quorafind

Copy link
Copy Markdown
Member

Fixes #575.

Changes

  • Constant-time token check (CWE-208). AuthMiddleware.validateRequest compared the bearer token with ===, which stops at the first differing character. Response timing could therefore reveal how much of a guessed token is right. The token is now compared by AuthMiddleware.constantTimeEquals, whose run time depends only on the two lengths.
  • Unpredictable tokens (CWE-338). AuthMiddleware.generateToken built tokens with Math.random, which is not meant for secrets. It now draws from window.crypto.getRandomValues and uses rejection sampling, so every character is equally likely. Tokens stay 32 alphanumeric characters.

Both changes are plain JavaScript or Web Crypto. No Node crypto import is added, so mobile is unaffected. Existing tokens keep working; only newly generated ones change.

This is our own implementation. #576 couldn't be merged because the CLA wasn't signed.

Tests

src/__tests__/AuthMiddleware.test.ts covers:

  • accepting the correct token, with and without +appId
  • rejecting wrong, partial, longer and missing tokens
  • rejecting a token that differs only in its last character
  • the token format, and that the Web Crypto generator is used instead of Math.random

validateRequest compared the bearer token with ===, which returns at the first differing character, so response times could reveal how much of a guessed token is right (CWE-208). Compare with a loop whose time depends only on the lengths. It is plain JavaScript, so nothing pulls in Node's crypto module. Refs #575.
generateToken picked characters with Math.random, whose output is predictable and not meant for secrets (CWE-338). Draw bytes from the Web Crypto random number generator instead, and skip bytes that would bias the character choice. Web Crypto is available on desktop and mobile, so no Node module is needed.
@Quorafind
Quorafind merged commit 8ac1ae4 into master Nov 29, 2026
3 checks passed
@Quorafind
Quorafind deleted the fix/575-timing-safe-token branch November 29, 2026 02:34
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Security: timing-vulnerable token comparison in AuthMiddleware (CWE-208)

1 participant