Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(site): centralize UUID generation and fix non-secure crash - #28285

Merged
phorcys420 merged 7 commits into
mainfrom
phorcys/centralize-uuid-generation
Aug 19, 2026
Merged

fix(site): centralize UUID generation and fix non-secure crash#28285
phorcys420 merged 7 commits into
mainfrom
phorcys/centralize-uuid-generation

Conversation

@phorcys420

@phorcys420 phorcys420 commented Aug 19, 2026

Copy link
Copy Markdown
Member

Fixes OSS-38: crypto.randomUUID is not a function when accessing Coder over plain HTTP.

Root cause

crypto.randomUUID is only defined in secure contexts (HTTPS or localhost). It is undefined when Coder is served over plain HTTP. The uuid package's v4() short-circuits to crypto.randomUUID() when it is truthy, and in the production bundle that path compiles down to a direct crypto.randomUUID() call, so uuidv4() throws over HTTP. Observed crash (Template Builder, in the useMemo that mints the telemetry session ID):

TypeError: crypto.randomUUID is not a function
  Rt/h<@.../assets/TemplateBuilderPage-*.js
  useMemo@...

The same uuidv4() path backed the Terminal reconnection token and the Agent chat tab IDs / reconnection tokens, so those flows were affected too.

Fix

Add a single generateUUID helper in site/src/utils/random.ts that derives the UUID from crypto.getRandomValues (the one Crypto member available in insecure contexts) and only calls crypto.randomUUID when it actually exists. Route all client-side UUID generation through it, so every call site is fixed at once and future ones stay safe.

Changes

Change Route Human tested/verified
utils/random.ts — new generateUUID() helper N/A N/A
TemplateBuilderPage.tsx — telemetry session ID /templates/new/builder [x]
TerminalPage.tsx — reconnection token /:username/:workspace/terminal [x]
AgentChatPageView.tsx — tab IDs + reconnection tokens /agents/:agentId [ ]
useChatDraftAttachments.ts — draft client IDs /agents/:agentId [x]
TaskApps.stories.tsx — mock app IDs (Storybook) /tasks/:username/:taskId [ ]
WorkspaceSharingIndicator.stories.tsx — mock actor IDs (Storybook) /workspaces [ ]
Removed unused uuid / @types/uuid direct deps N/A N/A

uuid is still pinned via a pnpm.overrides entry because mermaid/streamdown depend on it transitively.

Tests

  • random.test.ts covers a valid UUID, uniqueness, and the insecure-context path (with crypto.randomUUID stubbed to undefined).
  • TerminalPage.test.tsx now mocks #/utils/random instead of the uuid module.

Validation

  • make fmt, biome check, tsc --noEmit clean
  • vitest run green: random.test.ts (6), TerminalPage.test.tsx (16), useChatDraftAttachments.test.ts (17), both changed stories (12)
  • Reproduced the original crash over HTTP on a production build, top frame uuidv4() in Template Builder.

This PR was generated by Coder Agents on behalf of @phorcys420.

Add a generateUUID helper to site/src/utils/random.ts and route the
uuid v4 and crypto.randomUUID call sites through it.
@phorcys420 phorcys420 changed the title refactor(site/src): centralize UUID generation in utils/random fix(site/src): centralize client-side UUID generation on uuid v4 Aug 19, 2026
@linear-code

linear-code Bot commented Aug 19, 2026

Copy link
Copy Markdown

OSS-38

@phorcys420
phorcys420 marked this pull request as draft August 19, 2026 00:18
The uuid package's v4 compiles down to a crypto.randomUUID call in the
production bundle, which is undefined in insecure contexts and throws
"crypto.randomUUID is not a function" when Coder is served over plain
HTTP (OSS-38). Derive the UUID from crypto.getRandomValues, which is
available in insecure contexts, and only use crypto.randomUUID when it
exists.
@phorcys420 phorcys420 changed the title fix(site/src): centralize client-side UUID generation on uuid v4 fix(site/src): avoid crypto.randomUUID for client-side UUID generation (HTTP) Aug 19, 2026
No longer imported after centralizing UUID generation on generateUUID.
Also shorten the generateUUID doc comment.
@phorcys420 phorcys420 changed the title fix(site/src): avoid crypto.randomUUID for client-side UUID generation (HTTP) fix(site): centralize UUID generation and fix non-secure crash Aug 19, 2026
@phorcys420
phorcys420 marked this pull request as ready for review August 19, 2026 01:53
@phorcys420

phorcys420 commented Aug 19, 2026

Copy link
Copy Markdown
Member Author

i did review the pixel shots and they're fine so i approved them but i'm unsure how to get CI to pass, also i did reproduce that it stack traced before in an http context (accessing develop.sh instance via Coder Connect) and it doesn't anymore.

@phorcys420

Copy link
Copy Markdown
Member Author

(turns out this basically does what #27709 undid)

Comment thread site/src/utils/random.test.ts Outdated

it("produces a valid UUID without crypto.randomUUID (insecure context)", () => {
const descriptor = Object.getOwnPropertyDescriptor(crypto, "randomUUID");
Object.defineProperty(crypto, "randomUUID", {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use vi.stubGlobal in the unit tests instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure tbh claude did this

@phorcys420 phorcys420 Aug 19, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah this is horrible haha my bad

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh no worries! claude LOVES stubbing with Object.defineProperty, see it all the time

@jeremyruppel jeremyruppel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! thanks for finding and fixing this!

@phorcys420
phorcys420 enabled auto-merge (squash) August 19, 2026 15:41
@phorcys420
phorcys420 merged commit 1bb0978 into main Aug 19, 2026
26 checks passed
@phorcys420
phorcys420 deleted the phorcys/centralize-uuid-generation branch August 19, 2026 15:51
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants