Bind approved Windows PowerShell process policy and prove Linux-first contracts
Rust development slice / linux (push) Failing after 6s

This commit is contained in:
2026-11-14 10:06:10 +01:00
parent 8a315706ea
commit abea4eb4d9
28 changed files with 1275 additions and 32 deletions
+90 -2
View File
@@ -1,4 +1,4 @@
import { Action, PlanInput, RecordValue, Session, record, text, validateSystemService } from './api';
import { Action, PlanInput, RecordValue, Session, planTargetPreviews, record, text, validateSystemService, windowsScriptProfile } from './api';
export const hashPattern = /^[0-9a-f]{64}$/;
export const rootWarning = 'HOHES RISIKO: root kann das Zielsystem verändern. Keine automatische Rechteerhöhung; eigener expliziter Plan, Scope, Helper und Genehmigung erforderlich.';
@@ -101,7 +101,87 @@ export function systemPlanJobs(plan: RecordValue): RecordValue[] {
if (!Array.isArray(parsed.jobs)) return [];
return parsed.jobs.map(value => record(record(value).payload)).filter(payload => payload.execution_identity === 'system');
}
export interface PowerShellPlanJob { readonly assetId: string; readonly profile: 'inherited' | 'powershell_51_remote_signed' }
// Only successful verification writes this cache. Projection is copied from the
// verified bytes; later mutations of API views cannot change the displayed plan.
interface VerifiedPowerShellPlan { readonly jobs: readonly PowerShellPlanJob[]; readonly planHash: unknown; readonly planBytes: readonly number[] }
const verifiedPowerShellPlans = new WeakMap<RecordValue, VerifiedPowerShellPlan>();
export function powerShellPlanJobs(plan: RecordValue): readonly PowerShellPlanJob[] {
const verified = verifiedPowerShellPlans.get(plan);
if (!verified) throw new Error('PowerShell-Plan wurde nicht aus hashgeprüften gespeicherten Bytes verifiziert');
if (verified.planBytes.length) {
const current = bytes(plan.plan_payload_bytes);
if (plan.plan_hash !== verified.planHash || current.length !== verified.planBytes.length || verified.planBytes.some((value, i) => current[i] !== value)) throw new Error('PowerShell-Planbytes wurden nach Verifikation verändert');
const parsed = record(parseStrictJson(new TextDecoder('utf-8', { fatal: true }).decode(current)));
validatePlanAliases(plan, parsed);
validatePowerShellViews(plan, parsed.jobs);
} else if (scriptPayloads(planTargetPreviews(plan)).some(hasPowerShellProfile)) {
throw new Error('PowerShell-Plan benötigt exakte gespeicherte Planbytes');
}
return verified.jobs.map(job => ({ ...job }));
}
export function powerShellApprovalBlockers(plan: RecordValue): string[] {
try { powerShellPlanJobs(plan); return []; }
catch (error) { return [error instanceof Error ? error.message : 'PowerShell-Profil ist nicht verifiziert']; }
}
function scriptPayloads(jobs: unknown): RecordValue[] {
if (!Array.isArray(jobs)) return [];
return jobs.flatMap(job => {
if (!job || typeof job !== 'object') return [];
const payload = record(job).payload;
return payload && typeof payload === 'object' ? [record(payload)] : [];
});
}
function hasPowerShellProfile(payload: RecordValue): boolean {
if (Object.prototype.hasOwnProperty.call(payload, 'windows_execution_profile')) throw new Error('Windows-Ausführungsprofil muss Teil der festen Skriptaktion sein');
const action = payload.action;
return action !== null && typeof action === 'object' && (record(action).interpreter === 'powershell'
|| Object.prototype.hasOwnProperty.call(action, 'windows_execution_profile'));
}
function projectPowerShellJobs(jobs: unknown): PowerShellPlanJob[] {
return scriptPayloads(jobs).filter(hasPowerShellProfile).map(payload => {
const action = record(payload.action);
if (action.action !== 'run_script' || action.interpreter !== 'powershell' || payload.execution_identity !== 'agent_service') throw new Error('PowerShell-Profil benötigt Windows PowerShell5.1 als agent_service');
exact(action, ['action', 'interpreter', 'script_utf8', 'arguments', 'expected_stdout', ...(Object.prototype.hasOwnProperty.call(action, 'windows_execution_profile') ? ['windows_execution_profile'] : [])]);
if (typeof action.script_utf8 !== 'string' || !action.script_utf8 || new TextEncoder().encode(action.script_utf8).length > 32768 || action.script_utf8.includes('\0')
|| (!/^[\x00-\x7f]*$/.test(action.script_utf8) && !action.script_utf8.startsWith('\ufeff'))
|| !Array.isArray(action.arguments) || action.arguments.length > 32 || action.arguments.some(arg => typeof arg !== 'string' || new TextEncoder().encode(arg).length > 4096 || arg.includes('\0'))
|| !(action.expected_stdout === null || (typeof action.expected_stdout === 'string' && new TextEncoder().encode(action.expected_stdout).length <= 65536))) throw new Error('Vollständige unveränderte PowerShell-Skriptaktion erforderlich');
return { assetId: text(payload.asset_id, 'PowerShell-Zielasset'), profile: windowsScriptProfile(action) };
});
}
function validatePlanAliases(plan: RecordValue, parsed: unknown): void {
for (const alias of ['plan_payload', 'payload']) if (plan[alias] !== undefined && canonical(parsed) !== canonical(plan[alias])) throw new Error('Planansicht widerspricht gespeicherten Bytes');
}
function validatePowerShellViews(plan: RecordValue, rawJobs: unknown): void {
const rawPayloads = scriptPayloads(rawJobs), views = [planTargetPreviews(plan)];
for (const alias of ['jobs', 'payloads', 'resolved_targets']) {
if (plan[alias] !== undefined && !Array.isArray(plan[alias])) throw new Error('PowerShell-Zielpreview ist keine vollständige Liste');
if (Array.isArray(plan[alias])) views.push(plan[alias]);
}
for (const collection of new Set(views)) if (collection !== null) {
// IDs-only target lists remain metadata. Every supplied payload-bearing
// alias, however, must describe the same complete fixed PowerShell jobs.
if (!collection.some(value => value !== null && typeof value === 'object' && !Array.isArray(value) && 'payload' in value)) continue;
const viewPayloads = scriptPayloads(collection);
for (const fixed of rawPayloads.filter(hasPowerShellProfile)) if (!viewPayloads.some(view => view.asset_id === fixed.asset_id)) throw new Error('PowerShell-Zielpreview fehlt im festen Plan');
for (const view of viewPayloads) {
const raw = rawPayloads.find(payload => payload.asset_id === view.asset_id);
if ((hasPowerShellProfile(view) || (raw && hasPowerShellProfile(raw))) && (!raw || canonical(raw.action) !== canonical(view.action)
|| raw.execution_identity !== view.execution_identity)) throw new Error('PowerShell-Zielpreview widerspricht gespeicherten Planbytes');
}
}
}
export async function verifyPlanIntegrity(plan: RecordValue) {
verifiedPowerShellPlans.delete(plan);
const views = planTargetPreviews(plan), hasScriptView = scriptPayloads(views).some(hasPowerShellProfile)
|| scriptPayloads(plan.jobs).some(hasPowerShellProfile) || scriptPayloads(plan.payloads).some(hasPowerShellProfile)
|| scriptPayloads(plan.resolved_targets).some(hasPowerShellProfile)
|| scriptPayloads(record(plan.plan_payload ?? {}).jobs).some(hasPowerShellProfile)
|| scriptPayloads(record(plan.payload ?? {}).jobs).some(hasPowerShellProfile);
if (hasScriptView && plan.plan_payload_bytes === undefined) throw new Error('PowerShell-Plan benötigt exakte gespeicherte Planbytes');
let powerShellJobs: PowerShellPlanJob[] = [];
let verifiedBytes: readonly number[] = [];
if (plan.plan_payload_bytes === undefined && Array.isArray(plan.jobs) && plan.jobs.some(value => {
const payload = record(value).payload;
return payload !== null && typeof payload === 'object' && record(payload).execution_identity === 'system';
@@ -109,8 +189,15 @@ export async function verifyPlanIntegrity(plan: RecordValue) {
if (plan.plan_payload_bytes !== undefined) {
const exactBytes = bytes(plan.plan_payload_bytes); hash(plan.plan_hash);
if (await sha256(exactBytes) !== plan.plan_hash) throw new Error('Planhash stimmt nicht mit gespeicherten Bytes überein');
verifiedBytes = Array.from(exactBytes);
const parsed = parseStrictJson(new TextDecoder('utf-8', { fatal: true }).decode(exactBytes));
if (plan.plan_payload !== undefined && canonical(parsed) !== canonical(plan.plan_payload)) throw new Error('Planansicht widerspricht gespeicherten Bytes');
validatePlanAliases(plan, parsed);
const rawJobs = record(parsed).jobs;
powerShellJobs = projectPowerShellJobs(rawJobs);
if (hasScriptView || powerShellJobs.length) {
if (!Array.isArray(rawJobs)) throw new Error('Vollständige feste PowerShell-Zielpayloads fehlen');
validatePowerShellViews(plan, rawJobs);
}
}
for (const payload of systemPlanJobs(plan)) {
const action = record(payload.action);
@@ -122,6 +209,7 @@ export async function verifyPlanIntegrity(plan: RecordValue) {
for (const value of jobs) { if (!value || typeof value !== 'object') continue; const p = record(value).payload; if (!p || typeof p !== 'object') continue; const action = record(p).action;
if (action && typeof action === 'object' && record(action).apt_manifest !== undefined && record(action).apt_manifest !== null) await verifyAptManifest(record(action).apt_manifest, record(action).action === 'apt_native_acceptance');
}
verifiedPowerShellPlans.set(plan, { jobs: powerShellJobs.map(job => Object.freeze({ ...job })), planHash: plan.plan_hash, planBytes: verifiedBytes });
}
export function recipeInput(value: unknown): RecordValue {
const r = record(value); exact(r, ['schema_version', 'provider', 'package', 'version', 'architecture', 'recipe_version', 'installation_context', 'reboot_policy']);