-
-
Notifications
You must be signed in to change notification settings - Fork 9
fix(security): add timing-safe token comparison helper (#43) #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hoainho
merged 2 commits into
hoainho:main
from
iMindCap:fix/timing-safe-token-comparison
Jun 12, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { timingSafeEqual } from "../utils/crypto"; | ||
|
|
||
| describe("timingSafeEqual", () => { | ||
| it("returns true for identical strings", () => { | ||
| expect(timingSafeEqual("abc", "abc")).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false for same-length but different strings", () => { | ||
| expect(timingSafeEqual("abc", "abd")).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false for different-length strings", () => { | ||
| expect(timingSafeEqual("abc", "abcd")).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false for empty vs non-empty", () => { | ||
| expect(timingSafeEqual("", "a")).toBe(false); | ||
| }); | ||
|
|
||
| it("returns true for two empty strings", () => { | ||
| expect(timingSafeEqual("", "")).toBe(true); | ||
| }); | ||
|
|
||
| it("executes in constant time within ±200% CV (JS timing resolution limit)", () => { | ||
| const token = "a".repeat(64); | ||
| const correct = "a".repeat(64); | ||
| const wrong = "b".repeat(64); | ||
|
|
||
| for (let i = 0; i < 100; i++) { | ||
| timingSafeEqual(token, correct); | ||
| timingSafeEqual(token, wrong); | ||
| } | ||
|
|
||
| const correctTimes: number[] = []; | ||
| const wrongTimes: number[] = []; | ||
|
|
||
| for (let i = 0; i < 1000; i++) { | ||
| const t1 = performance.now(); | ||
| timingSafeEqual(token, correct); | ||
| correctTimes.push(performance.now() - t1); | ||
|
|
||
| const t2 = performance.now(); | ||
| timingSafeEqual(token, wrong); | ||
| wrongTimes.push(performance.now() - t2); | ||
| } | ||
|
|
||
| const mean = (arr: number[]) => arr.reduce((a, b) => a + b, 0) / arr.length; | ||
| const correctMean = mean(correctTimes); | ||
| const wrongMean = mean(wrongTimes); | ||
|
|
||
| // The means should be within 2x of each other — proves no short-circuit | ||
| const ratio = Math.max(correctMean, wrongMean) / Math.min(correctMean, wrongMean); | ||
| expect(ratio).toBeLessThan(2); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * Compare two strings in constant time to avoid timing side-channel attacks. | ||
| * Use this for token / secret comparisons instead of ===. | ||
| * | ||
| * Using === short-circuits on the first byte difference, leaking the | ||
| * prefix-length-match through timing. An attacker controlling the input can | ||
| * extract the correct token byte-by-byte via timing oracle in | ||
| * O(256 × 32 × N samples) requests. | ||
| * | ||
| * This implementation returns false immediately on length mismatch (safe | ||
| * because token lengths are fixed and public), then uses a XOR-accumulator | ||
| * loop with no branching to compare all bytes in constant time. | ||
| * | ||
| * NOTE: Once Web Crypto's timingSafeEqual ships universally | ||
| * (https://github.com/whatwg/webcrypto/issues/270), prefer that. | ||
| * | ||
| * @see https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html#timing-attacks | ||
| */ | ||
| export function timingSafeEqual(a: string, b: string): boolean { | ||
| if (a.length !== b.length) { | ||
| return false; | ||
| } | ||
| let mismatch = 0; | ||
| for (let i = 0; i < a.length; i++) { | ||
| mismatch |= a.charCodeAt(i) ^ b.charCodeAt(i); | ||
| } | ||
| return mismatch === 0; | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Security & Performance Issues
maxLenwill be huge, and the loop will run millions of times. This blocks the single-threaded Node.js event loop, creating an easy vector for CPU exhaustion/DoS attacks.i < a.length ? a.charCodeAt(i) : 0introduce conditional branching. Modern JIT engines (like V8) optimize branches based on execution history, which can introduce timing variations and defeat the constant-time guarantee.Recommended Solution
Return
falseimmediately if the lengths ofaandbare different. This is the standard approach used by cryptographic libraries (including Node's nativecrypto.timingSafeEqualand Web Crypto API / standard comparison helpers). Since token lengths are typically fixed and public, leaking a length mismatch is safe and prevents both the CPU DoS and the JIT branching side-channels.