Skip to content

Commit 6f10d71

Browse files
authored
Merge pull request #95 from ClayPulse/hotfix
Separate app route and workspace root
2 parents 5cfeea3 + 64f1d04 commit 6f10d71

File tree

1 file changed

+25
-24
lines changed
  • remote-workspace/src/servers/api-server/platform-api

1 file changed

+25
-24
lines changed

remote-workspace/src/servers/api-server/platform-api/handler.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@ 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(
8-
process.env.PLATFORM_API_ROOT ?? "/pulse-editor",
9-
);
107

11-
const settingsPath = path.join(SAFE_ROOT, "settings.json");
8+
const appRoot = "/pulse-editor";
129

13-
function safeResolve(uri: string): string {
10+
const workspaceRoot = "/workspace";
11+
12+
const settingsPath = path.join(appRoot, "settings.json");
13+
14+
function safeWorkspaceResolve(uri: string): string {
1415
if (!uri || typeof uri !== "string") {
1516
throw new Error("Invalid path");
1617
}
1718

18-
// Canonicalize the SAFE_ROOT once for this function
19-
const rootPath = path.resolve(SAFE_ROOT);
19+
// Canonicalize the workspaceRoot once for this function
20+
const rootPath = path.resolve(workspaceRoot);
2021
// Combine and normalize the user input relative to the safe root
21-
const candidate = path.resolve(SAFE_ROOT, uri);
22+
const candidate = path.resolve(uri);
2223

2324
// Check that candidate is strictly under rootPath (or equal to rootPath)
2425
const rel = path.relative(rootPath, candidate);
@@ -136,7 +137,7 @@ export async function handlePlatformAPIRequest(
136137

137138
// List all folders in a path
138139
async function handleListProjects(uri: string) {
139-
const rootPath = safeResolve(uri);
140+
const rootPath = safeWorkspaceResolve(uri);
140141
const files = await fs.promises.readdir(rootPath, { withFileTypes: true });
141142
const folders = files
142143
.filter((file) => file.isDirectory())
@@ -154,7 +155,7 @@ async function listPathContent(
154155
options: any,
155156
baseUri: string | undefined = undefined,
156157
) {
157-
const rootPath = safeResolve(uri);
158+
const rootPath = safeWorkspaceResolve(uri);
158159
const files = await fs.promises.readdir(rootPath, { withFileTypes: true });
159160

160161
const promise: Promise<any>[] = files
@@ -211,13 +212,13 @@ async function handleListPathContent(uri: string, options: any) {
211212

212213
async function handleCreateProject(uri: string) {
213214
// Create a folder at the validated path
214-
const safe = safeResolve(uri);
215+
const safe = safeWorkspaceResolve(uri);
215216
await fs.promises.mkdir(safe, { recursive: true });
216217
}
217218

218219
async function handleDeleteProject(uri: string) {
219220
// Delete the folder at the validated path
220-
const safe = safeResolve(uri);
221+
const safe = safeWorkspaceResolve(uri);
221222
await fs.promises.rm(safe, { recursive: true, force: true });
222223
}
223224

@@ -228,34 +229,34 @@ async function handleUpdateProject(
228229
ctime?: Date;
229230
},
230231
) {
231-
const safeOld = safeResolve(uri);
232+
const safeOld = safeWorkspaceResolve(uri);
232233
const newPathCandidate = path.join(path.dirname(safeOld), updatedInfo.name);
233-
const safeNew = safeResolve(newPathCandidate);
234+
const safeNew = safeWorkspaceResolve(newPathCandidate);
234235
await fs.promises.rename(safeOld, safeNew);
235236
}
236237

237238
async function handleCreateFolder(uri: string) {
238239
// Create a folder at the validated path
239-
const safe = safeResolve(uri);
240+
const safe = safeWorkspaceResolve(uri);
240241
await fs.promises.mkdir(safe, { recursive: true });
241242
}
242243

243244
async function handleCreateFile(uri: string) {
244245
// Create a file at the validated path
245-
const safe = safeResolve(uri);
246+
const safe = safeWorkspaceResolve(uri);
246247
// ensure parent exists
247248
await fs.promises.mkdir(path.dirname(safe), { recursive: true });
248249
await fs.promises.writeFile(safe, "");
249250
}
250251

251252
async function handleRename(oldUri: string, newUri: string) {
252-
const safeOld = safeResolve(oldUri);
253-
const safeNew = safeResolve(newUri);
253+
const safeOld = safeWorkspaceResolve(oldUri);
254+
const safeNew = safeWorkspaceResolve(newUri);
254255
await fs.promises.rename(safeOld, safeNew);
255256
}
256257

257258
async function handleDelete(uri: string) {
258-
const safe = safeResolve(uri);
259+
const safe = safeWorkspaceResolve(uri);
259260
await fs.promises.rm(safe, {
260261
recursive: true,
261262
force: true,
@@ -264,7 +265,7 @@ async function handleDelete(uri: string) {
264265

265266
async function handleHasPath(uri: string) {
266267
try {
267-
const safe = safeResolve(uri);
268+
const safe = safeWorkspaceResolve(uri);
268269
return fs.existsSync(safe);
269270
} catch (err) {
270271
return false;
@@ -273,14 +274,14 @@ async function handleHasPath(uri: string) {
273274

274275
async function handleReadFile(uri: string) {
275276
// Read the file at validated path
276-
const safe = safeResolve(uri);
277+
const safe = safeWorkspaceResolve(uri);
277278
const data = await fs.promises.readFile(safe, "utf-8");
278279
return data;
279280
}
280281

281282
async function handleWriteFile(data: any, uri: string) {
282283
// Write the data at validated path
283-
const safePath = safeResolve(uri);
284+
const safePath = safeWorkspaceResolve(uri);
284285
// create parent directory if it doesn't exist
285286
const dir = path.dirname(safePath);
286287
if (!fs.existsSync(dir)) {
@@ -292,8 +293,8 @@ async function handleWriteFile(data: any, uri: string) {
292293

293294
async function handleCopyFiles(from: string, to: string) {
294295
// Copy the files from the validated from path to the validated to path
295-
const safeFrom = safeResolve(from);
296-
const safeTo = safeResolve(to);
296+
const safeFrom = safeWorkspaceResolve(from);
297+
const safeTo = safeWorkspaceResolve(to);
297298
await fs.promises.cp(safeFrom, safeTo, { recursive: true });
298299
}
299300

0 commit comments

Comments
 (0)