amiss-bootstrap 0.9.0

Trusted CI wrapper that validates and launches a verified Amiss engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs::{File, OpenOptions};
use std::io::{Read as _, Write as _};
use std::path::PathBuf;
use std::process::{ExitCode, Stdio};
use std::time::Duration;

use amiss_bootstrap::result::{BootstrapResult, result_bytes};
use amiss_bootstrap::supervise::{
    Defect, Expectations, SealedControlExpectation, SealedExpectations, Supervised, settle,
    supervise,
};
use amiss_bootstrap::{Refusal, validate};
use amiss_git::{GitLimits, GitResources, ObjectKind, Repository};
use amiss_wire::controls::{ExecutionConstraintDescriptor, TrustedTimeStatement};
use amiss_wire::json::canonical;
use amiss_wire::report::{MACHINE_JSON_BYTES, WATCHDOG_MILLISECONDS};
use amiss_wire::requests::{
    ControlsRequest, EvaluationRequest, REQUEST_STREAM_BYTES, RequestMode, RequestStreams,
    SEALED_ENGINE_ARGUMENT, SnapshotRequest,
};

/// The operational wall ceiling from the security contract: the trusted
/// wrapper kills the whole evaluator after 120 seconds, and a killed evaluator
/// yields no accepted result.
const WATCHDOG_CEILING: Duration = Duration::from_millis(WATCHDOG_MILLISECONDS);

#[cfg(windows)]
const PRIVATE_ENGINE_NAME: &str = "engine.exe";

#[cfg(not(windows))]
const PRIVATE_ENGINE_NAME: &str = "engine";

/// The trusted bootstrap, which is also the trusted wrapper the security
/// contract names. It validates the pinned action tree as data, launches the
/// verified engine with a cleared environment and fixed arguments, holds it to
/// the wall ceiling, and publishes only an envelope it can accept. It never
/// runs the action's declared Node launcher, never resolves a binary through
/// `PATH`, and never downloads, installs, or discovers anything.
///
/// `amiss-bootstrap exec --action-repository P --repository P --constraint F
/// --evaluation-request F --snapshot-request F --controls-request F --scratch P
/// --report F --result F`
#[expect(clippy::print_stderr, reason = "the bootstrap's diagnostic channel")]
fn main() -> ExitCode {
    let argv: Vec<OsString> = env::args_os().skip(1).collect();
    let Some((parsed, mut output)) = parse_args(&argv)
        .and_then(|parsed| open_output(&parsed).ok().map(|output| (parsed, output)))
    else {
        eprintln!("amiss-bootstrap: invalid-invocation");
        return ExitCode::from(2);
    };
    let completion = execute(&parsed)
        .and_then(|accepted| publish(&mut output.report, accepted))
        .unwrap_or_else(failed_completion);
    if let Some(diagnostic) = completion.diagnostic {
        eprintln!("amiss-bootstrap: {diagnostic}");
    }
    if write_output(&mut output.result, result_bytes(completion.result)).is_err() {
        eprintln!("amiss-bootstrap: result-unavailable");
        return ExitCode::from(2);
    }
    completion.exit
}

#[derive(Clone, Copy)]
struct Failure {
    result: BootstrapResult,
    diagnostic: &'static str,
}

struct Accepted {
    wire: Vec<u8>,
    class: u8,
    result: BootstrapResult,
}

struct Completion {
    result: BootstrapResult,
    exit: ExitCode,
    diagnostic: Option<&'static str>,
}

type Execution<T> = Result<T, Failure>;

#[derive(Clone, Copy)]
enum ReadDefect {
    Unavailable,
    Oversized,
}

const fn unavailable(diagnostic: &'static str) -> Failure {
    Failure {
        result: BootstrapResult::Unavailable,
        diagnostic,
    }
}

const fn tampered(diagnostic: &'static str) -> Failure {
    Failure {
        result: BootstrapResult::TamperedRuntime,
        diagnostic,
    }
}

const fn input_failure(
    defect: ReadDefect,
    unavailable_diagnostic: &'static str,
    invalid_diagnostic: &'static str,
) -> Failure {
    match defect {
        ReadDefect::Unavailable => unavailable(unavailable_diagnostic),
        ReadDefect::Oversized => tampered(invalid_diagnostic),
    }
}

