-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathvalidate-export.test.js
More file actions
280 lines (233 loc) · 8.48 KB
/
Copy pathvalidate-export.test.js
File metadata and controls
280 lines (233 loc) · 8.48 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
const test = require('node:test');
const assert = require('node:assert/strict');
const crypto = require('node:crypto');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const zlib = require('node:zlib');
const { spawnSync } = require('node:child_process');
const VALIDATOR_PATH = path.join(
__dirname,
'..',
'..',
'skills',
'export-solution',
'scripts',
'validate-export.js'
);
function crc32(data) {
let crc = 0xffffffff;
for (const byte of data) {
crc ^= byte;
for (let bit = 0; bit < 8; bit++) {
crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1));
}
}
return (crc ^ 0xffffffff) >>> 0;
}
function createZip(entries) {
const localRecords = [];
const centralRecords = [];
let localOffset = 0;
for (const entry of entries) {
const name = Buffer.from(entry.name, 'utf8');
const data = Buffer.isBuffer(entry.data) ? entry.data : Buffer.from(entry.data, 'utf8');
const method = entry.method ?? 0;
const compressedData = method === 8 ? zlib.deflateRawSync(data) : data;
const checksum = crc32(data);
const localHeader = Buffer.alloc(30);
localHeader.writeUInt32LE(0x04034b50, 0);
localHeader.writeUInt16LE(20, 4);
localHeader.writeUInt16LE(0x0800, 6);
localHeader.writeUInt16LE(method, 8);
localHeader.writeUInt32LE(checksum, 14);
localHeader.writeUInt32LE(compressedData.length, 18);
localHeader.writeUInt32LE(data.length, 22);
localHeader.writeUInt16LE(name.length, 26);
localRecords.push(localHeader, name, compressedData);
const centralHeader = Buffer.alloc(46);
centralHeader.writeUInt32LE(0x02014b50, 0);
centralHeader.writeUInt16LE(20, 4);
centralHeader.writeUInt16LE(20, 6);
centralHeader.writeUInt16LE(0x0800, 8);
centralHeader.writeUInt16LE(method, 10);
centralHeader.writeUInt32LE(checksum, 16);
centralHeader.writeUInt32LE(compressedData.length, 20);
centralHeader.writeUInt32LE(data.length, 24);
centralHeader.writeUInt16LE(name.length, 28);
centralHeader.writeUInt32LE(localOffset, 42);
centralRecords.push(centralHeader, name);
localOffset += localHeader.length + name.length + compressedData.length;
}
const centralDirectory = Buffer.concat(centralRecords);
const endRecord = Buffer.alloc(22);
endRecord.writeUInt32LE(0x06054b50, 0);
endRecord.writeUInt16LE(entries.length, 8);
endRecord.writeUInt16LE(entries.length, 10);
endRecord.writeUInt32LE(centralDirectory.length, 12);
endRecord.writeUInt32LE(localOffset, 16);
return Buffer.concat([...localRecords, centralDirectory, endRecord]);
}
function makeProject(t) {
const projectRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'validate-export-'));
t.after(() => fs.rmSync(projectRoot, { recursive: true, force: true }));
return projectRoot;
}
function validSolutionEntries(extraEntries = []) {
return [
{
name: 'Solution.xml',
data: `<ImportExportXml>${crypto.randomBytes(1400).toString('hex')}</ImportExportXml>`,
method: 8,
},
...extraEntries,
];
}
function runValidator(projectRoot, env = process.env) {
return spawnSync(process.execPath, [VALIDATOR_PATH], {
cwd: projectRoot,
input: JSON.stringify({ cwd: projectRoot }),
encoding: 'utf8',
timeout: 10000,
env,
});
}
test('approves when no exported solution ZIP exists', (t) => {
const projectRoot = makeProject(t);
const result = runValidator(projectRoot);
assert.equal(result.status, 0, result.stderr);
});
test('approves a valid deflated solution ZIP containing Solution.xml', (t) => {
const projectRoot = makeProject(t);
fs.writeFileSync(
path.join(projectRoot, 'release_unmanaged.zip'),
createZip(validSolutionEntries())
);
const result = runValidator(projectRoot);
assert.equal(result.status, 0, result.stderr);
});
test('handles malicious archive and path names without invoking a shell', (t) => {
const projectRoot = makeProject(t);
const markerName = 'validator-command-ran';
const zipName = `release_$(touch ${markerName})_unmanaged.zip`;
fs.writeFileSync(
path.join(projectRoot, zipName),
createZip(validSolutionEntries([
{
name: 'assets/"quoted"; $(ignored) & payload.txt',
data: 'not executable',
},
]))
);
const result = runValidator(projectRoot);
assert.equal(result.status, 0, result.stderr);
assert.equal(fs.existsSync(path.join(projectRoot, markerName)), false);
});
test('handles spaces, quotes, and cross-platform filename metacharacters', (t) => {
const projectRoot = makeProject(t);
const zipPath = path.join(projectRoot, "release 'review copy' & (final); 100%_managed.zip");
fs.writeFileSync(zipPath, createZip(validSolutionEntries()));
const result = runValidator(projectRoot);
assert.equal(result.status, 0, result.stderr);
});
test('blocks a corrupt ZIP instead of approving by file size', (t) => {
const projectRoot = makeProject(t);
fs.writeFileSync(
path.join(projectRoot, 'corrupt_unmanaged.zip'),
crypto.randomBytes(1500)
);
const result = runValidator(projectRoot);
assert.equal(result.status, 2);
assert.match(result.stderr, /could not be validated/i);
});
test('blocks a structurally valid ZIP that does not contain Solution.xml', (t) => {
const projectRoot = makeProject(t);
fs.writeFileSync(
path.join(projectRoot, 'missing_solution_managed.zip'),
createZip([
{
name: 'Other.xml',
data: crypto.randomBytes(1400),
},
])
);
const result = runValidator(projectRoot);
assert.equal(result.status, 2);
assert.match(result.stderr, /does not contain solution\.xml/i);
});
test('blocks a ZIP when the Solution.xml payload fails its integrity check', (t) => {
const projectRoot = makeProject(t);
const archive = createZip([
{
name: 'Solution.xml',
data: crypto.randomBytes(1400),
method: 0,
},
]);
archive[30 + Buffer.byteLength('Solution.xml')] ^= 0xff;
fs.writeFileSync(path.join(projectRoot, 'damaged_managed.zip'), archive);
const result = runValidator(projectRoot);
assert.equal(result.status, 2);
assert.match(result.stderr, /integrity check/i);
});
test('blocks duplicate root Solution.xml entries', (t) => {
const projectRoot = makeProject(t);
fs.writeFileSync(
path.join(projectRoot, 'duplicate_unmanaged.zip'),
createZip([
...validSolutionEntries(),
{
name: 'solution.XML',
data: crypto.randomBytes(1400),
},
])
);
const result = runValidator(projectRoot);
assert.equal(result.status, 2);
assert.match(result.stderr, /duplicate Solution\.xml/i);
});
test('blocks a ZIP with a corrupt non-manifest local header', (t) => {
const projectRoot = makeProject(t);
const archive = createZip(validSolutionEntries([
{
name: 'Customizations.xml',
data: crypto.randomBytes(1400),
},
]));
const secondLocalHeader = archive.indexOf(Buffer.from([0x50, 0x4b, 0x03, 0x04]), 4);
archive.writeUInt32LE(0, secondLocalHeader);
fs.writeFileSync(path.join(projectRoot, 'corrupt_entry_managed.zip'), archive);
const result = runValidator(projectRoot);
assert.equal(result.status, 2);
assert.match(result.stderr, /local header.*invalid signature/i);
});
test('blocks local-header metadata that disagrees with the central directory', (t) => {
const projectRoot = makeProject(t);
const archive = createZip(validSolutionEntries());
archive.writeUInt32LE(1, 18);
fs.writeFileSync(path.join(projectRoot, 'corrupt_metadata_managed.zip'), archive);
const result = runValidator(projectRoot);
assert.equal(result.status, 2);
assert.match(result.stderr, /sizes or checksum.*disagree/i);
});
test('blocks oversized archives before reading them into memory', (t) => {
const projectRoot = makeProject(t);
const zipPath = path.join(projectRoot, 'oversized_unmanaged.zip');
fs.writeFileSync(zipPath, Buffer.alloc(0));
fs.truncateSync(zipPath, 100 * 1024 * 1024 + 1);
const result = runValidator(projectRoot);
assert.equal(result.status, 2);
assert.match(result.stderr, /exceeds the supported 100 MiB/i);
});
test('validates without unzip, grep, or any executable available on PATH', (t) => {
const projectRoot = makeProject(t);
fs.writeFileSync(
path.join(projectRoot, 'windows-compatible_unmanaged.zip'),
createZip(validSolutionEntries())
);
const envWithoutPath = Object.fromEntries(
Object.entries(process.env).filter(([key]) => key.toLowerCase() !== 'path')
);
const result = runValidator(projectRoot, { ...envWithoutPath, PATH: '' });
assert.equal(result.status, 0, result.stderr);
});