Skip to content

Commit 671c3a9

Browse files
fix(assail): refuse an unwritable KYAML copy or sidecar before replacing the report (#274)
## Problem Codex raised this on #264 after it merged. The `--output-format kyaml` preflight looks each output up, but lookup does not show that a path can be written. The run then replaces `report.json` and only afterwards fails on the copy, leaving the new report beside stale companions. Two inputs trigger this: - a read-only `report.kyaml` or `report.attestation.json`; - a directory the caller cannot create files in. The preflight is meant to be all-or-nothing, and this breaks it. ## Change - `kyaml_copy_path` now opens each output for writing during the preflight, without truncating it. An absent output is created and then removed again. - The kernel makes the decision, so ACLs and ownership count, not only mode bits. - The CHANGELOG `[Unreleased]` entry for the KYAML copy notes this. ## Validation Two new tests in `tests/coverage_php_tests.rs`: a read-only copy, and an attested run into an unwritable directory. Each checks that the run fails with "nothing was written" and that the old report is still there. - Run as `nobody`, both **fail without the fix** and pass with it. All 13 `kyaml_output_format` tests pass. - Root ignores permission bits, so as root the two tests detect that and skip. GitHub runners run unprivileged, so CI exercises them. - `cargo fmt --check` and `cargo clippy --all-targets -- -D warnings` are clean, and `cargo test` passes all 24 suites. Not covered here: the same problem in plain JSON mode with `--attest` (report plus sidecar), which has never had a preflight. That is on the follow-up list, and the planned generation-directory publish would close it for every mode. --- _Generated by [Claude Code](https://claude.ai/code/session_01PFaqv6iBXrt3W5Akd1Fq5k)_ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 39db4e3 commit 671c3a9

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

‎CHANGELOG.adoc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ may be the same file: a symbolic link at any of their paths, or two existing
1212
paths that are hard links to one file, is refused before anything is written.
1313
So is a path that exists but is not a regular file (a directory, say) or that
1414
cannot be looked up, such as a derived name over the filesystem's length limit.
15+
Each output is also opened for writing before anything is written (an absent
16+
one is created and removed again), so a read-only copy or sidecar, or a
17+
directory the caller cannot create files in, is refused too, instead of
18+
failing after the report has been replaced.
1519
These checks run before the writes, so a link swapped in between a check and
1620
its write is not caught, and a crash can leave a partial file: the planned
1721
generation-directory publish is meant to close both.

‎src/main.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,31 @@ fn kyaml_copy_path(out: &Path, sidecar: Option<&Path>) -> Result<PathBuf> {
14431443
}
14441444
}
14451445
}
1446+
// Lookup alone does not show that a path can be written: a read-only
1447+
// copy or sidecar, or a directory the caller may not create files in,
1448+
// would fail only after the report had been replaced. Open each output
1449+
// for writing now, without truncating it; an absent one is created and
1450+
// removed again. The kernel decides, so ACLs and ownership count too.
1451+
for (&(role, candidate), &existed) in outputs.iter().zip(&exists) {
1452+
let probe = if existed {
1453+
fs::OpenOptions::new().write(true).open(candidate).map(drop)
1454+
} else {
1455+
fs::OpenOptions::new()
1456+
.write(true)
1457+
.create_new(true)
1458+
.open(candidate)
1459+
.and_then(|file| {
1460+
drop(file);
1461+
fs::remove_file(candidate)
1462+
})
1463+
};
1464+
probe.map_err(|err| {
1465+
anyhow::anyhow!(
1466+
"cannot write the {role} {}: {err}; nothing was written",
1467+
candidate.display()
1468+
)
1469+
})?;
1470+
}
14461471
Ok(path)
14471472
}
14481473

‎tests/coverage_php_tests.rs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,61 @@ fn kyaml_output_format_refuses_a_directory_at_an_output_path() {
505505
"the report was written before the copy failed"
506506
);
507507
}
508+
509+
/// Root ignores permission bits, so the permission tests below only mean
510+
/// something when run unprivileged. They return early otherwise.
511+
#[cfg(unix)]
512+
fn writes_ignore_permissions(dir: &std::path::Path) -> bool {
513+
use std::os::unix::fs::PermissionsExt;
514+
let probe = dir.join("privilege-probe");
515+
std::fs::write(&probe, "").unwrap();
516+
std::fs::set_permissions(&probe, std::fs::Permissions::from_mode(0o444)).unwrap();
517+
let privileged = std::fs::OpenOptions::new().write(true).open(&probe).is_ok();
518+
std::fs::remove_file(&probe).unwrap();
519+
privileged
520+
}
521+
522+
#[cfg(unix)]
523+
#[test]
524+
fn kyaml_output_format_refuses_a_read_only_copy_before_writing() {
525+
use std::os::unix::fs::PermissionsExt;
526+
let (dir, project) = project_dir();
527+
if writes_ignore_permissions(dir.path()) {
528+
eprintln!("skipped: running with privileges that ignore permission bits");
529+
return;
530+
}
531+
let out = dir.path().join("report.json");
532+
std::fs::write(&out, "old report\n").unwrap();
533+
let copy = dir.path().join("report.kyaml");
534+
std::fs::write(&copy, "old copy\n").unwrap();
535+
std::fs::set_permissions(&copy, std::fs::Permissions::from_mode(0o444)).unwrap();
536+
let output = assail_kyaml(&project, &out);
537+
assert!(!output.status.success());
538+
let stderr = String::from_utf8_lossy(&output.stderr);
539+
assert!(stderr.contains("nothing was written"), "{stderr}");
540+
assert_eq!(std::fs::read_to_string(&out).unwrap(), "old report\n");
541+
assert_eq!(std::fs::read_to_string(&copy).unwrap(), "old copy\n");
542+
}
543+
544+
#[cfg(unix)]
545+
#[test]
546+
fn kyaml_output_format_refuses_a_directory_it_cannot_create_in() {
547+
use std::os::unix::fs::PermissionsExt;
548+
let (dir, project) = project_dir();
549+
if writes_ignore_permissions(dir.path()) {
550+
eprintln!("skipped: running with privileges that ignore permission bits");
551+
return;
552+
}
553+
let reports = dir.path().join("reports");
554+
std::fs::create_dir(&reports).unwrap();
555+
let out = reports.join("report.json");
556+
std::fs::write(&out, "old report\n").unwrap();
557+
std::fs::set_permissions(&reports, std::fs::Permissions::from_mode(0o555)).unwrap();
558+
let output = assail_kyaml_attested(&project, &out);
559+
std::fs::set_permissions(&reports, std::fs::Permissions::from_mode(0o755)).unwrap();
560+
assert!(!output.status.success());
561+
let stderr = String::from_utf8_lossy(&output.stderr);
562+
assert!(stderr.contains("nothing was written"), "{stderr}");
563+
assert_eq!(std::fs::read_to_string(&out).unwrap(), "old report\n");
564+
assert!(!reports.join("report.kyaml").exists());
565+
}

0 commit comments

Comments
 (0)