| 1 | import assert from "node:assert/strict"; |
| 2 | import { createPrivacyController } from "./browser-privacy-fixture.mjs"; |
| 3 | |
| 4 | const fixture = await createPrivacyController(); |
| 5 | try { |
| 6 | const { api, evaluate, waitFor, origin, restart } = fixture; |
| 7 | async function check(id) { |
| 8 | await waitFor(() => evaluate(id, "document.readyState === 'complete'"), "page loaded"); |
| 9 | assert.deepEqual(await evaluate(id, "[navigator.webdriver, globalThis.webdriverAtLoad]"), [false, false]); |
| 10 | assert.equal(await evaluate(id, "Object.getOwnPropertyDescriptor(Navigator.prototype, 'webdriver').get.toString()"), |
| 11 | "function get webdriver() { [native code] }", "browser identity must retain Chromium's native getter"); |
| 12 | } |
| 13 | await check((await api("/tabs")).active); |
| 14 | for (const incognito of [false, true]) { |
| 15 | const tab = await api("/new", { url: origin + "/identity", incognito }); |
| 16 | await check(tab.id); |
| 17 | const frameIdentity = await evaluate(tab.id, `new Promise(resolve => { |
| 18 | const frame = document.createElement('iframe'); |
| 19 | const received = event => { |
| 20 | if (event.source !== frame.contentWindow || !('fixtureIdentity' in (event.data || {}))) return; |
| 21 | removeEventListener('message', received); frame.remove(); resolve([event.data.fixtureIdentity, event.data.fixtureGetter]); |
| 22 | }; |
| 23 | addEventListener('message', received); |
| 24 | frame.src = ${JSON.stringify(origin.replace("localhost", "127.0.0.1") + "/frame")}; |
| 25 | document.body.append(frame); |
| 26 | })`); |
| 27 | assert.deepEqual(frameIdentity, [false, "function get webdriver() { [native code] }"], "cross-site frame must retain the native browser identity"); |
| 28 | await fixture.cdp(tab.id, "Runtime.evaluate", { |
| 29 | expression: `window.open(${JSON.stringify(origin + "/popup")}, '_blank'); true`, |
| 30 | userGesture: true, |
| 31 | }); |
| 32 | const popup = await waitFor(async () => (await api("/tabs")).tabs.find(item => item.url === origin + "/popup"), "popup loaded"); |
| 33 | await check(popup.id); |
| 34 | await api("/close", { id: popup.id }); |
| 35 | await api("/navigate", { id: tab.id, url: origin + "/next" }); |
| 36 | await check(tab.id); |
| 37 | await api("/close", { id: tab.id }); |
| 38 | } |
| 39 | await restart(); |
| 40 | await check((await api("/tabs")).active); |
| 41 | const beforeAudit = (await api("/tabs")).tabs.map(tab => tab.id); |
| 42 | const audit = await api("/audit", { timezone: "UTC", network: false }); |
| 43 | assert.ok(audit.language && audit.timezone); |
| 44 | assert.deepEqual((await api("/tabs")).tabs.map(tab => tab.id), beforeAudit, "settings audit must not retain a diagnostic tab"); |
| 45 | console.log("PASS: first page script, normal/private navigation, popups, cross-site frames and profile restart retain native browser identity"); |
| 46 | } finally { await fixture.close(); } |