Skip to content

🔒 [Fix unsafe timing-safe comparison with user input]#125

Merged
cmuench merged 2 commits into
mainfrom
security/timing-safe-equal-13534182648930956210
Jul 7, 2026
Merged

🔒 [Fix unsafe timing-safe comparison with user input]#125
cmuench merged 2 commits into
mainfrom
security/timing-safe-equal-13534182648930956210

Conversation

@cmuench

@cmuench cmuench commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

🎯 What: Fixed an unsafe usage of crypto.timingSafeEqual in lib/mcp.js. The previous implementation compared the length of the tokens as strings (string.length) before calling crypto.timingSafeEqual on their Buffer representations.
⚠️ Risk: If a provided token had the same string length as the expected token but contained multi-byte characters, its Buffer length would differ. Calling crypto.timingSafeEqual with unequal length buffers causes it to throw an exception. While the exception was caught in a try...catch block, relying on exception handling for standard authentication flows or passing unequal-length buffers to security functions can introduce unexpected behaviors, crashes, or minor timing discrepancies in some environments.
🛡️ Solution: The tokens are now converted into Buffer objects first. We then ensure that the byte lengths (buffer.length) match before executing crypto.timingSafeEqual(bufferA, bufferB). A unit test was added (tests/mcp-timing.test.js) to explicitly reproduce the issue and verify the fix against multi-byte characters.


PR created automatically by Jules for task 13534182648930956210 started by @cmuench

When using crypto.timingSafeEqual, comparing the string lengths before buffer
creation is unsafe because strings with multibyte characters can have
the same length but different byte sizes. This causes timingSafeEqual
to throw an error, which in this case might just get caught and ignored,
but is bad practice and can result in unwanted behaviors or crashes.

This fix correctly converts the strings into Buffers first, and then
compares the length of those buffers before calling timingSafeEqual.

Co-authored-by: cmuench <211294+cmuench@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Comment thread tests/mcp-timing.test.js Fixed
…tion or class'

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
@cmuench cmuench merged commit dae7790 into main Jul 7, 2026
3 checks passed
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.

1 participant