Skip to content

Commit f08dae4

Browse files
Merge branch 'main' into chore/a2ml-to-deed
Signed-off-by: Jonathan D.A. Jewell <6759885+hyperpolymath@users.noreply.github.com>
2 parents 088dfbb + 89d4810 commit f08dae4

4 files changed

Lines changed: 273 additions & 18 deletions

File tree

‎src/assail/classifications.rs‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::deed::{self, Item, Value};
2222
use crate::types::{AssailReport, WeakPointCategory};
2323
use anyhow::{anyhow, bail, Context, Result};
2424
use std::fs;
25-
use std::io::Read;
25+
use std::io::{ErrorKind, Read};
2626
use std::path::Path;
2727

2828
/// Repo-relative path of the deed registry.
@@ -88,9 +88,29 @@ pub struct UserClassification {
8888
///
8989
/// `Ok(vec![])` when no registry exists. An unreadable, oversized or
9090
/// malformed deed registry is an `Err` naming the file and line.
91+
///
92+
/// Only a deed that is definitely absent lets the reader fall back to the
93+
/// legacy A2ML files: `NotFound`, or `NotADirectory` (`audits` is a regular
94+
/// file, so nothing can sit beneath it). Any other failure to stat the deed
95+
/// path, such as an unreadable `audits/` directory, is an error: treating it
96+
/// as absent would silently swap the registry the project wrote for an older
97+
/// one. A dangling symlink at the deed path is likewise not absent; it fails
98+
/// when it is opened.
9199
pub fn load_user_classifications(project_root: &Path) -> Result<Vec<UserClassification>> {
92100
let deed_path = project_root.join(DEED_PATH);
93-
if deed_path.exists() {
101+
let deed_present = match fs::symlink_metadata(&deed_path) {
102+
Ok(_) => true,
103+
Err(e) if matches!(e.kind(), ErrorKind::NotFound | ErrorKind::NotADirectory) => false,
104+
Err(e) => {
105+
return Err(e).with_context(|| {
106+
format!(
107+
"cannot tell whether the classification registry {} exists",
108+
deed_path.display()
109+
)
110+
})
111+
}
112+
};
113+
if deed_present {
94114
let text = read_capped(&deed_path)?;
95115
return parse_classification_deed(&text, &deed_path)
96116
.with_context(|| format!("malformed classification registry {}", deed_path.display()));

‎src/assail/mod.rs‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,79 @@ mod classifications_tests {
824824
}
825825
}
826826

827+
/// An `audits/` the reader cannot search must not look like a missing
828+
/// deed: that would silently fall back to the root `.a2ml` registry.
829+
#[cfg(unix)]
830+
#[test]
831+
fn unreadable_audits_dir_is_an_error_not_a_fallback() {
832+
use std::os::unix::fs::PermissionsExt;
833+
let tmp = TempDir::new().unwrap();
834+
write_deed(tmp.path(), &deed_with(ZIG_BRIDGE_DEED_ENTRY));
835+
fs::write(
836+
tmp.path().join(".panic-attack-classifications.a2ml"),
837+
r#"(assail-classifications
838+
(classification (file "a/b.rs") (category "PanicPath")))
839+
"#,
840+
)
841+
.unwrap();
842+
let audits = tmp.path().join("audits");
843+
fs::set_permissions(&audits, fs::Permissions::from_mode(0o000)).unwrap();
844+
// Root, or any process with CAP_DAC_OVERRIDE/CAP_DAC_READ_SEARCH,
845+
// ignores the mode bits, so the deed stays readable and there is
846+
// nothing to test. Probe instead of checking the uid: the probe also
847+
// covers capabilities granted to a non-root user.
848+
let privileged = fs::read_dir(&audits).is_ok();
849+
let result = load_user_classifications(tmp.path());
850+
// Restore the mode so TempDir can clean up.
851+
fs::set_permissions(&audits, fs::Permissions::from_mode(0o755)).unwrap();
852+
if privileged {
853+
eprintln!(
854+
"skipped: running as root (or with DAC override), so a mode-000 directory stays readable"
855+
);
856+
return;
857+
}
858+
let err = result.expect_err("an unreadable audits/ must be an error");
859+
assert!(
860+
format!("{err:#}").contains("assail-classifications_praxis.deed"),
861+
"error must name the registry: {err:#}"
862+
);
863+
}
864+
865+
#[test]
866+
fn audits_as_a_regular_file_means_no_deed() {
867+
let tmp = TempDir::new().unwrap();
868+
fs::write(tmp.path().join("audits"), "not a directory\n").unwrap();
869+
fs::write(
870+
tmp.path().join(".panic-attack-classifications.a2ml"),
871+
r#"(assail-classifications
872+
(classification (file "a/b.rs") (category "PanicPath")))
873+
"#,
874+
)
875+
.unwrap();
876+
let classifications = load_user_classifications(tmp.path()).unwrap();
877+
assert_eq!(classifications.len(), 1, "falls back to the root registry");
878+
}
879+
880+
#[cfg(unix)]
881+
#[test]
882+
fn dangling_deed_symlink_is_an_error_not_a_fallback() {
883+
let tmp = TempDir::new().unwrap();
884+
let audits = tmp.path().join("audits");
885+
fs::create_dir_all(&audits).unwrap();
886+
std::os::unix::fs::symlink(
887+
tmp.path().join("nowhere.deed"),
888+
tmp.path().join(USER_CLASSIFICATIONS_DEED_PATH),
889+
)
890+
.unwrap();
891+
write_registry(
892+
tmp.path(),
893+
r#"(assail-classifications
894+
(classification (file "a/b.rs") (category "PanicPath")))
895+
"#,
896+
);
897+
assert!(load_user_classifications(tmp.path()).is_err());
898+
}
899+
827900
#[test]
828901
fn analyze_fails_on_a_malformed_deed() {
829902
let tmp = TempDir::new().unwrap();

‎src/assemblyline.rs‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,10 @@ fn scan_repo(repo_path: &Path) -> RepoResult {
374374
high_count: 0,
375375
total_files: 0,
376376
total_lines: 0,
377-
error: Some(e.to_string()),
377+
// `{:#}` keeps the whole anyhow chain ("outer: inner: …"), so a
378+
// malformed registry deed reports its line and reason here, not
379+
// just the outermost context.
380+
error: Some(format!("{e:#}")),
378381
fingerprint,
379382
report: None,
380383
},
@@ -576,6 +579,38 @@ mod tests {
576579
);
577580
}
578581

