packslip 1.11.3

A vendor-neutral signed release manifest: what shipped, and how to verify it
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//! Execute the action's create step with a mock CLI, without signing services.
#![cfg(unix)]

use serde_yaml_bw::Value;
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::process::Command;

fn run_create(commit: Option<&str>) -> Vec<String> {
    let action: Value = serde_yaml_bw::from_str(include_str!("../action.yml")).unwrap();
    let create = action["runs"]["steps"]
        .as_sequence()
        .unwrap()
        .iter()
        .find(|step| step["id"].as_str() == Some("create"))
        .unwrap();
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    let cli = root.join("packslip");
    fs::write(
        &cli,
        "#!/usr/bin/env bash\nprintf '%s\\0' \"$@\" > \"$CALL_LOG\"\nmkdir -p out\n: > out/packslip.sigstore.json\n",
    )
    .unwrap();
    fs::set_permissions(&cli, fs::Permissions::from_mode(0o755)).unwrap();
    fs::write(root.join("packslip-files"), "dist/tool-linux-x64.tar.xz\n").unwrap();

    let mut command = Command::new("bash");
    command
        .args(["-eu", "-c", create["run"].as_str().unwrap()])
        .current_dir(root)
        .env_clear()
        .env("RUNNER_TEMP", root)
        .env("GITHUB_OUTPUT", root.join("outputs"))
        .env("CALL_LOG", root.join("args"));
    let mut paths = vec![root.to_path_buf()];
    paths.extend(std::env::split_paths(&std::env::var_os("PATH").unwrap()));
    command.env("PATH", std::env::join_paths(paths).unwrap());

    // Read the real YAML input wiring and defaults. A missing or miswired
    // commit input must fail rather than being supplied directly by the test.
    for (name, expression) in create["env"].as_mapping().unwrap() {
        let expression = expression.as_str().unwrap();
        let value = if let Some(input) = expression
            .strip_prefix("${{ inputs.")
            .and_then(|s| s.strip_suffix(" }}"))
        {
            let supplied = match input {
                "commit" => commit,
                "tag" => Some("v1.2.3"),
                "out" => Some("out"),
                "attest" => Some("false"),
                _ => None,
            };
            supplied
                .unwrap_or_else(|| action["inputs"][input]["default"].as_str().unwrap_or(""))
                .to_owned()
        } else {
            match expression {
                "${{ github.repository }}" => "owner/tool".to_owned(),
                "${{ github.ref_name }}" => "main".to_owned(),
                "${{ github.ref_type }}" => "branch".to_owned(),
                "${{ github.sha }}" => "a".repeat(40),
                "${{ github.server_url }}" => "https://github.com".to_owned(),
                "${{ github.api_url }}" => "https://api.github.com".to_owned(),
                other => panic!("unhandled action expression: {other}"),
            }
        };
        command.env(name.as_str().unwrap(), value);
    }
    let output = command.output().unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let args = fs::read(root.join("args")).unwrap();
    assert_eq!(args.last(), Some(&0));
    let args: Vec<String> = args[..args.len() - 1]
        .split(|byte| *byte == 0)
        .map(|arg| String::from_utf8(arg.to_vec()).unwrap())
        .collect();
    assert_eq!(value_after(&args, "--tag"), "v1.2.3");
    assert_eq!(value_after(&args, "--version"), "1.2.3");
    args
}

fn value_after<'a>(args: &'a [String], flag: &str) -> &'a str {
    &args[args.iter().position(|arg| arg == flag).unwrap() + 1]
}

#[test]
fn default_preserves_workflow_commit() {
    assert_eq!(value_after(&run_create(None), "--commit"), "a".repeat(40));
}

#[test]
fn empty_commit_preserves_workflow_commit() {
    assert_eq!(
        value_after(&run_create(Some("")), "--commit"),
        "a".repeat(40)
    );
}

