-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathkernel-runtime.test.ts
More file actions
1026 lines (873 loc) · 34.5 KB
/
kernel-runtime.test.ts
File metadata and controls
1026 lines (873 loc) · 34.5 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Tests for the Node.js RuntimeDriver.
*
* Verifies driver interface contract, kernel mounting, command
* registration, KernelCommandExecutor routing, and isolate-based
* script execution.
*/
import { describe, it, expect, afterEach } from 'vitest';
import { mkdirSync, writeFileSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { createNodeRuntime } from '../src/kernel-runtime.ts';
import type { NodeRuntimeOptions } from '../src/kernel-runtime.ts';
import { createKernel } from '@secure-exec/core';
import type {
KernelRuntimeDriver as RuntimeDriver,
KernelInterface,
ProcessContext,
DriverProcess,
Kernel,
} from '@secure-exec/core';
/**
* Minimal mock RuntimeDriver for testing cross-runtime dispatch.
* Configurable per-command exit codes and stdout/stderr output.
*/
class MockRuntimeDriver implements RuntimeDriver {
name = 'mock';
commands: string[];
private _configs: Record<string, { exitCode?: number; stdout?: string; stderr?: string }>;
constructor(commands: string[], configs: Record<string, { exitCode?: number; stdout?: string; stderr?: string }> = {}) {
this.commands = commands;
this._configs = configs;
}
async init(_kernel: KernelInterface): Promise<void> {}
spawn(command: string, args: string[], ctx: ProcessContext): DriverProcess {
// Handle bash -c 'cmd ...' by extracting the inner command name
let resolvedCmd = command;
if ((command === 'bash' || command === 'sh') && args[0] === '-c' && args[1]) {
resolvedCmd = args[1].split(/\s+/)[0];
}
const config = this._configs[resolvedCmd] ?? {};
const exitCode = config.exitCode ?? 0;
let resolveExit!: (code: number) => void;
const exitPromise = new Promise<number>((r) => { resolveExit = r; });
const proc: DriverProcess = {
onStdout: null,
onStderr: null,
onExit: null,
writeStdin: () => {},
closeStdin: () => {},
kill: () => {},
wait: () => exitPromise,
};
// Emit output asynchronously
queueMicrotask(() => {
if (config.stdout) {
const data = new TextEncoder().encode(config.stdout);
ctx.onStdout?.(data);
proc.onStdout?.(data);
}
if (config.stderr) {
const data = new TextEncoder().encode(config.stderr);
ctx.onStderr?.(data);
proc.onStderr?.(data);
}
resolveExit(exitCode);
proc.onExit?.(exitCode);
});
return proc;
}
async dispose(): Promise<void> {}
}
// Minimal in-memory VFS for kernel tests
class SimpleVFS {
private files = new Map<string, Uint8Array>();
private dirs = new Set<string>(['/']);
async readFile(path: string): Promise<Uint8Array> {
const data = this.files.get(path);
if (!data) throw new Error(`ENOENT: ${path}`);
return data;
}
async readTextFile(path: string): Promise<string> {
return new TextDecoder().decode(await this.readFile(path));
}
async readDir(path: string): Promise<string[]> {
const prefix = path === '/' ? '/' : path + '/';
const entries: string[] = [];
for (const p of [...this.files.keys(), ...this.dirs]) {
if (p !== path && p.startsWith(prefix)) {
const rest = p.slice(prefix.length);
if (!rest.includes('/')) entries.push(rest);
}
}
return entries;
}
async readDirWithTypes(path: string) {
return (await this.readDir(path)).map(name => ({
name,
isDirectory: this.dirs.has(path === '/' ? `/${name}` : `${path}/${name}`),
}));
}
async writeFile(path: string, content: string | Uint8Array): Promise<void> {
const data = typeof content === 'string' ? new TextEncoder().encode(content) : content;
this.files.set(path, new Uint8Array(data));
const parts = path.split('/').filter(Boolean);
for (let i = 1; i < parts.length; i++) {
this.dirs.add('/' + parts.slice(0, i).join('/'));
}
}
async createDir(path: string) { this.dirs.add(path); }
async mkdir(path: string) { this.dirs.add(path); }
async exists(path: string): Promise<boolean> {
return this.files.has(path) || this.dirs.has(path);
}
async stat(path: string) {
const isDir = this.dirs.has(path);
const data = this.files.get(path);
if (!isDir && !data) throw new Error(`ENOENT: ${path}`);
return {
mode: isDir ? 0o40755 : 0o100644,
size: data?.length ?? 0,
isDirectory: isDir,
isSymbolicLink: false,
atimeMs: Date.now(),
mtimeMs: Date.now(),
ctimeMs: Date.now(),
birthtimeMs: Date.now(),
ino: 0,
nlink: 1,
uid: 1000,
gid: 1000,
};
}
async removeFile(path: string) { this.files.delete(path); }
async removeDir(path: string) { this.dirs.delete(path); }
async rename(oldPath: string, newPath: string) {
const data = this.files.get(oldPath);
if (data) { this.files.set(newPath, data); this.files.delete(oldPath); }
}
async realpath(path: string) { return path; }
async symlink(_target: string, _linkPath: string) {}
async readlink(_path: string): Promise<string> { return ''; }
async lstat(path: string) { return this.stat(path); }
async link(_old: string, _new: string) {}
async chmod(_path: string, _mode: number) {}
async chown(_path: string, _uid: number, _gid: number) {}
async utimes(_path: string, _atime: number, _mtime: number) {}
async truncate(_path: string, _length: number) {}
}
// -------------------------------------------------------------------------
// Tests
// -------------------------------------------------------------------------
describe('Node RuntimeDriver', () => {
describe('factory', () => {
it('createNodeRuntime returns a RuntimeDriver', () => {
const driver = createNodeRuntime();
expect(driver).toBeDefined();
expect(driver.name).toBe('node');
expect(typeof driver.init).toBe('function');
expect(typeof driver.spawn).toBe('function');
expect(typeof driver.dispose).toBe('function');
});
it('driver.name is "node"', () => {
const driver = createNodeRuntime();
expect(driver.name).toBe('node');
});
it('driver.commands contains node, npm, npx', () => {
const driver = createNodeRuntime();
expect(driver.commands).toContain('node');
expect(driver.commands).toContain('npm');
expect(driver.commands).toContain('npx');
});
it('accepts custom memoryLimit', () => {
// Verify option is stored and differs from default (128)
const driver = createNodeRuntime({ memoryLimit: 256 });
expect((driver as any)._memoryLimit).toBe(256);
});
it('memoryLimit defaults to 128', () => {
const driver = createNodeRuntime();
expect((driver as any)._memoryLimit).toBe(128);
});
});
describe('driver lifecycle', () => {
it('throws when spawning before init', () => {
const driver = createNodeRuntime();
const ctx: ProcessContext = {
pid: 1, ppid: 0, env: {}, cwd: '/home/user',
fds: { stdin: 0, stdout: 1, stderr: 2 },
};
expect(() => driver.spawn('node', ['-e', 'true'], ctx)).toThrow(/not initialized/);
});
it('dispose without init does not throw', async () => {
const driver = createNodeRuntime();
await driver.dispose();
});
it('dispose after init cleans up', async () => {
const driver = createNodeRuntime();
const mockKernel: Partial<KernelInterface> = {};
await driver.init(mockKernel as KernelInterface);
await driver.dispose();
});
});
describe('kernel integration', () => {
let kernel: Kernel;
afterEach(async () => {
await kernel?.dispose();
});
it('mounts to kernel successfully', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
const driver = createNodeRuntime();
await kernel.mount(driver);
expect(kernel.commands.get('node')).toBe('node');
expect(kernel.commands.get('npm')).toBe('node');
expect(kernel.commands.get('npx')).toBe('node');
});
it('node -e executes inline code and exits 0', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const proc = kernel.spawn('node', ['-e', 'console.log("hello from node")']);
const stdoutChunks: string[] = [];
// Collect stdout via wait â process completes and exec captures it
const code = await proc.wait();
expect(code).toBe(0);
});
it('node -e captures stdout', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', 'console.log("hello")'], {
onStdout: (data) => chunks.push(data),
});
await proc.wait();
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(output).toContain('hello');
});
it('node -e with error exits non-zero', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const proc = kernel.spawn('node', ['-e', 'throw new Error("boom")']);
const code = await proc.wait();
expect(code).not.toBe(0);
});
it('node script reads from VFS', async () => {
const vfs = new SimpleVFS();
await vfs.writeFile('/app/hello.js', 'console.log("from vfs")');
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['/app/hello.js'], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
expect(code).toBe(0);
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(output).toContain('from vfs');
});
it('node script with missing file exits non-zero', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const errChunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['/nonexistent.js'], {
onStderr: (data) => errChunks.push(data),
});
const code = await proc.wait();
expect(code).not.toBe(0);
const stderr = errChunks.map(c => new TextDecoder().decode(c)).join('');
expect(stderr).toContain('Cannot find module');
});
it('node -p evaluates expression', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-p', '1 + 2'], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
expect(code).toBe(0);
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(output).toContain('3');
});
it('node with no args exits non-zero', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const proc = kernel.spawn('node', []);
const code = await proc.wait();
expect(code).not.toBe(0);
});
it('dispose cleans up active isolates', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
const driver = createNodeRuntime();
await kernel.mount(driver);
await kernel.dispose();
// Double dispose is safe
await kernel.dispose();
});
});
describe('KernelCommandExecutor routing', () => {
let kernel: Kernel;
afterEach(async () => {
await kernel?.dispose();
});
it('child_process.spawnSync routes through kernel â spy driver records call', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
// Spy driver records every spawn call for later assertion
const spy = { calls: [] as { command: string; args: string[]; callerPid: number }[] };
const spyDriver = new MockRuntimeDriver(['echo'], {
echo: { exitCode: 0, stdout: 'spy-echo-output' },
});
const originalSpawn = spyDriver.spawn.bind(spyDriver);
spyDriver.spawn = (command: string, args: string[], ctx: ProcessContext): DriverProcess => {
spy.calls.push({ command, args: [...args], callerPid: ctx.ppid });
return originalSpawn(command, args, ctx);
};
await kernel.mount(spyDriver);
await kernel.mount(createNodeRuntime());
// spawnSync passes command and args directly (no bash -c wrapping)
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', `
const { spawnSync } = require('child_process');
const result = spawnSync('echo', ['hello']);
console.log('child output:', result.stdout.toString().trim());
`], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
// Spy proves routing happened â not just that output appeared
expect(spy.calls.length).toBe(1);
expect(spy.calls[0].command).toBe('echo');
expect(spy.calls[0].args).toEqual(['hello']);
expect(spy.calls[0].callerPid).toBeGreaterThan(0);
expect(code).toBe(0);
expect(output).toContain('spy-echo-output');
});
it('child_process stdout works with readline consumers', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', `
const { spawn } = require('child_process');
const readline = require('readline');
const child = spawn('node', ['-e', 'console.log("child-line")']);
const rl = readline.createInterface({ input: child.stdout });
rl.on('line', (line) => {
console.log('line:' + line);
});
child.on('exit', (code) => {
console.log('exit:' + code);
});
`], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(code).toBe(0);
expect(output).toContain('line:child-line');
expect(output).toContain('exit:0');
});
});
describe('stdin streaming', () => {
let kernel: Kernel;
afterEach(async () => {
await kernel?.dispose();
});
it('writeStdin delivers data to Node process', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', `
let d = '';
process.stdin.on('data', c => d += c);
process.stdin.on('end', () => console.log(d.trim()));
`], {
onStdout: (data) => chunks.push(data),
});
proc.writeStdin(new TextEncoder().encode('hello from stdin'));
proc.closeStdin();
const code = await proc.wait();
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(code).toBe(0);
expect(output).toContain('hello from stdin');
});
it('closeStdin without write triggers empty EOF', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', `
const data = process.stdin.read();
console.log(data === null ? 0 : data.length);
`], {
onStdout: (data) => chunks.push(data),
});
proc.closeStdin();
const code = await proc.wait();
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(code).toBe(0);
expect(output).toContain('0');
});
it('streamStdin writeStdin delivers data exactly once per write', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
// Script: count every stdin data event and log each chunk to stderr
const stderrChunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', `
let count = 0;
process.stdin.on('data', (chunk) => {
count++;
const text = typeof chunk === 'string' ? chunk : new TextDecoder().decode(chunk);
process.stderr.write('CHUNK:' + count + ':' + text.trim() + '\\n');
});
process.stdin.on('end', () => {
process.stderr.write('TOTAL:' + count + '\\n');
});
`], {
streamStdin: true,
onStderr: (data) => stderrChunks.push(data),
});
// Write 3 messages with small delays between them
const enc = new TextEncoder();
proc.writeStdin(enc.encode('msg1\n'));
await new Promise(r => setTimeout(r, 100));
proc.writeStdin(enc.encode('msg2\n'));
await new Promise(r => setTimeout(r, 100));
proc.writeStdin(enc.encode('msg3\n'));
await new Promise(r => setTimeout(r, 100));
proc.closeStdin();
const code = await proc.wait();
const stderr = stderrChunks.map(c => new TextDecoder().decode(c)).join('');
expect(code).toBe(0);
// Each message must arrive exactly once â no doubling
const chunkLines = stderr.split('\n').filter(l => l.startsWith('CHUNK:'));
expect(chunkLines).toHaveLength(3);
expect(chunkLines[0]).toContain('msg1');
expect(chunkLines[1]).toContain('msg2');
expect(chunkLines[2]).toContain('msg3');
// Total must be exactly 3
expect(stderr).toContain('TOTAL:3');
});
it('concurrent streamStdin processes receive data independently', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const stderrA: Uint8Array[] = [];
const stderrB: Uint8Array[] = [];
// Spawn two echo-stdin processes with streamStdin
const procA = kernel.spawn('node', ['-e', `
process.stdin.on('data', (d) => {
const text = typeof d === 'string' ? d : new TextDecoder().decode(d);
process.stderr.write('A:' + text.trim() + '\\n');
});
`], {
streamStdin: true,
onStderr: (data) => stderrA.push(data),
});
const procB = kernel.spawn('node', ['-e', `
process.stdin.on('data', (d) => {
const text = typeof d === 'string' ? d : new TextDecoder().decode(d);
process.stderr.write('B:' + text.trim() + '\\n');
});
`], {
streamStdin: true,
onStderr: (data) => stderrB.push(data),
});
// Wait for both to start
await new Promise(r => setTimeout(r, 500));
// Write to each process
const enc = new TextEncoder();
procA.writeStdin(enc.encode('hello-A\n'));
procB.writeStdin(enc.encode('hello-B\n'));
await new Promise(r => setTimeout(r, 500));
const outA = stderrA.map(c => new TextDecoder().decode(c)).join('');
const outB = stderrB.map(c => new TextDecoder().decode(c)).join('');
// Each process must receive only its own data
expect(outA).toContain('A:hello-A');
expect(outB).toContain('B:hello-B');
// Data must not cross between processes
expect(outA).not.toContain('hello-B');
expect(outB).not.toContain('hello-A');
procA.kill();
procB.kill();
});
});
describe('exploit/abuse paths', () => {
let kernel: Kernel;
afterEach(async () => {
await kernel?.dispose();
});
it('cannot escape isolate via process.exit', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const proc = kernel.spawn('node', ['-e', 'process.exit(42)']);
const code = await proc.wait();
// process.exit should not crash the host â just exit the isolate
expect(typeof code).toBe('number');
expect(code).not.toBe(0);
});
it('fs.readFileSync /etc/passwd returns error', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', `
const fs = require('fs');
try {
fs.readFileSync('/etc/passwd', 'utf8');
console.log('FAIL:no-error');
} catch (e) {
console.log('code:' + e.code);
}
`], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(code).toBe(0);
// Deny-by-default permissions block host path access
expect(output).toContain('code:EACCES');
expect(output).not.toContain('FAIL:no-error');
});
it('symlink traversal to /etc/passwd returns error', async () => {
const vfs = new SimpleVFS();
await vfs.createDir('/tmp');
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', `
const fs = require('fs');
try { fs.symlinkSync('/etc/passwd', '/tmp/escape'); } catch (e) {}
try {
fs.readFileSync('/tmp/escape', 'utf8');
console.log('FAIL:no-error');
} catch (e) {
console.log('code:' + e.code);
}
`], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(code).toBe(0);
// Symlink to host path is blocked by permissions
expect(output).toContain('code:EACCES');
expect(output).not.toContain('FAIL:no-error');
});
it('relative path traversal ../../etc/passwd returns error', async () => {
const vfs = new SimpleVFS();
await vfs.createDir('/app');
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', `
const fs = require('fs');
try {
fs.readFileSync('../../etc/passwd', 'utf8');
console.log('FAIL:no-error');
} catch (e) {
console.log('code:' + e.code);
}
`], {
onStdout: (data) => chunks.push(data),
cwd: '/app',
});
const code = await proc.wait();
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(code).toBe(0);
// Relative traversal to host path is blocked by permissions
expect(output).toContain('code:EACCES');
expect(output).not.toContain('FAIL:no-error');
});
it('concurrent child process spawning assigns unique PIDs', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
// Spy driver records child PIDs assigned by the kernel
const childPids: number[] = [];
const spyDriver = new MockRuntimeDriver(['echo'], {
echo: { exitCode: 0, stdout: 'ok' },
});
const originalSpawn = spyDriver.spawn.bind(spyDriver);
spyDriver.spawn = (command: string, args: string[], ctx: ProcessContext): DriverProcess => {
childPids.push(ctx.pid);
return originalSpawn(command, args, ctx);
};
await kernel.mount(spyDriver);
await kernel.mount(createNodeRuntime());
// Spawn 12 child processes via spawnSync â each routed through kernel
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', `
const { spawnSync } = require('child_process');
const results = [];
for (let i = 0; i < 12; i++) {
const r = spawnSync('echo', [String(i)]);
results.push(r.status === 0 ? 'ok' : 'err');
}
console.log(results.join(','));
`], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(code).toBe(0);
expect(output).toContain('ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok');
// Spy proves each child got a unique PID from kernel process table
expect(childPids.length).toBe(12);
const uniquePids = new Set(childPids);
expect(uniquePids.size).toBe(12);
});
});
describe('CJS event loop pumping', () => {
let kernel: Kernel;
afterEach(async () => {
await kernel?.dispose();
});
it('setTimeout callback fires before CJS script exits', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', `
setTimeout(() => {
console.log("TIMER_FIRED");
process.exit(0);
}, 100);
`], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(code).toBe(0);
expect(output).toContain('TIMER_FIRED');
});
it('async main with console.log produces output', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', `
async function main() {
console.log("ASYNC_HELLO");
}
main();
`], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(code).toBe(0);
expect(output).toContain('ASYNC_HELLO');
});
it('chained setTimeout callbacks all execute', async () => {
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime());
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('node', ['-e', `
let count = 0;
function step() {
count++;
console.log("STEP:" + count);
if (count < 3) {
setTimeout(step, 50);
} else {
process.exit(0);
}
}
setTimeout(step, 50);
`], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(code).toBe(0);
expect(output).toContain('STEP:1');
expect(output).toContain('STEP:2');
expect(output).toContain('STEP:3');
});
});
describe('bare command resolution from node_modules/.bin', () => {
let kernel: Kernel;
let tmpDir: string;
function createMockBinDir() {
tmpDir = join(tmpdir(), `se-bin-test-${Date.now()}`);
const binDir = join(tmpDir, 'node_modules', '.bin');
const pkgDir = join(tmpDir, 'node_modules', 'my-tool', 'dist');
mkdirSync(binDir, { recursive: true });
mkdirSync(pkgDir, { recursive: true });
// Create a real JS entry file
writeFileSync(
join(pkgDir, 'cli.js'),
'console.log("hello from bare command");',
);
// Create a pnpm-style shell wrapper in .bin
writeFileSync(
join(binDir, 'my-tool'),
[
'#!/bin/sh',
'basedir=$(dirname "$(echo "$0" | sed -e \'s,\\\\,/,g\')")',
'exec node "$basedir/../my-tool/dist/cli.js" "$@"',
].join('\n'),
{ mode: 0o755 },
);
return tmpDir;
}
afterEach(async () => {
await kernel?.dispose();
if (tmpDir) {
try { rmSync(tmpDir, { recursive: true, force: true }); } catch {}
}
});
it('tryResolve returns true for bare command in node_modules/.bin', () => {
createMockBinDir();
const driver = createNodeRuntime({ moduleAccessCwd: tmpDir });
expect(driver.tryResolve!('my-tool')).toBe(true);
});
it('tryResolve returns false for unknown bare command', () => {
createMockBinDir();
const driver = createNodeRuntime({ moduleAccessCwd: tmpDir });
expect(driver.tryResolve!('nonexistent-tool')).toBe(false);
});
it('tryResolve returns false when moduleAccessCwd is not set', () => {
const driver = createNodeRuntime();
expect(driver.tryResolve!('my-tool')).toBe(false);
});
it('bare command executes the resolved JS entry point', async () => {
createMockBinDir();
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime({ moduleAccessCwd: tmpDir }));
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('my-tool', [], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
expect(code).toBe(0);
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(output).toContain('hello from bare command');
});
it('bare command runs successfully even when args are passed', async () => {
createMockBinDir();
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime({ moduleAccessCwd: tmpDir }));
// Spawn with extra args â should not crash
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('my-tool', ['--flag', 'value'], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
expect(code).toBe(0);
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(output).toContain('hello from bare command');
});
it('handles direct node shebang scripts (npm/yarn symlink style)', async () => {
createMockBinDir();
// Replace the shell wrapper with a direct node script
writeFileSync(
join(tmpDir, 'node_modules', '.bin', 'my-tool'),
'#!/usr/bin/env node\nconsole.log("direct node script");',
{ mode: 0o755 },
);
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime({ moduleAccessCwd: tmpDir }));
const chunks: Uint8Array[] = [];
const proc = kernel.spawn('my-tool', [], {
onStdout: (data) => chunks.push(data),
});
const code = await proc.wait();
expect(code).toBe(0);
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(output).toContain('direct node script');
});
});
describe('dispose cleanup (no dangling handles)', () => {
it('kernel.dispose after killing a streamStdin process leaves no active handles', async () => {
const k = createKernel({ filesystem: new SimpleVFS() });
await k.mount(createNodeRuntime());
// Write a long-running stdin reader script
await k.writeFile('/tmp/reader.mjs', new TextEncoder().encode(
`process.stdin.setEncoding('utf8');\n` +
`process.stdin.on('data', (d) => process.stdout.write('GOT:' + d));\n`
));
const chunks: Uint8Array[] = [];
const proc = k.spawn('node', ['/tmp/reader.mjs'], {
streamStdin: true,
onStdout: (data) => chunks.push(data),
});
// Send data and verify it's received
proc.writeStdin('hello\n');
await new Promise(r => setTimeout(r, 200));
const output = chunks.map(c => new TextDecoder().decode(c)).join('');
expect(output).toContain('GOT:hello');
// Kill the process
proc.kill();
const code = await proc.wait();
expect(code).toBe(143); // SIGTERM = 128 + 15
// Dispose the kernel â if the IPC socket is still ref'd, vitest hangs
await k.dispose();
}, 10_000);
it('kernel.dispose after killing a streamStdin process closes stdin source', async () => {
const k = createKernel({ filesystem: new SimpleVFS() });
await k.mount(createNodeRuntime());
// Write a script that blocks on stdin
await k.writeFile('/tmp/blocker.mjs', new TextEncoder().encode(
`process.stdin.resume();\n` +
`process.stdin.on('data', () => {});\n`
));
const proc = k.spawn('node', ['/tmp/blocker.mjs'], {
streamStdin: true,
});
// Kill immediately â stdin source should be closed
proc.kill();
const code = await proc.wait();
expect(code).toBe(143);
await k.dispose();
// Test passes if it completes without hanging
}, 10_000);
});
});
describe('includeNodeShims option', () => {
let kernel: Kernel;
afterEach(async () => {
await kernel?.dispose();
});
it('globalThis.fs is undefined when includeNodeShims is false', async () => {
// With includeNodeShims: false, the bridge does NOT inject fs/http/etc.
// onto globalThis. This is useful for AI agents that need a clean scope.
const vfs = new SimpleVFS();
kernel = createKernel({ filesystem: vfs as any });
await kernel.mount(createNodeRuntime({ includeNodeShims: false }));
// Use exec() with node fallback (fixes #64 exec falls back to node
// when sh is not registered)
const result = await kernel.exec(
ode -e "console.log(typeof fs)",
);
expect(result.exitCode).toBe(0);
expect(result.stdout.trim()).toBe('undefined');
});
it('globalThis.fs is an object when includeNodeShims is true (default)', async () => {
// Default behavior: bridge injects fs, http, process, Buffer etc. onto globalThis.
const vfs = new SimpleVFS();