fn failed_completion(failure: Failure) -> Completion {
    Completion {
        result: failure.result,
        exit: ExitCode::from(2),
        diagnostic: Some(failure.diagnostic),
    }
}

fn execute(args: &Args) -> Execution<Accepted> {
    let constraint_bytes = read_input(
        &args.constraint,
        "constraint-unreadable",
        "constraint-invalid",
    )?;
    let constraint = ExecutionConstraintDescriptor::parse(&constraint_bytes)
        .map_err(|_defect| tampered("constraint-invalid"))?;
    let own_path = env::current_exe().map_err(|_defect| unavailable("self-unreadable"))?;
    let own_bytes = std::fs::read(own_path).map_err(|_defect| unavailable("self-unreadable"))?;
    let action = Repository::open(&args.action_repository, constraint.action_object_format)
        .map_err(|_defect| unavailable("action-tree-unavailable"))?;
    let mut resources = GitResources::new(GitLimits::CONTRACT);
    let validated =
        validate(&action, &mut resources, &constraint, &own_bytes).map_err(validation_failure)?;
    let sealed = capture_requests(args, &constraint)?;
    pre_acquired(&args.repository, &sealed.evaluation)
        .map_err(|()| unavailable("repository-not-pre-acquired"))?;
    run_engine(args, &validated, sealed)
}

const fn validation_failure(refusal: Refusal) -> Failure {
    match refusal {
        Refusal::Unavailable(diagnostic) => unavailable(diagnostic),
        Refusal::Tampered(diagnostic) => tampered(diagnostic),
    }
}

struct Args {
    action_repository: PathBuf,
    repository: PathBuf,
    constraint: PathBuf,
    evaluation_request: PathBuf,
    snapshot_request: PathBuf,
    controls_request: PathBuf,
    scratch: PathBuf,
    report: PathBuf,
    result: PathBuf,
}

fn parse_args(argv: &[OsString]) -> Option<Args> {
    let mut action_repository: Option<PathBuf> = None;
    let mut repository: Option<PathBuf> = None;
    let mut constraint: Option<PathBuf> = None;
    let mut evaluation_request: Option<PathBuf> = None;
    let mut snapshot_request: Option<PathBuf> = None;
    let mut controls_request: Option<PathBuf> = None;
    let mut scratch: Option<PathBuf> = None;
    let mut report: Option<PathBuf> = None;
    let mut result: Option<PathBuf> = None;
    let mut items = argv.iter();
    if items.next()? != "exec" {
        return None;
    }
    while let Some(flag) = items.next() {
        let value = items.next()?;
        let slot = match flag.to_str()? {
            "--action-repository" => &mut action_repository,
            "--repository" => &mut repository,
            "--constraint" => &mut constraint,
            "--evaluation-request" => &mut evaluation_request,
            "--snapshot-request" => &mut snapshot_request,
            "--controls-request" => &mut controls_request,
            "--scratch" => &mut scratch,
            "--report" => &mut report,
            "--result" => &mut result,
            _ => return None,
        };
        if slot.is_some() {
            return None;
        }
        *slot = Some(PathBuf::from(value));
    }
    let scratch = scratch?;
    if !scratch.is_absolute()
        || !std::fs::symlink_metadata(&scratch).is_ok_and(|metadata| metadata.file_type().is_dir())
    {
        return None;
    }
    let report = report?;
    let result = result?;
    if !output_path(&report, &scratch, "report") || !output_path(&result, &scratch, "result") {
        return None;
    }
    Some(Args {
        action_repository: action_repository?,
        repository: repository?,
        constraint: constraint?,
        evaluation_request: evaluation_request?,
        snapshot_request: snapshot_request?,
        controls_request: controls_request?,
        scratch,
        report,
        result,
    })
}

fn output_path(path: &std::path::Path, scratch: &std::path::Path, name: &str) -> bool {
    path.is_absolute()
        && path.parent() == Some(scratch)
        && path.file_name() == Some(OsStr::new(name))
        && std::fs::symlink_metadata(path)
            .is_ok_and(|metadata| metadata.file_type().is_file() && metadata.len() == 0)
}

struct OutputFiles {
    report: File,
    result: File,
}

fn open_output(args: &Args) -> std::io::Result<OutputFiles> {
    Ok(OutputFiles {
        report: open_output_file(&args.report)?,
        result: open_output_file(&args.result)?,
    })
}

