Repository navigation
fix: use crypto.getRandomValues on non-secure origins - #734
Open
smartcalsystems-sketch wants to merge 1 commit into
Open
smartcalsystems-sketch wants to merge 1 commit into
smartcalsystems-sketch wants to merge 1 commit into
Conversation
…#706) crypto.randomUUID requires a secure context (HTTPS or localhost), but that's not the only non-secure case in practice -- a plain-http staging deploy hits it too. When randomUUID is missing, generateIdempotencyKey fell all the way back to Math.random, which is predictable and collision-prone. Since idempotency keys are scoped per caller on the backend, a collision replays someone's cached response instead of executing the request. crypto.getRandomValues does not require a secure context and is still available in that case, so prefer it: draw 16 random bytes, fix the RFC 4122 v4 version/variant bits, and format as a standard UUID. Only fall back to Math.random if the Web Crypto API is entirely absent.
|
@smartcalsystems-sketch is attempting to deploy a commit to the chonilius' projects Team on Vercel. A member of the Team first needs to authorize it. |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
crypto.randomUUIDrequires a secure context (HTTPS orlocalhost) and is missing on any non-secure origin, including a plain-http staging deploy — not just "very unlikely browsers" as the old comment assumed. When it's missing,generateIdempotencyKeyfell all the way back toMath.random(), which is predictable and collision-prone. Since idempotency keys are scoped per caller on the backend, a collision replays someone's cached response instead of executing the request.Fix
crypto.getRandomValuesdoes not require a secure context and is still available in that case, so it's now used as the middle fallback: draw 16 random bytes, fix the RFC 4122 v4 version/variant bits, format as a standard UUID string.Math.randomis now only reached if the Web Crypto API is entirely absent.Testing
crypto.randomUUIDto simulate a non-secure origin: valid UUID v4 format, uniqueness across 50 calls, and an explicit assertion thatMath.randomis never called whengetRandomValuesis available.utils.test.tssuite: 96/96 passing (93 pre-existing + 3 new), zero regressions.tsc --noEmitandeslintclean on both changed files.Closes #706