14 files changed
+88
-23
No files matched your search
@@ -19,4 +19,14 @@ describe("uuid", () => {
|
||||
|
||||
expect(uuid()).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
|
||||
});
|
||||
|
||||
it("keeps working without crypto.randomUUID, the state of plain-HTTP deployments", () => {
|
||||
// Issue #9306: the Docker build served over HTTP has crypto.getRandomValues but no
|
||||
// crypto.randomUUID, and every bare crypto.randomUUID() call threw there.
|
||||
const getRandomValues = vi.fn((buffer: Uint8Array) => buffer.fill(0xab));
|
||||
vi.stubGlobal("crypto", { getRandomValues });
|
||||
|
||||
expect(uuid()).toBe("abababab-abab-4bab-abab-abababababab");
|
||||
expect(getRandomValues).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
import { readFileSync, readdirSync, statSync } from "node:fs";
|
||||
import { join, relative, sep } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
/**
|
||||
* `crypto.randomUUID` exists only in secure contexts, so every bare call in the
|
||||
* desktop/web bundle crashes under the plain-HTTP Docker deployment (issue #9306).
|
||||
* The guard-compatible forms — `crypto?.randomUUID?.()`, `globalThis.crypto?.randomUUID?.()`
|
||||
* — are allowed, as is the shared helper that owns the fallback.
|
||||
*/
|
||||
const SOURCE_ROOT = fileURLToPath(new URL("../../..", import.meta.url));
|
||||
const HELPER_PATH = join("lib", "common", "utils.ts");
|
||||
const BARE_CALL = /(?<!\?)crypto\s*\.\s*randomUUID\s*\(/;
|
||||
|
||||
function sourceFiles(directory: string): string[] {
|
||||
return readdirSync(directory).flatMap((entry) => {
|
||||
const path = join(directory, entry);
|
||||
if (statSync(path).isDirectory()) return entry === "__tests__" ? [] : sourceFiles(path);
|
||||
return entry.endsWith(".ts") || entry.endsWith(".vue") ? [path] : [];
|
||||
});
|
||||
}
|
||||
|
||||
describe("crypto.randomUUID call sites", () => {
|
||||
it("recognizes direct calls but not guard-compatible ones", () => {
|
||||
expect(BARE_CALL.test("const id = crypto.randomUUID();")).toBe(true);
|
||||
expect(BARE_CALL.test("globalThis.crypto.randomUUID()")).toBe(true);
|
||||
expect(BARE_CALL.test("crypto?.randomUUID?.() || fallback")).toBe(false);
|
||||
expect(BARE_CALL.test("globalThis.crypto?.randomUUID?.() ?? fallback")).toBe(false);
|
||||
});
|
||||
|
||||
it("routes every UUID through the shared helper instead of calling crypto.randomUUID directly", () => {
|
||||
const offenders = sourceFiles(SOURCE_ROOT)
|
||||
.map((path) => ({ path: relative(SOURCE_ROOT, path).split(sep).join("/"), text: readFileSync(path, "utf8") }))
|
||||
.filter(({ path }) => path !== HELPER_PATH.split(sep).join("/"))
|
||||
.filter(({ text }) => BARE_CALL.test(text))
|
||||
.map(({ path }) => path);
|
||||
|
||||
expect(offenders).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -6,10 +6,27 @@ export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random UUID (v4).
|
||||
*
|
||||
* `crypto.randomUUID` is exposed only in secure contexts, so the Docker/Web build
|
||||
* served over plain HTTP on a LAN address sees it as `undefined` while
|
||||
* `crypto.getRandomValues` keeps working (issue #9306). Call this helper instead of
|
||||
* `crypto.randomUUID()` directly; the generated value only has to be unique, so the
|
||||
* non-secure fallbacks are acceptable.
|
||||
*/
|
||||
export function uuid(): string {
|
||||
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
||||
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
||||
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
||||
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
||||
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0"))
|
||||
.join("")
|
||||
.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, "$1-$2-$3-$4-$5");
|
||||
}
|
||||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
||||
const r = (Math.random() * 16) | 0;
|
||||
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
|
||||
|
||||
Reference in new issue
Block a user