1use std::path::{Component, Path, PathBuf};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum ArchiveFormat {
13 Zip,
14 Tar,
15 TarGz,
16}
17
18impl ArchiveFormat {
19 pub const fn label(self) -> &'static str {
21 match self {
22 Self::Zip => "zip",
23 Self::Tar => "tar",
24 Self::TarGz => "tar.gz",
25 }
26 }
27}
28
29pub fn detect_format(path: &Path) -> Option<ArchiveFormat> {
36 let name = path.file_name()?.to_string_lossy().to_lowercase();
37
38 if name.ends_with(".tar.gz") || name.ends_with(".tgz") {
40 Some(ArchiveFormat::TarGz)
41 } else if name.ends_with(".tar") {
42 Some(ArchiveFormat::Tar)
43 } else if name.ends_with(".zip") {
44 Some(ArchiveFormat::Zip)
45 } else {
46 None
47 }
48}
49
50#[derive(Debug, Clone, PartialEq, Eq)]
52pub enum EntryTarget {
53 Within(PathBuf),
55 ArchiveRoot,
59 Escaping,
61}
62
63pub fn classify_entry_path(raw: &Path) -> EntryTarget {
71 let mut safe = PathBuf::new();
72
73 for component in raw.components() {
74 match component {
75 Component::Normal(part) => safe.push(part),
76 Component::CurDir => {}
78 Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
79 return EntryTarget::Escaping
80 }
81 }
82 }
83
84 if safe.as_os_str().is_empty() {
85 return EntryTarget::ArchiveRoot;
86 }
87 EntryTarget::Within(safe)
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 #[test]
95 fn detects_each_supported_format() {
96 assert_eq!(
97 detect_format(Path::new("/a/x.zip")),
98 Some(ArchiveFormat::Zip)
99 );
100 assert_eq!(
101 detect_format(Path::new("/a/x.tar")),
102 Some(ArchiveFormat::Tar)
103 );
104 assert_eq!(
105 detect_format(Path::new("/a/x.tar.gz")),
106 Some(ArchiveFormat::TarGz)
107 );
108 assert_eq!(
109 detect_format(Path::new("/a/x.tgz")),
110 Some(ArchiveFormat::TarGz)
111 );
112 }
113
114 #[test]
115 fn tar_gz_wins_over_tar() {
116 assert_eq!(
119 detect_format(Path::new("bundle.tar.gz")),
120 Some(ArchiveFormat::TarGz)
121 );
122 }
123
124 #[test]
125 fn detection_ignores_case() {
126 assert_eq!(detect_format(Path::new("X.ZIP")), Some(ArchiveFormat::Zip));
127 assert_eq!(
128 detect_format(Path::new("X.Tar.GZ")),
129 Some(ArchiveFormat::TarGz)
130 );
131 }
132
133 #[test]
134 fn unsupported_names_are_not_archives() {
135 assert_eq!(detect_format(Path::new("notes.txt")), None);
136 assert_eq!(detect_format(Path::new("archive.rar")), None);
137 assert_eq!(detect_format(Path::new("blob.gz")), None);
139 assert_eq!(detect_format(Path::new("noextension")), None);
140 }
141
142 #[test]
143 fn ordinary_entry_paths_are_kept_relative() {
144 assert_eq!(
145 classify_entry_path(Path::new("src/main.rs")),
146 EntryTarget::Within(PathBuf::from("src/main.rs"))
147 );
148 assert_eq!(
149 classify_entry_path(Path::new("./src/./main.rs")),
150 EntryTarget::Within(PathBuf::from("src/main.rs"))
151 );
152 }
153
154 #[test]
155 fn the_archive_root_entry_is_not_an_escape() {
156 assert_eq!(
159 classify_entry_path(Path::new("./")),
160 EntryTarget::ArchiveRoot
161 );
162 assert_eq!(
163 classify_entry_path(Path::new(".")),
164 EntryTarget::ArchiveRoot
165 );
166 assert_eq!(classify_entry_path(Path::new("")), EntryTarget::ArchiveRoot);
167 }
168
169 #[test]
170 fn entries_escaping_upwards_are_refused() {
171 assert_eq!(
172 classify_entry_path(Path::new("../evil")),
173 EntryTarget::Escaping
174 );
175 assert_eq!(
176 classify_entry_path(Path::new("a/../../evil")),
177 EntryTarget::Escaping
178 );
179 assert_eq!(
180 classify_entry_path(Path::new("a/b/../c")),
181 EntryTarget::Escaping
182 );
183 }
184
185 #[test]
186 fn absolute_entries_are_refused() {
187 assert_eq!(
188 classify_entry_path(Path::new("/etc/passwd")),
189 EntryTarget::Escaping
190 );
191 assert_eq!(classify_entry_path(Path::new("/")), EntryTarget::Escaping);
192 }
193}