Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249import { constants, } from 'node:fs';import { open, } from 'node:fs/promises';
//region Native executable recognition
/** Header byte count covering supported native executable signatures. */const NATIVE_EXECUTABLE_HEADER_BYTES = 4;
/** Bytes per kibibyte used to express script inspection bound. */const KIBIBYTE_BYTES = 1_024;
/** Kibibytes inspected from each script candidate. */const SELF_SHIM_INSPECTION_KIBIBYTES = 64;
/** Maximum bytes inspected from script candidates. Generated command shims are launchers; a fixed bound prevents arbitrary PATH files from controlling resolver memory use. */const MAX_SELF_SHIM_INSPECTION_BYTES = SELF_SHIM_INSPECTION_KIBIBYTES * KIBIBYTE_BYTES;
/** Hex prefixes for ELF, PE, Mach-O, and universal Mach-O executables. */const NATIVE_EXECUTABLE_HEX_PREFIXES: ReadonlySet<string> = new Set([ '7f454c46', '4d5a', 'feedface', 'feedfacf', 'cefaedfe', 'cffaedfe', 'cafebabe', 'bebafeca', 'cafebabf', 'bfbafeca',],);
/** Reports whether captured bytes identify a supported native executable format. @param header - Candidate file prefix. @returns Whether prefix is ELF, PE, Mach-O, or universal Mach-O. @example ```ts isNativeExecutableHeader(Buffer.from('7f454c46', 'hex')); // => true ``` */function isNativeExecutableHeader(header: Uint8Array,): boolean { /** Hexadecimal prefix compared without platform-endian conversion. */ const hex = Buffer.from(header,) .toString('hex'); return [...NATIVE_EXECUTABLE_HEX_PREFIXES,].some(function matchesNativePrefix(prefix,) { return hex.startsWith(prefix,); },);}
//endregion Native executable recognition
//region Git policy wrapper markers
/** Reports PATH candidate whose opened filesystem object is not a regular file. @example ```ts throw new GitCandidateFileTypeError('/tmp/git'); ``` */export class GitCandidateFileTypeError extends Error { /** Stable error classification independent of minification. */ override readonly name = 'GitCandidateFileTypeError';
/** Creates non-regular candidate evidence. @param candidatePath - Opened PATH candidate with unsupported filesystem type. @example ```ts const error = new GitCandidateFileTypeError('/tmp/git'); ``` */ constructor(candidatePath: string,) { super(`Git candidate is not a regular file: ${candidatePath}`,); }}
/** Reports non-native candidate whose script inspection would exceed fixed bound. @example ```ts throw new GitCandidateInspectionLimitError('/tmp/git'); ``` */export class GitCandidateInspectionLimitError extends Error { /** Stable error classification independent of minification. */ override readonly name = 'GitCandidateInspectionLimitError';
/** Creates bounded-inspection rejection evidence. @param candidatePath - Non-native candidate larger than inspection bound. @example ```ts const error = new GitCandidateInspectionLimitError('/tmp/git'); ``` */ constructor(candidatePath: string,) { super(`Non-native Git candidate exceeds script inspection bound: ${candidatePath}`,); }}
/** Package name used by scripts that delegate to Git policy wrapper. */const GIT_POLICY_CLI_PACKAGE_NAME = '@monochromatic-dev/git-policy-cli';
/** Bundle entry path used by pnpm command shims for Git policy wrapper. */const GIT_POLICY_CLI_BUNDLED_ENTRY = 'package/git-policy/cli/dist/final/node/index.mjs';
/** Windows package path emitted when command shims spell package scope with backslash. */const WINDOWS_GIT_POLICY_CLI_PACKAGE_PATH = String.raw`@monochromatic-dev\git-policy-cli`;
/** Windows bundle path emitted by command shims without package metadata. */const WINDOWS_GIT_POLICY_CLI_BUNDLED_ENTRY = String.raw`package\git-policy\cli\dist\final\node\index.mjs`;
/** Text markers that identify scripts delegating to Git policy wrapper. */const SELF_SHIM_MARKERS: ReadonlySet<string> = new Set([ GIT_POLICY_CLI_PACKAGE_NAME, GIT_POLICY_CLI_BUNDLED_ENTRY, WINDOWS_GIT_POLICY_CLI_PACKAGE_PATH, WINDOWS_GIT_POLICY_CLI_BUNDLED_ENTRY,],);
/** Checks whether candidate script delegates to Git policy wrapper. Native executables return after one header read. Only scripts and unknown formats undergo complete text inspection. @param candidatePath - Absolute candidate executable path. @returns Whether candidate delegates to Git policy wrapper. @throws When candidate cannot be opened or read, opened object is not a regular file, or non-native script exceeds inspection bound. @example ```ts await isGitPolicySelfShim('/workspace/node_modules/.bin/git'); ``` */export async function isGitPolicySelfShim(candidatePath: string,): Promise<boolean> { /** Open candidate shared by native header and text fallback inspection. */ await using candidate = await open( candidatePath, constants.O_RDONLY | constants.O_NONBLOCK, ); /** Opened candidate metadata resistant to path replacement after open. */ const candidateStats = await candidate.stat(); if (!candidateStats.isFile()) throw new GitCandidateFileTypeError(candidatePath,); /** Fixed native-signature prefix. */ const header = Buffer.alloc(NATIVE_EXECUTABLE_HEADER_BYTES,); /** Captured prefix length for files shorter than native headers. */ const { bytesRead, } = await candidate.read( header, 0, header.length, 0, ); if (isNativeExecutableHeader( header.subarray( 0, bytesRead, ), )) return false; if (candidateStats.size > MAX_SELF_SHIM_INSPECTION_BYTES) throw new GitCandidateInspectionLimitError(candidatePath,); /** Bounded non-native candidate bytes inspected for self-shim markers. */ const contentBytes = Buffer.alloc(MAX_SELF_SHIM_INSPECTION_BYTES,); /** Captured script bytes up to inspection bound. */ const { bytesRead: contentBytesRead, } = await candidate.read( contentBytes, 0, contentBytes.length, 0, ); /** UTF-8 script prefix containing command shim targets. */ const content = contentBytes .subarray( 0, contentBytesRead, ) .toString('utf8'); return [...SELF_SHIM_MARKERS,].some(function hasSelfShimMarker(marker,) { return content.includes(marker,); },);}
//endregion Git policy wrapper markers