Skip to content

fix(ui): use feature detection for crypto.randomUUID to support HTTP deployments#1867

Closed
wsszh wants to merge 1 commit into
kagent-dev:mainfrom
wsszh:fix/allow-http-uuid-fallback
Closed

fix(ui): use feature detection for crypto.randomUUID to support HTTP deployments#1867
wsszh wants to merge 1 commit into
kagent-dev:mainfrom
wsszh:fix/allow-http-uuid-fallback

Conversation

@wsszh
Copy link
Copy Markdown

@wsszh wsszh commented May 14, 2026

Summary

  • The UI uses crypto.randomUUID() which requires a Secure Context (HTTPS or localhost). Deployments served over plain HTTP crash on /agents/new with Uncaught TypeError: crypto.randomUUID is not a function.
  • Introduces a generateId() utility in ui/src/lib/utils.ts that detects crypto.randomUUID availability at runtime and falls back to the uuid package (already a dependency, v14) when unavailable.
  • Replaces all 8 crypto.randomUUID() call sites across 4 source files.

Changes

File Change
ui/src/lib/utils.ts Add generateId() with runtime feature detection
ui/src/lib/promptSourceRow.ts crypto.randomUUID()generateId()
ui/src/lib/openClawSandboxForm.ts crypto.randomUUID()generateId()
ui/src/components/prompts/FragmentEntriesEditor.tsx crypto.randomUUID()generateId()
ui/src/app/agents/new/page.tsx crypto.randomUUID()generateId()

Context

crypto.randomUUID() is a Web Crypto API gated behind Secure Context. Any deployment accessed via http:// (common in internal/dev clusters without TLS termination) crashes when rendering the agent creation form. The uuid npm package (v14, already in package.json) uses crypto.getRandomValues() which works in all contexts.

The fix uses runtime feature detection (typeof crypto.randomUUID === "function") rather than a build-time env var, so it works with the pre-built Docker image regardless of deployment configuration.

ui/public/mockServiceWorker.js also calls crypto.randomUUID() but is auto-generated by MSW and runs in a Service Worker context (always secure), so it is not modified.

Test plan

  • cd ui && npm run build passes
  • cd ui && npm run lint passes
  • Deploy over HTTP → /agents/new → "Skip wizard" → form loads without crash
  • Deploy over HTTPS → same flow works using native crypto.randomUUID()
  • grep -r 'crypto.randomUUID' ui/src/ returns only utils.ts feature-detection branch (and mockServiceWorker.js)

Copilot AI review requested due to automatic review settings May 14, 2026 08:04
@github-actions github-actions Bot added the bug Something isn't working label May 14, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to support UI deployments served over plain HTTP by adding a configurable ID generation helper and wiring a Helm value into the UI deployment environment.

Changes:

  • Adds generateId() in ui/src/lib/utils.ts using either native crypto.randomUUID() or uuid.
  • Replaces direct crypto.randomUUID() usages in agent/prompt/sandbox form code.
  • Adds ui.allowHttp Helm configuration and passes it as NEXT_PUBLIC_ALLOW_HTTP.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
ui/src/lib/utils.ts Adds shared ID generation utility.
ui/src/lib/promptSourceRow.ts Uses shared ID generation for prompt source rows.
ui/src/lib/openClawSandboxForm.ts Uses shared ID generation for sandbox channel rows.
ui/src/components/prompts/FragmentEntriesEditor.tsx Uses shared ID generation for fragment row IDs.
ui/src/app/agents/new/page.tsx Uses shared ID generation in agent form prompt source handling.
helm/kagent/values.yaml Adds ui.allowHttp chart value.
helm/kagent/templates/ui-deployment.yaml Adds NEXT_PUBLIC_ALLOW_HTTP env var to the UI container.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ui/src/lib/utils.ts Outdated
Comment on lines +41 to +49
* Generate a unique ID. Uses the native crypto.randomUUID() in Secure Contexts
* (HTTPS/localhost). For plain HTTP deployments, set NEXT_PUBLIC_ALLOW_HTTP=true
* in the Helm values to use the uuid package fallback (crypto.getRandomValues).
*/
export function generateId(): string {
if (process.env.NEXT_PUBLIC_ALLOW_HTTP === "true") {
return uuidv4();
}
return crypto.randomUUID();
…deployments

crypto.randomUUID() requires a Secure Context (HTTPS or localhost).
Deployments served over plain HTTP crash on /agents/new with
"Uncaught TypeError: crypto.randomUUID is not a function".

Add a generateId() utility that detects crypto.randomUUID availability
at runtime and falls back to the uuid package (already a dependency)
when unavailable. Replace all 8 crypto.randomUUID() call sites.

Signed-off-by: wsszh <wsszh@users.noreply.github.com>
Signed-off-by: sup <sup@supdeMacBook-Pro.local>
@wsszh wsszh force-pushed the fix/allow-http-uuid-fallback branch from d81a4b7 to f353b84 Compare May 14, 2026 08:14
@wsszh wsszh changed the title fix(ui): add NEXT_PUBLIC_ALLOW_HTTP config to support non-HTTPS deployments fix(ui): use feature detection for crypto.randomUUID to support HTTP deployments May 14, 2026
@github-actions github-actions Bot added bug Something isn't working and removed bug Something isn't working labels May 14, 2026
@wsszh wsszh closed this May 14, 2026
@wsszh wsszh deleted the fix/allow-http-uuid-fallback branch May 14, 2026 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants