Skip to content

fix: polyfill crypto.randomUUID for self-hosted HTTP (insecure context) - #257

Open
philippecottier wants to merge 4 commits into
Adam-CAD:masterfrom
philippecottier:fix/crypto-randomuuid-insecure-context
Open

fix: polyfill crypto.randomUUID for self-hosted HTTP (insecure context)#257
philippecottier wants to merge 4 commits into
Adam-CAD:masterfrom
philippecottier:fix/crypto-randomuuid-insecure-context

Conversation

@philippecottier

Copy link
Copy Markdown

Problem

crypto.randomUUID() is only available in secure contexts — HTTPS pages, or
http://localhost. A self-hosted CADAM instance served over plain HTTP on a LAN
address (e.g. http://192.168.1.50:3000) is not a secure context, so
crypto.randomUUID is undefined there.

The app calls it from several client components (PromptView, EditorView,
TextAreaChat, messageService, …), so it crashes on startup with:

TypeError: crypto.randomUUID is not a function

The error is swallowed by the root error boundary, so the user only sees the
"Oops! Something went wrong" screen. This makes the app unusable for anyone
self-hosting over HTTP on a LAN — a setup the README supports.

Reproduce: serve the app on any non-localhost HTTP origin and open it → blank error screen.

Fix

Add a small polyfill (src/lib/ensureRandomUUID.ts), imported for its side
effect at the very top of the client entry point (src/client.tsx) so it runs
before anything calls randomUUID. It derives a spec-compliant RFC 4122 v4 UUID
from crypto.getRandomValues() (which is available in insecure contexts) only
when the native method is missing.

  • No-op on HTTPS and on localhost (native implementation always preferred).
  • No new dependencies.
  • Server code untouched (Node exposes crypto.randomUUID natively).

Testing

  • tsc -b and eslint pass.
  • Verified on a self-hosted instance served over HTTP on a LAN IP: without the
    patch the app shows the error boundary; with it, the app loads and generation works.

crypto.randomUUID() is only exposed in secure contexts (HTTPS pages, or
http://localhost). A self-hosted instance served over plain HTTP on a LAN
address (e.g. http://192.168.1.50:3000) is not a secure context, so
crypto.randomUUID is undefined there and the app crashes on startup with
"TypeError: crypto.randomUUID is not a function" — it is called from
several client components (PromptView, EditorView, TextAreaChat, ...).

Add a small polyfill, imported for its side effect at the top of the
client entry point, that derives a spec-compliant RFC 4122 v4 UUID from
crypto.getRandomValues() when the native method is missing. The native
implementation is always preferred, so this is a no-op on HTTPS and on
localhost.
@vercel

vercel Bot commented Aug 26, 2026

Copy link
Copy Markdown

@philippecottier is attempting to deploy a commit to the Adam Team on Vercel.

A member of the Team first needs to authorize it.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 2 files

Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge because no blocking failure remains.

Summary

  • Adds an RFC 4122 version 4 formatter backed by crypto.getRandomValues().
  • Loads the fallback through the TanStack Start client entry point.

Reviews (3) · Last reviewed commit: "refactor(client): type the UUID polyfill..."

Comment thread src/lib/ensureRandomUUID.ts Outdated
Comment thread src/client.tsx
philippecottier and others added 3 commits August 26, 2026 02:54
- Separate the polyfill side-effect import from the external imports.
- Wrap the UUID assembly to stay within the 100-char line limit.
- Assert to the exact template-literal return type instead of
  ReturnType<Crypto['randomUUID']>, and document why a single assertion is
  unavoidable for a UUID built at runtime.
Addresses review: the side-effect import intentionally precedes the external imports so the polyfill is installed before any module that may call crypto.randomUUID during initialization. Kept in its own import group with an explanatory comment.
Addresses review: drop the `as ReturnType<Crypto['randomUUID']>` assertion. The polyfill now declares its return type as the UUID template-literal type, so the compiler verifies the produced shape. Splitting the segments into locals also keeps every line within the formatting limit.
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.

1 participant