Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hashsum: also escape/unescape files with checks #5868

Merged
merged 7 commits into from
Mar 12, 2024
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
42 changes: 29 additions & 13 deletions src/uu/hashsum/src/hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,15 @@
.short('c')
.long("check")
.help("read hashsums from the FILEs and check them")
.action(ArgAction::SetTrue),
.action(ArgAction::SetTrue)
.conflicts_with("tag"),
)
.arg(
Arg::new("tag")
.long("tag")
.help("create a BSD-style checksum")
.action(ArgAction::SetTrue),
.action(ArgAction::SetTrue)
.conflicts_with("text"),
)
.arg(
Arg::new("text")
Expand Down Expand Up @@ -630,7 +632,8 @@
}
let mut gnu_re = gnu_re_template(&bytes_marker, r"(?P<binary>[ \*])?")?;
let bsd_re = Regex::new(&format!(
r"^{algorithm} \((?P<fileName>.*)\) = (?P<digest>[a-fA-F0-9]{digest_size})",
// it can start with \
r"^(|\\){algorithm} \((?P<fileName>.*)\) = (?P<digest>[a-fA-F0-9]{digest_size})",
algorithm = options.algoname,
digest_size = bytes_marker,
))
Expand Down Expand Up @@ -699,7 +702,8 @@
}
},
};
let f = match File::open(ck_filename.clone()) {
let (ck_filename_unescaped, prefix) = unescape_filename(&ck_filename);
let f = match File::open(ck_filename_unescaped) {
Err(_) => {
failed_open_file += 1;
println!(
Expand Down Expand Up @@ -732,11 +736,11 @@
// easier (and more important) on Unix than on Windows.
if sum == real_sum {
if !options.quiet {
println!("{ck_filename}: OK");
println!("{prefix}{ck_filename}: OK");
}
} else {
if !options.status {
println!("{ck_filename}: FAILED");
println!("{prefix}{ck_filename}: FAILED");

Check warning on line 743 in src/uu/hashsum/src/hashsum.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/hashsum/src/hashsum.rs#L743

Added line #L743 was not covered by tests
}
failed_cksum += 1;
}
Expand All @@ -749,16 +753,19 @@
options.output_bits,
)
.map_err_context(|| "failed to read input".to_string())?;
let (escaped_filename, prefix) = escape_filename(filename);
if options.tag {
println!("{} ({}) = {}", options.algoname, filename.display(), sum);
println!(
"{}{} ({}) = {}",
prefix, options.algoname, escaped_filename, sum
);
} else if options.nonames {
println!("{sum}");
} else if options.zero {
// with zero, we don't escape the filename
print!("{} {}{}\0", sum, binary_marker, filename.display());
} else {
let (filename, has_prefix) = escape_filename(filename);
let prefix = if has_prefix { "\\" } else { "" };
println!("{}{} {}{}", prefix, sum, binary_marker, filename);
println!("{}{} {}{}", prefix, sum, binary_marker, escaped_filename);
}
}
}
Expand All @@ -783,14 +790,23 @@
Ok(())
}

fn escape_filename(filename: &Path) -> (String, bool) {
fn unescape_filename(filename: &str) -> (String, &'static str) {
let unescaped = filename
.replace("\\\\", "\\")
.replace("\\n", "\n")
.replace("\\r", "\r");
let prefix = if unescaped == filename { "" } else { "\\" };
(unescaped, prefix)
}

fn escape_filename(filename: &Path) -> (String, &'static str) {
let original = filename.as_os_str().to_string_lossy();
let escaped = original
.replace('\\', "\\\\")
.replace('\n', "\\n")
.replace('\r', "\\r");
let has_changed = escaped != original;
(escaped, has_changed)
let prefix = if escaped == original { "" } else { "\\" };
(escaped, prefix)
}

fn digest_reader<T: Read>(
Expand Down
60 changes: 60 additions & 0 deletions tests/by-util/test_hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,22 @@ fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}

#[test]
fn test_conflicting_arg() {
new_ucmd!()
.arg("--tag")
.arg("--check")
.arg("--md5")
.fails()
.code_is(1);
new_ucmd!()
.arg("--tag")
.arg("--text")
.arg("--md5")
.fails()
.code_is(1);
}

#[test]
fn test_tag() {
let scene = TestScenario::new(util_name!());
Expand Down Expand Up @@ -386,3 +402,47 @@ fn test_with_escape_filename() {
assert!(stdout.starts_with('\\'));
assert!(stdout.trim().ends_with("a\\nb"));
}

#[test]
#[cfg(not(windows))]
fn test_with_escape_filename_zero_text() {
let scene = TestScenario::new(util_name!());

let at = &scene.fixtures;
let filename = "a\nb";
at.touch(filename);
let result = scene
.ccmd("md5sum")
.arg("--text")
.arg("--zero")
.arg(filename)
.succeeds();
let stdout = result.stdout_str();
println!("stdout {}", stdout);
assert!(!stdout.starts_with('\\'));
assert!(stdout.contains("a\nb"));
}

#[test]
#[cfg(not(windows))]
fn test_check_with_escape_filename() {
let scene = TestScenario::new(util_name!());

let at = &scene.fixtures;

let filename = "a\nb";
at.touch(filename);
let result = scene.ccmd("md5sum").arg("--tag").arg(filename).succeeds();
let stdout = result.stdout_str();
println!("stdout {}", stdout);
assert!(stdout.starts_with("\\MD5"));
assert!(stdout.contains("a\\nb"));
at.write("check.md5", stdout);
let result = scene
.ccmd("md5sum")
.arg("--strict")
.arg("-c")
.arg("check.md5")
.succeeds();
result.stdout_is("\\a\\nb: OK\n");
}
7 changes: 5 additions & 2 deletions util/build-gnu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ cp "${UU_BUILD_DIR}/install" "${UU_BUILD_DIR}/ginstall" # The GNU tests rename t
# Create *sum binaries
for sum in b2sum b3sum md5sum sha1sum sha224sum sha256sum sha384sum sha512sum; do
sum_path="${UU_BUILD_DIR}/${sum}"
test -f "${sum_path}" || cp "${UU_BUILD_DIR}/hashsum" "${sum_path}"
test -f "${sum_path}" || (cd ${UU_BUILD_DIR} && ln -s "hashsum" "${sum}")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

otherwise, I was testing always the same program :)

done
test -f "${UU_BUILD_DIR}/[" || cp "${UU_BUILD_DIR}/test" "${UU_BUILD_DIR}/["
test -f "${UU_BUILD_DIR}/[" || (cd ${UU_BUILD_DIR} && ln -s "test" "[")

##

Expand Down Expand Up @@ -329,6 +329,9 @@ ls: invalid --time-style argument 'XX'\nPossible values are: [\"full-iso\", \"lo
# "hostid BEFORE --help AFTER " same for this
sed -i -e "s/env \$prog \$BEFORE \$opt > out2/env \$prog \$BEFORE \$opt > out2 #/" -e "s/env \$prog \$BEFORE \$opt AFTER > out3/env \$prog \$BEFORE \$opt AFTER > out3 #/" -e "s/compare exp out2/compare exp out2 #/" -e "s/compare exp out3/compare exp out3 #/" tests/help/help-version-getopt.sh

# The case doesn't really matter here
sed -i -e "s|WARNING: 1 line is improperly formatted|warning: 1 line is improperly formatted|" tests/cksum/md5sum-bsd.sh

# Add debug info + we have less syscall then GNU's. Adjust our check.
# Use GNU sed for /c command
"${SED}" -i -e '/test \$n_stat1 = \$n_stat2 \\/c\
Expand Down
Loading