| 1 | import assert from 'node:assert/strict'; |
| 2 | import test from 'node:test'; |
| 3 | import { execFileSync } from 'node:child_process'; |
| 4 | import { mkdtemp, access, rm } from 'node:fs/promises'; |
| 5 | import { join } from 'node:path'; |
| 6 | import { tmpdir } from 'node:os'; |
| 7 | import { Readable } from 'node:stream'; |
| 8 | import { BrowserExtensions } from './browser-extensions.mjs'; |
| 9 | |
| 10 | for (const kind of ['traversal', 'symlink', 'legacy']) test(`extension ZIP refuses ${kind} before installation`, async () => { |
| 11 | const root = await mkdtemp(join(tmpdir(), 'cage-extension-archive-')); |
| 12 | try { |
| 13 | const bytes = execFileSync('uv', ['run', '--no-project', 'python', '-c', `import io,json,stat,sys,zipfile |
| 14 | b=io.BytesIO() |
| 15 | with zipfile.ZipFile(b,'w') as z: |
| 16 | z.writestr('manifest.json',json.dumps({'manifest_version':2 if sys.argv[1]=='legacy' else 3,'name':'Fixture','version':'1.0'})) |
| 17 | if sys.argv[1]=='traversal':z.writestr('../outside','blocked') |
| 18 | if sys.argv[1]=='symlink': |
| 19 | link=zipfile.ZipInfo('link');link.create_system=3;link.external_attr=(stat.S_IFLNK|0o777)<<16;z.writestr(link,'/etc/passwd') |
| 20 | sys.stdout.buffer.write(b.getvalue())`, kind], { stdio: ['ignore', 'pipe', 'ignore'] }); |
| 21 | const manager = new BrowserExtensions({ directory: root, cdp: () => { throw new Error('must not install rejected ZIP'); }, control: null, protectedID: 'a'.repeat(32) }); |
| 22 | await assert.rejects(manager.upload(Readable.from([bytes])), /路径|符号链接|Manifest V3/); |
| 23 | assert.equal(manager.pending.size, 0); |
| 24 | await assert.rejects(access(join(root, 'extensions/staging/outside'))); |
| 25 | } finally { await rm(root, { recursive: true, force: true }); } |
| 26 | }); |