Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

fix(web): mint draft and shortcut ids without crypto.randomUUID (#687)

Yaowei Zhengc49b45de10b9
diff --git a/changelog/unreleased/2026-09-11-random-id-insecure-context.md b/changelog/unreleased/2026-09-11-random-id-insecure-context.md
new file mode 100644
index 00000000..4a43dc3f
--- /dev/null
+++ b/changelog/unreleased/2026-09-11-random-id-insecure-context.md
@@ -0,0 +1,15 @@
+# The "+" button parks a typed draft over plain HTTP
+
+- **Date:** 2026-09-11
+- **Type:** fix
+- **Scope:** `web`
+- **PR:** [#687](https://github.com/Prism-Shadow/penguin-harness/pull/687)
+
+[中文版](2026-09-11-random-id-insecure-context.zh.md)
+
+Parked-draft and user-shortcut ids come from `crypto.getRandomValues` now, not
+`crypto.randomUUID`. The latter exists only in a secure context (HTTPS, or localhost), so a Web
+App opened over plain HTTP from any address but localhost threw
+`crypto.randomUUID is not a function` the moment it had to park a typed draft — a click on "+"
+while the draft held text — and every later click failed the same way while that text remained.
+The ids keep their shape: `draft-` plus 8 hex characters, `sc-` plus 12.
diff --git a/changelog/unreleased/2026-09-11-random-id-insecure-context.zh.md b/changelog/unreleased/2026-09-11-random-id-insecure-context.zh.md
new file mode 100644
index 00000000..2f587ba9
--- /dev/null
+++ b/changelog/unreleased/2026-09-11-random-id-insecure-context.zh.md
@@ -0,0 +1,13 @@
+# 通过明文 HTTP 访问时,「+」按钮也能把已输入的草稿存入列表
+
+- **Date:** 2026-09-11
+- **Type:** fix
+- **Scope:** `web`
+- **PR:** [#687](https://github.com/Prism-Shadow/penguin-harness/pull/687)
+
+[English](2026-09-11-random-id-insecure-context.md)
+
+草稿会话与用户快捷方式的 id 改由 `crypto.getRandomValues` 生成,不再使用 `crypto.randomUUID`。后者只在安全
+上下文(HTTPS 或 localhost)中存在,因此以明文 HTTP 从 localhost 以外的任何地址打开 Web App 时,一旦需要把
+已输入的草稿存入列表——即草稿里有文字时点击「+」——就会抛出 `crypto.randomUUID is not a function`,且只要
+文字还在,之后每次点击都同样失败。id 的形态保持不变:`draft-` 后接 8 位十六进制,`sc-` 后接 12 位。
diff --git a/packages/web/src/features/chat/draft-sessions.ts b/packages/web/src/features/chat/draft-sessions.ts
index 7732962d..895d672d 100644
--- a/packages/web/src/features/chat/draft-sessions.ts
+++ b/packages/web/src/features/chat/draft-sessions.ts
@@ -20,6 +20,7 @@
*/
import { useStore } from "zustand/react";
import { createStore } from "zustand/vanilla";
+import { randomHex } from "../../lib/random-id";
import { clearDraft, draftFromUnknown, draftKey, loadDraft, saveDraft } from "./draft-cache";
import type { DraftCache, DraftStorage } from "./draft-cache";
@@ -126,7 +127,7 @@ export function useDraftSessions(
);
}
-const randomDraftId = (): string => `draft-${crypto.randomUUID().replace(/-/g, "").slice(0, 8)}`;
+const randomDraftId = (): string => `draft-${randomHex(8)}`;
/**
* Moves the ACTIVE new-chat draft into the parked list when it holds typed text;
diff --git a/packages/web/src/features/chat/user-shortcuts.ts b/packages/web/src/features/chat/user-shortcuts.ts
index b0a6f7c4..d38e6938 100644
--- a/packages/web/src/features/chat/user-shortcuts.ts
+++ b/packages/web/src/features/chat/user-shortcuts.ts
@@ -22,6 +22,7 @@
* `test/user-shortcuts.test.ts` fails if the two copies drift apart.
*/
import type { DraftShortcut } from "@prismshadow/penguin-server/api";
+import { randomHex } from "../../lib/random-id";
/** A saved shortcut, as stored and as rendered. Re-exported from the API type so the two cannot drift. */
export type UserShortcut = DraftShortcut;
@@ -38,7 +39,7 @@ export const SHORTCUT_PROMPT_MAX = 4000;
* opaque, and never derived from the title — a rename must not change what an edit addresses.
*/
export function newShortcutId(): string {
- return `sc-${crypto.randomUUID().replace(/-/g, "").slice(0, 12)}`;
+ return `sc-${randomHex(12)}`;
}
/** One trimmed, length-capped string field read back out of free-form JSON; null when unusable. */
diff --git a/packages/web/src/lib/random-id.ts b/packages/web/src/lib/random-id.ts
new file mode 100644
index 00000000..0833127d
--- /dev/null
+++ b/packages/web/src/lib/random-id.ts
@@ -0,0 +1,17 @@
+/**
+ * Random ids for the things the browser mints on its own: parked drafts, user shortcuts.
+ *
+ * Drawn from `crypto.getRandomValues`, not `crypto.randomUUID`. The latter exists only in a
+ * secure context (HTTPS, or localhost), so a Web App opened over plain HTTP from any address but
+ * localhost has no such function, and the first id it tried to mint threw — which is how the "+"
+ * button died as soon as there was a typed draft to park. `getRandomValues` is present wherever
+ * `crypto` is at all.
+ */
+
+/** `length` random lowercase hex characters. */
+export function randomHex(length: number): string {
+ const bytes = crypto.getRandomValues(new Uint8Array(Math.ceil(length / 2)));
+ return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0"))
+ .join("")
+ .slice(0, length);
+}
diff --git a/packages/web/test/random-id.test.ts b/packages/web/test/random-id.test.ts
new file mode 100644
index 00000000..46264306
--- /dev/null
+++ b/packages/web/test/random-id.test.ts
@@ -0,0 +1,61 @@
+/**
+ * randomHex (lib/random-id.ts) and the two ids it mints: parked drafts (draft-sessions.ts) and
+ * user shortcuts (user-shortcuts.ts). Both have to work in an insecure context — a Web App served
+ * over plain HTTP from any address but localhost — where `crypto.randomUUID` does not exist and
+ * the previous generator threw on the "+" button.
+ */
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { draftKey, saveDraft } from "../src/features/chat/draft-cache";
+import type { DraftStorage } from "../src/features/chat/draft-cache";
+import { parkActiveDraft } from "../src/features/chat/draft-sessions";
+import { newShortcutId } from "../src/features/chat/user-shortcuts";
+import { randomHex } from "../src/lib/random-id";
+
+describe("randomHex", () => {
+ it("returns exactly the requested number of lowercase hex characters", () => {
+ for (const length of [1, 8, 12, 13, 32]) {
+ expect(randomHex(length)).toMatch(new RegExp(`^[0-9a-f]{${length}}$`));
+ }
+ });
+
+ it("does not repeat", () => {
+ const ids = new Set(Array.from({ length: 200 }, () => randomHex(8)));
+ expect(ids.size).toBe(200);
+ });
+});
+
+/**
+ * The regression, driven through the two functions that actually threw. Stubbing the global and
+ * then asserting on randomHex alone would prove nothing: randomHex never names randomUUID, so
+ * such a test passes just as happily with randomUUID put back into either call site.
+ */
+describe("an insecure context, where crypto carries getRandomValues and no randomUUID", () => {
+ afterEach(() => vi.unstubAllGlobals());
+
+ function stubInsecureCrypto(): void {
+ const real = globalThis.crypto;
+ vi.stubGlobal("crypto", { getRandomValues: real.getRandomValues.bind(real) });
+ }
+
+ /** In-memory storage: vitest runs in a Node environment, so there is no localStorage. */
+ function memStorage(): DraftStorage {
+ const map = new Map<string, string>();
+ return {
+ getItem: (k) => map.get(k) ?? null,
+ setItem: (k, v) => void map.set(k, v),
+ removeItem: (k) => void map.delete(k),
+ };
+ }
+
+ it("parks a typed draft rather than throwing on the '+' click", () => {
+ const s = memStorage();
+ stubInsecureCrypto();
+ saveDraft(draftKey("u-insecure", "proj"), { text: "half-written prompt" }, s);
+ expect(parkActiveDraft("u-insecure", "proj", s)).toMatch(/^draft-[0-9a-f]{8}$/);
+ });
+
+ it("mints a shortcut id rather than throwing on save", () => {
+ stubInsecureCrypto();
+ expect(newShortcutId()).toMatch(/^sc-[0-9a-f]{12}$/);
+ });
+});