Skip to content

Commit b0f0a87

Browse files
authored
Remove obsoleted bash script (#1427)
Also replace a unit test that relied on this script to the test-framework
1 parent e171b3b commit b0f0a87

File tree

3 files changed

+40
-59
lines changed

3 files changed

+40
-59
lines changed

make-lcov-info.bash

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/common/resolve.rs

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ where
190190
}
191191

192192
/// Check whether a path points to a regular file and any executable flag is set
193-
pub(crate) fn is_valid_executable(path: &PathBuf) -> bool {
193+
pub(crate) fn is_valid_executable(path: &Path) -> bool {
194194
if path.is_file() {
195195
match fs::metadata(path) {
196196
Ok(meta) => meta.mode() & 0o111 != 0,
@@ -216,12 +216,12 @@ pub(crate) fn resolve_path(command: &Path, path: &str) -> Option<PathBuf> {
216216
// construct a possible executable absolute path candidate
217217
.map(|path| path.join(command))
218218
// check whether the candidate is a regular file and any executable flag is set
219-
.find(is_valid_executable)
219+
.find(|arg| is_valid_executable(arg))
220220
}
221221

222222
#[cfg(test)]
223223
mod tests {
224-
use std::path::PathBuf;
224+
use std::path::Path;
225225

226226
use crate::common::resolve::CurrentUser;
227227
use crate::system::ROOT_GROUP_NAME;
@@ -234,43 +234,18 @@ mod tests {
234234
let path = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
235235

236236
assert!(is_valid_executable(
237-
&resolve_path(&PathBuf::from("yes"), path).unwrap()
237+
&resolve_path(Path::new("yes"), path).unwrap()
238238
));
239239

240240
assert!(is_valid_executable(
241-
&resolve_path(&PathBuf::from("whoami"), path).unwrap()
241+
&resolve_path(Path::new("whoami"), path).unwrap()
242242
));
243243

244244
assert!(is_valid_executable(
245-
&resolve_path(&PathBuf::from("env"), path).unwrap()
245+
&resolve_path(Path::new("env"), path).unwrap()
246246
));
247-
assert_eq!(
248-
resolve_path(&PathBuf::from("thisisnotonyourfs"), path),
249-
None
250-
);
251-
assert_eq!(resolve_path(&PathBuf::from("thisisnotonyourfs"), "."), None);
252-
}
253-
254-
#[test]
255-
fn test_cwd_resolve_path() {
256-
// We modify the path to contain ".", which is supposed to be ignored
257-
let path = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:.";
258-
259-
let cwd = std::env::current_dir().unwrap();
260-
261-
// we filter for executable files, so it is most likely going to pick one of the shell
262-
// scripts in the project's root
263-
let some_file = cwd
264-
.read_dir()
265-
.unwrap()
266-
.filter_map(|entry| entry.ok())
267-
.find_map(|entry| {
268-
let pathbuf = PathBuf::from(entry.file_name());
269-
is_valid_executable(&pathbuf).then_some(pathbuf)
270-
})
271-
.unwrap();
272-
273-
assert_eq!(resolve_path(&some_file, path), None);
247+
assert_eq!(resolve_path(Path::new("thisisnotonyourfs"), path), None);
248+
assert_eq!(resolve_path(Path::new("thisisnotonyourfs"), "."), None);
274249
}
275250

276251
#[test]

test-framework/sudo-compliance-tests/src/sudo/path_search.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,38 @@ fn can_find_command_not_visible_to_regular_user() {
3131
.assert_success();
3232
}
3333

34+
#[test]
35+
//Cross-reference: sudoers::secure_path::if_set_searches_program_in_secure_path for
36+
//testing that relative paths in secure_path are also not matched.
37+
fn does_not_use_relative_paths() {
38+
let path = "/root/my-script";
39+
let env = Env("Defaults ignore_dot
40+
ALL ALL=(ALL:ALL) NOPASSWD: ALL")
41+
.user(USERNAME)
42+
.file(path, TextFile("#!/bin/sh").chmod("100"))
43+
.build();
44+
45+
let output = Command::new("sh")
46+
.args([
47+
"-c",
48+
&format!("export PATH=.; cd /root; {BIN_SUDO} my-script"),
49+
])
50+
.output(&env);
51+
52+
output.assert_exit_code(1);
53+
54+
if sudo_test::is_original_sudo() {
55+
assert_eq!(
56+
output.stderr(),
57+
"sudo: ignoring \"my-script\" found in '.'
58+
Use \"sudo ./my-script\" if this is the \"my-script\" you wish to run."
59+
);
60+
} else {
61+
//NOTE: we don't have a specialized error message for this case
62+
assert_eq!(output.stderr(), "sudo: 'my-script': command not found");
63+
}
64+
}
65+
3466
#[test]
3567
fn when_path_is_unset_does_not_search_in_default_path_set_for_command_execution() {
3668
let path = "/usr/bin/my-script";

0 commit comments

Comments
 (0)