| 1 | import assert from 'node:assert/strict'; |
| 2 | import test from 'node:test'; |
| 3 | import { browserCookies } from './browser-cookies.mjs'; |
| 4 | import { BrowserVault } from './browser-vault.mjs'; |
| 5 | |
| 6 | const tab = { targetId: 'tab', url: 'https://sub.example.test/page', browserContextId: 'private-context', incognito: true }; |
| 7 | const cookie = (domain, extra = {}) => ({ name: 'session', value: 'fixture-secret', domain, path: '/', expires: -1, session: true, ...extra }); |
| 8 | |
| 9 | test('site scope includes parent domain cookies and excludes host-only parent and lookalikes', async () => { |
| 10 | const calls = []; |
| 11 | const cdp = async (method, params) => { |
| 12 | calls.push({ method, params }); |
| 13 | return { cookies: [cookie('.example.test'), cookie('example.test'), cookie('sub.example.test'), cookie('.notexample.test'), cookie('other.example.test')] }; |
| 14 | }; |
| 15 | const result = await browserCookies(cdp, async () => 'sid', tab, { mode: 'list', scope: 'site' }); |
| 16 | assert.deepEqual(result.cookies.map(cookie => cookie.domain), ['.example.test', 'sub.example.test']); |
| 17 | assert.equal(result.incognito, true); |
| 18 | assert.ok(result.cookies.every(cookie => !('value' in cookie))); |
| 19 | assert.deepEqual(calls, [{ method: 'Storage.getCookies', params: { browserContextId: 'private-context' } }]); |
| 20 | }); |
| 21 | |
| 22 | test('opaque partitions refuse site-wide and individual deletion before any mutation', async () => { |
| 23 | const calls = []; |
| 24 | const cdp = async method => { calls.push(method); return { cookies: [cookie('sub.example.test'), cookie('sub.example.test', { partitionKeyOpaque: true })] }; }; |
| 25 | const data = await browserCookies(cdp, async () => 'sid', tab, { mode: 'list', scope: 'site' }); |
| 26 | await assert.rejects(browserCookies(cdp, async () => 'sid', tab, { mode: 'delete', scope: 'site', cookieKey: data.cookies.find(cookie => cookie.partitionKeyOpaque).key }), /分区/); |
| 27 | await assert.rejects(browserCookies(cdp, async () => 'sid', tab, { mode: 'clear', scope: 'site' }), /分区/); |
| 28 | assert.ok(calls.every(method => method === 'Storage.getCookies')); |
| 29 | }); |
| 30 | |
| 31 | test('stale page or authorization tabs cannot reach cookie storage', async () => { |
| 32 | const cdp = () => { throw new Error('must not contact cookie storage'); }; |
| 33 | await assert.rejects(browserCookies(cdp, null, tab, { mode: 'clear', scope: 'all', url: 'https://previous.test/' }), /网页已切换/); |
| 34 | await assert.rejects(browserCookies(cdp, null, { ...tab, login: true }, { mode: 'list', scope: 'all' }), /不可管理/); |
| 35 | }); |
| 36 | |
| 37 | test('vault never attaches credential observers to private or authorization tabs', async () => { |
| 38 | for (const mode of ['incognito', 'login']) { |
| 39 | const vault = new BrowserVault({ cdp: () => { throw new Error('must not install credential observer'); }, active: () => 'tab', tab: () => ({ [mode]: true }) }); |
| 40 | await vault.attach('tab', 'sid'); |
| 41 | assert.equal(vault.sessions.size, 0); |
| 42 | await vault.request({ action: 'status', lease: 'f'.repeat(64), tab: 'tab' }); |
| 43 | await assert.rejects(vault.request({ action: 'fill', lease: 'f'.repeat(64), tab: 'tab' }), /网页已切换/); |
| 44 | } |
| 45 | }); |