| 1 | import assert from 'node:assert/strict'; |
| 2 | import { readFile } from 'node:fs/promises'; |
| 3 | import { join } from 'node:path'; |
| 4 | import { createPrivacyController } from './browser-privacy-fixture.mjs'; |
| 5 | |
| 6 | const fixture = await createPrivacyController(); |
| 7 | const { api, cdp, evaluate, waitFor, origin, directory } = fixture; |
| 8 | const list = (id, extra = {}) => api('/cookies', { id, mode: 'list', scope: 'all', ...extra }); |
| 9 | const checks = []; |
| 10 | try { |
| 11 | const normal = (await api('/tabs')).active; |
| 12 | const ready = id => waitFor(() => evaluate(id, 'document.readyState === "complete"'), 'page loaded'); |
| 13 | await ready(normal); |
| 14 | await cdp(normal, 'Network.setCookie', { name: 'normal', value: 'normal-value', url: origin, expires: Date.now() / 1000 + 3600 }); |
| 15 | assert.equal((await list(normal)).cookies[0].name, 'normal'); |
| 16 | const restored = await readFile(join(directory, 'workbench-state.json'), 'utf8'); |
| 17 | const first = await api('/new', { url: origin + '/private', incognito: true }); |
| 18 | await ready(first.id); |
| 19 | assert.equal((await api('/tabs')).tabs.find(tab => tab.id === first.id).incognito, true); |
| 20 | assert.equal((await list(first.id)).total, 0); |
| 21 | assert.equal(await evaluate(first.id, 'document.cookie'), ''); |
| 22 | await evaluate(first.id, 'localStorage.setItem("private-only", "temporary"); document.cookie = "private=private-value;path=/"'); |
| 23 | assert.equal(await readFile(join(directory, 'workbench-state.json'), 'utf8'), restored); |
| 24 | const second = await api('/new', { url: origin + '/private-second', incognito: true }); |
| 25 | await ready(second.id); |
| 26 | assert.equal(await evaluate(second.id, 'localStorage.getItem("private-only")'), 'temporary'); |
| 27 | assert.match(await evaluate(second.id, 'document.cookie'), /private=private-value/); |
| 28 | checks.push('private cookies and localStorage isolated from normal tabs, shared by private tabs; restore URL unchanged'); |
| 29 | |
| 30 | const privateCookie = (await list(first.id)).cookies.find(cookie => cookie.name === 'private'); |
| 31 | assert.equal('value' in privateCookie, false); |
| 32 | assert.equal((await api('/cookies', { id: first.id, mode: 'details', scope: 'all', cookieKey: privateCookie.key })).value, 'private-value'); |
| 33 | await api('/cookies', { id: second.id, mode: 'clear', scope: 'all' }); |
| 34 | assert.equal((await list(first.id)).total, 0); |
| 35 | assert.equal((await list(normal)).total, 1); |
| 36 | checks.push('values are returned only on demand; clearing private cookies preserves normal login'); |
| 37 | |
| 38 | await api('/close', { id: first.id }); |
| 39 | assert.equal(await evaluate(second.id, 'localStorage.getItem("private-only")'), 'temporary'); |
| 40 | const popup = await cdp(second.id, 'Target.createTarget', { |
| 41 | url: origin + '/popup', browserContextId: (await cdp(second.id, 'Target.getTargetInfo')).targetInfo.browserContextId, |
| 42 | }); |
| 43 | await waitFor(async () => (await api('/tabs')).tabs.find(tab => tab.id === popup.targetId)?.incognito, 'private popup classification'); |
| 44 | await api('/close', { id: second.id }); |
| 45 | await ready(popup.targetId); |
| 46 | assert.equal(await evaluate(popup.targetId, 'localStorage.getItem("private-only")'), 'temporary'); |
| 47 | await api('/close', { id: popup.targetId }); |
| 48 | const third = await api('/new', { url: origin + '/private-fresh', incognito: true }); |
| 49 | await ready(third.id); |
| 50 | assert.equal(await evaluate(third.id, 'localStorage.getItem("private-only")'), null); |
| 51 | assert.equal((await list(third.id)).total, 0); |
| 52 | const vault = await api('/vault', { action: 'status', tab: third.id, lease: 'f'.repeat(64) }); |
| 53 | assert.equal(vault.origin, ''); assert.deepEqual(vault.candidates, []); |
| 54 | checks.push('private popups stay private; closing final private tab clears storage; private pages do not enter vault'); |
| 55 | |
| 56 | |
| 57 | await cdp(normal, 'Network.setCookies', { cookies: [ |
| 58 | { name: 'same', value: 'root', domain: 'localhost', path: '/' }, |
| 59 | { name: 'same', value: 'nested', domain: 'localhost', path: '/nested' }, |
| 60 | { name: 'unrelated', value: 'preserve', domain: 'other.test', path: '/' }, |
| 61 | { name: 'partition', value: 'plain', domain: 'localhost', path: '/', secure: true }, |
| 62 | { name: 'partition', value: 'partitioned', domain: 'localhost', path: '/', secure: true, partitionKey: { topLevelSite: 'https://example.test', hasCrossSiteAncestor: true } }, |
| 63 | ] }); |
| 64 | let cookies = await list(normal, { scope: 'site' }); |
| 65 | assert.ok(cookies.cookies.every(cookie => cookie.domain === 'localhost')); |
| 66 | const nested = cookies.cookies.find(cookie => cookie.name === 'same' && cookie.path === '/nested'); |
| 67 | await api('/cookies', { id: normal, mode: 'delete', scope: 'site', cookieKey: nested.key }); |
| 68 | cookies = await list(normal); |
| 69 | assert.equal(cookies.cookies.filter(cookie => cookie.name === 'same').length, 1); |
| 70 | const partitioned = cookies.cookies.find(cookie => cookie.partitionKey); |
| 71 | assert.ok(partitioned, 'real partitioned fixture stored'); |
| 72 | await api('/cookies', { id: normal, mode: 'delete', scope: 'all', cookieKey: partitioned.key }); |
| 73 | assert.equal((await list(normal)).cookies.filter(cookie => cookie.name === 'partition').length, 1); |
| 74 | checks.push('deletion respects path and partition key; site filtering excludes unrelated hosts'); |
| 75 | |
| 76 | await cdp(normal, 'Network.setCookies', { cookies: Array.from({ length: 57 }, (_, i) => ({ name: 'paged-' + i, value: String(i), domain: 'localhost', path: '/' })) }); |
| 77 | cookies = await list(normal, { query: 'paged-', page: 1 }); |
| 78 | assert.equal(cookies.total, 57); assert.equal(cookies.cookies.length, 7); |
| 79 | await api('/cookies', { id: normal, mode: 'clear', scope: 'site', query: 'paged-' }); |
| 80 | cookies = await list(normal); |
| 81 | assert.equal(cookies.total, 1); assert.equal(cookies.cookies[0].name, 'unrelated'); |
| 82 | checks.push('search and pagination; clearing a site ignores search and preserves other hosts'); |
| 83 | |
| 84 | await cdp(normal, 'Network.setCookie', { name: 'persistent', value: 'retained', url: origin, expires: Date.now() / 1000 + 3600 }); |
| 85 | await assert.rejects(api('/cookies', { id: normal, url: origin + '/stale-page', mode: 'clear', scope: 'all' }), /网页已切换/); |
| 86 | assert.ok((await list(normal)).cookies.some(cookie => cookie.name === 'persistent')); |
| 87 | await api('/activate', { id: normal }); |
| 88 | await api('/activate', { id: third.id }); |
| 89 | await fixture.restart(); |
| 90 | const afterRestart = await api('/tabs'); |
| 91 | assert.ok(afterRestart.tabs.every(tab => !tab.incognito && !tab.url.includes('/private'))); |
| 92 | assert.ok((await list(afterRestart.active)).cookies.some(cookie => cookie.name === 'persistent')); |
| 93 | checks.push('controller restart restores normal state and persistent cookies, never private pages'); |
| 94 | const lastPrivate = await api('/new', { url: origin + '/private-last', incognito: true }); |
| 95 | for (const tab of (await api('/tabs')).tabs.filter(tab => !tab.incognito)) await api('/close', { id: tab.id }); |
| 96 | await api('/close', { id: lastPrivate.id }); |
| 97 | const last = await api('/tabs'); |
| 98 | assert.equal(last.tabs.length, 0); assert.equal(last.active, ''); |
| 99 | checks.push('closing the only remaining private page leaves the browser empty'); |
| 100 | console.log(JSON.stringify({ passed: true, checks }, null, 2)); |
| 101 | } finally { await fixture.close(); } |