From af4ce911c8315e3f339ea809ccf04b65ef834075 Mon Sep 17 00:00:00 2001 From: ZauJulio Date: Thu, 16 Feb 2023 17:48:23 -0300 Subject: [PATCH 1/8] mktemp: fix PrefixContainsDirSeparator verification --- src/uu/mktemp/src/mktemp.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/uu/mktemp/src/mktemp.rs b/src/uu/mktemp/src/mktemp.rs index 390c77bfa..fed946577 100644 --- a/src/uu/mktemp/src/mktemp.rs +++ b/src/uu/mktemp/src/mktemp.rs @@ -281,7 +281,11 @@ impl Params { .join(prefix_from_template) .display() .to_string(); - if options.treat_as_template && prefix.contains(MAIN_SEPARATOR) { + + // Check that the prefix is valid. + let prefix_of_template = Path::new(prefix_from_template).display().to_string(); + + if options.treat_as_template && prefix_of_template.contains(MAIN_SEPARATOR) { return Err(MkTempError::PrefixContainsDirSeparator(options.template)); } if tmpdir.is_some() && Path::new(prefix_from_template).is_absolute() { From 9e2c543cd40c6b237097f17ae66141e8fc9d4413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Za=C3=BA=20J=C3=BAlio?= Date: Thu, 16 Feb 2023 18:43:32 -0300 Subject: [PATCH 2/8] mktemp: prefix prefix_of_template use to_string_lossy Co-authored-by: Terts Diepraam --- src/uu/mktemp/src/mktemp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/mktemp/src/mktemp.rs b/src/uu/mktemp/src/mktemp.rs index fed946577..1764b8e84 100644 --- a/src/uu/mktemp/src/mktemp.rs +++ b/src/uu/mktemp/src/mktemp.rs @@ -283,7 +283,7 @@ impl Params { .to_string(); // Check that the prefix is valid. - let prefix_of_template = Path::new(prefix_from_template).display().to_string(); + let prefix_of_template = Path::new(prefix_from_template).to_string_lossy(); if options.treat_as_template && prefix_of_template.contains(MAIN_SEPARATOR) { return Err(MkTempError::PrefixContainsDirSeparator(options.template)); From 3d2f3fc5b155a1ba9161e921ffbdf763f671712f Mon Sep 17 00:00:00 2001 From: ZauJulio Date: Wed, 22 Feb 2023 20:22:16 -0300 Subject: [PATCH 3/8] mktemp: add test to . in template prefix --- tests/by-util/test_mktemp.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/by-util/test_mktemp.rs b/tests/by-util/test_mktemp.rs index d6926c41b..6d4a7fb6f 100644 --- a/tests/by-util/test_mktemp.rs +++ b/tests/by-util/test_mktemp.rs @@ -21,6 +21,7 @@ static TEST_TEMPLATE6: &str = "tempXXXlate"; // spell-checker:disable-line static TEST_TEMPLATE7: &str = "XXXtemplate"; // spell-checker:disable-line #[cfg(unix)] static TEST_TEMPLATE8: &str = "tempXXXl/ate"; +static TEST_TEMPLATE9: &str = "a.XXXX"; #[cfg(windows)] static TEST_TEMPLATE8: &str = "tempXXXl\\ate"; @@ -569,6 +570,14 @@ fn test_template_path_separator() { )); } +/// Test that a prefix with a point is valid. +#[test] +fn test_prefix_template_separator() { + new_ucmd!() + .args(&["-t", TEST_TEMPLATE9]) + .succeeds(); +} + /// Test that a suffix with a path separator is invalid. #[test] fn test_suffix_path_separator() { From dc1fd027a6ca0f609cf9ba3eb63c6bc75ec9c011 Mon Sep 17 00:00:00 2001 From: ZauJulio Date: Sat, 25 Feb 2023 18:35:35 -0300 Subject: [PATCH 4/8] mktemp: fix fmt of test_prefix_template_separator --- tests/by-util/test_mktemp.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/by-util/test_mktemp.rs b/tests/by-util/test_mktemp.rs index dc09204b6..081e4da1b 100644 --- a/tests/by-util/test_mktemp.rs +++ b/tests/by-util/test_mktemp.rs @@ -575,9 +575,7 @@ fn test_template_path_separator() { /// Test that a prefix with a point is valid. #[test] fn test_prefix_template_separator() { - new_ucmd!() - .args(&["-t", TEST_TEMPLATE9]) - .succeeds(); + new_ucmd!().args(&["-t", TEST_TEMPLATE9]).succeeds(); } /// Test that a suffix with a path separator is invalid. From a87bc9f9291fe707900f3434a8bdc7803366eb44 Mon Sep 17 00:00:00 2001 From: ZauJulio Date: Mon, 27 Feb 2023 01:13:16 -0300 Subject: [PATCH 5/8] mktemp: remove unnecessary convertion for path -> str just use prefix_from_template --- src/uu/mktemp/src/mktemp.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/uu/mktemp/src/mktemp.rs b/src/uu/mktemp/src/mktemp.rs index 1764b8e84..5d4308958 100644 --- a/src/uu/mktemp/src/mktemp.rs +++ b/src/uu/mktemp/src/mktemp.rs @@ -281,11 +281,7 @@ impl Params { .join(prefix_from_template) .display() .to_string(); - - // Check that the prefix is valid. - let prefix_of_template = Path::new(prefix_from_template).to_string_lossy(); - - if options.treat_as_template && prefix_of_template.contains(MAIN_SEPARATOR) { + if options.treat_as_template && prefix_from_template.contains(MAIN_SEPARATOR) { return Err(MkTempError::PrefixContainsDirSeparator(options.template)); } if tmpdir.is_some() && Path::new(prefix_from_template).is_absolute() { From 9b4fb0cb6eb46b3e3cba1d318e317b821dfa9328 Mon Sep 17 00:00:00 2001 From: ZauJulio Date: Tue, 28 Feb 2023 12:38:18 -0300 Subject: [PATCH 6/8] mktemp: add tests UNIX and POSIX to check path in prefix --- tests/by-util/test_mktemp.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/by-util/test_mktemp.rs b/tests/by-util/test_mktemp.rs index 081e4da1b..7c3d62150 100644 --- a/tests/by-util/test_mktemp.rs +++ b/tests/by-util/test_mktemp.rs @@ -578,6 +578,26 @@ fn test_prefix_template_separator() { new_ucmd!().args(&["-t", TEST_TEMPLATE9]).succeeds(); } +#[test] +fn test_prefix_template_with_path_separator() { + #[cfg(not(windows))] + new_ucmd!() + .args(&["-t", "a/XXX"]) + .fails() + .stderr_only(format!( + "mktemp: invalid template, {}, contains directory separator\n", + "a/XXX".quote() + )); + #[cfg(windows)] + new_ucmd!() + .args(&["-t", r"a\XXX"]) + .fails() + .stderr_only(format!( + "mktemp: invalid template, {}, contains directory separator\n", + r"a\XXX".quote() + )); +} + /// Test that a suffix with a path separator is invalid. #[test] fn test_suffix_path_separator() { From e9bd69e0519c380663a0cda9abffb75209679acb Mon Sep 17 00:00:00 2001 From: ZauJulio Date: Wed, 1 Mar 2023 15:45:41 -0300 Subject: [PATCH 7/8] mktemp: fix test_prefix_template_separator adding -p param --- tests/by-util/test_mktemp.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/by-util/test_mktemp.rs b/tests/by-util/test_mktemp.rs index 7c3d62150..b6e3b2076 100644 --- a/tests/by-util/test_mktemp.rs +++ b/tests/by-util/test_mktemp.rs @@ -21,9 +21,9 @@ static TEST_TEMPLATE6: &str = "tempXXXlate"; // spell-checker:disable-line static TEST_TEMPLATE7: &str = "XXXtemplate"; // spell-checker:disable-line #[cfg(unix)] static TEST_TEMPLATE8: &str = "tempXXXl/ate"; -static TEST_TEMPLATE9: &str = "a.XXXX"; #[cfg(windows)] static TEST_TEMPLATE8: &str = "tempXXXl\\ate"; +static TEST_TEMPLATE9: &str = "a.XXXX"; #[cfg(not(windows))] const TMPDIR: &str = "TMPDIR"; @@ -575,7 +575,7 @@ fn test_template_path_separator() { /// Test that a prefix with a point is valid. #[test] fn test_prefix_template_separator() { - new_ucmd!().args(&["-t", TEST_TEMPLATE9]).succeeds(); + new_ucmd!().args(&["-p", ".", "-t", TEST_TEMPLATE9]).succeeds(); } #[test] From b94a0d2ebea6e151ad52f32f96b8ea9f09bb5a4d Mon Sep 17 00:00:00 2001 From: ZauJulio Date: Thu, 2 Mar 2023 15:30:55 -0300 Subject: [PATCH 8/8] mktemp: fix test fmt --- tests/by-util/test_mktemp.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/by-util/test_mktemp.rs b/tests/by-util/test_mktemp.rs index b6e3b2076..8d615899c 100644 --- a/tests/by-util/test_mktemp.rs +++ b/tests/by-util/test_mktemp.rs @@ -575,7 +575,9 @@ fn test_template_path_separator() { /// Test that a prefix with a point is valid. #[test] fn test_prefix_template_separator() { - new_ucmd!().args(&["-p", ".", "-t", TEST_TEMPLATE9]).succeeds(); + new_ucmd!() + .args(&["-p", ".", "-t", TEST_TEMPLATE9]) + .succeeds(); } #[test]