@@ -4,7 +4,9 @@ import path from "path";
44
55// Define a safe root directory for projects. Can be overridden by env or configured as needed.
66// All incoming URIs will be resolved and validated to ensure they don't escape this root.
7- const SAFE_ROOT = path . resolve ( process . env . PLATFORM_API_ROOT ?? "/pulse-editor" ) ;
7+ const SAFE_ROOT = path . resolve (
8+ process . env . PLATFORM_API_ROOT ?? "/pulse-editor" ,
9+ ) ;
810
911const settingsPath = path . join ( SAFE_ROOT , "settings.json" ) ;
1012
@@ -13,15 +15,14 @@ function safeResolve(uri: string): string {
1315 throw new Error ( "Invalid path" ) ;
1416 }
1517
16- // Resolve the input and the safe root to absolute, normalized paths.
17- const resolved = path . resolve ( uri ) ;
18- const root = SAFE_ROOT ;
18+ // Canonicalize the SAFE_ROOT once for this function
19+ const rootPath = path . resolve ( SAFE_ROOT ) ;
20+ // Combine and normalize the user input relative to the safe root
21+ const candidate = path . resolve ( SAFE_ROOT , uri ) ;
1922
20- const relative = path . relative ( root , resolved ) ;
21-
22- // If the relative path starts with '..' or is absolute, it escapes the SAFE_ROOT.
23- if ( relative === "" || ( ! relative . startsWith ( ".." ) && ! path . isAbsolute ( relative ) ) ) {
24- return resolved ;
23+ // Check that candidate is strictly under rootPath (or equal to rootPath)
24+ if ( candidate === rootPath || candidate . startsWith ( rootPath + path . sep ) ) {
25+ return candidate ;
2526 }
2627
2728 throw new Error ( "Can only access paths within the project home directory." ) ;
@@ -131,7 +132,6 @@ export async function handlePlatformAPIRequest(
131132 }
132133}
133134
134-
135135// List all folders in a path
136136async function handleListProjects ( uri : string ) {
137137 const rootPath = safeResolve ( uri ) ;
0 commit comments