|
| 1 | +import { isAbsolute } from "node:path"; |
| 2 | +import { |
| 3 | + artifactFileCancellationCause, |
| 4 | + artifactFilePrimaryFacts, |
| 5 | +} from "../../artifacts/ArtifactFileOperation.js"; |
| 6 | +import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; |
| 7 | +import type { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js"; |
| 8 | +import { readStableArtifact } from "../../artifacts/readStableArtifact.js"; |
| 9 | +import { |
| 10 | + projectStableArtifactFailure, |
| 11 | + stableArtifactInputRecovery, |
| 12 | +} from "../../artifacts/StableArtifactFailure.js"; |
| 13 | +import type { AnalysisError } from "../../domain/analysisErrorBase.js"; |
| 14 | +import { |
| 15 | + AnalysisAccessDeniedError, |
| 16 | + AnalysisArtifactChangedError, |
| 17 | + AnalysisCancelledError, |
| 18 | + AnalysisInputError, |
| 19 | + AnalysisResourceConstraintError, |
| 20 | +} from "../../domain/analysisErrorCore.js"; |
| 21 | +import { ArtifactOperationError } from "../../domain/artifactOperationError.js"; |
| 22 | +import { createEvidence, type Evidence } from "../../domain/evidence.js"; |
| 23 | +import { |
| 24 | + EVTX_ARCHIVE_OPERATION as operation, |
| 25 | + EVTX_ARCHIVE_PROVIDER, |
| 26 | + type EvtxArchive, |
| 27 | + type EvtxArchiveArtifact, |
| 28 | +} from "../../domain/evtxArchive/evtxArchive.js"; |
| 29 | +import { |
| 30 | + inspectEvtxArchiveInputSchema, |
| 31 | + type InspectEvtxArchiveInput, |
| 32 | +} from "../../domain/evtxArchive/evtxArchiveQuery.js"; |
| 33 | +import { parseEvtxArchive } from "../../domain/evtxArchive/parseEvtxArchive.js"; |
| 34 | +import { err, ok, type Result } from "../../domain/result.js"; |
| 35 | +import { parseAnalysisInput } from "../AnalysisInput.js"; |
| 36 | +import { completeProviderEvidence } from "../ProviderOperationExecution.js"; |
| 37 | + |
| 38 | +const inputFailure = (message: string, cause?: unknown) => |
| 39 | + new AnalysisInputError( |
| 40 | + operation, |
| 41 | + { |
| 42 | + cause, |
| 43 | + ...(cause instanceof ArtifactReaderFailure |
| 44 | + ? stableArtifactInputRecovery(cause) |
| 45 | + : {}), |
| 46 | + }, |
| 47 | + [{ path: ["path"], reason: "invalid_format", message }], |
| 48 | + ); |
| 49 | + |
| 50 | +/** Keep the reader's typed reasons instead of flattening every failure into "io". */ |
| 51 | +const readFailure = ( |
| 52 | + failure: unknown, |
| 53 | + input: InspectEvtxArchiveInput, |
| 54 | +): AnalysisError => { |
| 55 | + const { cause, origin } = artifactFilePrimaryFacts(failure); |
| 56 | + if ( |
| 57 | + origin === "owned-or-classified" && |
| 58 | + cause instanceof ArtifactReaderFailure |
| 59 | + ) { |
| 60 | + if (cause.reason === "cancelled") |
| 61 | + return new AnalysisCancelledError(operation, { |
| 62 | + cause: artifactFileCancellationCause(cause), |
| 63 | + }); |
| 64 | + if (cause.reason === "integrity") |
| 65 | + return new AnalysisArtifactChangedError( |
| 66 | + operation, |
| 67 | + input.path, |
| 68 | + cause.message, |
| 69 | + { cause }, |
| 70 | + ); |
| 71 | + if (cause.reason === "limit") |
| 72 | + return new AnalysisResourceConstraintError( |
| 73 | + operation, |
| 74 | + cause.boundary ?? "file-size", |
| 75 | + cause.message, |
| 76 | + { max_file_bytes: input.max_file_bytes }, |
| 77 | + { |
| 78 | + cause, |
| 79 | + retryable: false, |
| 80 | + remediationAction: |
| 81 | + "Select a log within max_file_bytes; reply paging does not change this file admission.", |
| 82 | + }, |
| 83 | + ); |
| 84 | + if (cause.reason === "path" || cause.reason === "format") |
| 85 | + return inputFailure(cause.message, cause); |
| 86 | + } |
| 87 | + if (cause instanceof Error && "code" in cause) { |
| 88 | + if (cause.code === "EACCES" || cause.code === "EPERM") |
| 89 | + return new AnalysisAccessDeniedError(operation, input.path, cause.code, { |
| 90 | + cause, |
| 91 | + }); |
| 92 | + } |
| 93 | + return new ArtifactOperationError( |
| 94 | + operation, |
| 95 | + "io", |
| 96 | + undefined, |
| 97 | + "Event log read failed.", |
| 98 | + { cause: failure }, |
| 99 | + ); |
| 100 | +}; |
| 101 | + |
| 102 | +const readArchive = async ( |
| 103 | + input: InspectEvtxArchiveInput, |
| 104 | + resources: ArtifactResourceScope, |
| 105 | + signal?: AbortSignal, |
| 106 | +): Promise<Result<{ bytes: Buffer; sha256: string }, AnalysisError>> => { |
| 107 | + try { |
| 108 | + const source = await resources.run(() => |
| 109 | + readStableArtifact( |
| 110 | + input.path, |
| 111 | + input.max_file_bytes, |
| 112 | + signal, |
| 113 | + undefined, |
| 114 | + resources, |
| 115 | + ), |
| 116 | + ); |
| 117 | + return ok(source); |
| 118 | + } catch (failure: unknown) { |
| 119 | + return err( |
| 120 | + projectStableArtifactFailure( |
| 121 | + failure, |
| 122 | + readFailure(failure, input), |
| 123 | + input.path, |
| 124 | + EVTX_ARCHIVE_PROVIDER.id, |
| 125 | + operation, |
| 126 | + ), |
| 127 | + ); |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +export interface EvtxArchiveInspection { |
| 132 | + readonly evidence: Evidence; |
| 133 | + /** The verified file snapshot, retained when cancellation follows the read. */ |
| 134 | + readonly observation: EvtxArchive["artifact"]; |
| 135 | +} |
| 136 | + |
| 137 | +/** Shared CLI/MCP import: one verified file read, no network, no replay, no execution. */ |
| 138 | +export const inspectEvtxArchiveEvidence = async ( |
| 139 | + rawInput: unknown, |
| 140 | + resources: ArtifactResourceScope, |
| 141 | + options: { readonly signal?: AbortSignal } = {}, |
| 142 | +): Promise<Result<EvtxArchiveInspection, AnalysisError>> => { |
| 143 | + const admitted = parseAnalysisInput( |
| 144 | + operation, |
| 145 | + rawInput, |
| 146 | + inspectEvtxArchiveInputSchema, |
| 147 | + options.signal, |
| 148 | + ); |
| 149 | + if (!admitted.ok) return admitted; |
| 150 | + const input = admitted.value; |
| 151 | + if (!isAbsolute(input.path)) |
| 152 | + return err( |
| 153 | + inputFailure("Expected an absolute filesystem path on this host."), |
| 154 | + ); |
| 155 | + const source = await readArchive(input, resources, options.signal); |
| 156 | + if (!source.ok) return source; |
| 157 | + const { bytes, sha256 } = source.value; |
| 158 | + const artifact: EvtxArchiveArtifact = { |
| 159 | + path: input.path, |
| 160 | + sha256, |
| 161 | + bytes: bytes.length, |
| 162 | + }; |
| 163 | + const result = parseEvtxArchive(bytes, artifact, { |
| 164 | + maxRecords: input.max_records, |
| 165 | + }); |
| 166 | + if (!result.ok) return result; |
| 167 | + const archive = result.value; |
| 168 | + const evidence = createEvidence( |
| 169 | + { path: input.path, format: "file", sha256 }, |
| 170 | + EVTX_ARCHIVE_PROVIDER, |
| 171 | + { |
| 172 | + operation, |
| 173 | + predicateType: "rea.evtx-archive-inventory", |
| 174 | + parameters: { |
| 175 | + path: input.path, |
| 176 | + format: input.format, |
| 177 | + max_file_bytes: input.max_file_bytes, |
| 178 | + max_records: input.max_records, |
| 179 | + }, |
| 180 | + result: archive, |
| 181 | + confidence: "observed", |
| 182 | + limitations: archive.limitations, |
| 183 | + locations: [{ kind: "artifact-path", path: input.path }], |
| 184 | + }, |
| 185 | + ); |
| 186 | + const completed = completeProviderEvidence(evidence, options.signal); |
| 187 | + return completed.ok |
| 188 | + ? ok({ evidence: completed.value, observation: artifact }) |
| 189 | + : completed; |
| 190 | +}; |
0 commit comments