123import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";456789describe("PCH-01-007 — inject keydown handlers gate on e.isTrusted", () => {10 let showAutofillSelector: typeof import("./inject").showAutofillSelector;11 let showTOTPSelector: typeof import("./inject").showTOTPSelector;12 let cleanup: (() => void) | null = null;1314 beforeEach(async () => {15 vi.resetModules();16 (globalThis as unknown as { chrome: unknown }).chrome = {17 runtime: { sendMessage: vi.fn(), getURL: (p: string) => p },18 };19 class NoopObserver {20 observe() {}21 unobserve() {}22 disconnect() {}23 }24 (globalThis as unknown as { ResizeObserver: unknown }).ResizeObserver = NoopObserver;25 (globalThis as unknown as { IntersectionObserver: unknown }).IntersectionObserver = NoopObserver;26 ({ showAutofillSelector, showTOTPSelector } = await import("./inject"));27 });2829 afterEach(() => {30 cleanup?.();31 cleanup = null;32 document.body.innerHTML = "";33 });3435 it("ignores untrusted ArrowDown/Enter — no fill without user intent", () => {36 const input = document.createElement("input");37 document.body.appendChild(input);38 const onSelect = vi.fn();39 cleanup = showAutofillSelector(40 input,41 [42 { id: "1", title: "GitHub", username: "june", url: "https://github.com" },43 { id: "2", title: "GitLab", username: "june", url: "https://gitlab.com" },44 ],45 onSelect,46 { domain: "github.com" },47 );4849 50 document.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true }));51 document.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));5253 expect(onSelect).not.toHaveBeenCalled();54 });5556 it("ignores untrusted single-character keys (type-to-search handlers no-op)", () => {57 const input = document.createElement("input");58 document.body.appendChild(input);59 const onSelect = vi.fn();60 cleanup = showAutofillSelector(61 input,62 [{ id: "1", title: "GitHub", username: "june", url: "https://github.com" }],63 onSelect,64 { domain: "github.com" },65 );6667 68 document.dispatchEvent(new KeyboardEvent("keydown", { key: "a", bubbles: true }));69 expect(onSelect).not.toHaveBeenCalled();70 });7172 it("keeps the TOTP selection unchanged after an untrusted arrow key", () => {73 const input = document.createElement("input");74 document.body.appendChild(input);75 const originalAttachShadow = Element.prototype.attachShadow;76 let closedShadow: ShadowRoot | null = null;77 const attachSpy = vi.spyOn(Element.prototype, "attachShadow").mockImplementation(function (78 this: Element,79 init: ShadowRootInit,80 ) {81 closedShadow = originalAttachShadow.call(this, init);82 return closedShadow;83 });84 const scrollIntoView = vi.fn();85 Object.defineProperty(HTMLElement.prototype, "scrollIntoView", {86 configurable: true,87 value: scrollIntoView,88 });8990 try {91 cleanup = showTOTPSelector(92 input,93 [94 { id: "1", title: "GitHub", username: "june", hasTotp: true },95 { id: "2", title: "GitLab", username: "june", hasTotp: true },96 ],97 vi.fn(),98 );99 expect(input.nextElementSibling?.shadowRoot).toBeNull();100101 document.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true }));102103 expect((closedShadow as ShadowRoot | null)?.querySelector(".entry.focused")).toBeNull();104 expect(scrollIntoView).not.toHaveBeenCalled();105 } finally {106 attachSpy.mockRestore();107 delete (HTMLElement.prototype as { scrollIntoView?: unknown }).scrollIntoView;108 }109 });110111 it("does not fill or copy after an untrusted TOTP Enter key", () => {112 const input = document.createElement("input");113 document.body.appendChild(input);114 const copy = vi.fn(() => Promise.resolve());115 const originalClipboard = Object.getOwnPropertyDescriptor(navigator, "clipboard");116 Object.defineProperty(navigator, "clipboard", {117 configurable: true,118 value: { writeText: copy },119 });120 const onFill = vi.fn(() => {121 input.value = "123456";122 void navigator.clipboard.writeText("123456");123 });124125 try {126 cleanup = showTOTPSelector(127 input,128 [{ id: "1", title: "GitHub", username: "june", hasTotp: true }],129 onFill,130 );131132 document.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));133134 expect(onFill).not.toHaveBeenCalled();135 expect(input.value).toBe("");136 expect(copy).not.toHaveBeenCalled();137 } finally {138 if (originalClipboard) {139 Object.defineProperty(navigator, "clipboard", originalClipboard);140 } else {141 delete (navigator as { clipboard?: Clipboard }).clipboard;142 }143 }144 });145146 it("keeps the click path usable inside the closed TOTP selector", () => {147 const input = document.createElement("input");148 document.body.appendChild(input);149 const originalAttachShadow = Element.prototype.attachShadow;150 let closedShadow: ShadowRoot | null = null;151 const attachSpy = vi.spyOn(Element.prototype, "attachShadow").mockImplementation(function (152 this: Element,153 init: ShadowRootInit,154 ) {155 closedShadow = originalAttachShadow.call(this, init);156 return closedShadow;157 });158 const onFill = vi.fn();159160 try {161 cleanup = showTOTPSelector(162 input,163 [{ id: "1", title: "GitHub", username: "june", hasTotp: true }],164 onFill,165 );166167 (closedShadow as ShadowRoot | null)?.querySelector<HTMLButtonElement>(".sp-fill-btn")?.click();168169 expect(onFill).toHaveBeenCalledWith("1");170 } finally {171 attachSpy.mockRestore();172 }173 });174175 it.each(["Escape", "Tab"])("does not dismiss the TOTP selector after an untrusted %s key", (key) => {176 const input = document.createElement("input");177 document.body.appendChild(input);178 const onDismiss = vi.fn();179 cleanup = showTOTPSelector(180 input,181 [{ id: "1", title: "GitHub", username: "june", hasTotp: true }],182 vi.fn(),183 { onDismiss },184 );185186 document.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true }));187188 expect(onDismiss).not.toHaveBeenCalled();189 });190});