diff options
| author | Tobias Bucher <tobiasbucher5991@gmail.com> | 2024-05-13 14:22:45 +0200 |
|---|---|---|
| committer | Tobias Bucher <tobiasbucher5991@gmail.com> | 2024-05-13 15:08:34 +0200 |
| commit | 700b3ea61ba2a37876af0a5d2d2f991aba5c7000 (patch) | |
| tree | ee79b635ad1f492c544c66ddd78a5404a23e749e /library/std/src/path | |
| parent | 6be7b0c7d2b085474f9f2f9323c2266f4df105d8 (diff) | |
| download | rust-700b3ea61ba2a37876af0a5d2d2f991aba5c7000.tar.gz rust-700b3ea61ba2a37876af0a5d2d2f991aba5c7000.zip | |
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/path')
| -rw-r--r-- | library/std/src/path/tests.rs | 23 |
1 files changed, 23 insertions, 0 deletions
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) { |
