Skip to content

Commit 5f86733

Browse files
authored
Merge pull request #601 from Dstack-TEE/fix-issue-558-kms-remove-cache
Fix path traversal in KMS remove_cache (#558)
2 parents 79c9531 + 01a89cb commit 5f86733

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

kms/src/main_service.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
use std::{path::PathBuf, sync::Arc};
5+
use std::{
6+
path::{Path, PathBuf},
7+
sync::Arc,
8+
};
69

710
use anyhow::{bail, Context, Result};
811
use dstack_kms_rpc::{
@@ -142,20 +145,28 @@ impl RpcHandler {
142145
self.state.config.image.cache_dir.join("images")
143146
}
144147

145-
fn remove_cache(&self, parent_dir: &PathBuf, sub_dir: &str) -> Result<()> {
148+
fn remove_cache(&self, parent_dir: &Path, sub_dir: &str) -> Result<()> {
146149
if sub_dir.is_empty() {
147150
return Ok(());
148151
}
152+
149153
if sub_dir == "all" {
150154
fs::remove_dir_all(parent_dir)?;
151-
} else {
152-
let path = parent_dir.join(sub_dir);
153-
if path.is_dir() {
154-
fs::remove_dir_all(path)?;
155-
} else {
156-
fs::remove_file(path)?;
157-
}
155+
return Ok(());
156+
}
157+
158+
if !sub_dir.chars().all(|c| c.is_ascii_hexdigit()) {
159+
bail!("Invalid cache key");
158160
}
161+
162+
let path = parent_dir.join(sub_dir);
163+
164+
if path.is_dir() {
165+
fs::remove_dir_all(path)?;
166+
} else if path.is_file() {
167+
fs::remove_file(path)?;
168+
}
169+
159170
Ok(())
160171
}
161172

0 commit comments

Comments
 (0)