haps 0.1.20

Hashtree Package System: signed packages, content-addressed distribution, social discovery
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
//! A reviewed local build has its own signer and payload, never the publisher's signature.
use crate::{AuditReviewArgs, Candidate, Config, audit_agent, publishing_keys, record_audit};
use anyhow::{Context, Result, ensure};
use haps::{
    audit::Evidence, build::Checkout, progress::Progress, repository::Repository, trust::Trust,
};
use sha2::{Digest, Sha256};
use std::{io::IsTerminal, path::Path, sync::Arc};

pub(super) struct InstallMode {
    pub prefer_published: bool,
    pub reuse_review: bool,
    pub allow_source_fallback: bool,
}

#[allow(clippy::too_many_arguments)]
pub(super) async fn from_release(
    home: &Path,
    config: &mut Config,
    trust: &mut Trust,
    upstream: &Candidate,
    options: &AuditReviewArgs,
    recipe_file: Option<&Path>,
    non_interactive: bool,
    mode: InstallMode,
) -> Result<Candidate> {
    options.build_execution.execution().preflight()?;
    let (progress, _reporter) = Progress::stderr(true)?;
    progress.stage("Fetching pinned source for audit");
    let source = upstream
        .release
        .data
        .package
        .source
        .clone()
        .context("this release does not declare pinned source")?;
    let source = haps::build::SourceCheckout::fetch(source, &home.join("audits/work"))?;
    let mut options = options.clone();
    let recipe = if let Some(path) = recipe_file {
        ensure!(
            std::fs::metadata(path)?.len() <= haps::model::MAX_METADATA as u64,
            "build manifest is too large"
        );
        haps::build::manifest::Manifest::select(
            &std::fs::read_to_string(path)?,
            haps::model::target(),
            Some(&upstream.release.data.package.name),
        )?
        .recipe()?
    } else if let Some(recipe) =
        source.recipe_for(None, Some(&upstream.release.data.package.name))?
    {
        recipe
    } else {
        ensure!(
            !mode.reuse_review,
            "the reviewed build recipe is not in this source; supply the exact --recipe-file with --from-source or use --audit for a fresh review"
        );
        progress.clear();
        let interactive =
            !non_interactive && std::io::stdin().is_terminal() && std::io::stderr().is_terminal();
        let agent = audit_agent(
            options
                .audit_agent
                .as_deref()
                .or(config.audit.agent.as_deref()),
            interactive,
        )?;
        let model = options
            .audit_model
            .as_deref()
            .or(config.audit.model.as_deref());
        let recipe =
            haps::audit::plan_build(&source, &upstream.release.data.package, &agent, model)?;
        options.audit_agent = Some(agent);
        recipe
    };
    let checkout = Checkout::with_recipe(source, recipe)?;
    let result = async {
    ensure!(
        checkout.recipe.package == upstream.release.data.package,
        "source manifest does not describe the selected package"
    );
    progress.clear();
    let trusted = if mode.reuse_review {
        let hash = haps::audit::recipe_sha256(&checkout.recipe)?;
        let previous = trust.source_build_reviews(&upstream.release).into_iter().find_map(|event| {
            let evidence = haps::trust::parse_source_review(event).ok()?;
            (evidence.build.as_ref()?.recipe_sha256 == hash).then(|| (event.id.to_hex(), evidence))
        }).context("the available source audit does not cover these exact build instructions; run again with --audit to review them")?;
        Some(previous)
    } else { None };
    build_reviewed(home, config, trust, &checkout, &options, non_interactive,
        Some(upstream), mode.prefer_published, mode.allow_source_fallback, trusted).await
    }.await;
    result.map_err(|error| haps::audit::retain_failed(checkout, error))
}

pub(super) async fn build(
    home: &Path,
    config: &mut Config,
    trust: &mut Trust,
    checkout: Checkout,
    options: &AuditReviewArgs,
    non_interactive: bool,
) -> Result<Candidate> {
    let result = build_reviewed(
        home,
        config,
        trust,
        &checkout,
        options,
        non_interactive,
        None,
        false,
        true,
        None,
    )
    .await;
    result.map_err(|error| haps::audit::retain_failed(checkout, error))
}

