/** Tagged managed Go operation metadata. This structural reader grants no * source/image correspondence, filesystem custody, catalog admission or exec. */ import { posix } from 'node:path'; export interface NativeGoKitReference { package: { name: string; version: string }; target: 'wasm32-wali-go-single-thread'; releaseSha256: string; producerSha256: string; sourceSha256: string; selection: { path: string; sha256: string }; recipe: { path: string; sha256: string }; installedRoot: string; } export interface NativeGoOperation { sourceService: string; role: 'server' | 'prerequisite' | 'health'; kit: string; program: { name: string; version: string; manifestId: string; manifestSha256: string; path: string }; /** Resolved arguments require separate comparison with the full source. */ args: string[]; /** Names of existing World values, never caller secrets in this record. */ environmentBindings: Record; requires: { service: string; condition: 'service_started' | 'service_healthy' | 'service_completed_successfully' }[]; } export interface NativeGoReference { version: 2; implementation: string; release: string; directory: string; snapshotSha256: string; declarationSha256: string; image: string; kits: Record; server: NativeGoOperation; prerequisites: NativeGoOperation[]; health: NativeGoOperation; } const fail = (field: string): never => { throw new Error('Native Go operation metadata is invalid: ' + field); }; function object(value: unknown, fields: readonly string[], label: string): Record { if (!value || typeof value !== 'object' || Array.isArray(value)) return fail(label); const row = value as Record; if (Object.keys(row).length !== fields.length || fields.some(key => !Object.hasOwn(row, key))) return fail(label); return row; } function text(value: unknown, label: string): string { if (typeof value !== 'string' || !value || value.includes('\0')) return fail(label); return value; } function digest(value: unknown, label: string): string { const out = text(value, label); if (!/^[a-f0-9]{64}$/u.test(out)) return fail(label); return out; } function relativePath(value: unknown, label: string): string { const out = text(value, label); if (out.startsWith('/') || out.includes('\\') || posix.normalize(out) !== out || out.split('/').some(part => !part || part === '.' || part === '..')) return fail(label); return out; } function installedPath(value: unknown, label: string): string { const out = text(value, label); if (!out.startsWith('/opt/programs/') || out.includes('\\') || posix.normalize(out) !== out || out.split('/').slice(1).some(part => !part || part === '.' || part === '..')) return fail(label); return out; } function pin(value: unknown, label: string): { path: string; sha256: string } { const row = object(value, ['path', 'sha256'], label); return { path: relativePath(row.path, label + '.path'), sha256: digest(row.sha256, label + '.sha256') }; } function kit(value: unknown, label: string): NativeGoKitReference { const row = object(value, ['package', 'target', 'releaseSha256', 'producerSha256', 'sourceSha256', 'selection', 'recipe', 'installedRoot'], label); const pack = object(row.package, ['name', 'version'], label + '.package'); if (row.target !== 'wasm32-wali-go-single-thread') return fail(label + '.target'); return { package: { name: text(pack.name, label + '.package.name'), version: text(pack.version, label + '.package.version') }, target: row.target, releaseSha256: digest(row.releaseSha256, label + '.releaseSha256'), producerSha256: digest(row.producerSha256, label + '.producerSha256'), sourceSha256: digest(row.sourceSha256, label + '.sourceSha256'), selection: pin(row.selection, label + '.selection'), recipe: pin(row.recipe, label + '.recipe'), installedRoot: installedPath(row.installedRoot, label + '.installedRoot') }; } function operation(value: unknown, role: NativeGoOperation['role'], kits: Record, label: string): NativeGoOperation { const row = object(value, ['sourceService', 'role', 'kit', 'program', 'args', 'environmentBindings', 'requires'], label); const selected = text(row.kit, label + '.kit'); if (row.role !== role || !Object.hasOwn(kits, selected)) return fail(label + '.role/kit'); const provider = kits[selected]!; const command = object(row.program, ['name', 'version', 'manifestId', 'manifestSha256', 'path'], label + '.program'); const path = installedPath(command.path, label + '.program.path'), version = text(command.version, label + '.program.version'); if (version !== provider.package.version || !path.startsWith(provider.installedRoot + '/')) return fail(label + '.program placement/version'); if (!Array.isArray(row.args)) return fail(label + '.args'); const args: string[] = []; for (const arg of row.args) { if (typeof arg !== 'string' || arg.includes('\0')) return fail(label + '.args.' + args.length); args.push(arg); } if (!row.environmentBindings || typeof row.environmentBindings !== 'object' || Array.isArray(row.environmentBindings)) return fail(label + '.environmentBindings'); const bindings: Record = Object.create(null); for (const [name, value] of Object.entries(row.environmentBindings)) { const source = text(value, label + '.environmentBindings.' + name); if (![name, source].every(item => /^[A-Za-z_][A-Za-z0-9_]*$/u.test(item))) return fail(label + '.environmentBindings name'); bindings[name] = source; } if (!Array.isArray(row.requires)) return fail(label + '.requires'); const dependencies: NativeGoOperation['requires'] = []; for (const value of row.requires) { const edge = object(value, ['service', 'condition'], label + '.requires'); if (edge.condition !== 'service_started' && edge.condition !== 'service_healthy' && edge.condition !== 'service_completed_successfully') return fail(label + '.requires.condition'); const service = text(edge.service, label + '.requires.service'); if (dependencies.some(prior => prior.service === service)) return fail(label + '.requires duplicate service'); dependencies.push({ service, condition: edge.condition }); } return { sourceService: text(row.sourceService, label + '.sourceService'), role, kit: selected, program: { name: text(command.name, label + '.program.name'), version, manifestId: text(command.manifestId, label + '.program.manifestId'), manifestSha256: digest(command.manifestSha256, label + '.program.manifestSha256'), path }, args, environmentBindings: bindings, requires: dependencies }; } /** All fields are declared data. The later owned consumer must authenticate * the complete source, kit, original image and admitted installed manifests. */ export function nativeGoReference(value: unknown): NativeGoReference { const row = object(value, ['version', 'implementation', 'release', 'directory', 'snapshotSha256', 'declarationSha256', 'image', 'kits', 'server', 'prerequisites', 'health'], 'reference'); if (row.version !== 2) return fail('version'); const image = text(row.image, 'image'); if (!/^[^\s@]+@sha256:[a-f0-9]{64}$/u.test(image)) return fail('image'); if (!row.kits || typeof row.kits !== 'object' || Array.isArray(row.kits) || !Object.keys(row.kits).length) return fail('kits'); const kits: Record = Object.create(null); for (const [name, value] of Object.entries(row.kits)) kits[text(name, 'kit name')] = kit(value, 'kits.' + name); const server = operation(row.server, 'server', kits, 'server'), health = operation(row.health, 'health', kits, 'health'); const implementation = text(row.implementation, 'implementation'), release = text(row.release, 'release'); if (kits[server.kit]!.package.name !== implementation || kits[server.kit]!.package.version !== release || health.sourceService !== server.sourceService) return fail('server/health implementation'); if (!Array.isArray(row.prerequisites)) return fail('prerequisites'); const prerequisites: NativeGoOperation[] = []; for (const value of row.prerequisites) prerequisites.push(operation(value, 'prerequisite', kits, 'prerequisites.' + prerequisites.length)); if (new Set(prerequisites.map(item => item.sourceService)).size !== prerequisites.length || prerequisites.some(item => item.sourceService === server.sourceService)) return fail('prerequisite service'); const usedKits = new Set([server.kit, health.kit, ...prerequisites.map(item => item.kit)]); if (Object.keys(kits).some(name => !usedKits.has(name))) return fail('unconsumed kit'); for (const item of prerequisites) if (!server.requires.some(edge => edge.service === item.sourceService && edge.condition === 'service_completed_successfully')) return fail('server prerequisite completion edge'); return { version: 2, implementation, release, directory: relativePath(row.directory, 'directory'), snapshotSha256: digest(row.snapshotSha256, 'snapshotSha256'), declarationSha256: digest(row.declarationSha256, 'declarationSha256'), image, kits, server, prerequisites, health }; }