44//! executables. Git continues to own Git semantics; this boundary supplies IO.
55
66use crate :: sessions:: { lookup, Connection } ;
7+ use lithe_platform:: {
8+ paths:: { invalid_relative_path, is_absolute_path, join_native, slash_separated} ,
9+ Platform ,
10+ } ;
711use lithe_protocol:: { CoreError , ErrorCode } ;
812use serde:: Deserialize ;
913use std:: cell:: RefCell ;
@@ -16,7 +20,7 @@ use std::sync::Arc;
1620pub struct Workspace {
1721 session_id : String ,
1822 local_root : PathBuf ,
19- remote_root : PathBuf ,
23+ remote_root : String ,
2024}
2125
2226/// The connected host a request's workspace lives on, and its path mapping.
@@ -38,16 +42,17 @@ impl Scope {
3842 /// with `..`, or when the session is not connected.
3943 pub fn begin ( workspace : Option < Workspace > ) -> Result < Self , CoreError > {
4044 let host = workspace
41- . map ( |workspace| {
42- if ! valid_root ( & workspace. local_root )
43- || workspace . local_root . file_name ( ) . is_none ( )
44- || ! valid_root ( & workspace. remote_root )
45- {
45+ . map ( |mut workspace| {
46+ let root = normalize_remote_path ( & workspace. remote_root ) ;
47+ let Some ( root ) = root . filter ( |_| {
48+ valid_root ( & workspace. local_root ) && workspace . local_root . file_name ( ) . is_some ( )
49+ } ) else {
4650 return Err ( CoreError :: new (
4751 ErrorCode :: InvalidRequest ,
4852 "Workspace roots must be absolute without parent components" ,
4953 ) ) ;
50- }
54+ } ;
55+ workspace. remote_root = root;
5156 Ok ( Host {
5257 connection : lookup ( & workspace. session_id ) ?,
5358 workspace,
@@ -70,6 +75,28 @@ fn valid_root(path: &Path) -> bool {
7075 . any ( |component| matches ! ( component, std:: path:: Component :: ParentDir ) )
7176}
7277
78+ fn normalize_remote_path ( path : & str ) -> Option < String > {
79+ // Shipped workspace servers run on Unix, even when the client is Windows.
80+ if !is_absolute_path ( path, Platform :: Unix ) || path. contains ( '\0' ) {
81+ return None ;
82+ }
83+ let mut normalized = String :: with_capacity ( path. len ( ) ) ;
84+ for part in path
85+ . split ( '/' )
86+ . filter ( |part| !part. is_empty ( ) && * part != "." )
87+ {
88+ if part == ".." {
89+ return None ;
90+ }
91+ normalized. push ( '/' ) ;
92+ normalized. push_str ( part) ;
93+ }
94+ if normalized. is_empty ( ) {
95+ normalized. push ( '/' ) ;
96+ }
97+ Some ( normalized)
98+ }
99+
73100impl Drop for Scope {
74101 fn drop ( & mut self ) {
75102 HOST . with ( |current| current. replace ( self . 0 . take ( ) ) ) ;
@@ -114,45 +141,66 @@ impl Host {
114141 /// Translates a placeholder path to the host path it stands for; paths
115142 /// outside the workspace stay as they are.
116143 pub fn remote_path ( & self , path : & Path ) -> PathBuf {
117- if let Ok ( suffix) = path. strip_prefix ( self . host_paths_root ( ) ) {
118- return Path :: new ( "/" ) . join ( suffix) ;
119- }
120- if path == self . workspace . local_root {
121- return self . workspace . remote_root . clone ( ) ;
122- }
123- path. strip_prefix ( & self . workspace . local_root )
124- . map ( |suffix| self . workspace . remote_root . join ( suffix) )
125- . unwrap_or_else ( |_| path. to_owned ( ) )
144+ self . workspace
145+ . project_to_remote ( path, Platform :: HOST )
146+ . unwrap_or_else ( || path. to_owned ( ) )
126147 }
127148
128149 pub ( crate ) fn local_path ( & self , path : & Path ) -> PathBuf {
129- if path == self . workspace . remote_root {
130- return self . workspace . local_root . clone ( ) ;
150+ path. to_str ( )
151+ . and_then ( |path| self . workspace . project_to_local ( path, Platform :: HOST ) )
152+ . unwrap_or_else ( || path. to_owned ( ) )
153+ }
154+
155+ pub ( crate ) fn identity ( & self ) -> & str {
156+ & self . workspace . session_id
157+ }
158+ }
159+
160+ impl Workspace {
161+ fn project_to_remote ( & self , path : & Path , client : Platform ) -> Option < PathBuf > {
162+ let ( root, suffix) = if let Ok ( suffix) = path. strip_prefix ( self . host_paths_root ( ) ) {
163+ ( "/" , suffix)
164+ } else {
165+ (
166+ self . remote_root . as_str ( ) ,
167+ path. strip_prefix ( & self . local_root ) . ok ( ) ?,
168+ )
169+ } ;
170+ let suffix = slash_separated ( suffix. to_str ( ) ?, client) ;
171+ Some ( if suffix. is_empty ( ) {
172+ PathBuf :: from ( root)
173+ } else {
174+ PathBuf :: from ( join_native ( root, & suffix, Platform :: Unix ) )
175+ } )
176+ }
177+
178+ fn project_to_local ( & self , path : & str , client : Platform ) -> Option < PathBuf > {
179+ let path = normalize_remote_path ( path) ?;
180+ if path == self . remote_root {
181+ return Some ( self . local_root . clone ( ) ) ;
131182 }
132- path. strip_prefix ( & self . workspace . remote_root )
133- . map ( |suffix| self . workspace . local_root . join ( suffix) )
134- . unwrap_or_else ( |_| {
135- path. strip_prefix ( "/" )
136- . map ( |suffix| self . host_paths_root ( ) . join ( suffix) )
137- . unwrap_or_else ( |_| path. to_owned ( ) )
138- } )
183+ let ( root, suffix) = if let Some ( suffix) = path
184+ . strip_prefix ( self . remote_root . trim_end_matches ( '/' ) )
185+ . and_then ( |suffix| suffix. strip_prefix ( '/' ) )
186+ {
187+ ( self . local_root . clone ( ) , suffix)
188+ } else {
189+ ( self . host_paths_root ( ) , path. trim_start_matches ( '/' ) )
190+ } ;
191+ if suffix. is_empty ( ) {
192+ return Some ( root) ;
193+ }
194+ // A name legal on Unix can be a separator, drive or stream on Windows.
195+ // Mapping it to a native path would change its identity.
196+ ( !invalid_relative_path ( suffix, client) ) . then ( || root. join ( suffix) )
139197 }
140198
141199 fn host_paths_root ( & self ) -> PathBuf {
142200 // A sibling namespace cannot collide with files in the opened folder.
143- // Keep this projection in sync with RemoteWorkspaceBinding on macOS.
144- let mut name = self
145- . workspace
146- . local_root
147- . file_name ( )
148- . unwrap ( )
149- . to_os_string ( ) ;
201+ let mut name = self . local_root . file_name ( ) . unwrap ( ) . to_os_string ( ) ;
150202 name. push ( ".lithe-remote-host" ) ;
151- self . workspace . local_root . with_file_name ( name)
152- }
153-
154- pub ( crate ) fn identity ( & self ) -> & str {
155- & self . workspace . session_id
203+ self . local_root . with_file_name ( name)
156204 }
157205}
158206
@@ -176,3 +224,87 @@ pub fn identity_key(path: &Path) -> PathBuf {
176224 . map ( |host| PathBuf :: from ( host. identity ( ) ) . join ( path. strip_prefix ( "/" ) . unwrap_or ( path) ) )
177225 . unwrap_or_else ( || path. to_owned ( ) )
178226}
227+
228+ #[ cfg( test) ]
229+ mod tests {
230+ use super :: * ;
231+
232+ fn workspace ( remote_root : & str ) -> Workspace {
233+ Workspace {
234+ session_id : "missing-test-session" . into ( ) ,
235+ local_root : std:: env:: temp_dir ( ) . join ( "remote-workspace" ) ,
236+ remote_root : remote_root. into ( ) ,
237+ }
238+ }
239+
240+ #[ test]
241+ fn remote_root_validation_precedes_connection_lookup ( ) {
242+ for ( path, expected) in [
243+ ( "/srv/repo" , ErrorCode :: ConnectionClosed ) ,
244+ ( "/" , ErrorCode :: ConnectionClosed ) ,
245+ ( "/srv/./repo//" , ErrorCode :: ConnectionClosed ) ,
246+ ( "srv/repo" , ErrorCode :: InvalidRequest ) ,
247+ ( r"C:\srv\repo" , ErrorCode :: InvalidRequest ) ,
248+ ( "/srv/../repo" , ErrorCode :: InvalidRequest ) ,
249+ ( "/srv/\0 repo" , ErrorCode :: InvalidRequest ) ,
250+ ] {
251+ let error = match Scope :: begin ( Some ( workspace ( path) ) ) {
252+ Err ( error) => error,
253+ Ok ( _) => panic ! ( "unexpected session for {path:?}" ) ,
254+ } ;
255+ assert_eq ! (
256+ std:: mem:: discriminant( & error. code) ,
257+ std:: mem:: discriminant( & expected) ,
258+ "{path:?}: got {:?}" ,
259+ error. code
260+ ) ;
261+ }
262+ }
263+
264+ #[ test]
265+ fn workspace_and_host_paths_keep_their_namespace ( ) {
266+ let workspace = workspace ( "/srv/repo" ) ;
267+ for client in Platform :: ALL {
268+ let relative = Path :: new ( "src" ) . join ( "main.rs" ) ;
269+ let local = workspace. local_root . join ( relative) ;
270+ let remote = workspace. project_to_remote ( & local, client) . unwrap ( ) ;
271+ assert_eq ! ( remote. to_str( ) , Some ( "/srv/repo/src/main.rs" ) ) ;
272+ assert_eq ! (
273+ workspace. project_to_local( "/srv/repo/./src//main.rs" , client) ,
274+ Some ( local)
275+ ) ;
276+
277+ let external = workspace. host_paths_root ( ) . join ( "srv/repository/lib.rs" ) ;
278+ assert_eq ! (
279+ workspace. project_to_local( "/srv/repository/lib.rs" , client) ,
280+ Some ( external. clone( ) )
281+ ) ;
282+ assert_eq ! (
283+ workspace
284+ . project_to_remote( & external, client)
285+ . unwrap( )
286+ . to_str( ) ,
287+ Some ( "/srv/repository/lib.rs" )
288+ ) ;
289+ assert_eq ! (
290+ workspace. project_to_local( "/" , client) ,
291+ Some ( workspace. host_paths_root( ) )
292+ ) ;
293+ }
294+ }
295+
296+ #[ test]
297+ fn unix_file_names_cannot_become_windows_separators_or_drives ( ) {
298+ let workspace = workspace ( "/" ) ;
299+ for name in [ r"dir\leaf" , "D:leaf" ] {
300+ let remote = format ! ( "/{name}" ) ;
301+ assert_eq ! (
302+ workspace. project_to_local( & remote, Platform :: Unix ) ,
303+ Some ( workspace. local_root. join( name) )
304+ ) ;
305+ assert ! ( workspace
306+ . project_to_local( & remote, Platform :: Windows )
307+ . is_none( ) ) ;
308+ }
309+ }
310+ }
0 commit comments