🔒 [Fix unsafe timing-safe comparison with user input]#125
Conversation
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>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
…tion or class' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
🎯 What: Fixed an unsafe usage of
⚠️ 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.timingSafeEqualinlib/mcp.js. The previous implementation compared the length of the tokens as strings (string.length) before callingcrypto.timingSafeEqualon their Buffer representations.crypto.timingSafeEqualwith unequal length buffers causes it to throw an exception. While the exception was caught in atry...catchblock, 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
Bufferobjects first. We then ensure that the byte lengths (buffer.length) match before executingcrypto.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