@@ -90,21 +90,55 @@ pub const STORE_DIRECTORY: &str = "tgrep-managed-v2";
9090pub const MAX_REQUEST_BYTES : usize = 1024 * 1024 ;
9191pub const MAX_RESPONSE_BYTES : usize = 64 * 1024 * 1024 ;
9292
93+ /// Refuses ordinary index access to managed storage. Besides managed generation
94+ /// files, only a store directory itself and paths below one of its repository
95+ /// namespaces are reserved, so an unrelated directory that merely shares the
96+ /// store name stays usable. The absolute path is checked lexically after
97+ /// `.`/`..` normalization, without following links.
9398pub ( crate ) fn reject_unguarded ( path : & std:: path:: Path ) -> crate :: Result < ( ) > {
94- if path. join ( "paths.tgm" ) . try_exists ( ) ?
95- || path. ancestors ( ) . any ( |ancestor| {
96- ancestor
97- . file_name ( )
98- . is_some_and ( |name| name == STORE_DIRECTORY )
99- } )
100- {
99+ if reserved_store_path ( path) ? || path. join ( "paths.tgm" ) . try_exists ( ) ? {
101100 return Err (
102101 Error :: incompatible ( "managed storage requires a protected managed reader" ) . into ( ) ,
103102 ) ;
104103 }
105104 Ok ( ( ) )
106105}
107106
107+ fn reserved_store_path ( path : & std:: path:: Path ) -> std:: io:: Result < bool > {
108+ use std:: path:: Component ;
109+ // `Path::join` treats an empty index directory as the current directory.
110+ let path = if path. as_os_str ( ) . is_empty ( ) {
111+ std:: path:: Path :: new ( "." )
112+ } else {
113+ path
114+ } ;
115+ let absolute = std:: path:: absolute ( path) ?;
116+ let mut names = Vec :: new ( ) ;
117+ for component in absolute. components ( ) {
118+ match component {
119+ Component :: Normal ( name) => names. push ( name) ,
120+ Component :: ParentDir => {
121+ names. pop ( ) ;
122+ }
123+ Component :: Prefix ( _) | Component :: RootDir | Component :: CurDir => { }
124+ }
125+ }
126+ // Case-insensitive volumes resolve either spelling to the same directory.
127+ let store = |name : & std:: ffi:: OsStr | {
128+ name. to_str ( )
129+ . is_some_and ( |name| name. eq_ignore_ascii_case ( STORE_DIRECTORY ) )
130+ } ;
131+ let namespace = |name : & std:: ffi:: OsStr | {
132+ name. to_str ( ) . is_some_and ( |name| {
133+ name. len ( ) == 64 && name. bytes ( ) . all ( |byte| byte. is_ascii_hexdigit ( ) )
134+ } )
135+ } ;
136+ Ok ( names. last ( ) . is_some_and ( |name| store ( name) )
137+ || names
138+ . windows ( 2 )
139+ . any ( |pair| store ( pair[ 0 ] ) && namespace ( pair[ 1 ] ) ) )
140+ }
141+
108142/// A physical incarnation, never a logical generation key or a filesystem path.
109143#[ derive( Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Hash , Serialize ) ]
110144#[ serde( transparent) ]
@@ -302,4 +336,38 @@ mod tests {
302336 assert_eq ! ( json[ "status" ] , "unavailable" ) ;
303337 assert ! ( json. get( "value" ) . is_none( ) ) ;
304338 }
339+
340+ #[ test]
341+ fn only_store_and_namespace_paths_are_reserved_from_unguarded_access ( ) {
342+ use std:: path:: PathBuf ;
343+ let namespace = "0123456789abcdef" . repeat ( 4 ) ;
344+ let base = std:: env:: temp_dir ( ) . join ( "parent" ) ;
345+ let store = base. join ( STORE_DIRECTORY ) ;
346+ let relative = PathBuf :: from ( STORE_DIRECTORY ) ;
347+ for ( path, reserved) in [
348+ ( store. clone ( ) , true ) ,
349+ ( store. join ( & namespace) , true ) ,
350+ (
351+ store. join ( & namespace) . join ( "objects" ) . join ( "preparing" ) ,
352+ true ,
353+ ) ,
354+ (
355+ base. join ( STORE_DIRECTORY . to_ascii_uppercase ( ) )
356+ . join ( namespace. to_ascii_uppercase ( ) ) ,
357+ true ,
358+ ) ,
359+ ( store. join ( "project" ) . join ( ".." ) . join ( & namespace) , true ) ,
360+ ( relative. join ( & namespace) . join ( "objects" ) , true ) ,
361+ ( store. join ( "project" ) , false ) ,
362+ ( store. join ( "project" ) . join ( ".tgrep" ) , false ) ,
363+ ( store. join ( "project" ) . join ( & namespace) , false ) ,
364+ ( store. join ( & namespace) . join ( ".." ) . join ( "project" ) , false ) ,
365+ ( store. join ( & namespace[ ..63 ] ) , false ) ,
366+ ( base. join ( "other" ) . join ( & namespace) , false ) ,
367+ ( relative. join ( "project" ) . join ( ".tgrep" ) , false ) ,
368+ ( PathBuf :: new ( ) , false ) ,
369+ ] {
370+ assert_eq ! ( reserved_store_path( & path) . unwrap( ) , reserved, "{path:?}" ) ;
371+ }
372+ }
305373}
0 commit comments