| 1 | import fs, { |
| 2 | lstatSync, |
| 3 | readFileSync, |
| 4 | symlinkSync, |
| 5 | writeFileSync, |
| 6 | } from 'node:fs' |
| 7 | import { resolve } from 'node:path' |
| 8 | import t from 'tap' |
| 9 | import { extractscip-typescript npm tar 7.5.19 src/`extract.ts`/extract. } from '../src/extract.js'scip-typescript npm tar 7.5.19 src/`extract.ts`/ |
| 10 | import { Headerscip-typescript npm tar 7.5.19 src/`header.ts`/Header# } from '../src/header.js'scip-typescript npm tar 7.5.19 src/`header.ts`/ |
| 11 | |
| 12 | if (typeof fs.constants.O_NOFOLLOW !== 'number') { |
| 13 | t.plan(0, 'no O_NOFOLLOW flag') |
| 14 | process.exit(0) |
| 15 | } |
| 16 | |
| 17 | const makeTarballscip-typescript npm tar 7.5.19 test/`symlink-race-extract.ts`/makeTarball. = () => { |
| 18 | const header = Buffer.alloc(512) |
| 19 | new Headerscip-typescript npm tar 7.5.19 src/`header.ts`/Header#`<constructor>`().({ |
| 20 | pathscip-typescript npm tar 7.5.19 test/`symlink-race-extract.ts`/path0:: 'victim.txt', |
| 21 | typescip-typescript npm tar 7.5.19 test/`symlink-race-extract.ts`/type0:: 'File', |
| 22 | sizescip-typescript npm tar 7.5.19 test/`symlink-race-extract.ts`/size0:: 6, |
| 23 | }).encodescip-typescript npm tar 7.5.19 src/`header.ts`/Header#encode().(header) |
| 24 | |
| 25 | return Buffer.concat([ |
| 26 | header, |
| 27 | Buffer.from('PWNED\n'.padEndscip-typescript npm typescript 5.9.3 lib/`lib.es2017.string.d.ts`/String#padEnd().(512, '\0')), |
| 28 | Buffer.alloc(1024), |
| 29 | ]) |
| 30 | } |
| 31 | |
| 32 | t.test('extract does not follow a raced-in symlink', async t => { |
| 33 | const dir = t.testdir({ |
| 34 | cwdscip-typescript npm tar 7.5.19 test/`symlink-race-extract.ts`/cwd0:: {}, |
| 35 | 'target.txt'scip-typescript npm tar 7.5.19 test/`symlink-race-extract.ts`/`'target.txt'0`:: 'ORIGINAL\n', |
| 36 | }) |
| 37 | const cwd = resolve(dir, 'cwd') |
| 38 | const target = resolve(dir, 'target.txt') |
| 39 | const tarball = resolve(dir, 'poc.tar') |
| 40 | const victim = resolve(cwd, 'victim.txt') |
| 41 | writeFileSync(tarball, makeTarballscip-typescript npm tar 7.5.19 test/`symlink-race-extract.ts`/makeTarball.()) |
| 42 | |
| 43 | const warnings: [code: string, message: string][] = [] |
| 44 | const lstat = fs.lstat |
| 45 | let raced = false |
| 46 | fs.lstat = ((path, options, cb) => { |
| 47 | const callback = |
| 48 | typeof options === 'function' ? options : ( |
| 49 | (cb as Parametersscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Parameters#<typeof fs.lstat>[1] & |
| 50 | ((err: NodeJS.ErrnoException | null, stats: fs.Stats) => void)) |
| 51 | ) |
| 52 | if (!raced && Stringscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#(path) === victim) { |
| 53 | raced = true |
| 54 | symlinkSync(target, victim) |
| 55 | const er = Objectscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Object#.assignscip-typescript npm typescript 5.9.3 lib/`lib.es2015.core.d.ts`/ObjectConstructor#assign().(new Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#('raced symlink'), { |
| 56 | codescip-typescript npm tar 7.5.19 test/`symlink-race-extract.ts`/code0:: 'ENOENT', |
| 57 | }) |
| 58 | process.nextTick(() => callback(er, undefined as never)) |
| 59 | return |
| 60 | } |
| 61 | return typeof options === 'function' ? |
| 62 | lstat(path, options) |
| 63 | : lstat(path, options, cb as never) |
| 64 | }) as typeof fs.lstat |
| 65 | t.teardown(() => { |
| 66 | fs.lstat = lstat |
| 67 | }) |
| 68 | |
| 69 | await extractscip-typescript npm tar 7.5.19 src/`extract.ts`/extract.({ |
| 70 | cwdscip-typescript npm tar 7.5.19 src/`options.ts`/TarOptions#cwd., |
| 71 | filescip-typescript npm tar 7.5.19 src/`options.ts`/TarOptions#file.: tarball, |
| 72 | onwarnscip-typescript npm tar 7.5.19 src/`options.ts`/TarOptions#onwarn.: (code, message) => warnings.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().([code, Stringscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#(message)]), |
| 73 | }) |
| 74 | |
| 75 | t.equal(readFileSync(target, 'utf8'), 'ORIGINAL\n') |
| 76 | t.equal(lstatSync(victim).isSymbolicLink(), true) |
| 77 | t.match(warnings, [ |
| 78 | ['TAR_ENTRY_ERROR', /ELOOP|symbolic link|Too many symbolic links/], |
| 79 | ]) |
| 80 | }) |