#[test]
fn manual_release_can_override_workflow_commit() {
    let commit = "b".repeat(40);
    assert_eq!(value_after(&run_create(Some(&commit)), "--commit"), commit);
}

#[test]
fn override_is_one_literal_argument_not_shell_code() {
    // The real CLI validates commits; the action must pass this literally.
    let commit = "$(exit 77) two words";
    assert_eq!(value_after(&run_create(Some(commit)), "--commit"), commit);
}

#[test]
fn both_actions_wire_the_optional_download_digest_to_the_installer() {
    for source in [
        include_str!("../action.yml"),
        include_str!("../releases/action.yml"),
    ] {
        let action: Value = serde_yaml_bw::from_str(source).unwrap();
        assert_eq!(
            action["inputs"]["packslip-sha256"]["default"].as_str(),
            Some("")
        );
        let install = action["runs"]["steps"]
            .as_sequence()
            .unwrap()
            .iter()
            .find(|step| step["name"].as_str() == Some("Install packslip"))
            .unwrap();
        assert_eq!(
            install["env"]["PACKSLIP_SHA256"].as_str(),
            Some("${{ inputs.packslip-sha256 }}")
        );
    }
}

#[test]
fn release_promotions_queue_without_replacing_pending_runs() {
    let path =
        std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(".github/workflows/release.yml");
    // Workflows are deliberately excluded from the crates.io/distro source
    // package. Assert repository wiring where the workflow is available.
    if !path.exists() {
        return;
    }
    let workflow: Value = serde_yaml_bw::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
    assert_eq!(workflow["concurrency"]["queue"].as_str(), Some("max"));
    assert_eq!(
        workflow["concurrency"]["cancel-in-progress"].as_bool(),
        Some(false)
    );
    assert!(
        !fs::read_to_string(&path)
            .unwrap()
            .contains("needs.release.outputs.current-")
    );
    for step in workflow["jobs"]["release"]["steps"].as_sequence().unwrap() {
        if matches!(
            step["name"].as_str(),
            Some("Rewrite the release body with Communiqué")
                | Some("Publish this release's packslip")
                | Some("Publish the release")
        ) {
            assert_eq!(
                step["if"].as_str(),
                Some("env.CANDIDATE_PUBLISHED != 'true'")
            );
        }
    }
    let release_plz_path =
        std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(".github/workflows/release-plz.yml");
    let release_plz: Value =
        serde_yaml_bw::from_str(&fs::read_to_string(release_plz_path).unwrap()).unwrap();
    for name in ["release", "release-pr"] {
        assert!(
            release_plz["jobs"][name]["if"]
                .as_str()
                .unwrap()
                .contains("github.ref == 'refs/heads/main'")
        );
    }
    for step in release_plz["jobs"]["release"]["steps"]
        .as_sequence()
        .unwrap()
    {
        if step["uses"].as_str().is_some_and(|value| {
            value.starts_with("rust-lang/crates-io-auth-action@")
                || value.starts_with("release-plz/action@")
        }) {
            assert_eq!(
                step["if"].as_str(),
                Some("steps.gate.outputs.publish == 'true'")
            );
        }
    }
    for (file, jobs) in [
        ("distro-source.yml", vec!["ppa", "copr"]),
        ("package-repositories.yml", vec!["publish"]),
    ] {
        let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join(".github/workflows")
            .join(file);
        let guarded: Value = serde_yaml_bw::from_str(&fs::read_to_string(path).unwrap()).unwrap();
        assert_eq!(guarded["concurrency"]["queue"].as_str(), Some("max"));
        assert!(
            guarded["concurrency"]["group"]
                .as_str()
                .unwrap()
                .contains("packslip-release")
        );
        for job in jobs {
            let steps = guarded["jobs"][job]["steps"].as_sequence().unwrap();
            let guard = steps
                .iter()
                .position(|step| step["id"].as_str() == Some("freshness"))
                .unwrap();
            assert!(
                steps[guard]["run"]
                    .as_str()
                    .unwrap()
                    .contains("freshness --tag")
            );
            assert_eq!(
                steps[guard + 1]["if"].as_str(),
                Some("steps.freshness.outputs.current-latest == 'true'")
            );
        }
    }
}

#[cfg(target_os = "linux")]
#[test]
fn old_release_recovery_never_pushes_moving_image_aliases() {
    let path =
        std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(".github/workflows/release.yml");
    if !path.exists() {
        return;
    }
    let workflow: Value = serde_yaml_bw::from_str(&fs::read_to_string(path).unwrap()).unwrap();
    let step = workflow["jobs"]["image"]["steps"]
        .as_sequence()
        .unwrap()
        .iter()
        .find(|step| step["id"].as_str() == Some("push"))
        .unwrap();
    for (major, latest, expected) in [
        (false, false, vec!["ghcr.io/jdx/packslip:1.6.0"]),
        (
            true,
            false,
            vec![
                "ghcr.io/jdx/packslip:1.6.0",
                "ghcr.io/jdx/packslip:1.6",
                "ghcr.io/jdx/packslip:1",
            ],
        ),
        (
            true,
            true,
            vec![
                "ghcr.io/jdx/packslip:1.6.0",
                "ghcr.io/jdx/packslip:1.6",
                "ghcr.io/jdx/packslip:1",
                "ghcr.io/jdx/packslip:latest",
            ],
        ),
    ] {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::create_dir(root.join("container")).unwrap();
        fs::create_dir(root.join("bin")).unwrap();
        let builder = root.join("container/build.sh");
        fs::write(
            &builder,
            "#!/usr/bin/env bash\nprintf '%s\\0' \"$@\" > \"$CALL_LOG\"\nprintf '{\"containerimage.digest\":\"sha256:fixture\"}' > meta.json\n",
        )
        .unwrap();
        fs::set_permissions(&builder, fs::Permissions::from_mode(0o755)).unwrap();
        let docker = root.join("bin/docker");
        fs::write(
            &docker,
            "#!/usr/bin/env bash\nif [ \"$1\" = login ]; then cat >/dev/null; fi\nif [ \"$1\" = run ]; then printf 'packslip 1.6.0\\n'; fi\n",
        )
        .unwrap();
        fs::set_permissions(&docker, fs::Permissions::from_mode(0o755)).unwrap();
        let mut paths = vec![root.join("bin")];
        paths.extend(std::env::split_paths(&std::env::var_os("PATH").unwrap()));
        // Model a fresh forge result, not cached needs outputs, at the exact
        // image write boundary. Deliberately supply stale true env values.
        let python = root.join("bin/python3");
        fs::write(
            &python,
            format!(
                "#!/usr/bin/env bash\nprintf 'CURRENT_MAJOR={}\\nCURRENT_LATEST={}\\n'\n",
                major, latest
            ),
        )
        .unwrap();
        fs::set_permissions(&python, fs::Permissions::from_mode(0o755)).unwrap();
        let result = Command::new("bash")
            .args(["-eu", "-c", step["run"].as_str().unwrap()])
            .current_dir(root)
            .env("PATH", std::env::join_paths(paths).unwrap())
            .env("GH_TOKEN", "credential-free-fixture")
            .env("GITHUB_REF_NAME", "v1.6.0")
            .env("GITHUB_REPOSITORY", "jdx/packslip")
            .env("GITHUB_ACTOR", "fixture")
            .env("GITHUB_OUTPUT", root.join("output"))
            .env("CURRENT_MAJOR", "true")
            .env("CURRENT_LATEST", "true")
            .env("CALL_LOG", root.join("arguments"))
            .output()
            .unwrap();
        assert!(
            result.status.success(),
            "{}",
            String::from_utf8_lossy(&result.stderr)
        );
        let arguments = fs::read(root.join("arguments")).unwrap();
        let args: Vec<&str> = arguments
            .split(|byte| *byte == 0)
            .filter(|part| !part.is_empty())
            .map(|part| std::str::from_utf8(part).unwrap())
            .collect();
        let tags: Vec<&str> = args
            .windows(2)
            .filter(|pair| pair[0] == "--tag")
            .map(|pair| pair[1])
            .collect();
        assert_eq!(tags, expected);
    }
}

#[cfg(target_os = "linux")]
fn run_install(expected_digest: Option<&str>, version_override: &str) -> std::process::Output {
    run_install_fixture(expected_digest, version_override, None, true)
}

#[cfg(target_os = "linux")]
fn run_install_fixture(
    expected_digest: Option<&str>,
    version_override: &str,
    lock_digest: Option<&str>,
    write_lock: bool,
) -> std::process::Output {
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    let fixture = root.join("packslip-fixture");
    fs::create_dir(&fixture).unwrap();
    let executable = fixture.join("packslip");
    fs::write(&executable, "#!/usr/bin/env bash\nprintf invoked\n").unwrap();
    fs::set_permissions(&executable, fs::Permissions::from_mode(0o755)).unwrap();

    let arch = match std::env::consts::ARCH {
        "x86_64" => "x64",
        "aarch64" => "arm64",
        other => panic!("unsupported test architecture: {other}"),
    };
    let version = if version_override.is_empty() {
        env!("CARGO_PKG_VERSION")
    } else {
        version_override
    };
    let asset = format!("packslip-v{version}-linux-{arch}.tar.xz");
    let archive = root.join(&asset);
    assert!(
        Command::new("tar")
            .args([
                "-cJf",
                archive.to_str().unwrap(),
                "-C",
                root.to_str().unwrap(),
                "packslip-fixture",
            ])
            .status()
            .unwrap()
            .success()
    );
    let actual = String::from_utf8(
        Command::new("sha256sum")
            .arg(&archive)
            .output()
            .unwrap()
            .stdout,
    )
    .unwrap()
    .split_whitespace()
    .next()
    .unwrap()
    .to_owned();

    let action_root = root.join("action-root");
    fs::create_dir_all(action_root.join("action")).unwrap();
    fs::write(
        action_root.join("Cargo.toml"),
        format!("[package]\nversion = \"{version}\"\n"),
    )
    .unwrap();
    if write_lock {
        let metadata = serde_json::json!({
            "schema": 1, "version": version, "source_commit": "a".repeat(40),
            "assets": {asset.clone(): {"sha256": lock_digest.unwrap_or(&actual)}}
        });
        fs::write(
            action_root.join("action/release.json"),
            serde_json::to_vec(&metadata).unwrap(),
        )
        .unwrap();
    }

    let bin = root.join("bin");
    fs::create_dir(&bin).unwrap();
    let gh = bin.join("gh");
    fs::write(
        &gh,
        "#!/usr/bin/env bash\nset -eu\nif [ \"$1\" = release ]; then\n  while [ \"$#\" -gt 0 ]; do\n    if [ \"$1\" = -D ]; then\n      cp \"$FIXTURE\" \"$2/$ASSET\"\n      exit 0\n    fi\n    shift\n  done\nfi\nif [ \"$1\" = attestation ]; then printf 'attested %s\\n' \"$*\"; exit 0; fi\nexit 88\n",
    )
    .unwrap();
    fs::set_permissions(&gh, fs::Permissions::from_mode(0o755)).unwrap();

    let mut paths = vec![bin];
    paths.extend(std::env::split_paths(&std::env::var_os("PATH").unwrap()));
    Command::new("bash")
        .args(["scripts/install-packslip.sh"])
        .current_dir(env!("CARGO_MANIFEST_DIR"))
        .env("RUNNER_TEMP", root)
        .env("GITHUB_PATH", root.join("github-path"))
        .env("PACKSLIP_ACTION_ROOT", action_root)
        .env("PACKSLIP_VERSION", version_override)
        .env("PACKSLIP_SHA256", expected_digest.unwrap_or(&actual))
        .env("PACKSLIP_PATH", "")
        .env("FIXTURE", archive)
        .env("ASSET", asset)
        .env("PATH", std::env::join_paths(paths).unwrap())
        .output()
        .unwrap()
}

