Add versioned script library, reconcile permit refusals and compact helper captures

This commit is contained in:
2026-11-17 15:45:56 +01:00
parent a576c10e8a
commit 8e2fef7007
29 changed files with 1225 additions and 50 deletions
+12 -2
View File
@@ -2,6 +2,7 @@ import { canonicalJson, record, RecordValue, text, windowsScriptProfile, privile
import { bytes, hashPattern, parseStrictJson, sha256 } from './safety';
export interface ScriptPlanJob {
revision?: { id: string; name: string; version: number };
assetId: string; agentId: string; jobId: string; attemptId: string;
interpreter: string; identity: string; windowsProfile: string; privilegedProfile: string;
script: string; scriptHash: string; jobHash: string; scriptBytes: number;
@@ -61,11 +62,19 @@ export async function verifyScriptPlan(plan: RecordValue): Promise<void> {
if (!Array.isArray(parsed.jobs)) throw new Error('Vollständige feste Jobdefinitionen fehlen');
const jobs = parsed.jobs.map(value => record(value));
validateViews(plan, parsed, jobs);
let revision: ScriptPlanJob['revision'], revisionHash: string | undefined;
if (Object.hasOwn(parsed, 'script_revision')) {
const ref = record(parsed.script_revision);
exact(ref, ['revision_id','name','version','script_sha256']);
revision = { id: uuid(ref.revision_id), name: text(ref.name, 'Skriptname'), version: integer(ref.version, 1, 2147483647) };
revisionHash = digest(ref.script_sha256);
if (!jobs.length || jobs.some(job => !isScript(record(job.payload).action))) throw new Error('Bibliotheksversion benötigt feste Skriptjobs');
}
if (!jobs.some(job => isScript(record(job.payload).action))) {
if (snapshot(plan) !== before) throw new Error('Plan wurde während der Prüfung verändert');
verified.set(plan, { snapshot: before, jobs: [] }); return;
}
exact(parsed, ['schema_version', 'plan_id', 'tenant_id', 'principal', 'reason', 'jobs', 'max_parallel', 'halt_after_failures', ...(Object.hasOwn(parsed, 'recipe_references') ? ['recipe_references'] : []), ...(Object.hasOwn(parsed, 'artifact_references') ? ['artifact_references'] : [])]);
exact(parsed, ['schema_version', 'plan_id', 'tenant_id', 'principal', 'reason', 'jobs', 'max_parallel', 'halt_after_failures', ...(revision ? ['script_revision'] : []), ...(Object.hasOwn(parsed, 'recipe_references') ? ['recipe_references'] : []), ...(Object.hasOwn(parsed, 'artifact_references') ? ['artifact_references'] : [])]);
if (parsed.schema_version !== 1 || !jobs.length || jobs.length > 100) throw new Error('Skriptplan: unbekanntes Schema oder Zielbudget');
uuid(parsed.plan_id); uuid(parsed.tenant_id); text(parsed.principal, 'Antragsteller'); text(parsed.reason, 'Begründung');
integer(parsed.max_parallel, 1, 10); integer(parsed.halt_after_failures, 1, Number.MAX_SAFE_INTEGER);
@@ -94,8 +103,9 @@ export async function verifyScriptPlan(plan: RecordValue): Promise<void> {
const orderedAction = { action: 'run_script', interpreter, script_utf8: script, arguments: action.arguments, expected_stdout: action.expected_stdout, ...(hasWindowsProfile ? { windows_execution_profile: action.windows_execution_profile } : {}), ...(hasPrivilegedProfile ? { privileged_script_profile: action.privileged_script_profile } : {}) };
const ordered = { schema_version: p.schema_version, tenant_id: p.tenant_id, asset_id: assetId, agent_id: agentId, enrollment_generation: p.enrollment_generation, assignment_version: p.assignment_version, job_id: jobId, attempt_id: attemptId, action: orderedAction, execution_identity: identity, not_before: notBefore, start_before: startBefore, max_runtime_seconds: runtimeSeconds, max_output_bytes: outputBytes };
const scriptHash = digest(job.script_sha256), jobHash = digest(job.job_payload_hash);
if (revisionHash && revisionHash !== scriptHash) throw new Error('Skripthash widerspricht der Bibliotheksversion');
if (await sha256(scriptBytes) !== scriptHash || await sha256(new TextEncoder().encode(JSON.stringify(ordered))) !== jobHash) throw new Error('Skript-/Jobhash widerspricht exakten Rust-Payloadbytes');
result.push({ assetId, agentId, jobId, attemptId, interpreter, identity, windowsProfile, privilegedProfile, script, scriptHash, jobHash, scriptBytes: scriptBytes.length, arguments: [...action.arguments] as string[], expectedStdout: action.expected_stdout as string | null, notBefore, startBefore, runtimeSeconds, outputBytes });
result.push({ ...(revision ? { revision } : {}), assetId, agentId, jobId, attemptId, interpreter, identity, windowsProfile, privilegedProfile, script, scriptHash, jobHash, scriptBytes: scriptBytes.length, arguments: [...action.arguments] as string[], expectedStdout: action.expected_stdout as string | null, notBefore, startBefore, runtimeSeconds, outputBytes });
}
if (snapshot(plan) !== before) throw new Error('Skriptplan wurde während der Hashprüfung verändert');
verified.set(plan, { snapshot: before, jobs: result });