Skip to content

Commit 32aa2d5

Browse files
Merge branch 'main' into claude/gallant-bell-4bo4oy-n-idris-abi-ci
2 parents 94fe849 + 671c3a9 commit 32aa2d5

4 files changed

Lines changed: 96 additions & 16 deletions

File tree

‎CHANGELOG.adoc‎

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

‎Cargo.lock‎

Lines changed: 9 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎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)