diff options
| author | bors <bors@rust-lang.org> | 2024-05-26 04:14:32 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-05-26 04:14:32 +0000 |
| commit | bd184cc3e1ee16c1d74444fa246dcfbade9a31d5 (patch) | |
| tree | 364e2ec0b4064a1da01f6f14460bacae93272b34 /library/std/src | |
| parent | 75e2c5dcd0ddce0fe0eb3d4a2195cd51073c729b (diff) | |
| parent | 700b3ea61ba2a37876af0a5d2d2f991aba5c7000 (diff) | |
| download | rust-bd184cc3e1ee16c1d74444fa246dcfbade9a31d5.tar.gz rust-bd184cc3e1ee16c1d74444fa246dcfbade9a31d5.zip | |
Auto merge of #125070 - tbu-:pr_set_extension_panic, r=jhpratt
Panic if `PathBuf::set_extension` would add a path separator This is likely never intended and potentially a security vulnerability if it happens. I'd guess that it's mostly literal strings that are passed to this function in practice, so I'm guessing this doesn't break anyone. CC #125060
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/path.rs | 13 | ||||
| -rw-r--r-- | library/std/src/path/tests.rs | 23 |
2 files changed, 36 insertions, 0 deletions
diff --git a/library/std/src/path.rs b/library/std/src/path.rs index f835b69f0cf..f4e1e2a38c1 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -1425,6 +1425,11 @@ impl PathBuf { /// If `extension` is the empty string, [`self.extension`] will be [`None`] /// afterwards, not `Some("")`. /// + /// # Panics + /// + /// Panics if the passed extension contains a path separator (see + /// [`is_separator`]). + /// /// # Caveats /// /// The new `extension` may contain dots and will be used in its entirety, @@ -1470,6 +1475,14 @@ impl PathBuf { } fn _set_extension(&mut self, extension: &OsStr) -> bool { + for &b in extension.as_encoded_bytes() { + if b < 128 { + if is_separator(b as char) { + panic!("extension cannot contain path separators: {:?}", extension); + } + } + } + let file_stem = match self.file_stem() { None => return false, Some(f) => f.as_encoded_bytes(), diff --git a/library/std/src/path/tests.rs b/library/std/src/path/tests.rs index fde6ed4f0c0..2d8e50d1f88 100644 --- a/library/std/src/path/tests.rs +++ b/library/std/src/path/tests.rs @@ -1803,6 +1803,29 @@ fn test_windows_absolute() { assert_eq!(absolute(r"COM1").unwrap().as_os_str(), Path::new(r"\\.\COM1").as_os_str()); } +#[test] +#[should_panic = "path separator"] +fn test_extension_path_sep() { + let mut path = PathBuf::from("path/to/file"); + path.set_extension("d/../../../../../etc/passwd"); +} + +#[test] +#[should_panic = "path separator"] +#[cfg(windows)] +fn test_extension_path_sep_alternate() { + let mut path = PathBuf::from("path/to/file"); + path.set_extension("d\\test"); +} + +#[test] +#[cfg(not(windows))] +fn test_extension_path_sep_alternate() { + let mut path = PathBuf::from("path/to/file"); + path.set_extension("d\\test"); + assert_eq!(path, Path::new("path/to/file.d\\test")); +} + #[bench] #[cfg_attr(miri, ignore)] // Miri isn't fast... fn bench_path_cmp_fast_path_buf_sort(b: &mut test::Bencher) { |
