Skip to content

Comments

Fix timing attack length leak in examples#28135

Merged
elithrar merged 2 commits intoproductionfrom
opencode/issue23623-20260206111551
Feb 11, 2026
Merged

Fix timing attack length leak in examples#28135
elithrar merged 2 commits intoproductionfrom
opencode/issue23623-20260206111551

Conversation

@ask-bonk
Copy link
Contributor

@ask-bonk ask-bonk bot commented Feb 6, 2026

All tasks complete. Here's a summary of the changes:

Changes made to src/content/docs/workers/examples/protect-against-timing-attacks.mdx

Problem: All three code examples (TypeScript, Python, Hono) returned early when the user input and secret had different lengths. This defeats the purpose of timing-safe comparison — an attacker could determine the exact length of the secret by measuring response times (early return vs. full comparison).

Fix applied across all three examples (TypeScript, Python, Hono):

  • Removed the early-return length checks
  • When lengths match: compare userValue against secretValue directly via timingSafeEqual
  • When lengths differ: compare userValue against itself (always true) and negate it (becomes false), ensuring a constant-time comparison always runs regardless of length mismatch
  • Added inline comments in each code block explaining why early returns must be avoided

Prose changes:

  • Added a :::caution admonition block explicitly warning users not to return early on length mismatch and explaining the correct pattern

Review results: All three code snippets pass correctness review — the timing-safe logic is sound, edge cases (empty Authorization header producing zero-length buffers) are handled correctly, and the timingSafeEqual API usage matches the Cloudflare Workers runtime.

Note: The basic-auth.mdx example has the same early-return vulnerability in its timingSafeEqual wrapper function. That's a separate issue worth a follow-up fix.

Closes #23623

github run

Co-authored-by: elithrar <elithrar@users.noreply.github.com>
@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2026

This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:

Pattern Owners
/src/content/docs/workers/ @cloudflare/workers-docs, @GregBrimble, @irvinebroque, @mikenomitch, @korinne, @WalshyDev, @cloudflare/deploy-config, @cloudflare/pcx-technical-writing, @cloudflare/wrangler, @mattietk

@elithrar
Copy link
Collaborator

elithrar commented Feb 6, 2026

/bonk fix basic-auth.mdx too. Keep the change simple.

Replace early 'return false' on length mismatch with
'return !timingSafeEqual(aBytes, aBytes)' so a constant-time
comparison always runs, preventing an attacker from deducing the
secret length through response timing.
@ask-bonk
Copy link
Contributor Author

ask-bonk bot commented Feb 6, 2026

Done. Changed both the JavaScript and TypeScript timingSafeEqual wrapper functions in basic-auth.mdx:

  • Before: return false when lengths differ — leaks the secret's length via timing.
  • After: return !crypto.subtle.timingSafeEqual(aBytes, aBytes) — always runs a constant-time comparison (comparing input against itself, then negating to still return false).

The Rust and Hono examples in that file don't use this wrapper pattern, so they weren't affected.

github run

@elithrar elithrar enabled auto-merge (squash) February 6, 2026 12:14
@elithrar elithrar merged commit 6278935 into production Feb 11, 2026
9 checks passed
@elithrar elithrar deleted the opencode/issue23623-20260206111551 branch February 11, 2026 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

product:workers Related to Workers product size/s

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using timingSafeEqual examples are not safe?

7 participants