#[cfg(target_os = "linux")]
#[test]
fn install_checks_the_pinned_archive_digest_before_running_it() {
    let accepted = run_install(None, "1.5.1");
    assert!(
        accepted.status.success(),
        "{}",
        String::from_utf8_lossy(&accepted.stderr)
    );
    assert!(String::from_utf8_lossy(&accepted.stdout).contains("invoked"));
    let rejected = run_install(Some("not-a-digest"), "1.5.1");
    assert!(!rejected.status.success());
    assert!(String::from_utf8_lossy(&rejected.stderr).contains("packslip-sha256 must be"));
}

#[cfg(target_os = "linux")]
#[test]
fn install_rejects_a_tampered_pinned_archive_before_provenance_or_execution() {
    let rejected = run_install(Some(&"0".repeat(64)), "1.5.1");
    assert!(!rejected.status.success());
    assert!(
        String::from_utf8_lossy(&rejected.stderr).contains("packslip archive SHA-256 mismatch")
    );
    assert!(!String::from_utf8_lossy(&rejected.stdout).contains("invoked"));
    assert!(!String::from_utf8_lossy(&rejected.stdout).contains("attested"));
}

#[cfg(target_os = "linux")]
#[test]
fn default_uses_internal_digest_and_actual_candidate_provenance() {
    let accepted = run_install(Some(""), "");
    assert!(
        accepted.status.success(),
        "{}",
        String::from_utf8_lossy(&accepted.stderr)
    );
    assert!(String::from_utf8_lossy(&accepted.stdout).contains("invoked"));
    let pinned = run_install(None, "");
    assert!(pinned.status.success());
    let output = String::from_utf8_lossy(&accepted.stdout);
    assert!(output.contains("--source-digest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
    assert!(output.contains("--source-ref refs/heads/release-plz"));
    assert!(output.contains("--signer-digest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
    assert!(output.contains("--signer-workflow jdx/packslip/.github/workflows/release.yml"));
    assert!(output.contains("--deny-self-hosted-runners"));
}

#[cfg(target_os = "linux")]
#[test]
fn default_rejects_missing_lock_and_tampered_bytes_before_attesting() {
    let missing = run_install_fixture(Some(""), "", None, false);
    assert!(!missing.status.success());
    assert!(String::from_utf8_lossy(&missing.stderr).contains("no release digest map"));
    let tampered = run_install_fixture(Some(""), "", Some(&"0".repeat(64)), true);
    assert!(!tampered.status.success());
    assert!(String::from_utf8_lossy(&tampered.stderr).contains("SHA-256 mismatch"));
    assert!(!String::from_utf8_lossy(&tampered.stdout).contains("attested"));
    assert!(!String::from_utf8_lossy(&tampered.stdout).contains("invoked"));
}

/// Run the releases action's bundle and self-check steps for `project` in
/// `repository`, with `inputs` set beyond the required ones and a mock CLI
/// whose `verify --help` names --allow-other-repository when
/// `knows_opt_out` is set. Return the arguments of each `packslip verify`
/// they ran, or the stderr of the step that failed.
fn run_releases_verify(
    project: &str,
    repository: &str,
    inputs: &[(&str, &str)],
    knows_opt_out: bool,
) -> Result<Vec<Vec<String>>, String> {
    run_releases_verify_under("v1.0.0", project, repository, inputs, knows_opt_out)
}

fn run_releases_verify_under(
    tag: &str,
    project: &str,
    repository: &str,
    inputs: &[(&str, &str)],
    knows_opt_out: bool,
) -> Result<Vec<Vec<String>>, String> {
    let action: Value = serde_yaml_bw::from_str(include_str!("../releases/action.yml")).unwrap();
    let steps = action["runs"]["steps"].as_sequence().unwrap();
    let step = |name: &str| {
        steps
            .iter()
            .find(|step| step["name"].as_str() == Some(name))
            .unwrap()
    };
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    fs::create_dir_all(root.join("lists").join(tag)).unwrap();
    fs::write(
        root.join("lists").join(tag).join("packslip.sigstore.json"),
        "",
    )
    .unwrap();
    let cli = root.join("packslip");
    fs::write(
        &cli,
        r#"#!/usr/bin/env bash
set -eu
case "$1" in
  verify)
    if [ "${2:-}" = --help ]; then printf '%s\n' "$VERIFY_HELP"; exit 0; fi
    printf '%s\0' "$@" >> "$CALL_LOG"
    printf '\n' >> "$CALL_LOG"
    ;;
  show) printf '{"predicate":{"project":"%s"}}\n' "$STATEMENT_PROJECT" ;;
  *) exit 88 ;;
esac
"#,
    )
    .unwrap();
    fs::set_permissions(&cli, fs::Permissions::from_mode(0o755)).unwrap();
    let help = if knows_opt_out {
        "--pin <PIN>\n--allow-other-repository\n--allow-unlogged"
    } else {
        "--pin <PIN>\n--allow-unlogged"
    };

    for name in ["Verify the published bundles", "Verify what was written"] {
        let step = step(name);
        let mut command = Command::new("bash");
        command
            .args(["-eu", "-c", step["run"].as_str().unwrap()])
            .current_dir(root)
            .env_clear()
            .env("RUNNER_TEMP", root)
            .env("GITHUB_OUTPUT", root.join("outputs"))
            .env("CALL_LOG", root.join("calls"))
            .env("VERIFY_HELP", help)
            .env("STATEMENT_PROJECT", project);
        let mut paths = vec![root.to_path_buf()];
        paths.extend(std::env::split_paths(&std::env::var_os("PATH").unwrap()));
        command.env("PATH", std::env::join_paths(paths).unwrap());
        for (name, expression) in step["env"].as_mapping().unwrap() {
            let expression = expression.as_str().unwrap();
            let value = if let Some(input) = expression
                .strip_prefix("${{ inputs.")
                .and_then(|s| s.strip_suffix(" }}"))
            {
                match input {
                    "project" => project.to_owned(),
                    "dir" => "lists".to_owned(),
                    "url-base" => inputs
                        .iter()
                        .find(|(name, _)| *name == input)
                        .map_or("https://lists.example.com", |(_, value)| *value)
                        .to_owned(),
                    _ => inputs
                        .iter()
                        .find(|(name, _)| *name == input)
                        .map(|(_, value)| *value)
                        .unwrap_or_else(|| {
                            action["inputs"][input]["default"].as_str().unwrap_or("")
                        })
                        .to_owned(),
                }
            } else {
                match expression {
                    "${{ github.repository }}" => repository.to_owned(),
                    "${{ github.server_url }}" => "https://github.com".to_owned(),
                    "${{ steps.build.outputs.list }}" => "list.sigstore.json".to_owned(),
                    other => panic!("unhandled action expression: {other}"),
                }
            };
            command.env(name.as_str().unwrap(), value);
        }
        let output = command.output().unwrap();
        if !output.status.success() {
            return Err(String::from_utf8_lossy(&output.stderr).into_owned());
        }
    }
    Ok(fs::read_to_string(root.join("calls"))
        .unwrap()
        .lines()
        .map(|call| {
            call.trim_end_matches('\0')
                .split('\0')
                .map(str::to_owned)
                .collect()
        })
        .collect())
}

#[test]
fn releases_action_passes_allow_other_repository_when_needed() {
    let issuer = "https://token.actions.githubusercontent.com";
    let pin = |prefix: &str, opt_out: bool| {
        let mut pin = vec![
            "--issuer".to_owned(),
            issuer.to_owned(),
            "--identity-prefix".to_owned(),
            prefix.to_owned(),
        ];
        if opt_out {
            pin.push("--allow-other-repository".to_owned());
        }
        pin
    };
    let calls = |pin: Vec<String>| {
        ["lists/v1.0.0/packslip.sigstore.json", "list.sigstore.json"]
            .map(|doc| [vec!["verify".to_owned(), doc.to_owned()], pin.clone()].concat())
            .to_vec()
    };
    for (project, repository, opt_out) in [
        // The project's own repository, or a tool in it: verify holds the
        // signer to that repository as well as to the pin.
        ("github.com/owner/tool", "owner/tool", false),
        ("github.com/owner/tool/cli", "owner/tool", false),
        // GitHub resolves the repository's name in any case.
        ("github.com/Owner/Tool", "owner/tool", false),
        // A project on its own domain names no repository.
        ("tool.example.com", "owner/tool", false),
        // A registry's or repackager's list for another repository's
        // project: the pin alone says who signed.
        ("github.com/vendor/tool", "registry/lists", true),
        ("github.com/vendor/tool/cli", "registry/lists", true),
        ("gitlab.com/group/sub/tool", "registry/lists", true),
    ] {
        assert_eq!(
            run_releases_verify(project, repository, &[], true).unwrap(),
            calls(pin(&format!("https://github.com/{repository}/"), opt_out)),
            "{project} in {repository}"
        );
    }

    // The project's own repository listing bundles another repository of
    // its owner signed opts out with the input.
    let owner_wide = [
        ("identity-prefix", "https://github.com/owner/"),
        ("allow-other-repository", "true"),
    ];
    assert_eq!(
        run_releases_verify("github.com/owner/tool", "owner/tool", &owner_wide, true).unwrap(),
        calls(pin("https://github.com/owner/", true))
    );
    assert_eq!(
        run_releases_verify(
            "github.com/owner/tool",
            "owner/tool",
            &owner_wide[..1],
            true
        )
        .unwrap(),
        calls(pin("https://github.com/owner/", false))
    );
    let err = run_releases_verify(
        "github.com/owner/tool",
        "owner/tool",
        &[("allow-other-repository", "yes")],
        true,
    )
    .unwrap_err();
    assert!(
        err.contains("allow-other-repository must be true or false, not \"yes\""),
        "{err}"
    );

    // A packslip from before the flag takes the pin as the whole policy
    // already, and would refuse a flag it does not know.
    for (repository, inputs) in [("registry/lists", &[][..]), ("owner/tool", &owner_wide[..])] {
        let calls =
            run_releases_verify("github.com/owner/tool", repository, inputs, false).unwrap();
        assert_eq!(calls.len(), 2);
        for call in calls {
            assert!(
                !call.contains(&"--allow-other-repository".to_owned()),
                "{call:?}"
            );
        }
    }
}

#[test]
fn releases_action_refuses_a_bundle_name_or_url_base_the_release_argument_would_split() {
    for input in [
        ("bundle", "foo=bar.json"),
        ("url-base", "https://example.com/?x=y"),
    ] {
        let err =
            run_releases_verify("github.com/owner/tool", "owner/tool", &[input], true).unwrap_err();
        assert!(err.contains("cannot contain an ="), "{input:?}: {err}");
    }
}

#[test]
fn releases_action_refuses_a_tag_the_release_argument_would_split() {
    let err = run_releases_verify_under("v1=x", "github.com/owner/tool", "owner/tool", &[], true)
        .unwrap_err();
    assert!(err.contains("a tag with an = in it"), "{err}");
}