Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

escape-attempts.test.ts

130 lines4.5 KB

1import { describe, expect, it } from 'vitest'
2import { createNodeIsolateDriver, probeIsolatedVm } from '../src/isolate-driver'
3​
4const addonAvailable = probeIsolatedVm().compatible
5​
6/**
7 * Run a user snippet inside a fresh Node isolate and return the result.
8 * Each test gets its own context so side effects cannot bleed between them.
9 */
10async function runInIsolate(
11 code: string,
12 opts?: { timeout?: number },
13): Promise<{
14 success: boolean
15 value: unknown
16 error?: { name: string; message: string }
17}> {
18 const driver = createNodeIsolateDriver()
19 const context = await driver.createContext({
20 bindings: {},
21 ...(opts?.timeout !== undefined ? { timeout: opts.timeout } : {}),
22 })
23 try {
24 const res = await context.execute(code)
25 return {
26 success: res.success,
27 value: res.value,
28 ...(res.error !== undefined ? { error: res.error } : {}),
29 }
30 } finally {
31 await context.dispose()
32 }
34​
35describe.skipIf(!addonAvailable)(
36 'Node isolate — sandbox escape attempts',
37 () => {
38 it('does not expose `process`', async () => {
39 const res = await runInIsolate('return typeof process')
40 expect(res.success).toBe(true)
41 expect(res.value).toBe('undefined')
42 })
43​
44 it('does not expose `require`', async () => {
45 const res = await runInIsolate('return typeof require')
46 expect(res.success).toBe(true)
47 expect(res.value).toBe('undefined')
48 })
49​
50 it('does not expose `fetch`', async () => {
51 const res = await runInIsolate('return typeof fetch')
52 expect(res.success).toBe(true)
53 expect(res.value).toBe('undefined')
54 })
55​
56 it('does not expose Node built-ins via dynamic import', async () => {
57 const res = await runInIsolate(
58 `try { return await import('fs') } catch (e) { return 'blocked: ' + e.message }`,
59 )
60 // Either: isolate throws because import is unavailable, or the result
61 // is an error string. The sandbox must NOT return the actual fs module.
62 if (res.success) {
63 expect(String(res.value)).toMatch(/blocked/i)
64 } else {
65 expect(res.error).toBeDefined()
66 }
67 })
68​
69 it('cannot poll Object.prototype to reach host state', async () => {
70 const res = await runInIsolate(`
71 Object.prototype.__sandboxLeak = 'leaked'
72 return 'done'
73 `)
74 // Inside the sandbox, the pollution is possible but scoped to the
75 // isolate's own Object constructor. It MUST NOT leak to the host process.
76 expect(
77 (Object.prototype as { __sandboxLeak?: unknown }).__sandboxLeak,
78 ).toBeUndefined()
79 // Cleanup just in case
80 delete (Object.prototype as { __sandboxLeak?: unknown }).__sandboxLeak
81 expect(res.success).toBe(true)
82 })
83​
84 it('does not leak prototype pollution between separate contexts', async () => {
85 const driver = createNodeIsolateDriver()
86 const ctxA = await driver.createContext({ bindings: {} })
87 const ctxB = await driver.createContext({ bindings: {} })
88 try {
89 await ctxA.execute(`Object.prototype.__ctxAProbe = 'a'; return 1;`)
90 const res = await ctxB.execute(`return ({}).__ctxAProbe`)
91 expect(res.success).toBe(true)
92 expect(res.value).toBeUndefined()
93 } finally {
94 await ctxA.dispose()
95 await ctxB.dispose()
96 }
97 })
98​
99 it('terminates a synchronous CPU-spin loop via timeout (does not hang)', async () => {
100 const start = Date.now()
101 const res = await runInIsolate('while (true) {}', { timeout: 200 })
102 const elapsed = Date.now() - start
103 expect(res.success).toBe(false)
104 // Must actually stop, not hang for multiple seconds
105 expect(elapsed).toBeLessThan(5000)
106 })
107​
108 it('rejects attempts to redefine Function.prototype.constructor to escape', async () => {
109 // Even if user code tries to grab Function constructors, they operate
110 // inside the isolate — they cannot reach the host.
111 const res = await runInIsolate(`
112 try {
113 const C = (function(){}).constructor
114 return typeof C('return process')()
115 } catch (e) {
116 return 'blocked: ' + e.message
117 }
118 `)
119 // The inner Function() executes inside the isolate; 'process' is
120 // undefined there, so either "undefined" or "blocked" is acceptable —
121 // what's NOT acceptable is getting a real process object.
122 expect(res.success).toBe(true)
123 expect(['undefined', 'blocked:']).toContainEqual(
124 typeof res.value === 'string' && res.value.startsWith('blocked:')
125 ? 'blocked:'
126 : res.value,
127 )
128 })
129 },