okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
be885026f
main
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 => { 23try { 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 ) => { 28const host = hostOf ( input ); 29if ( ! LOOPBACK . has ( host )) { note ( 'fetch' , String ( input ?. url ?? input )); return Promise . reject ( new TypeError ( 'blocked by the acquisition spy' )); } 30return realFetch ( input , init ); 31}; 32 33for ( const [ module , label ] of [[ http , 'http' ], [ https , 'https' ]]) { 34for ( const name of [ 'request' , 'get' ]) { 35const original = module [ name ]; 36module [ name ] = function patched ( ...args ) { 37const first = args [ 0 ]; 38const host = typeof first === 'string' || first instanceof URL ?hostOf ( first ) :( first ?. hostname ?? first ?. host ?? '' ). replace ( / :\d + $ / , '' ); 39if ( ! LOOPBACK . has ( host )) { note ( ` ${ label } . ${ name } ` , String ( host )); throw new Error ( 'blocked by the acquisition spy' ); } 40return original . apply ( this , args ); 41}; 42} 43} 44 45const originalConnect = net . Socket . prototype . connect ; 46net . Socket . prototype . connect = function patched ( ...args ) { 47const first = args [ 0 ]; 48const host = typeof first === 'object' && first ?( first . host ?? 'localhost' ) :( typeof args [ 1 ] === 'string' ?args [ 1 ] :'localhost' ); 49if ( typeof first === 'object' && first ?. path ) return originalConnect . apply ( this , args ); 50if ( ! LOOPBACK . has ( String ( host ))) { note ( 'socket' , String ( host )); throw new Error ( 'blocked by the acquisition spy' ); } 51return originalConnect . apply ( this , args ); 52}; 53 54const looksLikeAcquisition = ( command , args ) => { 55const name = basename ( String ( command )). toLowerCase (). replace ( / \. ( exe | cmd | bat ) $ / , '' ); 56if ( FORBIDDEN . has ( name )) return true ; 57const list = ( Array . isArray ( args ) ?args :[]). map ( String ); 58if ( / ^python [ \d . ] * $ | ^py$ / . test ( name ) && ( list . includes ( 'pip' ) || list . some ( a => / ( ^ | [ \/ ]) setup\.py$ / . test ( a )))) return true ; 59return false ; 60}; 61for ( const name of [ 'spawn' , 'spawnSync' , 'execFile' , 'execFileSync' ]) { 62const original = childProcess [ name ]; 63childProcess [ name ] = function patched ( command , ...rest ) { 64const args = Array . isArray ( rest [ 0 ]) ?rest [ 0 ] :[]; 65if ( looksLikeAcquisition ( command , args )) { note ( name , [ command , ...args ]. join ( ' ' )); throw new Error ( 'blocked by the acquisition spy' ); } 66return original . call ( this , command , ...rest ); 67}; 68} 69for ( const name of [ 'exec' , 'execSync' ]) { 70const original = childProcess [ name ]; 71childProcess [ name ] = function patched ( line , ...rest ) { 72const first = String ( line ). trim (). split ( / \s +/ )[ 0 ]; 73if ( looksLikeAcquisition ( first , String ( line ). split ( / \s +/ ). slice ( 1 ))) { note ( name , String ( line )); throw new Error ( 'blocked by the acquisition spy' ); } 74return original . call ( this , line , ...rest ); 75}; 76} 77syncBuiltinESMExports ();