#[allow(clippy::too_many_arguments)]
async fn build_reviewed(
    home: &Path,
    config: &mut Config,
    trust: &mut Trust,
    checkout: &Checkout,
    options: &AuditReviewArgs,
    non_interactive: bool,
    upstream: Option<&Candidate>,
    prefer_published: bool,
    allow_source_fallback: bool,
    trusted: Option<(String, Evidence)>,
) -> Result<Candidate> {
    let execution = options.build_execution.execution();
    execution.preflight()?;
    let interactive =
        !non_interactive && std::io::stdin().is_terminal() && std::io::stderr().is_terminal();
    let keys = publishing_keys(home, config)?;
    *trust = crate::load_trust(home, config)?;
    if let (Some(upstream), Some((event_id, _))) = (upstream, trusted.as_ref()) {
        ensure!(
            trust
                .source_build_reviews(&upstream.release)
                .iter()
                .any(|event| event.id.to_hex() == *event_id),
            "the selected source review is no longer trusted"
        );
    }
    let reused_review = trusted.is_some();
    // Rebuilding someone else's review is useful local evidence, not another
    // independent source audit. Never automatically republish it as our review.
    let publication = if reused_review {
        (Some(false), false)
    } else {
        (options.publish_audit, interactive)
    };
    let (reviewer, note, reviewed_by) = if let Some((event_id, _)) = trusted {
        eprintln!("Using a trusted audit of this exact source and build recipe.");
        (
            None,
            format!("Built from source and recipe reviewed in signed audit {event_id}."),
            format!("Source review reused from signed audit {event_id}."),
        )
    } else {
        let agent = audit_agent(
            options
                .audit_agent
                .as_deref()
                .or(config.audit.agent.as_deref()),
            interactive,
        )?;
        let model = options.audit_model.clone().or_else(|| {
            (agent != "manual")
                .then(|| config.audit.model.clone())
                .flatten()
        });
        let review = haps::audit::review(checkout, &agent, model.as_deref(), interactive)?;
        (
            Some(review.reviewer),
            review.note,
            "Source and build recipe reviewed before execution.".into(),
        )
    };
    eprintln!("Reviewed source and build instructions; building and recording the output hash.");
    let (progress, _reporter) = Progress::stderr(true)?;
    let scan = if haps::security::enabled(home) {
        progress.stage("Scanning reviewed source before building");
        Some(haps::security::scan_source(
            home,
            checkout
                .recipe
                .package
                .source
                .as_ref()
                .context("source missing")?,
            &checkout.root(),
        )?)
    } else {
        None
    };
    let recipe_bytes = serde_json::to_vec(&checkout.recipe)?;
    let recipe_hash = hex::encode(Sha256::digest(&recipe_bytes));
    progress.stage("Building reviewed source");
    progress.clear(); // Build commands own the terminal while they run.
    let payload = checkout.execute_with(execution)?;
    progress.stage("Recording the local build");
    let repo = Repository::local(home.join("built-packages"))?;
    let source_name = format!("local-builds-{}", &keys.public_key().to_hex()[..16]);
    let location = repo
        .path()
        .context("local repository missing")?
        .canonicalize()?
        .to_str()
        .context("non-UTF8 build repository path")?
        .to_owned();
    if let Some(existing) = config.sources.get(&source_name) {
        ensure!(
            existing.author == keys.public_key().to_hex() && existing.location == location,
            "local build catalog name is already used by another source"
        );
    }
    let mut package = checkout.recipe.package.clone();
    let manifest = repo.pack_payload(&package, &payload).await?;
    let method = reviewer
        .as_ref()
        .map_or("trusted-source-build".to_owned(), |r| {
            format!("{}-review-and-rebuild", r.agent)
        });
    if let Some(upstream) = upstream {
        let binary_match = manifest == upstream.release.data.manifest;
        let evidence = Evidence {
            schema: "haps.audit.v1".into(),
            method: if binary_match {
                method.clone()
            } else {
                "source-review".into()
            },
            provenance: format!(
                "{reviewed_by} Built pinned source {} using recipe SHA-256 {recipe_hash}. {} Built payload manifest: {manifest}. Publisher binary match: {binary_match}. Dependencies and toolchain are not independently verified.",
                checkout
                    .recipe
                    .package
                    .source
                    .as_ref()
                    .context("source missing")?
                    .rev,
                execution.description()
            ),
            binary_match,
            reviewer: reviewer.clone(),
            build: Some(haps::audit::BuildIdentity {
                recipe_sha256: recipe_hash.clone(),
                manifest: manifest.clone(),
            }),
        };
        progress.clear();
        if !reused_review || binary_match {
            record_audit(
                home,
                config,
                trust,
                &keys,
                &upstream.release,
                (&evidence, note.clone()),
                publication,
            )
            .await?;
        }
        if binary_match && prefer_published {
            if let Some(scan) = scan {
                haps::security::bind_report(home, &upstream.release, scan)?;
            }
            eprintln!("Rebuilt payload matches the published release.");
            return Ok(upstream.clone());
        }
        if !binary_match {
            ensure!(
                allow_source_fallback,
                "published binary differs from the reviewed build; installing a different local payload cannot satisfy the required independent audit count"
            );
            eprintln!(
                "Published binary differs from the audited build; installing the local source build."
            );
        }
    }
    // Include both inputs and output so two local builds cannot silently replace
    // one immutable release when a toolchain or dependency changes its output.
    let build_id = hex::encode(Sha256::digest(format!("{recipe_hash}:{manifest}")));
    package.version.build = semver::BuildMetadata::new(&format!("haps.{}", &build_id[..20]))?;
    let release = repo.publish_reusable(&keys, package, &payload).await?;
    let recipe_cid = repo.put_json(&checkout.recipe).await?;
    let evidence = Evidence {
        schema: "haps.audit.v1".into(),
        method: reviewer
            .as_ref()
            .map_or("trusted-source-build".to_owned(), |r| {
                format!("{}-review-local-build", r.agent)
            }),
        provenance: format!(
            "{reviewed_by} Built locally from reviewed pinned source {} and recipe {} (SHA-256 {}). {} This audit approves only this local payload. Dependencies and toolchain are not independently verified.",
            checkout
                .recipe
                .package
                .source
                .as_ref()
                .context("source missing")?
                .rev,
            recipe_cid,
            recipe_hash,
            execution.description()
        ),
        binary_match: false,
        reviewer,
        build: Some(haps::audit::BuildIdentity {
            recipe_sha256: recipe_hash,
            manifest: release.data.manifest.clone(),
        }),
    };

    if let Some(scan) = scan {
        haps::security::bind_report(home, &release, scan)?;
    }
    progress.clear();
    record_audit(
        home,
        config,
        trust,
        &keys,
        &release,
        (&evidence, note),
        publication,
    )
    .await?;
    let snapshot = repo.catalog(&keys.public_key().to_hex()).await?;
    config.sources.insert(
        source_name,
        crate::Source {
            location,
            author: keys.public_key().to_hex(),
            sequence: snapshot.head.sequence,
            event_id: snapshot.event.id.to_hex(),
        },
    );
    crate::save_config(home, config)?;
    Ok(Candidate {
        repository: Arc::new(repo),
        package_card: None,
        release,
    })
}

