Repository navigation
fix(mcp): harden the MCP server auth token - #723
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #575.
Changes
AuthMiddleware.validateRequestcompared 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 byAuthMiddleware.constantTimeEquals, whose run time depends only on the two lengths.AuthMiddleware.generateTokenbuilt tokens withMath.random, which is not meant for secrets. It now draws fromwindow.crypto.getRandomValuesand uses rejection sampling, so every character is equally likely. Tokens stay 32 alphanumeric characters.Both changes are plain JavaScript or Web Crypto. No Node
cryptoimport 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.tscovers:+appIdMath.random