| 1 | |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { createRequire } from "node:module"; |
| 4 | import { BrowserVault } from "../internal/cage/browser-vault.mjs"; |
| 5 | const require = createRequire(import.meta.url); |
| 6 | const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 7 | const browser = await chromium.launch({ headless: true }); |
| 8 | const context = await browser.newContext(); |
| 9 | const page = await context.newPage(); |
| 10 | const html = `<form onsubmit="event.preventDefault()"><label>Email<input name="email" type="email" autocomplete="username"></label><label>Password<input name="pass" type="password" autocomplete="current-password"></label><button>Sign in</button></form>`; |
| 11 | await context.route("**/*", route => { |
| 12 | const pathname = new URL(route.request().url()).pathname; |
| 13 | const body = pathname === "/username-step" ? `<form action="/password-step"><label>Email<input type="email" name="email" autocomplete="username"></label><button>Next</button></form>` |
| 14 | : pathname === "/password-step" ? `<form onsubmit="event.preventDefault()"><label>Password<input type="password" autocomplete="current-password"></label><button>Sign in</button></form>` |
| 15 | : pathname === "/framed" ? `<iframe src="https://frame.example.test/login"></iframe>` : html; |
| 16 | return route.fulfill({ contentType: "text/html", body }); |
| 17 | }); |
| 18 | await page.goto("https://mail.example.test/login"); |
| 19 | const session = await context.newCDPSession(page); |
| 20 | const vault = new BrowserVault({ cdp: (method, args) => session.send(method, args), active: () => "tab", tab: () => ({}) }); |
| 21 | for (const method of ["Runtime.executionContextCreated", "Runtime.executionContextDestroyed", "Runtime.executionContextsCleared", "Runtime.bindingCalled", "Page.frameNavigated"]) |
| 22 | session.on(method, params => vault.event({ method, params, sessionId: "session" })); |
| 23 | await session.send("Page.enable"); |
| 24 | await vault.attach("tab", "session"); |
| 25 | const lease = "a".repeat(64); |
| 26 | const request = value => vault.request({ lease, tab: "tab", ...value }); |
| 27 | const status = () => request({ action: "status" }); |
| 28 | const waitCandidate = async () => { |
| 29 | await assert.doesNotReject(async () => { |
| 30 | for (let i = 0; i < 30 && !vault.pending.size; i++) await new Promise(resolve => setTimeout(resolve, 20)); |
| 31 | assert.ok(vault.pending.size, "trusted submission produced no candidate"); |
| 32 | }); |
| 33 | return (await status()).candidates[0]; |
| 34 | }; |
| 35 | try { |
| 36 | let state = await status(); |
| 37 | assert.equal(state.origin, "https://mail.example.test"); |
| 38 | assert.ok(state.document); |
| 39 | assert.equal(await page.evaluate(() => typeof globalThis.cageVaultSubmitted), "undefined", "page gained vault binding"); |
| 40 | await page.getByLabel("Email").fill("[email protected]"); |
| 41 | await page.getByLabel("Password").fill("first-secret"); |
| 42 | await page.evaluate(() => document.querySelector("form").dispatchEvent(new Event("submit", { bubbles: true }))); |
| 43 | assert.equal(vault.pending.size, 0, "synthetic submission was captured"); |
| 44 | await page.getByRole("button", { name: "Sign in" }).click(); |
| 45 | let candidate = await waitCandidate(); |
| 46 | assert.equal(candidate.password, "first-secret"); |
| 47 | assert.equal(candidate.username, "[email protected]"); |
| 48 | await request({ action: "dismiss", candidate: candidate.id }); |
| 49 | await page.getByLabel("Email").fill(""); |
| 50 | await page.getByLabel("Password").fill(""); |
| 51 | await request({ action: "fill", document: state.document, origin: state.origin, username: "[email protected]", password: "second-secret" }); |
| 52 | assert.equal(await page.getByLabel("Email").inputValue(), "[email protected]"); |
| 53 | assert.equal(await page.getByLabel("Password").inputValue(), "second-secret"); |
| 54 | await assert.rejects(request({ action: "fill", document: state.document, origin: "https://evil.test", username: "bad", password: "wrong" })); |
| 55 | assert.equal(await page.getByLabel("Password").inputValue(), "second-secret"); |
| 56 | await page.goto("https://mail.example.test/other"); |
| 57 | await assert.rejects(request({ action: "fill", document: state.document, origin: state.origin, username: "bad", password: "wrong" })); |
| 58 | state = await status(); |
| 59 | await page.getByLabel("Email").fill("[email protected]"); |
| 60 | await page.getByLabel("Password").fill("updated-secret"); |
| 61 | await page.getByRole("button", { name: "Sign in" }).click(); |
| 62 | candidate = await waitCandidate(); |
| 63 | assert.equal(candidate.password, "updated-secret"); |
| 64 | await request({ action: "lock" }); |
| 65 | assert.equal(vault.pending.size, 0); |
| 66 | await assert.rejects(request({ action: "candidate", candidate: candidate.id })); |
| 67 | await page.getByRole("button", { name: "Sign in" }).click(); |
| 68 | assert.equal(vault.pending.size, 0); |
| 69 | const freshLease = "b".repeat(64); |
| 70 | await vault.request({ action: "status", lease: freshLease, tab: "tab" }); |
| 71 | assert.equal(vault.pending.size, 0); |
| 72 | await vault.request({ action: "lock", lease }); |
| 73 | assert.equal(vault.lease, freshLease, "late lock cleared new epoch"); |
| 74 | await page.goto("http://plain.example.test/login"); |
| 75 | state = await vault.request({ action: "status", lease: freshLease, tab: "tab" }); |
| 76 | assert.equal(state.origin, ""); |
| 77 | await page.getByLabel("Password").fill("must-not-capture"); |
| 78 | await page.getByRole("button", { name: "Sign in" }).click(); |
| 79 | assert.equal(vault.pending.size, 0); |
| 80 | await page.goto("https://mail.example.test/login"); |
| 81 | await vault.request({ action: "status", lease: freshLease, tab: "tab", blocked: ["https://mail.example.test"] }); |
| 82 | await page.getByLabel("Password").fill("ignored-secret"); |
| 83 | await page.getByRole("button", { name: "Sign in" }).click(); |
| 84 | assert.equal(vault.pending.size, 0); |
| 85 | await page.goto("https://mail.example.test/username-step"); |
| 86 | await vault.request({ action: "status", lease: freshLease, tab: "tab", blocked: [] }); |
| 87 | await page.getByLabel("Email").fill("[email protected]"); |
| 88 | await page.getByRole("button", { name: "Next", exact: true }).click(); |
| 89 | await page.waitForURL("**/password-step?*"); |
| 90 | await vault.request({ action: "status", lease: freshLease, tab: "tab" }); |
| 91 | await page.getByLabel("Password").fill("step-secret"); |
| 92 | await page.getByRole("button", { name: "Sign in" }).click(); |
| 93 | for (let i = 0; i < 30 && !vault.pending.size; i++) await new Promise(resolve => setTimeout(resolve, 20)); |
| 94 | assert.equal([...vault.pending.values()][0]?.username, "[email protected]", "username lost across same-origin steps"); |
| 95 | vault.deadline = 0; vault.prune(); |
| 96 | assert.equal(vault.pending.size, 0, "expired lease retained password"); |
| 97 | assert.equal(vault.usernames.size, 0); |
| 98 | await page.goto("https://mail.example.test/framed"); |
| 99 | await vault.request({ action: "status", lease: freshLease, tab: "tab" }); |
| 100 | await page.frameLocator("iframe").getByLabel("Password").fill("frame-secret"); |
| 101 | await page.frameLocator("iframe").getByRole("button", { name: "Sign in" }).click(); |
| 102 | assert.equal(vault.pending.size, 0, "iframe credential was captured"); |
| 103 | console.log("PASS: real Chromium capture, isolated binding, synthetic rejection, fill, exact origin, stale navigation, lock, lease, HTTP, blocked sites, multi-step login, lease expiry and iframe exclusion"); |
| 104 | } finally { |
| 105 | clearInterval(vault.timer); |
| 106 | await browser.close(); |
| 107 | } |