// @vitest-environment jsdom import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; // PCH-01-007: document-level keydown handlers inside inline selectors must // ignore page-synthesised KeyboardEvents (isTrusted === false), // so a host page cannot script selection + fill without a real keypress. describe("PCH-01-007 — inject keydown handlers gate on e.isTrusted", () => { let showAutofillSelector: typeof import("./inject").showAutofillSelector; let showTOTPSelector: typeof import("./inject").showTOTPSelector; let cleanup: (() => void) | null = null; beforeEach(async () => { vi.resetModules(); (globalThis as unknown as { chrome: unknown }).chrome = { runtime: { sendMessage: vi.fn(), getURL: (p: string) => p }, }; class NoopObserver { observe() {} unobserve() {} disconnect() {} } (globalThis as unknown as { ResizeObserver: unknown }).ResizeObserver = NoopObserver; (globalThis as unknown as { IntersectionObserver: unknown }).IntersectionObserver = NoopObserver; ({ showAutofillSelector, showTOTPSelector } = await import("./inject")); }); afterEach(() => { cleanup?.(); cleanup = null; document.body.innerHTML = ""; }); it("ignores untrusted ArrowDown/Enter — no fill without user intent", () => { const input = document.createElement("input"); document.body.appendChild(input); const onSelect = vi.fn(); cleanup = showAutofillSelector( input, [ { id: "1", title: "GitHub", username: "june", url: "https://github.com" }, { id: "2", title: "GitLab", username: "june", url: "https://gitlab.com" }, ], onSelect, { domain: "github.com" }, ); // Synthetic events have isTrusted === false in jsdom → handlers no-op. document.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true })); document.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })); expect(onSelect).not.toHaveBeenCalled(); }); it("ignores untrusted single-character keys (type-to-search handlers no-op)", () => { const input = document.createElement("input"); document.body.appendChild(input); const onSelect = vi.fn(); cleanup = showAutofillSelector( input, [{ id: "1", title: "GitHub", username: "june", url: "https://github.com" }], onSelect, { domain: "github.com" }, ); // A page-dispatched printable key must not drive selection/fill. document.dispatchEvent(new KeyboardEvent("keydown", { key: "a", bubbles: true })); expect(onSelect).not.toHaveBeenCalled(); }); it("keeps the TOTP selection unchanged after an untrusted arrow key", () => { const input = document.createElement("input"); document.body.appendChild(input); const originalAttachShadow = Element.prototype.attachShadow; let closedShadow: ShadowRoot | null = null; const attachSpy = vi.spyOn(Element.prototype, "attachShadow").mockImplementation(function ( this: Element, init: ShadowRootInit, ) { closedShadow = originalAttachShadow.call(this, init); return closedShadow; }); const scrollIntoView = vi.fn(); Object.defineProperty(HTMLElement.prototype, "scrollIntoView", { configurable: true, value: scrollIntoView, }); try { cleanup = showTOTPSelector( input, [ { id: "1", title: "GitHub", username: "june", hasTotp: true }, { id: "2", title: "GitLab", username: "june", hasTotp: true }, ], vi.fn(), ); expect(input.nextElementSibling?.shadowRoot).toBeNull(); document.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true })); expect((closedShadow as ShadowRoot | null)?.querySelector(".entry.focused")).toBeNull(); expect(scrollIntoView).not.toHaveBeenCalled(); } finally { attachSpy.mockRestore(); delete (HTMLElement.prototype as { scrollIntoView?: unknown }).scrollIntoView; } }); it("does not fill or copy after an untrusted TOTP Enter key", () => { const input = document.createElement("input"); document.body.appendChild(input); const copy = vi.fn(() => Promise.resolve()); const originalClipboard = Object.getOwnPropertyDescriptor(navigator, "clipboard"); Object.defineProperty(navigator, "clipboard", { configurable: true, value: { writeText: copy }, }); const onFill = vi.fn(() => { input.value = "123456"; void navigator.clipboard.writeText("123456"); }); try { cleanup = showTOTPSelector( input, [{ id: "1", title: "GitHub", username: "june", hasTotp: true }], onFill, ); document.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })); expect(onFill).not.toHaveBeenCalled(); expect(input.value).toBe(""); expect(copy).not.toHaveBeenCalled(); } finally { if (originalClipboard) { Object.defineProperty(navigator, "clipboard", originalClipboard); } else { delete (navigator as { clipboard?: Clipboard }).clipboard; } } }); it("keeps the click path usable inside the closed TOTP selector", () => { const input = document.createElement("input"); document.body.appendChild(input); const originalAttachShadow = Element.prototype.attachShadow; let closedShadow: ShadowRoot | null = null; const attachSpy = vi.spyOn(Element.prototype, "attachShadow").mockImplementation(function ( this: Element, init: ShadowRootInit, ) { closedShadow = originalAttachShadow.call(this, init); return closedShadow; }); const onFill = vi.fn(); try { cleanup = showTOTPSelector( input, [{ id: "1", title: "GitHub", username: "june", hasTotp: true }], onFill, ); (closedShadow as ShadowRoot | null)?.querySelector(".sp-fill-btn")?.click(); expect(onFill).toHaveBeenCalledWith("1"); } finally { attachSpy.mockRestore(); } }); it.each(["Escape", "Tab"])("does not dismiss the TOTP selector after an untrusted %s key", (key) => { const input = document.createElement("input"); document.body.appendChild(input); const onDismiss = vi.fn(); cleanup = showTOTPSelector( input, [{ id: "1", title: "GitHub", username: "june", hasTotp: true }], vi.fn(), { onDismiss }, ); document.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true })); expect(onDismiss).not.toHaveBeenCalled(); }); });