|
| 1 | +const assert = require('assert'); |
| 2 | +const tmp = require('../lib/tmp'); |
| 3 | +const fs = require('fs'); |
| 4 | +const os = require('os'); |
| 5 | +const { join } = require('path'); |
| 6 | +const { randomBytes } = require('crypto'); |
| 7 | + |
| 8 | +function getRandomChars() { |
| 9 | + return randomBytes(10).toString('hex'); |
| 10 | +} |
| 11 | + |
| 12 | +function getRandomPath(dir) { |
| 13 | + return join(dir, getRandomChars()); |
| 14 | +} |
| 15 | + |
| 16 | +describe('GHSA-52f5-9888-hmc6', function () { |
| 17 | + const realTmpdir = os.tmpdir(); |
| 18 | + const restricted = getRandomPath(realTmpdir); |
| 19 | + const tmpdir = getRandomPath(realTmpdir); |
| 20 | + const evilSymlinkPath = getRandomPath(tmpdir); |
| 21 | + |
| 22 | + before(function () { |
| 23 | + fs.mkdirSync(restricted); |
| 24 | + fs.mkdirSync(tmpdir); |
| 25 | + fs.symlinkSync(restricted, evilSymlinkPath); |
| 26 | + }); |
| 27 | + |
| 28 | + after(function () { |
| 29 | + fs.rmSync(restricted, { recursive: true }); |
| 30 | + fs.rmSync(tmpdir, { recursive: true }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('#fileSync with `dir`', function () { |
| 34 | + it('should not allow dirs outside of dir', function (done) { |
| 35 | + assert.throws(function () { |
| 36 | + tmp.fileSync({ tmpdir: tmpdir, dir: evilSymlinkPath }); |
| 37 | + }, new RegExp('^Error: dir option must be relative to')); |
| 38 | + |
| 39 | + done(); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('#fileSync with `template`', function () { |
| 44 | + it('should not allow dirs outside of dir', function (done) { |
| 45 | + assert.throws(function () { |
| 46 | + tmp.fileSync({ tmpdir: tmpdir, template: join(evilSymlinkPath, 'XXXXXX') }); |
| 47 | + }, new RegExp('^Error: template option must be relative to')); |
| 48 | + |
| 49 | + done(); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('#file with `dir`', function () { |
| 54 | + it('should not allow dirs outside of dir', function (done) { |
| 55 | + tmp.file({ tmpdir: tmpdir, dir: evilSymlinkPath }, function (err, file) { |
| 56 | + assert.ok(err instanceof Error, 'should have failed'); |
| 57 | + assert.ifError(file); |
| 58 | + |
| 59 | + done(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('#file with `template`', function () { |
| 65 | + it('should not allow dirs outside of dir', function (done) { |
| 66 | + tmp.file({ tmpdir: tmpdir, template: join(evilSymlinkPath, 'XXXXXX') }, function (err, file) { |
| 67 | + assert.ok(err instanceof Error, 'should have failed'); |
| 68 | + assert.ifError(file, 'should be null'); |
| 69 | + |
| 70 | + done(); |
| 71 | + }); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('#dirSync with `dir`', function () { |
| 76 | + it('should not allow dirs outside of dir', function (done) { |
| 77 | + assert.throws(function () { |
| 78 | + tmp.dirSync({ |
| 79 | + tmpdir: tmpdir, |
| 80 | + dir: evilSymlinkPath |
| 81 | + }); |
| 82 | + }, new RegExp('^Error: dir option must be relative to')); |
| 83 | + |
| 84 | + done(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('#dirSync with `template`', function () { |
| 89 | + it('should not allow dirs outside of dir', function (done) { |
| 90 | + assert.throws(function () { |
| 91 | + tmp.dirSync({ |
| 92 | + tmpdir: tmpdir, |
| 93 | + template: join(evilSymlinkPath, 'XXXXXX') |
| 94 | + }); |
| 95 | + }, new RegExp('^Error: template option must be relative to')); |
| 96 | + |
| 97 | + done(); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('#dir with `dir`', function () { |
| 102 | + it('should not allow dirs outside of dir', function (done) { |
| 103 | + tmp.dir({ tmpdir: tmpdir, dir: evilSymlinkPath }, function (err, dir) { |
| 104 | + assert.ok(err instanceof Error, 'should have failed'); |
| 105 | + assert.ifError(dir); |
| 106 | + |
| 107 | + done(); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('#dir with `template`', function () { |
| 113 | + it('should not allow dirs outside of dir', function (done) { |
| 114 | + tmp.dir({ tmpdir: tmpdir, template: join(evilSymlinkPath, 'XXXXXX') }, function (err, dir) { |
| 115 | + assert.ok(err instanceof Error, 'should have failed'); |
| 116 | + assert.ifError(dir, 'should be null'); |
| 117 | + |
| 118 | + done(); |
| 119 | + }); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments