Debian Codegraph @

debian/rust-vendor/zip/src/path.rs

raw
1 //! Path manipulation utilities
2
3 use std::{
4 ffi::OsStr,
5 path::{Path, PathBuf},
6 };
7 use typed_path::{Utf8WindowsComponent, Utf8WindowsPath};
8
9 /// Simplify a path by removing the prefix and parent directories and only return normal components
10 pub(crate) fn simplified_components(input: &Path) -> Option<Vec<&OsStr>> {
11 let mut out = Vec::new();
12 let input_str = input.to_str()?;
13 for component in Utf8WindowsPath::new(input_str).components() {
14 match component {
15 // Skip prefix and root directory components instead of rejecting the entire path
16 // This allows extraction of ZIP files with absolute paths, similar to other ZIP tools
17 Utf8WindowsComponent::Prefix(_) | Utf8WindowsComponent::RootDir => (),
18 Utf8WindowsComponent::ParentDir => {
19 out.pop()?;
20 }
21 Utf8WindowsComponent::Normal(s) => out.push(OsStr::new(s)),
22 Utf8WindowsComponent::CurDir => (),
23 }
24 }
25 Some(out)
26 }
27
28 pub(crate) fn file_name_sanitized(no_null_filename: &str) -> PathBuf {
29 Utf8WindowsPath::new(no_null_filename)
30 .components()
31 .filter_map(|component| match component {
32 Utf8WindowsComponent::Normal(s) => Some(s),
33 _ => None,
34 })
35 .collect()
36 }
37
38 pub(crate) fn enclosed_name(file_name: &str) -> Option<PathBuf> {
39 let mut depth = 0usize;
40 let mut out_path = PathBuf::new();
41 for component in Utf8WindowsPath::new(file_name).components() {
42 match component {
43 Utf8WindowsComponent::Prefix(_) | Utf8WindowsComponent::RootDir => {
44 if depth > 0 {
45 return None;
46 }
47 }
48 Utf8WindowsComponent::ParentDir => {
49 depth = depth.checked_sub(1)?;
50 out_path.pop();
51 }
52 Utf8WindowsComponent::Normal(s) => {
53 depth += 1;
54 out_path.push(s);
55 }
56 Utf8WindowsComponent::CurDir => (),
57 }
58 }
59 Some(out_path)
60 }
61
62 #[cfg(test)]
63 mod tests {
64 use super::simplified_components;
65 use std::path::Path;
66
67 #[test]
68 fn test_simplified_components_relative_path() {
69 let path = Path::new("foo/bar/baz.txt");
70 let components = simplified_components(path).unwrap();
71 assert_eq!(components.len(), 3);
72 assert_eq!(components[0], "foo");
73 assert_eq!(components[1], "bar");
74 assert_eq!(components[2], "baz.txt");
75 }
76
77 #[test]
78 fn test_simplified_components_absolute_unix_path() {
79 let path = Path::new("/foo/bar/baz.txt");
80 let components = simplified_components(path).unwrap();
81 assert_eq!(components.len(), 3);
82 assert_eq!(components[0], "foo");
83 assert_eq!(components[1], "bar");
84 assert_eq!(components[2], "baz.txt");
85 }
86
87 #[test]
88 fn test_simplified_components_with_parent_dirs() {
89 let path = Path::new("foo/../bar/baz.txt");
90 let components = simplified_components(path).unwrap();
91 assert_eq!(components.len(), 2);
92 assert_eq!(components[0], "bar");
93 assert_eq!(components[1], "baz.txt");
94 }
95
96 #[test]
97 fn test_simplified_components_too_many_parent_dirs() {
98 let path = Path::new("foo/../../bar");
99 let result = simplified_components(path);
100 assert!(result.is_none()); // Should still fail for directory traversal attacks
101 }
102
103 #[test]
104 fn test_simplified_components_with_current_dir() {
105 let path = Path::new("foo/./bar/baz.txt");
106 let components = simplified_components(path).unwrap();
107 assert_eq!(components.len(), 3);
108 assert_eq!(components[0], "foo");
109 assert_eq!(components[1], "bar");
110 assert_eq!(components[2], "baz.txt");
111 }
112
113 #[test]
114 fn test_simplified_components_empty_path() {
115 let path = Path::new("");
116 let components = simplified_components(path).unwrap();
117 assert_eq!(components.len(), 0);
118 }
119
120 #[test]
121 fn test_simplified_components_root_only() {
122 let path = Path::new("/");
123 let components = simplified_components(path).unwrap();
124 assert_eq!(components.len(), 0);
125 }
126
127 #[cfg(windows)]
128 #[test]
129 fn test_simplified_components_windows_absolute_path() {
130 let path = Path::new(r"C:\foo\bar\baz.txt");
131 let components = simplified_components(path).unwrap();
132 assert_eq!(components.len(), 3);
133 assert_eq!(components[0], "foo");
134 assert_eq!(components[1], "bar");
135 assert_eq!(components[2], "baz.txt");
136 }
137
138 #[cfg(windows)]
139 #[test]
140 fn test_simplified_components_windows_unc_path() {
141 let path = Path::new(r"\\server\share\foo\bar.txt");
142 let components = simplified_components(path).unwrap();
143 assert_eq!(components.len(), 2);
144 assert_eq!(components[0], "foo");
145 assert_eq!(components[1], "bar.txt");
146 }
147 }