Implement bounded Windows Event Log jobs and verify contracts on cloud Linux
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
+58
-10
@@ -1,4 +1,4 @@
|
||||
import { Action, PlanInput, RecordValue, Session, planTargetPreviews, record, text, validateSystemService, windowsScriptProfile } from './api';
|
||||
import { Action, PlanInput, RecordValue, Session, planTargetPreviews, record, text, validateSystemService, windowsScriptProfile, logWindow, validateWindowsLogLimits, windowsEventLogFixtureSource, windowsEventLogFixtureChannel, windowsEventLogFixtureProvider } 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.';
|
||||
@@ -104,7 +104,7 @@ export function systemPlanJobs(plan: RecordValue): RecordValue[] {
|
||||
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[] }
|
||||
interface VerifiedPowerShellPlan { readonly jobs: readonly PowerShellPlanJob[]; readonly logJobs: readonly QueryLogsPlanJob[]; 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);
|
||||
@@ -115,6 +115,7 @@ export function powerShellPlanJobs(plan: RecordValue): readonly PowerShellPlanJo
|
||||
const parsed = record(parseStrictJson(new TextDecoder('utf-8', { fatal: true }).decode(current)));
|
||||
validatePlanAliases(plan, parsed);
|
||||
validatePowerShellViews(plan, parsed.jobs);
|
||||
validateQueryLogsViews(plan, parsed.jobs);
|
||||
} else if (scriptPayloads(planTargetPreviews(plan)).some(hasPowerShellProfile)) {
|
||||
throw new Error('PowerShell-Plan benötigt exakte gespeicherte Planbytes');
|
||||
}
|
||||
@@ -153,25 +154,66 @@ function projectPowerShellJobs(jobs: unknown): PowerShellPlanJob[] {
|
||||
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 {
|
||||
function validateFixedViews(plan: RecordValue, rawJobs: unknown, selected: (payload: RecordValue) => boolean, label: string, fields = ['action', 'execution_identity']): 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 (plan[alias] !== undefined && !Array.isArray(plan[alias])) throw new Error(`${label}-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 fixed of rawPayloads.filter(selected)) if (!viewPayloads.some(view => view.asset_id === fixed.asset_id)) throw new Error(`${label}-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');
|
||||
if ((selected(view) || (raw && selected(raw))) && (!raw || fields.some(field => canonical(raw[field]) !== canonical(view[field])))) throw new Error(`${label}-Zielpreview widerspricht gespeicherten Planbytes`);
|
||||
}
|
||||
}
|
||||
}
|
||||
function validatePowerShellViews(plan: RecordValue, rawJobs: unknown): void {
|
||||
validateFixedViews(plan, rawJobs, hasPowerShellProfile, 'PowerShell');
|
||||
}
|
||||
function isQueryLogs(payload: RecordValue): boolean {
|
||||
return payload.action !== null && typeof payload.action === 'object' && record(payload.action).action === 'query_logs';
|
||||
}
|
||||
function validateQueryLogsViews(plan: RecordValue, rawJobs: unknown): void {
|
||||
validateFixedViews(plan, rawJobs, isQueryLogs, 'Log', ['action', 'execution_identity', 'max_runtime_seconds', 'max_output_bytes']);
|
||||
}
|
||||
export interface QueryLogsPlanJob {
|
||||
readonly assetId: string; readonly source: string; readonly since: string; readonly until: string;
|
||||
readonly maxLines: number; readonly runtimeSeconds: number; readonly outputBytes: number; readonly windows: boolean;
|
||||
readonly channel: string | null; readonly provider: string | null;
|
||||
}
|
||||
function projectQueryLogsJobs(jobs: unknown): QueryLogsPlanJob[] {
|
||||
return scriptPayloads(jobs).filter(isQueryLogs).map(payload => {
|
||||
const action = record(payload.action);
|
||||
exact(action, ['action', 'unit', 'since', 'until', 'max_lines']);
|
||||
const source = text(action.unit, 'Logquelle'), since = text(action.since, 'since'), until = text(action.until, 'until');
|
||||
logWindow(since, until);
|
||||
if (typeof action.max_lines !== 'number' || !Number.isSafeInteger(action.max_lines) || action.max_lines < 1 || action.max_lines > 1000
|
||||
|| typeof payload.max_runtime_seconds !== 'number' || !Number.isSafeInteger(payload.max_runtime_seconds) || payload.max_runtime_seconds < 1 || payload.max_runtime_seconds > 3600
|
||||
|| typeof payload.max_output_bytes !== 'number' || !Number.isSafeInteger(payload.max_output_bytes) || payload.max_output_bytes < 1 || payload.max_output_bytes > 262144) throw new Error('Vollständige feste Loglimits erforderlich');
|
||||
const windows = source === windowsEventLogFixtureSource;
|
||||
if (source.startsWith('windows.eventlog.') && !windows) throw new Error('Unbekannte Windows-Eventlogquelle');
|
||||
if (windows) validateWindowsLogLimits(payload.execution_identity, payload.max_runtime_seconds, payload.max_output_bytes, since);
|
||||
else if (!['agent_service', 'root'].includes(String(payload.execution_identity))) throw new Error('Unbekannte Log-Ausführungsidentität');
|
||||
return { assetId: text(payload.asset_id, 'Log-Zielasset'), source, since, until, maxLines: action.max_lines,
|
||||
runtimeSeconds: payload.max_runtime_seconds, outputBytes: payload.max_output_bytes, windows,
|
||||
channel: windows ? windowsEventLogFixtureChannel : null, provider: windows ? windowsEventLogFixtureProvider : null };
|
||||
});
|
||||
}
|
||||
export function queryLogsPlanJobs(plan: RecordValue): readonly QueryLogsPlanJob[] {
|
||||
// Reuse the same verified-object/immutable-byte gate as the PowerShell view.
|
||||
powerShellPlanJobs(plan);
|
||||
const verified = verifiedPowerShellPlans.get(plan)!;
|
||||
if (!verified.planBytes.length && ['jobs', 'payloads', 'resolved_targets'].some(alias => scriptPayloads(plan[alias]).some(isQueryLogs))) throw new Error('Logplan benötigt exakte gespeicherte Planbytes');
|
||||
return verified.logJobs.map(job => ({ ...job }));
|
||||
}
|
||||
export function queryLogsApprovalBlockers(plan: RecordValue): string[] {
|
||||
try { queryLogsPlanJobs(plan); return []; }
|
||||
catch (error) { return [error instanceof Error ? error.message : 'Logplan ist nicht verifiziert']; }
|
||||
}
|
||||
|
||||
export async function verifyPlanIntegrity(plan: RecordValue) {
|
||||
verifiedPowerShellPlans.delete(plan);
|
||||
const views = planTargetPreviews(plan), hasScriptView = scriptPayloads(views).some(hasPowerShellProfile)
|
||||
@@ -180,6 +222,10 @@ export async function verifyPlanIntegrity(plan: RecordValue) {
|
||||
|| 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');
|
||||
const hasLogView = ['jobs', 'payloads', 'resolved_targets'].some(alias => scriptPayloads(plan[alias]).some(isQueryLogs))
|
||||
|| scriptPayloads(record(plan.plan_payload ?? {}).jobs).some(isQueryLogs) || scriptPayloads(record(plan.payload ?? {}).jobs).some(isQueryLogs);
|
||||
if (hasLogView && plan.plan_payload_bytes === undefined) throw new Error('Logplan benötigt exakte gespeicherte Planbytes');
|
||||
let logJobs: QueryLogsPlanJob[] = [];
|
||||
let powerShellJobs: PowerShellPlanJob[] = [];
|
||||
let verifiedBytes: readonly number[] = [];
|
||||
if (plan.plan_payload_bytes === undefined && Array.isArray(plan.jobs) && plan.jobs.some(value => {
|
||||
@@ -194,6 +240,8 @@ export async function verifyPlanIntegrity(plan: RecordValue) {
|
||||
validatePlanAliases(plan, parsed);
|
||||
const rawJobs = record(parsed).jobs;
|
||||
powerShellJobs = projectPowerShellJobs(rawJobs);
|
||||
logJobs = projectQueryLogsJobs(rawJobs);
|
||||
if (hasLogView || logJobs.length) validateQueryLogsViews(plan, rawJobs);
|
||||
if (hasScriptView || powerShellJobs.length) {
|
||||
if (!Array.isArray(rawJobs)) throw new Error('Vollständige feste PowerShell-Zielpayloads fehlen');
|
||||
validatePowerShellViews(plan, rawJobs);
|
||||
@@ -209,7 +257,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 });
|
||||
verifiedPowerShellPlans.set(plan, { jobs: powerShellJobs.map(job => Object.freeze({ ...job })), logJobs: logJobs.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']);
|
||||
|
||||
Reference in New Issue
Block a user