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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320/** Trust record schema and permission validation. @module */import { constants, lstat, open,} from 'node:fs/promises';import { join, } from 'node:path';import { assertRecordDirectoryIdentity, validateSnapshotRelativePath,} from './registry-path.ts';import { assertPrivatePathProtection, } from './registry-io.ts';import type { TrustIdentity, TrustRecord, TrustSourceRecord,} from './types.ts';
/** Group and other permission mask. */const NON_OWNER_PERMISSION_MASK = 0o077;
/** Corrupt or unsafe trust record. */export class TrustRecordError extends Error { /** Creates record validation failure. @param message - safe failure explanation */ public constructor(message: string,) { super(message,); this.name = 'TrustRecordError'; }}
/** Asserts ordinary object record. @param value - unknown candidate @returns Nothing after narrowing candidate */function assertRecord(value: unknown,): asserts value is Record<string, unknown> { if (((typeof value) !== 'object') || (value === null) || Array.isArray(value,)) throw new TrustRecordError('Trust metadata value must be an object.',);}
/** Checks non-empty decimal string. @param value - unknown numeric metadata @returns whether value contains only ASCII decimal digits */function isDecimalString(value: unknown,): value is string { if (((typeof value) !== 'string') || (value.length === 0)) return false; for (let index = 0; index < value.length; index += 1) { if ((value.charAt(index,) < '0') || (value.charAt(index,) > '9')) return false; } return true;}
/** Validates complete trust identity. @param value - unknown identity @returns copied identity */function validateIdentity(value: unknown,): TrustIdentity { assertRecord(value,); if (((typeof value.filesystemId) !== 'string') || (value.filesystemId .length === 0) || value.filesystemId .includes(':',) || ((typeof value.canonicalConfigPath) !== 'string') || (value.canonicalConfigPath .length === 0)) throw new TrustRecordError('Trust identity fields are invalid.',); return { filesystemId: value.filesystemId, canonicalConfigPath: value.canonicalConfigPath, };}
/** Validates one source metadata entry. @param value - unknown source record @returns copied source metadata */function validateSource(value: unknown,): TrustSourceRecord { assertRecord(value,); if (((typeof value.canonicalPath) !== 'string') || (value.canonicalPath .length === 0) || (!isDecimalString(value.size,)) || (!isDecimalString(value.mtimeNanoseconds,))) throw new TrustRecordError('Trust source metadata is invalid.',); return { canonicalPath: value.canonicalPath, snapshotFile: validateSnapshotRelativePath(value.snapshotFile,), size: value.size, mtimeNanoseconds: value.mtimeNanoseconds, };}
/** Parses and validates schema-version-one trust record. @param value - unknown JSON value @returns copied validated record @example ```ts validateTrustRecord(JSON.parse(recordJson)); ``` */export function validateTrustRecord(value: unknown,): TrustRecord { assertRecord(value,); if ((value.schemaVersion !== 1) || ((typeof value.repositoryRoot) !== 'string') || (value.repositoryRoot .length === 0) || ((value.format !== 'mjs') && (value.format !== 'typescript')) || (!Array.isArray(value.sources,)) || (value.sources .length === 0) || (!isDecimalString(value.executableSize,)) || ((typeof value.recursiveChildren) !== 'boolean') || (!Array.isArray(value.authorizingRoots,)) || ((typeof value.recordedAt) !== 'string') || Number.isNaN(Date.parse(value.recordedAt,))) throw new TrustRecordError('Trust record metadata is invalid.',); /** Complete validated record identity. */ const identity = validateIdentity(value.identity,); /** Canonically ordered source metadata. */ const sources = value.sources .map(validateSource,); /** Recursive trust provenance identities. */ const authorizingRoots = value.authorizingRoots .map(validateIdentity,); /** Duplicate-detection keys for provenance identities. */ const provenanceKeys = authorizingRoots.map(function identityKey(authorizer,) { return `${authorizer.filesystemId}:${authorizer.canonicalConfigPath}`; },); if (new Set(provenanceKeys,).size !== provenanceKeys.length) throw new TrustRecordError('Trust authorizing roots contain duplicates.',); return { schemaVersion: 1, identity, repositoryRoot: value.repositoryRoot, format: value.format, sources, executableSnapshotFile: validateSnapshotRelativePath(value.executableSnapshotFile,), executableSize: value.executableSize, recursiveChildren: value.recursiveChildren, authorizingRoots, recordedAt: value.recordedAt, };}
/** Asserts restrictive POSIX mode when platform exposes permission bits. @param mode - stat mode @param label - path diagnostic label */function assertPrivateMode({ mode, label,}: Readonly<{ mode: number; label: string;}>,): void { if ((process.platform !== 'win32') && ((mode & NON_OWNER_PERMISSION_MASK) !== 0)) throw new TrustRecordError(`${label} has unsafe group or other permissions.`,);}
/** Reads one file without following final symbolic link. @param path - absolute file path @returns exact file bytes @example ```ts await readPrivateFile('/private/record.json'); ``` */export async function readPrivateFile(path: string,): Promise<Uint8Array> { await assertPrivatePathProtection({ path, directory: false, },); /** No-follow handle for exact private file. */ const handle = await open( path, constants.O_RDONLY | constants.O_NOFOLLOW, ); /** Automatically closed private file handle. */ await using disposableHandle = handle; /** Same-handle metadata used for type and mode checks. */ const metadata = await handle.stat(); if (!metadata.isFile()) throw new TrustRecordError(`Trust file is not regular: ${path}`,); if ((process.platform !== 'win32') && (process.getuid?.() !== metadata.uid)) throw new TrustRecordError(`Trust file is not owned by current account: ${path}`,); assertPrivateMode({ mode: metadata.mode, label: path, },); return Uint8Array.from(await handle.readFile(),);}
/** Reads and validates record plus identity/path agreement. @param registryRoot - complete registry root @param directory - expected record directory @returns validated trust record @example ```ts await readRecord({ registryRoot: '/r', directory: '/r/records/key' }); ``` */export async function readRecord({ registryRoot, directory,}: Readonly<{ registryRoot: string; directory: string;}>,): Promise<TrustRecord> { /** Final record directory metadata, read before ACL verification so absence retains ENOENT classification. */ const directoryMetadata = await lstat(directory,); if ((!directoryMetadata.isDirectory()) || directoryMetadata.isSymbolicLink()) throw new TrustRecordError('Trust record directory is unsafe.',); await assertPrivatePathProtection({ path: directory, directory: true, },); assertPrivateMode({ mode: directoryMetadata.mode, label: directory, },); /** Exact record JSON bytes. */ const recordBytes = await readPrivateFile(join( directory, 'record.json', ),); /** Parsed JSON retained as unknown until schema validation. */ const parsed: unknown = (function parseRecordJson() { try { /** JSON parser output held as unknown. */ const jsonValue: unknown = JSON.parse(new TextDecoder( 'utf-8', { fatal: true, }, ).decode(recordBytes,),); return jsonValue; } catch (error: unknown) { throw new TrustRecordError(`Trust record JSON is invalid: ${String(error,)}`,); } })(); /** Runtime-authoritative record. */ const record = validateTrustRecord(parsed,); assertRecordDirectoryIdentity({ registryRoot, identity: record.identity, directory, },); return record;}