// PB-26-106: content-script entry mutations must be bound to the entry's own // domain set. // // The autofill READ path re-validates the browser-attested frame origin against // the entry's domains on every request (GET_ENTRY_FOR_FILL). The write path had // no symmetric binding, and `additionalUrls` feeds the same domain index the // read path consults — so a content script that knew an entry id could repoint // the entry at its own origin and then walk through the (origin-correct) read // path to obtain the full credential. // // The binding has two halves and both are required: // // 1. the sender's attested origin must still be inside the entry's domain set // AFTER the write, and // 2. every domain the write ADDS must be the sender's own origin. // // Half 1 alone is not enough: a content script sitting on the entry's own // (legitimate) origin would pass it while repointing the entry at a third // party — the escalation this file pins down. Half 2 is what makes the write // self-binding, which is also what keeps the inline "remember this site" toast // working: that toast legitimately adds the page the user is standing on. import { beforeEach, describe, expect, it, vi } from "vitest"; type StorageShape = Record; function makeChromeStorageMock() { const storage: StorageShape = {}; const chrome = { storage: { local: { get: vi.fn(async (key?: string | string[]) => { if (!key) return { ...storage }; if (typeof key === "string") return { [key]: storage[key] }; return (Array.isArray(key) ? key : [key]).reduce( (acc, k) => { acc[k] = storage[k]; return acc; }, {}, ); }), set: vi.fn(async (next: StorageShape) => { Object.assign(storage, next); }), remove: vi.fn(async (keys: string | string[]) => { const ks = Array.isArray(keys) ? keys : [keys]; for (const k of ks) delete storage[k]; }), }, session: { get: vi.fn(async () => ({})), set: vi.fn(async () => {}), remove: vi.fn(async () => {}), }, }, alarms: { clear: vi.fn(), create: vi.fn(), onAlarm: { addListener: vi.fn() } }, idle: { setDetectionInterval: vi.fn(), onStateChanged: { addListener: vi.fn() } }, runtime: { id: "test-extension-id", getURL: vi.fn((path: string) => `chrome-extension://test-extension-id/${path}`), }, } as unknown as typeof globalThis.chrome; return { chrome }; } const LEGIT = "https://example.com/login"; const FOREIGN = "https://attacker.example.net/steal"; function pageSender(url: string): chrome.runtime.MessageSender { return { tab: { url }, url } as unknown as chrome.runtime.MessageSender; } const extensionSender = { url: "chrome-extension://test-extension-id/src/popup/index.html", } as unknown as chrome.runtime.MessageSender; describe("PB-26-106: content-script entry write origin binding", () => { let vault: typeof import("./vault"); let messages: typeof import("./messages"); beforeEach(async () => { vi.resetModules(); const mock = makeChromeStorageMock(); (globalThis as Record).chrome = mock.chrome; vi.stubGlobal("fetch", vi.fn(async () => { throw new Error("no network"); })); vi.stubGlobal("__PEACH_E2E_PASSKEYS__", false); vault = await import("./vault"); messages = await import("./messages"); await vault.createVault("test-messages-pass"); }); async function addLegitEntry() { return vault.addEntry({ type: "login", title: "Example", username: "victim", password: "hunter2", url: "https://example.com", }); } // The assessment's stated criterion: a foreign-origin sender may not mutate // an entry it does not own, even when it knows the id. it("UPDATE_ENTRY from a foreign-origin sender against a known id is refused", async () => { const entry = await addLegitEntry(); const res = await messages.handleMessage( { type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { title: "pwned" } } }, pageSender(FOREIGN), ); expect(res).toMatchObject({ ok: false, error: "origin_binding_required" }); }); // The escalation: a content script on the entry's OWN (legitimate) origin // passes any pre-write origin check and repoints the entry at a third party. // This is the case that made the read path's binding worthless. it("UPDATE_ENTRY from the entry's own origin cannot repoint it at a foreign domain", async () => { const entry = await addLegitEntry(); const res = await messages.handleMessage( { type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { additionalUrls: ["https://attacker.example.net"] } }, }, pageSender(LEGIT), ); expect(res).toMatchObject({ ok: false, error: "origin_binding_required" }); expect(vault.getEntryById(entry.id)?.additionalUrls ?? []).toEqual([]); }); // Control: a non-origin field from the entry's own origin still succeeds. it("UPDATE_ENTRY from the entry's own origin still patches non-origin fields", async () => { const entry = await addLegitEntry(); const res = await messages.handleMessage( { type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { title: "Renamed" } } }, pageSender(LEGIT), ); expect(res).toMatchObject({ ok: true }); expect(vault.getEntryById(entry.id)?.title).toBe("Renamed"); }); // Same escalation through the dedicated additional-url handler. it("ADD_ADDITIONAL_URL cannot attach a third-party origin to the entry", async () => { const entry = await addLegitEntry(); const res = await messages.handleMessage( { type: "ADD_ADDITIONAL_URL", payload: { id: entry.id, url: "https://attacker.example.net" }, }, pageSender(LEGIT), ); expect(res).toMatchObject({ ok: false, error: "origin_binding_required" }); expect(vault.getEntryById(entry.id)?.additionalUrls ?? []).toEqual([]); }); // A page may not grow the set even with the origin it is standing on — that // is precisely what the attacker in the chain below would attach. it("ADD_ADDITIONAL_URL is refused even for the sender's own origin", async () => { const entry = await addLegitEntry(); const res = await messages.handleMessage( { type: "ADD_ADDITIONAL_URL", payload: { id: entry.id, url: "https://new-site.example.org" }, }, pageSender("https://new-site.example.org/checkout"), ); expect(res).toMatchObject({ ok: false, error: "origin_binding_required" }); expect(vault.getEntryById(entry.id)?.additionalUrls ?? []).toEqual([]); }); // The legitimate replacement: the inline "remember this site" toast asks, and // a page may only ever ask about its own attested origin. The write itself // happens from the popup, which is exempt. it("REQUEST_REMEMBER_SITE queues the sender's own origin for the popup to confirm", async () => { const entry = await addLegitEntry(); const res = await messages.handleMessage( { type: "REQUEST_REMEMBER_SITE", payload: { id: entry.id, url: "https://new-site.example.org" }, }, pageSender("https://new-site.example.org/checkout"), ); expect(res).toMatchObject({ ok: true }); expect(chrome.storage.session.set).toHaveBeenCalledWith({ pendingRememberSite: { entryId: entry.id, url: "https://new-site.example.org" }, }); // Nothing was written yet — the popup confirms. expect(vault.getEntryById(entry.id)?.additionalUrls ?? []).toEqual([]); }); it("REQUEST_REMEMBER_SITE cannot queue a third-party origin", async () => { const entry = await addLegitEntry(); const res = await messages.handleMessage( { type: "REQUEST_REMEMBER_SITE", payload: { id: entry.id, url: "https://attacker.example.net" }, }, pageSender(LEGIT), ); expect(res).toMatchObject({ ok: false, error: "origin_binding_required" }); expect(chrome.storage.session.set).not.toHaveBeenCalled(); }); // A page may still shrink the set — clearing a stale mapping needs no popup. it("UPDATE_ENTRY from the entry's own origin may drop an additional url", async () => { const entry = await addLegitEntry(); await vault.updateEntry(entry.id, { additionalUrls: ["https://example.com"] }); const res = await messages.handleMessage( { type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { additionalUrls: [] } }, }, pageSender(LEGIT), ); expect(res).toMatchObject({ ok: true }); expect(vault.getEntryById(entry.id)?.additionalUrls ?? []).toEqual([]); }); it("ADD_ENTRY from a page sender cannot create an entry bound to a foreign site", async () => { const res = await messages.handleMessage( { type: "ADD_ENTRY", payload: { type: "login", title: "Bank", username: "u", password: "attacker-chosen", url: "https://bank.example.org", }, }, pageSender(LEGIT), ); expect(res).toMatchObject({ ok: false, error: "origin_binding_required" }); expect(vault.getEntries()).toHaveLength(0); }); it("ADD_ENTRY from a page sender succeeds for its own origin", async () => { const res = await messages.handleMessage( { type: "ADD_ENTRY", payload: { type: "login", title: "Example", username: "u", password: "p", url: "https://example.com", }, }, pageSender(LEGIT), ); expect(res).toMatchObject({ ok: true }); expect(vault.getEntries()).toHaveLength(1); }); // The popup creates entries for sites it is not standing on. Extension pages // are exempt from the binding. it("ADD_ENTRY from the popup can still create an entry for any site", async () => { const res = await messages.handleMessage( { type: "ADD_ENTRY", payload: { type: "login", title: "Bank", username: "u", password: "p", url: "https://bank.example.org", }, }, extensionSender, ); expect(res).toMatchObject({ ok: true }); expect(vault.getEntries()).toHaveLength(1); }); // End-to-end shape of the reported attack: repoint the entry, then read the // credential from the attacker origin. Both halves must fail. it("the repoint-then-read chain stays closed from the attacker origin", async () => { const entry = await addLegitEntry(); await messages.handleMessage( { type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { additionalUrls: ["https://attacker.example.net"] } }, }, pageSender(FOREIGN), ); const res = await messages.handleMessage( { type: "GET_ENTRY_FOR_FILL", payload: { id: entry.id } }, pageSender(FOREIGN), ); expect(res).toMatchObject({ ok: false, error: "entry_not_found" }); expect(vault.getEntryById(entry.id)?.additionalUrls ?? []).toEqual([]); }); it.each(["ADD_ENTRY", "UPDATE_ENTRY"] as const)("PB-26-944: %s rejects protected own keys even when undefined", async (type) => { const entry = await addLegitEntry(); const before = structuredClone(vault.getEntries()); for (const key of ["id", "notes", "totp", "totpAlgorithm", "totpDigits", "totpPeriod", "passkey", "canary", "passwordHistory", "passwordChangedAt", "breach", "tags", "createdAt", "modifiedAt", "deletedAt", "cardNumber", "unexpected"]) { const fields = type === "ADD_ENTRY" ? { title: "Page draft", url: LEGIT, [key]: undefined } : { [key]: undefined }; const payload = type === "ADD_ENTRY" ? fields : { id: entry.id, patch: fields }; const res = await messages.handleMessage({ type, payload }, pageSender(LEGIT)); expect(res, key).toMatchObject({ ok: false, error: "invalid_payload" }); expect(vault.getEntries(), key).toEqual(before); } }); it.each(["ADD_ENTRY", "UPDATE_ENTRY"] as const)("PB-26-944: %s rejects malformed allowed values without mutation", async (type) => { const entry = await addLegitEntry(); const before = structuredClone(vault.getEntries()); for (const fields of [ { title: 7 }, { username: false }, { email: {} }, { password: ["secret"] }, { url: 42 }, { title: undefined }, { username: undefined }, { password: undefined }, { type: undefined }, { additionalUrls: undefined }, { additionalUrls: "https://example.com" }, { additionalUrls: [LEGIT, 42] }, ]) { const draft = { title: "Page draft", url: LEGIT, ...fields }; const payload = type === "ADD_ENTRY" ? draft : { id: entry.id, patch: fields }; const res = await messages.handleMessage({ type, payload }, pageSender(LEGIT)); expect(res).toMatchObject({ ok: false, error: "invalid_payload" }); expect(vault.getEntries()).toEqual(before); } }); it.each([undefined, "", "example.com", "not a URL", "ftp://example.com/file", "javascript:alert(1)", "https://"])("PB-26-944: ADD_ENTRY requires a valid primary HTTP(S) URL: %s", async (url) => { const res = await messages.handleMessage({ type: "ADD_ENTRY", payload: { title: "Page draft", url, additionalUrls: [LEGIT] }, }, pageSender(LEGIT)); expect(res.ok).toBe(false); expect(vault.getEntries()).toEqual([]); }); it.each(["", "example.com", "ftp://example.com/file", "javascript:alert(1)", 42])("PB-26-944: ADD_ENTRY rejects every invalid additional URL: %s", async (extra) => { const res = await messages.handleMessage({ type: "ADD_ENTRY", payload: { title: "Page draft", url: LEGIT, additionalUrls: [LEGIT, extra] }, }, pageSender(LEGIT)); expect(res.ok).toBe(false); expect(vault.getEntries()).toEqual([]); }); it.each([undefined, "", "example.com", "ftp://example.com/file"])("PB-26-944: UPDATE_ENTRY rejects an invalid supplied primary despite an owned additional URL: %s", async (url) => { const entry = await addLegitEntry(); await vault.updateEntry(entry.id, { additionalUrls: [LEGIT] }); const before = structuredClone(vault.getEntries()); const res = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { url } }, }, pageSender(LEGIT)); expect(res.ok).toBe(false); expect(vault.getEntries()).toEqual(before); }); it("PB-26-944: UPDATE_ENTRY rejects invalid additional URLs without dropping trusted mappings", async () => { const entry = await addLegitEntry(); await vault.updateEntry(entry.id, { additionalUrls: [FOREIGN] }); const before = structuredClone(vault.getEntries()); const sparseUrls = Object.assign(new Array(3), { 0: LEGIT, 2: LEGIT }); for (const additionalUrls of [[LEGIT, "ftp://example.com/path"], [LEGIT, ""], [LEGIT, "example.com"], sparseUrls]) { const res = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { additionalUrls } } }, pageSender(LEGIT)); expect(res.ok).toBe(false); expect(vault.getEntries()).toEqual(before); } }); it.each([ { url: "http://localhost", additionalUrls: [LEGIT] }, { url: LEGIT, additionalUrls: ["http://localhost"] }, ])("PB-26-944: ADD_ENTRY rejects every URL without a project domain: %j", async (urls) => { const res = await messages.handleMessage({ type: "ADD_ENTRY", payload: { title: "Unbound URL", ...urls } }, pageSender(LEGIT)); expect(res).toMatchObject({ ok: false, error: "origin_binding_required" }); expect(vault.getEntries()).toHaveLength(0); }); it.each([ { url: "http://localhost" }, { additionalUrls: ["http://localhost", LEGIT] }, ])("PB-26-944: UPDATE_ENTRY rejects supplied URLs without a project domain: %j", async (patch) => { const entry = await addLegitEntry(); await vault.updateEntry(entry.id, { additionalUrls: [LEGIT] }); const before = structuredClone(vault.getEntries()); const res = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch } }, pageSender(LEGIT)); expect(res).toMatchObject({ ok: false, error: "origin_binding_required" }); expect(vault.getEntries()).toEqual(before); }); it("PB-26-944: non-URL edits preserve untouched legacy mappings without a project domain", async () => { const entry = await addLegitEntry(); await vault.updateEntry(entry.id, { additionalUrls: ["http://localhost"] }); const res = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { title: "Legacy mappings" } } }, pageSender(LEGIT)); expect(res.ok).toBe(true); expect(vault.getEntryById(entry.id)?.additionalUrls).toEqual(["http://localhost"]); }); it("PB-26-944: content creation canonicalizes valid URL spelling before binding and storage", async () => { const res = await messages.handleMessage({ type: "ADD_ENTRY", payload: { title: "Canonical URL", url: " HTTPS://EXAMPLE.COM/login ", additionalUrls: ["HTTP://LOGIN.EXAMPLE.COM/path"], } }, pageSender(LEGIT)); expect(res.ok).toBe(true); expect(vault.getEntries()[0]).toMatchObject({ url: LEGIT, additionalUrls: ["http://login.example.com/path"] }); }); it("PB-26-944: origin-only content saves retain legacy duplicate detection", async () => { await addLegitEntry(); const saved = await messages.handleMessage({ type: "ADD_ENTRY", payload: { title: "Example", username: "victim", password: "hunter2", url: "HTTPS://EXAMPLE.COM", } }, pageSender(LEGIT)); expect(saved.ok).toBe(true); expect(vault.getEntries()[1]?.url).toBe("https://example.com"); expect(await messages.handleMessage({ type: "COUNT_DUPLICATES" }, extensionSender)) .toMatchObject({ ok: true, data: 1 }); }); it.each(["example.com", ""])("PB-26-944: edits preserve an untouched legacy primary URL: %j", async (url) => { const entry = await vault.addEntry({ type: "login", title: "Legacy", url, additionalUrls: [LEGIT], password: "old-password" }); const updated = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { password: "new-password" }, } }, pageSender(LEGIT)); expect(updated.ok).toBe(true); expect(vault.getEntryById(entry.id)).toMatchObject({ url, password: "new-password", additionalUrls: [LEGIT] }); }); it.each([ ["https://example.com/", "https://example.com/"], ["HTTPS://synthetic:secret@EXAMPLE.COM", "https://synthetic:secret@example.com"], ["https://example.com?", "https://example.com/?"], ["https://example.com#", "https://example.com/#"], ])("PB-26-944: URL normalization preserves supplied components: %s", async (url, expected) => { const saved = await messages.handleMessage({ type: "ADD_ENTRY", payload: { title: "URL components", url } }, pageSender(LEGIT)); expect(saved.ok).toBe(true); expect(vault.getEntries()[0]?.url).toBe(expected); }); it("PB-26-944: content updates canonicalize supplied URLs but preserve untouched mappings", async () => { const entry = await addLegitEntry(); await vault.updateEntry(entry.id, { additionalUrls: [FOREIGN] }); const res = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { url: " HTTPS://EXAMPLE.COM/login " } } }, pageSender(LEGIT)); expect(res.ok).toBe(true); expect(vault.getEntryById(entry.id)).toMatchObject({ url: LEGIT, additionalUrls: [FOREIGN] }); }); it("PB-26-944: popup URL writes remain unchanged by the content-only schema", async () => { const res = await messages.handleMessage({ type: "ADD_ENTRY", payload: { title: "Local service", url: "http://localhost" } }, extensionSender); expect(res.ok).toBe(true); expect(vault.getEntries()[0]?.url).toBe("http://localhost"); }); it.each(["note", "passkey", "card"] as const)("PB-26-944: content cannot create or edit a %s entry", async (type) => { const res = await messages.handleMessage({ type: "ADD_ENTRY", payload: { type, title: "Not a login", url: LEGIT } }, pageSender(LEGIT)); expect(res).toMatchObject({ ok: false, error: "invalid_payload" }); expect(vault.getEntries()).toHaveLength(0); const entry = await vault.addEntry({ type, title: "Privileged entry", url: LEGIT, ...(type === "card" ? { cardholderName: "Synthetic User", cardNumber: "4111111111111111" } : {}) }); const before = structuredClone(vault.getEntries()); const update = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { title: "Page edit" } } }, pageSender(LEGIT)); expect(update).toMatchObject({ ok: false, error: "login_entries_only" }); expect(vault.getEntries()).toEqual(before); }); it("PB-26-944: UPDATE_ENTRY cannot change even a login entry's type", async () => { const entry = await addLegitEntry(); const before = structuredClone(vault.getEntries()); const res = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { type: "login" } } }, pageSender(LEGIT)); expect(res).toMatchObject({ ok: false, error: "invalid_payload" }); expect(vault.getEntries()).toEqual(before); }); it("PB-26-944: valid content saves default to login and preserve optional email clearing", async () => { const res = await messages.handleMessage({ type: "ADD_ENTRY", payload: { title: "Page draft", url: LEGIT, additionalUrls: ["http://login.example.com/path"], username: "alice", email: undefined, password: "new-password" }, }, pageSender(LEGIT)); expect(res.ok).toBe(true); const entry = vault.getEntries()[0]!; expect(entry.type).toBe("login"); await vault.updateEntry(entry.id, { email: "old@example.com" }); const update = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { email: undefined, username: "updated", password: "updated-password" } } }, pageSender(LEGIT)); expect(update.ok).toBe(true); expect(vault.getEntryById(entry.id)).toMatchObject({ username: "updated", password: "updated-password" }); expect(vault.getEntryById(entry.id)?.email).toBeUndefined(); }); it("PB-26-944: title edits retain trusted foreign mappings and may remove them", async () => { const entry = await addLegitEntry(); await vault.updateEntry(entry.id, { additionalUrls: [FOREIGN] }); const rename = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { title: "Renamed" } } }, pageSender(LEGIT)); expect(rename.ok).toBe(true); expect(vault.getEntryById(entry.id)?.additionalUrls).toEqual([FOREIGN]); const shrink = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { additionalUrls: [] } } }, pageSender(LEGIT)); expect(shrink.ok).toBe(true); expect(vault.getEntryById(entry.id)?.additionalUrls ?? []).toEqual([]); }); it("PB-26-944: legacy entries with an omitted type remain login-editable", async () => { const entry = await addLegitEntry(); delete entry.type; expect(vault.getEntryById(entry.id)?.type).toBeUndefined(); const res = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { title: "Legacy login" } } }, pageSender(LEGIT)); expect(res.ok).toBe(true); expect(vault.getEntryById(entry.id)?.title).toBe("Legacy login"); }); it("PB-26-944: popup writes retain protected fields and URL-less entries", async () => { const res = await messages.handleMessage({ type: "ADD_ENTRY", payload: { type: "note", title: "Private note", notes: "privileged notes", totp: "JBSWY3DPEHPK3PXP" } }, extensionSender); expect(res.ok).toBe(true); const entry = vault.getEntries()[0]!; const update = await messages.handleMessage({ type: "UPDATE_ENTRY", payload: { id: entry.id, patch: { notes: "updated notes" } } }, extensionSender); expect(update.ok).toBe(true); expect(vault.getEntryById(entry.id)?.notes).toBe("updated notes"); }); });