-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathnode-runtime.test.mts
More file actions
53 lines (47 loc) · 1.88 KB
/
Copy pathnode-runtime.test.mts
File metadata and controls
53 lines (47 loc) · 1.88 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
import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs'
import { safeDelete } from '@socketsecurity/lib-stable/fs/safe'
import os from 'node:os'
import path from 'node:path'
import sea from 'node:sea'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { resolveNodeRuntime } from '../../../../src/util/spawn/node-runtime.mts'
import { spawnNode } from '../../../../src/util/spawn/spawn-node.mts'
import { clearSystemToolCache } from '../../../../src/util/spawn/system-tool.mts'
const temporaryDirectories: string[] = []
afterEach(async () => {
vi.restoreAllMocks()
clearSystemToolCache()
for (const directory of temporaryDirectories.splice(0)) {
await safeDelete(directory)
}
})
describe('Node runtime resolution', () => {
it('uses the current interpreter outside SEA', async () => {
const runtime = await resolveNodeRuntime({ env: { EXAMPLE: 'value' } })
expect(runtime.executable).toBe(process.execPath)
expect(runtime.environment).toEqual({ EXAMPLE: 'value' })
})
it('runs JavaScript in a trusted Node child from SEA', async () => {
vi.spyOn(sea, 'isSea').mockReturnValue(true)
const result = await spawnNode(
['-e', "process.stdout.write(String(require('node:sea').isSea()))"],
{ stdio: 'pipe' },
)
expect(result.code).toBe(0)
expect(result.stdout).toBe('false')
})
it('rejects a checkout-provided Node executable', async () => {
vi.spyOn(sea, 'isSea').mockReturnValue(true)
const root = mkdtempSync(path.join(os.tmpdir(), 'socket-node-runtime-'))
temporaryDirectories.push(root)
const binary = path.join(
root,
process.platform === 'win32' ? 'node.exe' : 'node',
)
mkdirSync(path.join(root, '.git'))
writeFileSync(binary, 'example hostile executable', { mode: 0o755 })
await expect(
resolveNodeRuntime({ cwd: root, env: { PATH: root } }),
).rejects.toThrow()
})
})