Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/assail/classifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::deed::{self, Item, Value};
use crate::types::{AssailReport, WeakPointCategory};
use anyhow::{anyhow, bail, Context, Result};
use std::fs;
use std::io::Read;
use std::io::{ErrorKind, Read};
use std::path::Path;

/// Repo-relative path of the deed registry.
Expand Down Expand Up @@ -88,9 +88,29 @@ pub struct UserClassification {
///
/// `Ok(vec![])` when no registry exists. An unreadable, oversized or
/// malformed deed registry is an `Err` naming the file and line.
///
/// Only a deed that is definitely absent lets the reader fall back to the
/// legacy A2ML files: `NotFound`, or `NotADirectory` (`audits` is a regular
/// file, so nothing can sit beneath it). Any other failure to stat the deed
/// path, such as an unreadable `audits/` directory, is an error: treating it
/// as absent would silently swap the registry the project wrote for an older
/// one. A dangling symlink at the deed path is likewise not absent; it fails
/// when it is opened.
pub fn load_user_classifications(project_root: &Path) -> Result<Vec<UserClassification>> {
let deed_path = project_root.join(DEED_PATH);
if deed_path.exists() {
let deed_present = match fs::symlink_metadata(&deed_path) {
Ok(_) => true,
Err(e) if matches!(e.kind(), ErrorKind::NotFound | ErrorKind::NotADirectory) => false,
Err(e) => {
return Err(e).with_context(|| {
format!(
"cannot tell whether the classification registry {} exists",
deed_path.display()
)
})
}
};
if deed_present {
let text = read_capped(&deed_path)?;
return parse_classification_deed(&text, &deed_path)
.with_context(|| format!("malformed classification registry {}", deed_path.display()));
Expand Down
73 changes: 73 additions & 0 deletions src/assail/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,79 @@ mod classifications_tests {
}
}

/// An `audits/` the reader cannot search must not look like a missing
/// deed: that would silently fall back to the root `.a2ml` registry.
#[cfg(unix)]
#[test]
fn unreadable_audits_dir_is_an_error_not_a_fallback() {
use std::os::unix::fs::PermissionsExt;
let tmp = TempDir::new().unwrap();
write_deed(tmp.path(), &deed_with(ZIG_BRIDGE_DEED_ENTRY));
fs::write(
tmp.path().join(".panic-attack-classifications.a2ml"),
r#"(assail-classifications
(classification (file "a/b.rs") (category "PanicPath")))
"#,
)
.unwrap();
let audits = tmp.path().join("audits");
fs::set_permissions(&audits, fs::Permissions::from_mode(0o000)).unwrap();
// Root, or any process with CAP_DAC_OVERRIDE/CAP_DAC_READ_SEARCH,
// ignores the mode bits, so the deed stays readable and there is
// nothing to test. Probe instead of checking the uid: the probe also
// covers capabilities granted to a non-root user.
let privileged = fs::read_dir(&audits).is_ok();
let result = load_user_classifications(tmp.path());
// Restore the mode so TempDir can clean up.
fs::set_permissions(&audits, fs::Permissions::from_mode(0o755)).unwrap();
if privileged {
eprintln!(
"skipped: running as root (or with DAC override), so a mode-000 directory stays readable"
);
return;
}
let err = result.expect_err("an unreadable audits/ must be an error");
assert!(
format!("{err:#}").contains("assail-classifications_praxis.deed"),
"error must name the registry: {err:#}"
);
}

#[test]
fn audits_as_a_regular_file_means_no_deed() {
let tmp = TempDir::new().unwrap();
fs::write(tmp.path().join("audits"), "not a directory\n").unwrap();
fs::write(
tmp.path().join(".panic-attack-classifications.a2ml"),
r#"(assail-classifications
(classification (file "a/b.rs") (category "PanicPath")))
"#,
)
.unwrap();
let classifications = load_user_classifications(tmp.path()).unwrap();
assert_eq!(classifications.len(), 1, "falls back to the root registry");
}

#[cfg(unix)]
#[test]
fn dangling_deed_symlink_is_an_error_not_a_fallback() {
let tmp = TempDir::new().unwrap();
let audits = tmp.path().join("audits");
fs::create_dir_all(&audits).unwrap();
std::os::unix::fs::symlink(
tmp.path().join("nowhere.deed"),
tmp.path().join(USER_CLASSIFICATIONS_DEED_PATH),
)
.unwrap();
write_registry(
tmp.path(),
r#"(assail-classifications
(classification (file "a/b.rs") (category "PanicPath")))
"#,
);
assert!(load_user_classifications(tmp.path()).is_err());
}

#[test]
fn analyze_fails_on_a_malformed_deed() {
let tmp = TempDir::new().unwrap();
Expand Down
37 changes: 36 additions & 1 deletion src/assemblyline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,10 @@ fn scan_repo(repo_path: &Path) -> RepoResult {
high_count: 0,
total_files: 0,
total_lines: 0,
error: Some(e.to_string()),
// `{:#}` keeps the whole anyhow chain ("outer: inner: …"), so a
// malformed registry deed reports its line and reason here, not
// just the outermost context.
error: Some(format!("{e:#}")),
fingerprint,
report: None,
},
Expand Down Expand Up @@ -576,6 +579,38 @@ mod tests {
);
}

#[test]
fn scan_error_keeps_the_deed_line_and_reason() {
let tmp = tempfile::TempDir::new().unwrap();
std::fs::write(tmp.path().join("main.rs"), "fn main() {}\n").unwrap();
let audits = tmp.path().join("audits");
std::fs::create_dir_all(&audits).unwrap();
// Line 7 carries a category the registry does not know.
std::fs::write(
audits.join("assail-classifications_praxis.deed"),
";; SPDX-License-Identifier: MPL-2.0\n\
(praxis-deed\n \
:schema-version \"1.0.0\"\n \
:canonical-name \"x\"\n \
:beholding-chora #uuid\"0889b37b-defb-831f-973c-e119500db00e\"\n \
:profile assail-classifications\n \
(classification :file \"a.rs\" :category UnsafeKode))\n",
)
.unwrap();
let err = scan_repo(tmp.path())
.error
.expect("a malformed deed fails the scan");
assert!(
err.contains("assail-classifications_praxis.deed"),
"names the file: {err}"
);
assert!(err.contains("line 7"), "keeps the line: {err}");
assert!(
err.contains("unknown weak-point category UnsafeKode"),
"keeps the reason: {err}"
);
}

#[test]
fn test_is_source_file() {
assert!(is_source_file(Path::new("main.rs")));
Expand Down
Loading
Loading