-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathexec.test.ts
More file actions
68 lines (59 loc) · 2.26 KB
/
Copy pathexec.test.ts
File metadata and controls
68 lines (59 loc) · 2.26 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
import { test } from 'vitest';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { runCmd, whichCmd } from '../exec.ts';
test('runCmd enforces timeoutMs and rejects with COMMAND_FAILED', async () => {
await assert.rejects(
runCmd(process.execPath, ['-e', 'setTimeout(() => {}, 10_000)'], { timeoutMs: 100 }),
(error: unknown) => {
const err = error as { code?: string; message?: string; details?: Record<string, unknown> };
return (
err?.code === 'COMMAND_FAILED' &&
typeof err?.message === 'string' &&
err.message.includes('timed out') &&
err.details?.timeoutMs === 100
);
},
);
});
test('whichCmd resolves absolute executable paths without invoking a shell', async () => {
assert.equal(await whichCmd(process.execPath), true);
});
test('whichCmd resolves bare commands from PATH', async () => {
assert.equal(await whichCmd('node'), true);
});
test.runIf(process.platform !== 'win32')(
'runCmd allows explicit relative executable paths when shell execution is disabled',
async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-runcmd-relative-'));
const target = path.join(root, 'local-node');
fs.symlinkSync(process.execPath, target);
try {
const result = await runCmd('./local-node', ['-e', 'process.stdout.write("ok")'], {
cwd: root,
});
assert.equal(result.stdout, 'ok');
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
},
);
test('whichCmd rejects suspicious command strings', async () => {
assert.equal(await whichCmd('node; rm -rf /'), false);
assert.equal(await whichCmd('./node'), false);
});
test.sequential('whichCmd ignores directories that match a command name in PATH', async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-whichcmd-'));
const fakeCommandDir = path.join(root, 'fake-tool');
fs.mkdirSync(fakeCommandDir);
const previousPath = process.env.PATH;
process.env.PATH = `${root}${path.delimiter}${previousPath ?? ''}`;
try {
assert.equal(await whichCmd('fake-tool'), false);
} finally {
process.env.PATH = previousPath;
fs.rmSync(root, { recursive: true, force: true });
}
});