582+
#[test]
583+
fn scan_error_keeps_the_deed_line_and_reason() {
584+
let tmp = tempfile::TempDir::new().unwrap();
585+
std::fs::write(tmp.path().join("main.rs"), "fn main() {}\n").unwrap();
586+
let audits = tmp.path().join("audits");
587+
std::fs::create_dir_all(&audits).unwrap();
588+
// Line 7 carries a category the registry does not know.
589+
std::fs::write(
590+
audits.join("assail-classifications_praxis.deed"),
591+
";; SPDX-License-Identifier: MPL-2.0\n\
592+
(praxis-deed\n \
593+
:schema-version \"1.0.0\"\n \
594+
:canonical-name \"x\"\n \
595+
:beholding-chora #uuid\"0889b37b-defb-831f-973c-e119500db00e\"\n \
596+
:profile assail-classifications\n \
597+
(classification :file \"a.rs\" :category UnsafeKode))\n",
598+
)
599+
.unwrap();
600+
let err = scan_repo(tmp.path())
601+
.error
602+
.expect("a malformed deed fails the scan");
603+
assert!(
604+
err.contains("assail-classifications_praxis.deed"),
605+
"names the file: {err}"
606+
);
607+
assert!(err.contains("line 7"), "keeps the line: {err}");
608+
assert!(
609+
err.contains("unknown weak-point category UnsafeKode"),
610+
"keeps the reason: {err}"
611+
);
612+
}
613+
579614
#[test]
580615
fn test_is_source_file() {
581616
assert!(is_source_file(Path::new("main.rs")));

0 commit comments

Comments
 (0)