fn open_output_file(path: &std::path::Path) -> std::io::Result<File> {
    let file = OpenOptions::new().write(true).open(path)?;
    let metadata = file.metadata()?;
    if !metadata.is_file() || metadata.len() != 0 {
        return Err(std::io::Error::other("invalid output file"));
    }
    Ok(file)
}

#[derive(Clone)]
struct SealedRun {
    streams: RequestStreams,
    evaluation: EvaluationRequest,
    expected: SealedExpectations,
}

fn capture_requests(
    args: &Args,
    constraint: &ExecutionConstraintDescriptor,
) -> Execution<SealedRun> {
    let streams = request_streams(args)?;
    let evaluation = EvaluationRequest::parse(&streams.evaluation)
        .map_err(|_defect| tampered("evaluation-request-invalid"))?;
    let snapshot = SnapshotRequest::parse(&streams.snapshot)
        .map_err(|_defect| tampered("snapshot-request-invalid"))?;
    let controls = ControlsRequest::parse(&streams.controls)
        .map_err(|_defect| tampered("controls-request-invalid"))?;
    let canonical_requests = evaluation.canonical_bytes().ok().as_deref()
        == Some(streams.evaluation.as_slice())
        && snapshot.canonical_bytes().ok().as_deref() == Some(streams.snapshot.as_slice())
        && controls.canonical_bytes().ok().as_deref() == Some(streams.controls.as_slice());
    if !canonical_requests {
        return Err(tampered("request-noncanonical"));
    }
    let candidate = match (evaluation.mode, evaluation.candidate_commit.as_ref()) {
        (RequestMode::CommitPair, Some(candidate))
            if snapshot.materialization == RequestMode::CommitPair =>
        {
            candidate.clone()
        }
        (RequestMode::CommitPair | RequestMode::Index, None | Some(_)) => {
            return Err(tampered("request-mode-mismatch"));
        }
    };
    let repository = sealed_identity(&evaluation).map_err(tampered)?;
    let supplied_constraint = controls
        .execution_constraint
        .as_ref()
        .ok_or_else(|| tampered("execution-constraint-absent"))?;
    let embedded_constraint =
        ExecutionConstraintDescriptor::parse(&canonical(&supplied_constraint.value))
            .map_err(|_defect| tampered("execution-constraint-invalid"))?;
    if embedded_constraint.digest != supplied_constraint.expected_digest
        || embedded_constraint != *constraint
    {
        return Err(tampered("execution-constraint-mismatch"));
    }
    let supplied_time = controls
        .trusted_time
        .as_ref()
        .ok_or_else(|| tampered("trusted-time-absent"))?;
    let statement = TrustedTimeStatement::parse(&canonical(&supplied_time.value))
        .map_err(|_defect| tampered("trusted-time-invalid"))?;
    if statement.digest != supplied_time.expected_digest
        || statement.provider != supplied_time.provider
        || statement.provider_run_id != supplied_time.provider_run_id
        || statement.provider_run_attempt != supplied_time.provider_run_attempt
    {
        return Err(tampered("trusted-time-mismatch"));
    }
    let expected = SealedExpectations {
        profile: match evaluation.profile {
            amiss_wire::controls::Profile::Observe => "observe",
            amiss_wire::controls::Profile::Enforce => "enforce",
        }
        .to_owned(),
        candidate_ref: evaluation
            .candidate_ref
            .as_ref()
            .map_or_else(String::new, |reference| reference.as_str().to_owned()),
        target_ref: evaluation
            .target_ref
            .as_ref()
            .map_or_else(String::new, |reference| reference.as_str().to_owned()),
        repository,
        provider: supplied_time.provider.clone(),
        provider_run_id: supplied_time.provider_run_id.clone(),
        provider_run_attempt: supplied_time.provider_run_attempt,
        candidate_identity_digest: statement.candidate_identity_digest.to_string(),
        organization_floor: control_expectation(controls.organization_floor.as_ref()),
        debt_snapshot: control_expectation(controls.debt_snapshot.as_ref()),
        waiver_bundle: control_expectation(controls.waiver_bundle.as_ref()),
        execution_constraint: SealedControlExpectation {
            digest: constraint.digest.to_string(),
            trust_source: supplied_constraint.trust_source.as_str().to_owned(),
        },
        trusted_time_digest: statement.digest.to_string(),
    };
    let mut evaluation = evaluation;
    evaluation.candidate_commit = Some(candidate);
    Ok(SealedRun {
        streams,
        evaluation,
        expected,
    })
}