/// Keep command dispatch's poll frame small enough for the Windows entry stack.
pub(super) async fn execute_build(
    home: &Path,
    config: &mut Config,
    trust: &mut Trust,
    installation: &crate::Installation,
    command: crate::Command,
    non_interactive: bool,
) -> Result<()> {
    use crate::*;
    let Command::Build {
        repository,
        rev,
        recipe,
        recipe_file,
        package,
        execute,
        install,
        audit_review,
        attest_scan,
    } = command
    else {
        unreachable!("build dispatcher")
    };

    if execute {
        audit_review.build_execution.execution().preflight()?;
    }

    ensure!(
        install
            || (audit_review.audit_agent.is_none()
                && audit_review.audit_model.is_none()
                && audit_review.publish_audit.is_none()),
        "audit options for build require --install"
    );
    let source = haps::build::SourceCheckout::fetch(
        SourceInfo {
            git: repository,
            rev,
        },
        &home.join("builds"),
    )?;
    let recipe = if let Some(path) = recipe_file {
        ensure!(
            fs::metadata(&path)?.len() <= MAX_METADATA as u64,
            "build manifest is too large"
        );
        haps::build::manifest::Manifest::select(
            &fs::read_to_string(path)?,
            target(),
            package.as_deref(),
        )?
        .recipe()?
    } else {
        source
            .recipe_for(recipe.as_deref(), package.as_deref())?
            .context(
                "no build instructions declared; add [build] to haps.toml or supply --recipe-file",
            )?
    };
    let checkout = haps::build::Checkout::with_recipe(source, recipe)?;
    println!("{}", serde_json::to_string_pretty(&checkout.recipe)?);
    if execute && install {
        ensure!(
            !attest_scan || haps::security::enabled(home),
            "enable source scans with haps security --rules FILE first"
        );
        let candidate = cli_source::build(
            home,
            config,
            trust,
            checkout,
            &audit_review,
            non_interactive,
        )
        .await?;
        cli_dependencies::install(
            home,
            config,
            trust,
            installation,
            &candidate,
            AuditOptions {
                requested: false,
                review: &audit_review,
                non_interactive,
                allow_untrusted: true,
                allow_warnings: false,
                allow_unaudited: false,
                minimum: 1,
            },
            false,
            &[],
        )
        .await?;
        if attest_scan {
            let keys = own_keys(home, config)?;
            let report = haps::model::read_json(
                &home
                    .join("security/releases")
                    .join(format!("{}.json", candidate.release.event.id)),
            )?;
            publish_scan(
                home,
                config,
                haps::security::attestation(&keys, &candidate.release, &report)?,
            )
            .await?;
        }
        println!(
            "Built {} {}",
            candidate.release.identity(),
            candidate.release.event.id
        );
    } else if execute {
        let keys = own_keys(home, config)?;
        let scan = if haps::security::enabled(home) {
            Some(haps::security::scan_source(
                home,
                checkout
                    .recipe
                    .package
                    .source
                    .as_ref()
                    .context("source is missing")?,
                &checkout.root(),
            )?)
        } else {
            None
        };
        let payload = checkout.execute_with(audit_review.build_execution.execution())?;
        let repo = Repository::local(home.join("built-packages"))?;
        let release = repo
            .publish(&keys, checkout.recipe.package.clone(), &payload)
            .await?;
        if let Some(report) = scan {
            haps::security::bind_report(home, &release, report)?;
        }
        println!("Built {} {}", release.identity(), release.event.id);
    } else {
        println!(
            "Preview only. Add --execute to build in isolation with network disabled; add --install to review and activate the result. Downloads require --allow-build-network."
        );
    }
    Ok(())
}