File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
710use anyhow:: { bail, Context , Result } ;
811use 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
You can’t perform that action at this time.
0 commit comments