okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-split-s3: Control bundled in the installer, local engine components, no runtime acquisitionbe885026f

main
4.0 KiB77 linesraw
1/**
2 * A preload (`node --import acquire-spy.mjs server/index.mjs`) that watches a host process for anything that acquires
3 * software, records each attempt as a JSON line in $ACQUIRE_SPY_LOG and blocks it:
4 *   - fetch / http(s) requests / sockets to anything but loopback;
5 *   - a child process that is a package manager, a compiler, git, a downloader or `python -m pip`.
6 * The test that uses it fails if the log has a line. A host that was never allowed to acquire leaves it empty.
7 */
8import { appendFileSync } from 'node:fs';
9import { basename } from 'node:path';
10import childProcess from 'node:child_process';
11import http from 'node:http';
12import https from 'node:https';
13import net from 'node:net';
14import { syncBuiltinESMExports } from 'node:module';
15
16const LOG = process.env.ACQUIRE_SPY_LOG;
17const note = (kind, detail) => { try { appendFileSync(LOG, JSON.stringify({ kind, detail }) + '\n'); } catch { /* the test reads the log */ } };
18const LOOPBACK = new Set(['127.0.0.1', 'localhost', '::1', '[::1]', '0.0.0.0', '']);
19const FORBIDDEN = new Set(['pip', 'pip3', 'npm', 'npx', 'pnpm', 'yarn', 'git', 'curl', 'wget', 'cmake', 'ninja', 'cl', 'link', 'msbuild',
20  'winget', 'choco', 'scoop', 'apt', 'apt-get', 'dnf', 'pacman', 'brew', 'make', 'gcc', 'g++', 'cc', 'clang', 'rustup', 'cargo']);
21
22const hostOf = value => {
23  try { return new URL(typeof value === 'string' ? value : value?.url ?? String(value)).hostname.replace(/^\[|\]$/g, ''); } catch { return ''; }
24};
25
26const realFetch = globalThis.fetch;
27globalThis.fetch = (input, init) => {
28  const host = hostOf(input);
29  if (!LOOPBACK.has(host)) { note('fetch', String(input?.url ?? input)); return Promise.reject(new TypeError('blocked by the acquisition spy')); }
30  return realFetch(input, init);
31};
32
33for (const [module, label] of [[http, 'http'], [https, 'https']]) {
34  for (const name of ['request', 'get']) {
35    const original = module[name];
36    module[name] = function patched(...args) {
37      const first = args[0];
38      const host = typeof first === 'string' || first instanceof URL ? hostOf(first) : (first?.hostname ?? first?.host ?? '').replace(/:\d+$/, '');
39      if (!LOOPBACK.has(host)) { note(`${label}.${name}`, String(host)); throw new Error('blocked by the acquisition spy'); }
40      return original.apply(this, args);
41    };
42  }
43}
44
45const originalConnect = net.Socket.prototype.connect;
46net.Socket.prototype.connect = function patched(...args) {
47  const first = args[0];
48  const host = typeof first === 'object' && first ? (first.host ?? 'localhost') : (typeof args[1] === 'string' ? args[1] : 'localhost');
49  if (typeof first === 'object' && first?.path) return originalConnect.apply(this, args);
50  if (!LOOPBACK.has(String(host))) { note('socket', String(host)); throw new Error('blocked by the acquisition spy'); }
51  return originalConnect.apply(this, args);
52};
53
54const looksLikeAcquisition = (command, args) => {
55  const name = basename(String(command)).toLowerCase().replace(/\.(exe|cmd|bat)$/, '');
56  if (FORBIDDEN.has(name)) return true;
57  const list = (Array.isArray(args) ? args : []).map(String);
58  if (/^python[\d.]*$|^py$/.test(name) && (list.includes('pip') || list.some(a => /(^|[\/])setup\.py$/.test(a)))) return true;
59  return false;
60};
61for (const name of ['spawn', 'spawnSync', 'execFile', 'execFileSync']) {
62  const original = childProcess[name];
63  childProcess[name] = function patched(command, ...rest) {
64    const args = Array.isArray(rest[0]) ? rest[0] : [];
65    if (looksLikeAcquisition(command, args)) { note(name, [command, ...args].join(' ')); throw new Error('blocked by the acquisition spy'); }
66    return original.call(this, command, ...rest);
67  };
68}
69for (const name of ['exec', 'execSync']) {
70  const original = childProcess[name];
71  childProcess[name] = function patched(line, ...rest) {
72    const first = String(line).trim().split(/\s+/)[0];
73    if (looksLikeAcquisition(first, String(line).split(/\s+/).slice(1))) { note(name, String(line)); throw new Error('blocked by the acquisition spy'); }
74    return original.call(this, line, ...rest);
75  };
76}
77syncBuiltinESMExports();