Skip to content

Commit 01a89cb

Browse files
committed
kms: restrict clear_image_cache key to hex
1 parent 27ff829 commit 01a89cb

1 file changed

Lines changed: 8 additions & 37 deletions

File tree

kms/src/main_service.rs

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -149,53 +149,24 @@ impl RpcHandler {
149149
if sub_dir.is_empty() {
150150
return Ok(());
151151
}
152+
152153
if sub_dir == "all" {
153154
fs::remove_dir_all(parent_dir)?;
154-
155155
return Ok(());
156156
}
157157

158-
let sub_path = Path::new(sub_dir);
159-
if sub_path.is_absolute() {
160-
bail!("Invalid cache path");
158+
if !sub_dir.chars().all(|c| c.is_ascii_hexdigit()) {
159+
bail!("Invalid cache key");
161160
}
162161

163-
let mut cleaned = PathBuf::new();
164-
for component in sub_path.components() {
165-
use std::path::Component;
166-
167-
match component {
168-
Component::Normal(part) => cleaned.push(part),
169-
Component::CurDir => {}
170-
Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
171-
bail!("Invalid cache path");
172-
}
173-
}
174-
}
162+
let path = parent_dir.join(sub_dir);
175163

176-
if cleaned.as_os_str().is_empty() {
177-
// Only separators or current-dir components – nothing to do.
178-
return Ok(());
164+
if path.is_dir() {
165+
fs::remove_dir_all(path)?;
166+
} else if path.is_file() {
167+
fs::remove_file(path)?;
179168
}
180169

181-
let path = parent_dir.join(&cleaned);
182-
183-
let canonical_parent = parent_dir
184-
.canonicalize()
185-
.unwrap_or_else(|_| parent_dir.to_path_buf());
186-
let canonical = path
187-
.canonicalize()
188-
.unwrap_or_else(|_| canonical_parent.join(&cleaned));
189-
190-
if !canonical.starts_with(&canonical_parent) {
191-
bail!("Invalid cache path");
192-
}
193-
194-
if canonical.is_dir() {
195-
fs::remove_dir_all(canonical)?;
196-
} else {
197-
fs::remove_file(canonical)?;
198-
}
199170
Ok(())
200171
}
201172

0 commit comments

Comments
 (0)