|
| 1 | +import fs from 'fs'; |
| 2 | +import os from 'os'; |
| 3 | +import path from 'path'; |
| 4 | +import zlib from 'zlib'; |
| 5 | +import unzip from '../../src/unzip.js'; |
| 6 | + |
| 7 | +// minimal crc32 used to craft valid zip fixtures (zlib.crc32 requires node 20+) |
| 8 | +function crc32(buf) { |
| 9 | + let crc = ~0; |
| 10 | + |
| 11 | + for (let byte of buf) { |
| 12 | + crc ^= byte; |
| 13 | + for (let i = 0; i < 8; i++) { |
| 14 | + crc = (crc >>> 1) ^ (0xEDB88320 & -(crc & 1)); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + return ~crc >>> 0; |
| 19 | +} |
| 20 | + |
| 21 | +// builds a zip buffer from entry descriptors: { name, data, mode, deflate } |
| 22 | +function makeZip(entries) { |
| 23 | + let locals = []; |
| 24 | + let centrals = []; |
| 25 | + let offset = 0; |
| 26 | + |
| 27 | + for (let { name, data = '', mode = 0o644, deflate = false } of entries) { |
| 28 | + let nameBuf = Buffer.from(name); |
| 29 | + let contents = Buffer.from(data); |
| 30 | + let crc = crc32(contents); |
| 31 | + let stored = deflate ? zlib.deflateRawSync(contents) : contents; |
| 32 | + let method = deflate ? 8 : 0; |
| 33 | + |
| 34 | + let local = Buffer.alloc(30); |
| 35 | + local.writeUInt32LE(0x04034b50, 0); |
| 36 | + local.writeUInt16LE(20, 4); // version needed |
| 37 | + local.writeUInt16LE(method, 8); |
| 38 | + local.writeUInt32LE(crc, 14); |
| 39 | + local.writeUInt32LE(stored.length, 18); |
| 40 | + local.writeUInt32LE(contents.length, 22); |
| 41 | + local.writeUInt16LE(nameBuf.length, 26); |
| 42 | + locals.push(local, nameBuf, stored); |
| 43 | + |
| 44 | + let central = Buffer.alloc(46); |
| 45 | + central.writeUInt32LE(0x02014b50, 0); |
| 46 | + central.writeUInt16LE((3 << 8) | 20, 4); // version made by unix |
| 47 | + central.writeUInt16LE(20, 6); // version needed |
| 48 | + central.writeUInt16LE(method, 10); |
| 49 | + central.writeUInt32LE(crc, 16); |
| 50 | + central.writeUInt32LE(stored.length, 20); |
| 51 | + central.writeUInt32LE(contents.length, 24); |
| 52 | + central.writeUInt16LE(nameBuf.length, 28); |
| 53 | + central.writeUInt32LE((mode << 16) >>> 0, 38); // external attributes |
| 54 | + central.writeUInt32LE(offset, 42); |
| 55 | + centrals.push(central, nameBuf); |
| 56 | + |
| 57 | + offset += local.length + nameBuf.length + stored.length; |
| 58 | + } |
| 59 | + |
| 60 | + let cd = Buffer.concat(centrals); |
| 61 | + let eocd = Buffer.alloc(22); |
| 62 | + eocd.writeUInt32LE(0x06054b50, 0); |
| 63 | + eocd.writeUInt16LE(entries.length, 8); |
| 64 | + eocd.writeUInt16LE(entries.length, 10); |
| 65 | + eocd.writeUInt32LE(cd.length, 12); |
| 66 | + eocd.writeUInt32LE(offset, 16); |
| 67 | + |
| 68 | + return Buffer.concat([...locals, cd, eocd]); |
| 69 | +} |
| 70 | + |
| 71 | +describe('Unit / Unzip', () => { |
| 72 | + let tmp, archive; |
| 73 | + |
| 74 | + beforeEach(() => { |
| 75 | + tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'percy-unzip-')); |
| 76 | + archive = path.join(tmp, 'fixture.zip'); |
| 77 | + }); |
| 78 | + |
| 79 | + afterEach(() => { |
| 80 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('extracts stored and deflated entries', async () => { |
| 84 | + let big = 'percy'.repeat(100_000); |
| 85 | + |
| 86 | + fs.writeFileSync(archive, makeZip([ |
| 87 | + { name: 'dir/', mode: 0o40755 }, |
| 88 | + { name: 'dir/stored.txt', data: 'stored contents' }, |
| 89 | + { name: 'dir/deflated.txt', data: big, deflate: true } |
| 90 | + ])); |
| 91 | + |
| 92 | + await unzip(archive, { dir: path.join(tmp, 'out') }); |
| 93 | + |
| 94 | + expect(fs.readFileSync(path.join(tmp, 'out/dir/stored.txt'), 'utf8')).toBe('stored contents'); |
| 95 | + expect(fs.readFileSync(path.join(tmp, 'out/dir/deflated.txt'), 'utf8')).toBe(big); |
| 96 | + }); |
| 97 | + |
| 98 | + it('creates intermediate directories for nested entries', async () => { |
| 99 | + fs.writeFileSync(archive, makeZip([ |
| 100 | + { name: 'a/b/c/file.txt', data: 'nested' } |
| 101 | + ])); |
| 102 | + |
| 103 | + await unzip(archive, { dir: path.join(tmp, 'out') }); |
| 104 | + |
| 105 | + expect(fs.readFileSync(path.join(tmp, 'out/a/b/c/file.txt'), 'utf8')).toBe('nested'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('skips macOS resource fork entries', async () => { |
| 109 | + fs.writeFileSync(archive, makeZip([ |
| 110 | + { name: 'file.txt', data: 'real' }, |
| 111 | + { name: '__MACOSX/file.txt', data: 'fork' } |
| 112 | + ])); |
| 113 | + |
| 114 | + await unzip(archive, { dir: path.join(tmp, 'out') }); |
| 115 | + |
| 116 | + expect(fs.existsSync(path.join(tmp, 'out/file.txt'))).toBe(true); |
| 117 | + expect(fs.existsSync(path.join(tmp, 'out/__MACOSX'))).toBe(false); |
| 118 | + }); |
| 119 | + |
| 120 | + it('rejects entries that would escape the target directory', async () => { |
| 121 | + fs.writeFileSync(archive, makeZip([ |
| 122 | + { name: '../evil.txt', data: 'oops' } |
| 123 | + ])); |
| 124 | + |
| 125 | + // yauzl 3 rejects relative paths at parse time; the unzip module also |
| 126 | + // guards against zip-slip for entries yauzl allows through |
| 127 | + await expectAsync(unzip(archive, { dir: path.join(tmp, 'out') })) |
| 128 | + .toBeRejectedWithError(/invalid relative path|Out of bound path/); |
| 129 | + |
| 130 | + expect(fs.existsSync(path.join(tmp, 'evil.txt'))).toBe(false); |
| 131 | + }); |
| 132 | + |
| 133 | + it('rejects relative target directories', async () => { |
| 134 | + fs.writeFileSync(archive, makeZip([ |
| 135 | + { name: 'file.txt', data: 'contents' } |
| 136 | + ])); |
| 137 | + |
| 138 | + await expectAsync(unzip(archive, { dir: 'relative/out' })) |
| 139 | + .toBeRejectedWithError('Target directory is expected to be absolute'); |
| 140 | + }); |
| 141 | + |
| 142 | + if (process.platform !== 'win32') { |
| 143 | + it('preserves executable file modes', async () => { |
| 144 | + fs.writeFileSync(archive, makeZip([ |
| 145 | + { name: 'bin/exec.sh', data: '#!/bin/sh\n', mode: 0o100755 }, |
| 146 | + { name: 'bin/plain.txt', data: 'plain', mode: 0o100644 } |
| 147 | + ])); |
| 148 | + |
| 149 | + await unzip(archive, { dir: path.join(tmp, 'out') }); |
| 150 | + |
| 151 | + expect(fs.statSync(path.join(tmp, 'out/bin/exec.sh')).mode & 0o777).toBe(0o755); |
| 152 | + expect(fs.statSync(path.join(tmp, 'out/bin/plain.txt')).mode & 0o777).toBe(0o644); |
| 153 | + }); |
| 154 | + |
| 155 | + it('restores symlink entries', async () => { |
| 156 | + fs.writeFileSync(archive, makeZip([ |
| 157 | + { name: 'target.txt', data: 'linked contents' }, |
| 158 | + { name: 'link.txt', data: 'target.txt', mode: 0o120755 } |
| 159 | + ])); |
| 160 | + |
| 161 | + await unzip(archive, { dir: path.join(tmp, 'out') }); |
| 162 | + |
| 163 | + expect(fs.lstatSync(path.join(tmp, 'out/link.txt')).isSymbolicLink()).toBe(true); |
| 164 | + expect(fs.readlinkSync(path.join(tmp, 'out/link.txt'))).toBe('target.txt'); |
| 165 | + expect(fs.readFileSync(path.join(tmp, 'out/link.txt'), 'utf8')).toBe('linked contents'); |
| 166 | + }); |
| 167 | + } |
| 168 | +}); |
0 commit comments