Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import * as fs from 'node:fs'; |
| 2 | import * as os from 'node:os'; |
| 3 | import * as path from 'node:path'; |
| 4 | import { describe, expect, it } from '@effect/vitest'; |
| 5 | import { onTestFinished } from 'vitest'; |
| 6 | import { |
| 7 | extractZipSafely, |
| 8 | isSymlinkZipEntry, |
| 9 | UnsafeZipEntryError, |
| 10 | } from 'src/utils/extract-zip-safely'; |
| 11 | |
| 12 | const FIXTURES_DIR = path.join(__dirname, 'zip-fixtures'); |
| 13 | |
| 14 | /** A fresh extraction target, removed once the current test finishes. */ |
| 15 | const makeTempDir = (): string => { |
| 16 | const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'extract-zip-safely-')); |
| 17 | onTestFinished(() => fs.rmSync(dir, { recursive: true, force: true })); |
| 18 | return dir; |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * `mode << 16` is how a zip stores the Unix mode in its external attributes. |
| 23 | */ |
| 24 | const externalAttributesFor = (mode: number): number => mode << 16; |
| 25 | |
| 26 | describe('isSymlinkZipEntry', () => { |
| 27 | it('detects a symbolic link', () => { |
| 28 | expect(isSymlinkZipEntry(externalAttributesFor(0o120755))).toBe(true); |
| 29 | }); |
| 30 | |
| 31 | it('accepts a regular file', () => { |
| 32 | expect(isSymlinkZipEntry(externalAttributesFor(0o100644))).toBe(false); |
| 33 | }); |
| 34 | |
| 35 | it('accepts a directory', () => { |
| 36 | expect(isSymlinkZipEntry(externalAttributesFor(0o40755))).toBe(false); |
| 37 | }); |
| 38 | |
| 39 | it('accepts a Windows-authored entry that carries no Unix mode', () => { |
| 40 | expect(isSymlinkZipEntry(0)).toBe(false); |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | describe('extractZipSafely', () => { |
| 45 | it('extracts an archive of regular files and directories', async () => { |
| 46 | const dir = makeTempDir(); |
| 47 | |
| 48 | await extractZipSafely(path.join(FIXTURES_DIR, 'benign.zip'), dir); |
| 49 | |
| 50 | expect(fs.readFileSync(path.join(dir, 'a.txt'), 'utf8').trim()).toBe('hello'); |
| 51 | expect(fs.readFileSync(path.join(dir, 'sub', 'b.txt'), 'utf8').trim()).toBe('world'); |
| 52 | }); |
| 53 | |
| 54 | it.each([ |
| 55 | ['symlink-absolute.zip', 'escape-abs'], |
| 56 | ['symlink-relative.zip', 'escape-rel'], |
| 57 | ])('refuses %s and plants nothing', async (fixture, entryName) => { |
| 58 | const dir = makeTempDir(); |
| 59 | |
| 60 | await expect(extractZipSafely(path.join(FIXTURES_DIR, fixture), dir)).rejects.toThrow( |
| 61 | UnsafeZipEntryError |
| 62 | ); |
| 63 | |
| 64 | // The guard runs before the entry is written, so the target stays empty. |
| 65 | expect(fs.readdirSync(dir)).toEqual([]); |
| 66 | expect(fs.existsSync(path.join(dir, entryName))).toBe(false); |
| 67 | }); |
| 68 | }); |