Skip to content

fix: remove early length exit from timingSafeEqStr#81

Open
bmersereau wants to merge 3 commits into
willchen96:mainfrom
bmersereau:fix/72-timing-safe-eq
Open

fix: remove early length exit from timingSafeEqStr#81
bmersereau wants to merge 3 commits into
willchen96:mainfrom
bmersereau:fix/72-timing-safe-eq

Conversation

@bmersereau
Copy link
Copy Markdown

Summary

  • timingSafeEqStr returned false immediately when string lengths differed, before calling crypto.timingSafeEqual — violating the constant-time contract and creating a theoretical timing oracle
  • Fixed by always running the constant-time comparison first (on zero-padded buffers), then checking length equality afterward
  • In practice, all SHA-256 base64url signatures are 43 characters so this gap has no real exploit path for standard tokens; this is a defense-in-depth fix for the pattern

Closes #72

Changes

  • backend/src/lib/downloadTokens.ts — early length check removed; timingSafeEqStr now pads the shorter buffer and runs timingSafeEqual unconditionally before length verification; exported for testability
  • backend/src/lib/__tests__/timingSafeEq.test.ts — 5 unit tests

Test plan

  • Unit tests: equal strings, same-length mismatch, different-length mismatch, empty strings
  • Build and typecheck pass

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.

[Security][Low] Timing oracle in download token verification — early length exit before constant-time compare

1 participant