Skip to content

fix(web): fall back to getRandomValues when crypto.randomUUID is unavailable#523

Merged
tiann merged 1 commit intotiann:mainfrom
junmo-kim:fix/desktop-attach
Apr 24, 2026
Merged

fix(web): fall back to getRandomValues when crypto.randomUUID is unavailable#523
tiann merged 1 commit intotiann:mainfrom
junmo-kim:fix/desktop-attach

Conversation

@junmo-kim
Copy link
Copy Markdown
Contributor

Problem

The web UI uses crypto.randomUUID() in five places (attachment adapter,
message local IDs, toast IDs, file attachment metadata, terminal IDs).
crypto.randomUUID is only defined in secure contexts — HTTPS, or the
special-cased http://localhost / http://127.0.0.1.

When the web app is opened over HTTP from a LAN IP (a common dev /
home-network setup — e.g. http://192.168.x.x:3006), crypto.randomUUID
is undefined. The first place this surfaces is the attachment flow:

TypeError: crypto.randomUUID is not a function
    at attachmentAdapter.add
    at addAttachment (assistant-ui)
    at ComposerAddAttachment.onchange

assistant-ui's ComposerPrimitive.AddAttachment swallows the rejection,
so from the user's side "pick a file" silently does nothing — no chip, no
upload request, no error indicator. Mobile browsers that go through the
HTTPS tunnel aren't affected.

Solution

Add a small web/src/lib/randomId.ts helper and call it everywhere the
web currently calls crypto.randomUUID() directly:

  1. If crypto.randomUUID is available, use it (unchanged behavior for
    HTTPS and localhost).
  2. Otherwise, use crypto.getRandomValues() to produce an RFC 4122 v4
    UUID string — same format and character set, so existing consumers
    (DB columns, SSE payloads, RPC params) see identical values.
  3. If neither is available (very old environments), fall back to a
    Date.now()-based string.

Only the web package is changed. CLI / hub / shared still use
crypto.randomUUID() directly — Node has no secure-context concept there.

Tests

  • bun typecheck passes.
  • bun run test (web): 170 tests pass, including new
    randomId.test.ts covering all three branches.
  • Manual verification on http://<LAN-IP>:<port>/ (not localhost):
    • Before: console TypeError: crypto.randomUUID is not a function,
      no attachment chip after picking a file.
    • After: chip appears, no console error, upload proceeds normally.

…ailable

crypto.randomUUID is only exposed in secure contexts (HTTPS or
localhost). When the web app is served over HTTP on a LAN IP the
attachment adapter, toast provider, message localId helper, file
attachment metadata and terminal id creation all call
crypto.randomUUID() synchronously and throw TypeError, so the UI
silently does nothing (e.g. the file picker opens and closes with no
chip).

Add a small web/src/lib/randomId helper that tries crypto.randomUUID
first, then falls back to crypto.getRandomValues-derived UUID v4,
and finally to a Date.now/Math.random string for very old
environments. Route all five call sites through it. Output format is
identical for secure contexts and UUID v4 for the getRandomValues
path, so existing DB/SSE/RPC consumers see the same shape.
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Review mode: initial

Findings

  • No issues identified in the modified lines.

Summary

  • No blocking correctness, security, or regression issues found in the latest diff.
  • Residual risk/testing gap: direct unit coverage was added for web/src/lib/randomId.test.ts; Not found in repo/docs: direct tests for the updated attachment, toast, or terminal ID-generation paths. I also could not run bun in this runner (bun: command not found).

Testing

  • Not run (automation environment missing bun)

HAPI Bot

@tiann tiann merged commit b712ee6 into tiann:main Apr 24, 2026
2 checks passed
@junmo-kim junmo-kim deleted the fix/desktop-attach branch April 24, 2026 07:55
dmnkf pushed a commit to dmnkf/hapi that referenced this pull request Apr 25, 2026
…ailable (tiann#523)

crypto.randomUUID is only exposed in secure contexts (HTTPS or
localhost). When the web app is served over HTTP on a LAN IP the
attachment adapter, toast provider, message localId helper, file
attachment metadata and terminal id creation all call
crypto.randomUUID() synchronously and throw TypeError, so the UI
silently does nothing (e.g. the file picker opens and closes with no
chip).

Add a small web/src/lib/randomId helper that tries crypto.randomUUID
first, then falls back to crypto.getRandomValues-derived UUID v4,
and finally to a Date.now/Math.random string for very old
environments. Route all five call sites through it. Output format is
identical for secure contexts and UUID v4 for the getRandomValues
path, so existing DB/SSE/RPC consumers see the same shape.
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.

2 participants