-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathtar-extract.test.ts
More file actions
173 lines (154 loc) · 6.99 KB
/
tar-extract.test.ts
File metadata and controls
173 lines (154 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { mkdtempSync, readFileSync, rmSync, writeFileSync, symlinkSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import * as tar from 'tar';
import { InstallException } from '../src/errors';
import { extractNpmPackage, extractOciPlugin } from '../src/tar-extract';
const PAYLOAD_LIMIT = 20_000_000;
async function makeTarball(
archivePath: string,
entries: string[],
prep: (root: string) => Promise<void> | void,
): Promise<void> {
const stageDir = mkdtempSync(join(tmpdir(), 'tar-stage-'));
try {
await prep(stageDir);
await tar.c({ file: archivePath, cwd: stageDir, portable: true }, entries);
} finally {
rmSync(stageDir, { recursive: true, force: true });
}
}
describe('extractOciPlugin', () => {
let workDir: string;
let tarball: string;
beforeEach(() => {
workDir = mkdtempSync(join(tmpdir(), 'oci-extract-'));
tarball = join(workDir, 'layer.tar');
});
afterEach(() => rmSync(workDir, { recursive: true, force: true }));
it('extracts just the requested plugin subdirectory', async () => {
await makeTarball(tarball, ['plugin-one', 'plugin-two'], stage => {
require('node:fs').mkdirSync(join(stage, 'plugin-one'));
require('node:fs').mkdirSync(join(stage, 'plugin-two'));
writeFileSync(join(stage, 'plugin-one/index.js'), 'module.exports = 1');
writeFileSync(join(stage, 'plugin-two/index.js'), 'module.exports = 2');
});
const dest = join(workDir, 'out');
await extractOciPlugin(tarball, 'plugin-one', dest);
expect(existsSync(join(dest, 'plugin-one/index.js'))).toBe(true);
expect(existsSync(join(dest, 'plugin-two'))).toBe(false);
});
it("rejects plugin paths with a '..' segment", async () => {
await makeTarball(tarball, ['plugin-one'], stage => {
require('node:fs').mkdirSync(join(stage, 'plugin-one'));
writeFileSync(join(stage, 'plugin-one/index.js'), 'x');
});
await expect(extractOciPlugin(tarball, '../etc/passwd', workDir)).rejects.toBeInstanceOf(
InstallException,
);
await expect(extractOciPlugin(tarball, 'foo/../bar', workDir)).rejects.toBeInstanceOf(
InstallException,
);
});
it("rejects plugin paths with a '.' segment or empty segments", async () => {
await makeTarball(tarball, ['plugin-one'], stage => {
require('node:fs').mkdirSync(join(stage, 'plugin-one'));
writeFileSync(join(stage, 'plugin-one/index.js'), 'x');
});
await expect(extractOciPlugin(tarball, './plugin-one', workDir)).rejects.toBeInstanceOf(
InstallException,
);
await expect(extractOciPlugin(tarball, 'foo//bar', workDir)).rejects.toBeInstanceOf(
InstallException,
);
});
it("accepts a plugin name that contains '..' inside a segment (not as a segment)", async () => {
await makeTarball(tarball, ['my..plugin'], stage => {
require('node:fs').mkdirSync(join(stage, 'my..plugin'));
writeFileSync(join(stage, 'my..plugin/index.js'), 'ok');
});
const dest = join(workDir, 'out');
await extractOciPlugin(tarball, 'my..plugin', dest);
expect(existsSync(join(dest, 'my..plugin/index.js'))).toBe(true);
});
it('rejects absolute plugin paths', async () => {
await makeTarball(tarball, ['plugin-one'], stage => {
require('node:fs').mkdirSync(join(stage, 'plugin-one'));
writeFileSync(join(stage, 'plugin-one/index.js'), 'x');
});
await expect(extractOciPlugin(tarball, '/etc/passwd', workDir)).rejects.toBeInstanceOf(
InstallException,
);
});
it('raises when any entry exceeds MAX_ENTRY_SIZE', async () => {
await makeTarball(tarball, ['plugin-one'], stage => {
require('node:fs').mkdirSync(join(stage, 'plugin-one'));
const bigPath = join(stage, 'plugin-one/big.bin');
const fd = require('node:fs').openSync(bigPath, 'w');
require('node:fs').ftruncateSync(fd, PAYLOAD_LIMIT + 1);
require('node:fs').closeSync(fd);
});
await expect(extractOciPlugin(tarball, 'plugin-one', join(workDir, 'out'))).rejects.toThrow(
/Zip bomb/,
);
});
it('skips symlinks whose target escapes the destination', async () => {
await makeTarball(tarball, ['plugin-one'], stage => {
require('node:fs').mkdirSync(join(stage, 'plugin-one'));
writeFileSync(join(stage, 'plugin-one/ok.txt'), 'ok');
symlinkSync('/etc/passwd', join(stage, 'plugin-one/bad-link'));
});
const dest = join(workDir, 'out');
await extractOciPlugin(tarball, 'plugin-one', dest);
expect(existsSync(join(dest, 'plugin-one/ok.txt'))).toBe(true);
expect(existsSync(join(dest, 'plugin-one/bad-link'))).toBe(false);
});
it('does not extract sibling directories with the same name prefix', async () => {
await makeTarball(tarball, ['plugin-one', 'plugin-one-evil'], stage => {
require('node:fs').mkdirSync(join(stage, 'plugin-one'));
require('node:fs').mkdirSync(join(stage, 'plugin-one-evil'));
writeFileSync(join(stage, 'plugin-one/index.js'), 'module.exports = 1');
writeFileSync(join(stage, 'plugin-one-evil/index.js'), 'module.exports = 2');
});
const dest = join(workDir, 'out');
await extractOciPlugin(tarball, 'plugin-one', dest);
expect(existsSync(join(dest, 'plugin-one/index.js'))).toBe(true);
expect(existsSync(join(dest, 'plugin-one-evil'))).toBe(false);
});
});
describe('extractNpmPackage', () => {
let workDir: string;
beforeEach(() => (workDir = mkdtempSync(join(tmpdir(), 'npm-extract-'))));
afterEach(() => rmSync(workDir, { recursive: true, force: true }));
it("strips the 'package/' prefix and returns the pkg directory name", async () => {
const archive = join(workDir, 'pkg.tgz');
await makeTarball(archive, ['package'], stage => {
require('node:fs').mkdirSync(join(stage, 'package'));
writeFileSync(join(stage, 'package/package.json'), '{"name":"x"}');
writeFileSync(join(stage, 'package/index.js'), 'module.exports={};');
});
const dir = await extractNpmPackage(archive);
expect(dir).toBe('pkg');
expect(readFileSync(join(workDir, 'pkg', 'package.json'), 'utf8')).toBe('{"name":"x"}');
});
it("rejects archives with entries outside 'package/'", async () => {
const archive = join(workDir, 'pkg.tgz');
await makeTarball(archive, ['package', 'evil.txt'], stage => {
require('node:fs').mkdirSync(join(stage, 'package'));
writeFileSync(join(stage, 'package/index.js'), 'x');
writeFileSync(join(stage, 'evil.txt'), 'x');
});
await expect(extractNpmPackage(archive)).rejects.toThrow(/package\//);
});
it('rejects zip-bomb entries', async () => {
const archive = join(workDir, 'pkg.tgz');
await makeTarball(archive, ['package'], stage => {
require('node:fs').mkdirSync(join(stage, 'package'));
const bigPath = join(stage, 'package/big.bin');
const fd = require('node:fs').openSync(bigPath, 'w');
require('node:fs').ftruncateSync(fd, PAYLOAD_LIMIT + 1);
require('node:fs').closeSync(fd);
});
await expect(extractNpmPackage(archive)).rejects.toThrow(/Zip bomb/);
});
});