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
55 changes: 54 additions & 1 deletion crates/lib/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ fn lint_inner<'skip>(
let name = lint.name;
let r = match r {
Ok(r) => r,
Err(e) => anyhow::bail!("Unexpected runtime error running lint {name}: {e}"),
Err(e) => {
return Err(e.context(format!("Unexpected runtime error running lint {name}")));
}
};

if let Err(e) = r {
Expand Down Expand Up @@ -1586,4 +1588,55 @@ mod tests {
);
Ok(())
}

#[test]
fn test_runtime_deps_escape() -> Result<()> {
let root = &fixture()?;
root.create_dir("usr")?;
// Same shape as /usr/local -> ../../var/usrlocal, on the directory
// runtime dependency lookup actually searches.
root.symlink_contents("../../var/usrbin", "usr/bin")?;

let config = &LintExecutionConfig::default();
let mut out = Vec::new();
let err = lint_inner(
root,
RootType::Alternative,
config,
LINTS
.iter()
.filter(|lint| lint.name != "runtime-deps")
.map(|lint| lint.name),
&mut out,
)
.expect_err("lookup failure must remain a runtime error");

let bin = resolved_runtime_bins()
.into_iter()
.next()
.expect("runtime dependency");
let attempted = format!("usr/bin/{bin}");
assert_eq!(
err.to_string(),
"Unexpected runtime error running lint runtime-deps"
);
assert!(
err.chain()
.any(|source| source.to_string().contains(&attempted)),
"{err:?}"
);
assert!(
err.chain().any(|source| {
source
.downcast_ref::<std::io::Error>()
.is_some_and(|io_err| {
io_err
.to_string()
.contains("a path led outside of the filesystem")
})
}),
"{err:?}"
);
Ok(())
}
}
60 changes: 59 additions & 1 deletion crates/lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ fn executable_candidates(name: &str) -> Vec<PathBuf> {
pub fn have_executable_in_root(root: &Dir, name: &str) -> Result<bool> {
for candidate in executable_candidates(name) {
let candidate = candidate.strip_prefix("/").unwrap_or(&candidate);
if root.try_exists(candidate)? {
if root
.try_exists(candidate)
.with_context(|| format!("checking whether {} exists", candidate.display()))?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nonblocking but we have our own little helper for this to quote file paths in a nicer way we should add to the Rust agent review checklist to use

{
return Ok(true);
}
}
Expand Down Expand Up @@ -368,6 +371,61 @@ mod tests {
root.write("usr/bin/podman", "")?;
assert!(have_executable_in_root(&root, "podman")?);
assert!(!have_executable_in_root(&root, "missing")?);

// An absolute name replaces each PATH entry via PathBuf::push, so the
// candidate is independent of the process PATH.
const PROBE: &str = "/usr/local/bin/probe";
let cases = [
("present", Some(true)),
("absent", Some(false)),
("escapes", None),
("contained-symlink", Some(true)),
];
for (label, expected) in cases {
let root = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
match label {
"present" => {
root.create_dir_all("usr/local/bin")?;
root.write("usr/local/bin/probe", "")?;
}
"absent" => {}
"escapes" => {
root.create_dir("usr")?;
root.symlink_contents("../../var/usrlocal", "usr/local")?;
}
"contained-symlink" => {
root.create_dir_all("var/usrlocal/bin")?;
root.write("var/usrlocal/bin/probe", "")?;
root.create_dir("usr")?;
root.symlink_contents("../var/usrlocal", "usr/local")?;
}
_ => unreachable!("{label}"),
}
let found = have_executable_in_root(&root, PROBE);
match expected {
Some(expected) => assert_eq!(found?, expected, "{label}"),
None => {
let err = found.expect_err(label);
assert!(
err.chain()
.any(|source| source.to_string().contains("usr/local/bin/probe")),
"{label}: {err:?}"
);
assert!(
err.chain().any(|source| {
source
.downcast_ref::<std::io::Error>()
.is_some_and(|io_err| {
io_err
.to_string()
.contains("a path led outside of the filesystem")
})
}),
"{label}: {err:?}"
);
}
}
}
Ok(())
}
}
Loading