fn request_streams(args: &Args) -> Execution<RequestStreams> {
    let streams = RequestStreams {
        evaluation: read_input(
            &args.evaluation_request,
            "evaluation-request-unreadable",
            "evaluation-request-invalid",
        )?,
        snapshot: read_input(
            &args.snapshot_request,
            "snapshot-request-unreadable",
            "snapshot-request-invalid",
        )?,
        controls: read_input(
            &args.controls_request,
            "controls-request-unreadable",
            "controls-request-invalid",
        )?,
    };
    Ok(streams)
}

fn sealed_identity(
    evaluation: &EvaluationRequest,
) -> Result<amiss_wire::model::RepositoryIdentity, &'static str> {
    let Some(repository) = evaluation.repository.clone() else {
        return Err("evaluation-identity-absent");
    };
    if evaluation.forge.is_none()
        || evaluation.candidate_ref.is_none()
        || evaluation.target_ref.is_none()
        || evaluation.default_branch_ref.is_none()
    {
        return Err("evaluation-identity-absent");
    }
    Ok(repository)
}

fn control_expectation(
    supplied: Option<&amiss_wire::requests::SuppliedControl>,
) -> Option<SealedControlExpectation> {
    supplied.map(|control| SealedControlExpectation {
        digest: control.expected_digest.to_string(),
        trust_source: control.trust_source.as_str().to_owned(),
    })
}

fn read_bounded(path: &std::path::Path) -> Result<Vec<u8>, ReadDefect> {
    let file = File::open(path).map_err(|_defect| ReadDefect::Unavailable)?;
    let mut bytes = Vec::new();
    file.take(REQUEST_STREAM_BYTES.saturating_add(1))
        .read_to_end(&mut bytes)
        .map_err(|_defect| ReadDefect::Unavailable)?;
    if u64::try_from(bytes.len()).unwrap_or(u64::MAX) > REQUEST_STREAM_BYTES {
        return Err(ReadDefect::Oversized);
    }
    Ok(bytes)
}

fn read_input(
    path: &std::path::Path,
    unavailable_diagnostic: &'static str,
    invalid_diagnostic: &'static str,
) -> Execution<Vec<u8>> {
    read_bounded(path)
        .map_err(|defect| input_failure(defect, unavailable_diagnostic, invalid_diagnostic))
}

fn pre_acquired(path: &std::path::Path, evaluation: &EvaluationRequest) -> Result<(), ()> {
    let repository = Repository::open(path, evaluation.object_format).map_err(|_defect| ())?;
    let mut resources = GitResources::new(GitLimits::CONTRACT);
    repository
        .read_expected(&mut resources, &evaluation.base_commit, ObjectKind::Commit)
        .map_err(|_defect| ())?;
    let candidate = evaluation.candidate_commit.as_ref().ok_or(())?;
    repository
        .read_expected(&mut resources, candidate, ObjectKind::Commit)
        .map_err(|_defect| ())?;
    Ok(())
}

/// Writes the verified engine bytes into a private directory and launches them
/// with an empty environment. The bytes come from the validated tree, never
/// from a worktree file, a `PATH` lookup, or the action's launcher.
fn run_engine(
    args: &Args,
    validated: &amiss_bootstrap::Validated,
    sealed: SealedRun,
) -> Execution<Accepted> {
    let expectations = Expectations {
        engine_digest: validated.engine_digest.to_string(),
        base_commit: sealed.evaluation.base_commit.as_str().to_owned(),
        candidate_commit: sealed
            .evaluation
            .candidate_commit
            .as_ref()
            .map(|candidate| candidate.as_str().to_owned()),
        sealed: Some(sealed.expected.clone()),
    };

    let private = tempfile::TempDir::new_in(&args.scratch)
        .map_err(|_defect| unavailable("private-storage-unavailable"))?;
    let engine = private.path().join(PRIVATE_ENGINE_NAME);
    std::fs::write(&engine, &validated.binary)
        .map_err(|_defect| unavailable("private-storage-unavailable"))?;
    executable_bit(&engine).map_err(|_defect| unavailable("private-storage-unavailable"))?;

    let mut child = std::process::Command::new(&engine)
        .arg(SEALED_ENGINE_ARGUMENT)
        .current_dir(&args.repository)
        .env_clear()
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::inherit())
        .spawn()
        .map_err(|_defect| unavailable("engine-launch-failed"))?;
    let (outcome, wire) = collect(&mut child, sealed.streams)
        .map_err(|_defect| unavailable("engine-collection-failed"))?;
    let class = settle(&outcome, &wire, &expectations)
        .map_err(|defect| settlement_failure(defect, wire.is_empty()))?;
    let (class, result) = match class {
        0 => (0, BootstrapResult::Pass),
        1 => (1, BootstrapResult::Block),
        _ => return Err(tampered("report-exit-class")),
    };
    Ok(Accepted {
        wire,
        class,
        result,
    })
}

