| 1 | // Ambient Node file access from inside a host plugin. Under the host sandbox |
| 2 | // the read of a Codewhale secret must fail even though the plugin is trusted. |
| 3 | import { readFile, writeFile, readlink } from 'node:fs/promises' |
| 4 | import { connect } from 'node:net' |
| 5 | import { createSocket } from 'node:dgram' |
| 6 | import { execFile } from 'node:child_process' |
| 7 | |
| 8 | export const name = 'secret-probe' |
| 9 | export const inject = ['tools'] |
| 10 | |
| 11 | export function apply(ctx) { |
| 12 | ctx.tools.register({ |
| 13 | name: 'probe_read', |
| 14 | description: 'Read a file with node:fs and report what happened.', |
| 15 | parameters: { type: 'object', properties: { path: { type: 'string' }, child: { type: 'boolean' } }, required: ['path'] }, |
| 16 | async execute(args) { |
| 17 | try { |
| 18 | if (args.child) return await new Promise((resolve, reject) => { |
| 19 | execFile(process.execPath, ['-e', "try { process.stdout.write(JSON.stringify({ ok: true, text: require('node:fs').readFileSync(process.argv[1], 'utf8') })) } catch (error) { process.stdout.write(JSON.stringify({ ok: false, code: error.code })) }", args.path], { encoding: 'utf8', timeout: 5000, maxBuffer: 4096 }, (error, stdout) => { |
| 20 | if (error) reject(error) |
| 21 | else { try { resolve(JSON.parse(stdout)) } catch (error) { reject(error) } } |
| 22 | }) |
| 23 | }) |
| 24 | return { ok: true, text: await readFile(args.path, 'utf8') } |
| 25 | } catch (error) { |
| 26 | return { ok: false, code: error.code ?? String(error) } |
| 27 | } |
| 28 | }, |
| 29 | }) |
| 30 | ctx.tools.register({ |
| 31 | name: 'probe_write', |
| 32 | description: 'Write a file with node:fs and report what happened.', |
| 33 | parameters: { type: 'object', properties: { path: { type: 'string' } }, required: ['path'] }, |
| 34 | async execute(args) { |
| 35 | try { |
| 36 | await writeFile(args.path, 'probe') |
| 37 | return { ok: true } |
| 38 | } catch (error) { |
| 39 | return { ok: false, code: error.code ?? String(error) } |
| 40 | } |
| 41 | }, |
| 42 | }) |
| 43 | ctx.tools.register({ |
| 44 | name: 'probe_connect', |
| 45 | description: 'Connect to the test controller loopback listener and report the OS result.', |
| 46 | parameters: { type: 'object', properties: { port: { type: 'integer', minimum: 1, maximum: 65535 } }, required: ['port'] }, |
| 47 | async execute(args) { |
| 48 | // Linux's private loopback cannot reach the controller's listener. |
| 49 | // Report the actual kernel namespace, rather than infer isolation from errno. |
| 50 | const networkNamespace = process.platform === 'linux' |
| 51 | ? await readlink('/proc/self/ns/net') |
| 52 | : undefined |
| 53 | return new Promise((resolve) => { |
| 54 | const socket = connect({ host: '127.0.0.1', port: args.port }) |
| 55 | const finish = (result) => { |
| 56 | socket.destroy() |
| 57 | resolve(networkNamespace === undefined ? result : { ...result, network_namespace: networkNamespace }) |
| 58 | } |
| 59 | socket.once('connect', () => finish({ ok: true })) |
| 60 | socket.once('error', (error) => finish({ ok: false, code: error.code ?? String(error) })) |
| 61 | socket.setTimeout(1000, () => finish({ ok: false, code: 'ETIMEDOUT' })) |
| 62 | }) |
| 63 | }, |
| 64 | }) |
| 65 | ctx.tools.register({ |
| 66 | name: 'probe_send', |
| 67 | description: 'Send a datagram to the test controller and report the unmodified OS result.', |
| 68 | parameters: { type: 'object', properties: { port: { type: 'integer', minimum: 1, maximum: 65535 } }, required: ['port'] }, |
| 69 | async execute(args) { |
| 70 | return new Promise((resolve) => { |
| 71 | const socket = createSocket('udp4') |
| 72 | let finished = false |
| 73 | const finish = (result) => { |
| 74 | if (finished) return |
| 75 | finished = true |
| 76 | clearTimeout(timer) |
| 77 | // A refused bind leaves the socket unopened; cleanup must not replace |
| 78 | // the original access-denied result with ERR_SOCKET_DGRAM_NOT_RUNNING. |
| 79 | try { socket.close() } catch {} |
| 80 | resolve(result) |
| 81 | } |
| 82 | const failure = (error) => finish({ ok: false, code: error.code ?? String(error), syscall: error.syscall ?? null }) |
| 83 | const timer = setTimeout(() => finish({ ok: false, code: 'ETIMEDOUT' }), 1000) |
| 84 | socket.once('error', failure) |
| 85 | try { |
| 86 | socket.send('native-network-probe', args.port, '127.0.0.1', (error) => { |
| 87 | if (error) failure(error) |
| 88 | else finish({ ok: true }) |
| 89 | }) |
| 90 | } catch (error) { failure(error) } |
| 91 | }) |
| 92 | }, |
| 93 | }) |
| 94 | } |
| 95 |