|
| 1 | +/* @vitest-environment jsdom */ |
| 2 | +// Plain-HTTP origins have no crypto.subtle; device identity must still mint, |
| 3 | +// fingerprint, and sign with the pure-JS paths so pairing works everywhere. |
| 4 | +import { hashes, verifyAsync } from "@noble/ed25519"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import { createStorageMock } from "../../test-helpers/storage.ts"; |
| 7 | +import { loadOrCreateDeviceIdentity, signDevicePayload } from "./index.ts"; |
| 8 | + |
| 9 | +function base64UrlDecode(value: string): Uint8Array { |
| 10 | + const normalized = value.replaceAll("-", "+").replaceAll("_", "/"); |
| 11 | + const binary = atob(normalized); |
| 12 | + return Uint8Array.from(binary, (char) => char.charCodeAt(0)); |
| 13 | +} |
| 14 | + |
| 15 | +describe("device identity on an insecure context", () => { |
| 16 | + beforeEach(() => { |
| 17 | + vi.stubGlobal("localStorage", createStorageMock()); |
| 18 | + // Real insecure contexts keep getRandomValues; only crypto.subtle is gated. |
| 19 | + vi.stubGlobal("crypto", { |
| 20 | + getRandomValues: (array: Uint8Array) => { |
| 21 | + for (let i = 0; i < array.length; i += 1) { |
| 22 | + array[i] = (i * 37 + 11) % 256; |
| 23 | + } |
| 24 | + return array; |
| 25 | + }, |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + vi.unstubAllGlobals(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("mints, persists, and signs a device identity without crypto.subtle", async () => { |
| 34 | + expect(globalThis.crypto.subtle).toBeUndefined(); |
| 35 | + |
| 36 | + const identity = await loadOrCreateDeviceIdentity(); |
| 37 | + expect(identity.deviceId).toMatch(/^[0-9a-f]{64}$/); |
| 38 | + |
| 39 | + // The identity round-trips from storage with a stable id. |
| 40 | + const reloaded = await loadOrCreateDeviceIdentity(); |
| 41 | + expect(reloaded.deviceId).toBe(identity.deviceId); |
| 42 | + |
| 43 | + const payload = "insecure-context-payload"; |
| 44 | + const signature = await signDevicePayload(identity.privateKey, payload); |
| 45 | + await expect( |
| 46 | + verifyAsync( |
| 47 | + base64UrlDecode(signature), |
| 48 | + new TextEncoder().encode(payload), |
| 49 | + base64UrlDecode(identity.publicKey), |
| 50 | + ), |
| 51 | + ).resolves.toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + it("keeps one stable identity per page lifetime when storage is blocked", async () => { |
| 55 | + // The suite's default RNG stub is deterministic, which would mint identical |
| 56 | + // keys and mask churn; every draw must differ for this regression to bite. |
| 57 | + let draw = 0; |
| 58 | + vi.stubGlobal("crypto", { |
| 59 | + getRandomValues: (array: Uint8Array) => { |
| 60 | + draw += 1; |
| 61 | + for (let i = 0; i < array.length; i += 1) { |
| 62 | + array[i] = (i * 37 + draw) % 256; |
| 63 | + } |
| 64 | + return array; |
| 65 | + }, |
| 66 | + }); |
| 67 | + vi.stubGlobal("localStorage", undefined); |
| 68 | + vi.resetModules(); |
| 69 | + const cold = await import("./index.ts"); |
| 70 | + |
| 71 | + const first = await cold.loadOrCreateDeviceIdentity(); |
| 72 | + const second = await cold.loadOrCreateDeviceIdentity(); |
| 73 | + expect(first.deviceId).toMatch(/^[0-9a-f]{64}$/); |
| 74 | + expect(second.deviceId).toBe(first.deviceId); |
| 75 | + }); |
| 76 | + |
| 77 | + it("still mints an identity when the store rejects writes", async () => { |
| 78 | + vi.stubGlobal("localStorage", { |
| 79 | + getItem: () => null, |
| 80 | + setItem: () => { |
| 81 | + throw new Error("quota exceeded"); |
| 82 | + }, |
| 83 | + }); |
| 84 | + vi.resetModules(); |
| 85 | + const cold = await import("./index.ts"); |
| 86 | + |
| 87 | + const identity = await cold.loadOrCreateDeviceIdentity(); |
| 88 | + expect(identity.deviceId).toMatch(/^[0-9a-f]{64}$/); |
| 89 | + }); |
| 90 | + |
| 91 | + it("resolves the SHA-512 provider without crypto.subtle", async () => { |
| 92 | + expect(typeof hashes.sha512Async).toBe("function"); |
| 93 | + const digest = await hashes.sha512Async?.(new Uint8Array([1, 2, 3])); |
| 94 | + expect(digest).toHaveLength(64); |
| 95 | + }); |
| 96 | +}); |
0 commit comments