/// Drains the engine's stdout while the watchdog runs. A supervisor that only
/// polls would deadlock the moment the engine's report outgrew the pipe
/// buffer: the engine would block writing, never exit, and be killed for a
/// slowness that was the supervisor's own.
fn collect(
    child: &mut std::process::Child,
    requests: RequestStreams,
) -> std::io::Result<(Supervised, Vec<u8>)> {
    let mut stdin = child
        .stdin
        .take()
        .ok_or_else(|| std::io::Error::other("no engine stdin"))?;
    let stdout = child
        .stdout
        .take()
        .ok_or_else(|| std::io::Error::other("no engine stdout"))?;
    let writer = std::thread::spawn(move || {
        requests.write_to(&mut stdin)?;
        stdin.flush()
    });
    let reader = std::thread::spawn(move || {
        let mut wire = Vec::new();
        let mut bounded = stdout.take(MACHINE_JSON_BYTES.saturating_add(1));
        bounded.read_to_end(&mut wire).map(|_count| wire)
    });
    let outcome = match supervise(child, WATCHDOG_CEILING) {
        Ok(outcome) => outcome,
        Err(defect) => {
            let _signalled = child.kill();
            let _reaped = child.wait();
            let _writer = writer.join();
            let _reader = reader.join();
            return Err(defect);
        }
    };
    let write_result = writer
        .join()
        .map_err(|_panic| std::io::Error::other("engine request writer failed"));
    if !matches!(outcome, Supervised::Killed) {
        write_result??;
    }
    let wire = reader
        .join()
        .map_err(|_panic| std::io::Error::other("engine reader failed"))??;
    Ok((outcome, wire))
}

/// Publishes the accepted envelope before exposing its result record.
fn publish(report: &mut File, accepted: Accepted) -> Execution<Completion> {
    let Accepted {
        wire,
        class,
        result,
    } = accepted;
    write_output(report, &wire).map_err(|_defect| unavailable("report-publish-failed"))?;
    Ok(Completion {
        result,
        exit: ExitCode::from(class),
        diagnostic: None,
    })
}

fn write_output(file: &mut File, bytes: &[u8]) -> std::io::Result<()> {
    file.write_all(bytes)?;
    file.flush()
}

const fn settlement_failure(defect: Defect, empty: bool) -> Failure {
    match defect {
        Defect::Killed => Failure {
            result: BootstrapResult::Timeout,
            diagnostic: "evaluator-watchdog-kill",
        },
        Defect::Signalled => unavailable("evaluator-signalled"),
        Defect::Oversize => Failure {
            result: BootstrapResult::OversizedOutput,
            diagnostic: "report-over-wire-ceiling",
        },
        Defect::ExitMismatch => tampered("evaluator-exit-mismatch"),
        Defect::Acceptance(_defect) if empty => Failure {
            result: BootstrapResult::MissingOutput,
            diagnostic: "report-missing",
        },
        Defect::Acceptance(_defect) => tampered("report-rejected"),
    }
}

#[cfg(unix)]
fn executable_bit(path: &std::path::Path) -> std::io::Result<()> {
    use std::os::unix::fs::PermissionsExt as _;
    std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700))
}

#[cfg(not(unix))]
fn executable_bit(_path: &std::path::Path) -> std::io::Result<()> {
    Ok(())
}