test(utils): cover generateId's insecure-context fallback - #7189
Open
reddynitish wants to merge 1 commit into
Open
test(utils): cover generateId's insecure-context fallback#7189reddynitish wants to merge 1 commit into
reddynitish wants to merge 1 commit into
Conversation
Refs simstudioai#3393 The crash reported there (crypto.randomUUID is not a function, hit on self-hosted deployments served over plain HTTP — browsers only expose crypto.randomUUID() in secure contexts) is already fixed on staging: generateId() has feature-detected crypto.randomUUID and fallen back to a crypto.getRandomValues()-based UUID v4 since b5674d9 (simstudioai#4228), and no client-side code calls crypto.randomUUID directly anymore (enforced by scripts/check-utils-enforcement.ts). What was missing: the existing test suite never exercised the fallback branch, since Node/Bun's crypto.randomUUID is always present in the test environment. A future refactor could silently break the exact path that fixes simstudioai#3393 with nothing catching it. Adds two tests that stub crypto.randomUUID to undefined (restored in afterEach) and assert the fallback still produces valid, unique UUID v4s. Verified these tests actually fail with the pre-fix TypeError when generateId() is reverted to a naive crypto.randomUUID() call. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
@reddynitish is attempting to deploy a commit to the Sim Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
Greptile SummaryThis PR adds regression coverage for
Confidence Score: 5/5The test-only change appears safe to merge. The new tests exercise the existing fallback path and restore the temporarily modified crypto method after every test, with no production-code changes or accepted defects.
|
| Filename | Overview |
|---|---|
| packages/utils/src/id.test.ts | Adds focused, isolated tests for the existing insecure-context UUID fallback; no actionable issues found. |
Reviews (1): Last reviewed commit: "test(utils): cover generateId's insecure..." | Re-trigger Greptile
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.
Summary
TypeError: crypto.randomUUID is not a function. Browsers only exposecrypto.randomUUID()in secure contexts.generateId()(packages/utils/src/id.ts) has feature-detectedcrypto.randomUUIDand fallen back to acrypto.getRandomValues()-based UUID v4 sinceb5674d9ed4(improvement(codebase): centralize test mocks, extract @sim/utils, remove dead code #4228). A repo-wide grep confirms no client-side code inapps/simcallscrypto.randomUUIDdirectly anymore, andscripts/check-utils-enforcement.tsbans direct usage outside that one definition file. This PR does not fix a bug — nothing here changes production behavior.generateId()only ever exercised the "happy path" — Node/Bun'scrypto.randomUUIDis always present in the test environment, so the fallback branch that actually fixes [Bug] White screen / "Application error" - crypto.randomUUID is not a function #3393 had zero coverage. A future refactor could silently reintroduce the exact crash with nothing catching it.packages/utils/src/id.test.tsthat stubcrypto.randomUUIDtoundefined(restored inafterEach, verified not to leak across tests or files) and assertgenerateId()still returns a valid, unique UUID v4. Verified these tests genuinely fail with the originalTypeErrorwhengenerateId()is temporarily reverted to a naivecrypto.randomUUID()call, and pass again once restored.Refs #3393 (using
Refs, notFixes— the underlying bug isn't something this PR resolves; it already isn't reproducible on staging).Type of Change
Testing
cd packages/utils && bunx vitest run src/id.test.ts— PASS, 12/12.generateId()to the pre-fixreturn crypto.randomUUID()(no guard) and re-ran the same file — the two new tests failed withTypeError: crypto.randomUUID is not a function, the exact error from [Bug] White screen / "Application error" - crypto.randomUUID is not a function #3393; all other tests still passed. Restored the file and confirmedgit diffclean before committing.cd packages/utils && bunx vitest run(full workspace, all 14 files) — PASS, 194/194, both before and after the regression check above — theafterEachrestore doesn't leak across tests or files.bunx tsc --noEmit/bunx turbo run type-check --filter=@sim/utils— PASS. Confirms the two@ts-expect-errordirectives suppress a real type error rather than being stale (an unused directive is itself a type error).bunx biome check packages/utils/src/id.test.ts— PASS.bun run check:utils— pre-existing baseline failures only (13 hits, all in files already on the allowlist —id.ts's own definition,helpers.ts,random.ts, etc.); none are inid.test.ts, which is already allowlisted atscripts/check-utils-enforcement.ts:37for referencing the banned pattern directly (needed to test it).apps/simforcrypto.randomUUID/randomUUID— 6 hits, allvi.fn()test mocks; zero production callsites.An independent adversarial review (Claude subagent, no session context, in place of Codex which isn't installed on this machine) reproduced all of the above itself — including a stress test running the full suite with
--no-isolate --no-file-parallelismto rule out cross-file leakage from thecrypto.randomUUIDstub — and confirmed the diff is correct as-is. It also independently traced the fix tob5674d9ed4and flagged that the PR should be framed as coverage rather than a fix (reflected above), which is the only change it asked for.Checklist
Screenshots/Videos
N/A — test-only change, no UI.