跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

browser-cookies.test.mjs

45 行 · 3.0 KB历史下载
1import assert from 'node:assert/strict';
2import test from 'node:test';
3import { browserCookies } from './browser-cookies.mjs';
4import { BrowserVault } from './browser-vault.mjs';
5
6const tab = { targetId: 'tab', url: 'https://sub.example.test/page', browserContextId: 'private-context', incognito: true };
7const cookie = (domain, extra = {}) => ({ name: 'session', value: 'fixture-secret', domain, path: '/', expires: -1, session: true, ...extra });
8
9test('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
22test('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
31test('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
37test('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});