-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpayloadStaging.test.js
More file actions
67 lines (60 loc) · 2.61 KB
/
Copy pathpayloadStaging.test.js
File metadata and controls
67 lines (60 loc) · 2.61 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
'use strict';
const assert = require('node:assert');
const { Readable } = require('node:stream');
const { readFile, stat } = require('node:fs/promises');
const path = require('node:path');
const testUtils = require('../testUtils.js');
testUtils.preTestPrep();
const { stagePayloadToTempFile } = require('#src/components/payloadStaging');
describe('stagePayloadToTempFile', () => {
it('writes a streamed payload to a temp file with the expected contents', async () => {
const payload = Buffer.from('abcdefghij'.repeat(10000), 'utf8'); // 100 KB
const { path: tmpPath, cleanup } = await stagePayloadToTempFile(Readable.from(payload), 'demo');
try {
const written = await readFile(tmpPath);
assert.strictEqual(written.length, payload.length);
assert.deepStrictEqual(written, payload);
assert.match(tmpPath, /harper-deploy-demo-/, 'temp dir is named after the project');
assert.strictEqual(path.basename(tmpPath), 'payload.tar.gz');
} finally {
await cleanup();
}
});
it('cleanup() removes the staged file and its parent temp dir', async () => {
const { path: tmpPath, cleanup } = await stagePayloadToTempFile(Readable.from('hello'), 'cleanup-test');
await cleanup();
await assert.rejects(stat(tmpPath), /ENOENT/, 'staged file must be gone');
await assert.rejects(stat(path.dirname(tmpPath)), /ENOENT/, 'staged dir must be gone');
});
it('cleanup() is safe to call twice (force: true)', async () => {
const { cleanup } = await stagePayloadToTempFile(Readable.from('hello'), 'double-cleanup');
await cleanup();
await cleanup(); // must not throw
});
it('sanitizes path-traversal characters in the project name', async () => {
const { path: tmpPath, cleanup } = await stagePayloadToTempFile(Readable.from('x'), '../../evil/name');
try {
// Path separators must be replaced so the temp dir lives in a single mkdtemp slot
// directly under tmpdir, not navigated upstream. `..` segments alone don't traverse
// because there's no `/` between them after sanitization.
const os = require('node:os');
assert.strictEqual(
path.dirname(path.dirname(tmpPath)),
path.resolve(os.tmpdir()),
'staged dir must live directly under tmpdir'
);
assert.doesNotMatch(path.basename(path.dirname(tmpPath)), /\//);
assert.match(path.basename(path.dirname(tmpPath)), /harper-deploy-.+_evil_name-/);
} finally {
await cleanup();
}
});
it('propagates source-stream errors through pipeline', async () => {
const source = new Readable({
read() {
this.destroy(new Error('source boom'));
},
});
await assert.rejects(stagePayloadToTempFile(source, 'erroring'), /source boom/);
});
});