-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(files): add workspace file browser with path sandbox #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0r0b0r011
wants to merge
2
commits into
fathah:main
Choose a base branch
from
0r0b0r011:split/files-browser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| import { ipcMain } from "electron"; | ||
| import { | ||
| existsSync, | ||
| lstatSync, | ||
| readFileSync, | ||
| realpathSync, | ||
| readdirSync, | ||
| statSync, | ||
| } from "fs"; | ||
| import { dirname, isAbsolute, join, relative, resolve } from "path"; | ||
| import { HERMES_HOME } from "./installer"; | ||
| import { safeWriteFile } from "./utils"; | ||
| import { isRemoteOnlyMode } from "./hermes"; | ||
|
|
||
| const ROOT_FILE = join(HERMES_HOME, "desktop", "files-workspace-root.txt"); | ||
| const MAX_READ_BYTES = 1024 * 1024; | ||
| const MAX_WRITE_BYTES = 1024 * 1024; | ||
|
|
||
| interface FileEntry { | ||
| name: string; | ||
| isDir: boolean; | ||
| path: string; | ||
| error?: string; | ||
| } | ||
|
|
||
| type FilesFailure = { | ||
| success: false; | ||
| error: string; | ||
| unsupportedMode?: boolean; | ||
| }; | ||
|
|
||
| type FilesSuccess<T = void> = { | ||
| success: true; | ||
| data?: T; | ||
| }; | ||
|
|
||
| type FilesResult<T = void> = FilesSuccess<T> | FilesFailure; | ||
|
|
||
| function isFilesFailure<T>( | ||
| result: { realRoot: string; realTarget: string } | { realRoot: string; target: string } | FilesResult<T>, | ||
| ): result is FilesResult<T> { | ||
| return "success" in result && result.success === false; | ||
| } | ||
|
|
||
| interface ListResult { | ||
| root: string | null; | ||
| cwd: string | null; | ||
| entries: FileEntry[]; | ||
| } | ||
|
|
||
| function unsupported(): FilesFailure { | ||
| return { | ||
| success: false, | ||
| unsupportedMode: true, | ||
| error: "Files is only available in local or SSH tunnel modes.", | ||
| }; | ||
| } | ||
|
|
||
| function fail(error: string): FilesFailure { | ||
| return { success: false, error }; | ||
| } | ||
|
|
||
| function readWorkspaceRoot(): string | null { | ||
| try { | ||
| if (!existsSync(ROOT_FILE)) return null; | ||
| const raw = readFileSync(ROOT_FILE, "utf-8").trim(); | ||
| if (!raw) return null; | ||
| return realpathSync(raw); | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function writeWorkspaceRoot(root: string): void { | ||
| safeWriteFile(ROOT_FILE, root); | ||
| } | ||
|
|
||
| export function isPathUnderRoot(realTarget: string, realRoot: string): boolean { | ||
| const rel = relative(realRoot, realTarget); | ||
| return rel === "" || (!!rel && !rel.startsWith("..") && !isAbsolute(rel)); | ||
| } | ||
|
|
||
| function realpathParentOrThrow(parent: string): string { | ||
| try { | ||
| if (!existsSync(parent)) throw new Error("Path not allowed"); | ||
| return realpathSync(parent); | ||
| } catch (err) { | ||
| if ((err as NodeJS.ErrnoException).code === "ENOENT") { | ||
| throw new Error("Path not allowed"); | ||
| } | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| /** Resolve a path and ensure it (or its parent for new files) stays under workspace roots. */ | ||
| export function assertPathAllowed(target: string, workspaceRoots: string[]): string { | ||
| if (workspaceRoots.length === 0) { | ||
| throw new Error("Path not allowed"); | ||
| } | ||
|
|
||
| let resolved: string; | ||
| try { | ||
| resolved = existsSync(target) ? realpathSync(target) : resolve(target); | ||
| } catch { | ||
| throw new Error("Path not allowed"); | ||
| } | ||
|
|
||
| const parentReal = realpathParentOrThrow(dirname(resolved)); | ||
| const allowed = workspaceRoots.some((root) => { | ||
| try { | ||
| const realRoot = realpathSync(root); | ||
| if (isPathUnderRoot(parentReal, realRoot)) return true; | ||
| if (existsSync(resolved)) { | ||
| return isPathUnderRoot(realpathSync(target), realRoot); | ||
| } | ||
| return false; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }); | ||
|
|
||
| if (!allowed) throw new Error("Path not allowed"); | ||
| return resolved; | ||
| } | ||
|
|
||
| function validateRoot(): string | FilesFailure { | ||
| const root = readWorkspaceRoot(); | ||
| if (!root) return fail("Choose a workspace folder first."); | ||
| return root; | ||
| } | ||
|
|
||
| function validateExistingTarget( | ||
| path: unknown, | ||
| ): { realRoot: string; realTarget: string } | FilesFailure { | ||
| if (typeof path !== "string") return fail("Invalid path."); | ||
| const realRoot = validateRoot(); | ||
| if (typeof realRoot !== "string") return realRoot; | ||
| try { | ||
| const realTarget = assertPathAllowed(path || realRoot, [realRoot]); | ||
| return { realRoot, realTarget }; | ||
| } catch (err) { | ||
| if (err instanceof Error && err.message === "Path not allowed") { | ||
| return fail("Path is outside the workspace."); | ||
| } | ||
| return fail(err instanceof Error ? err.message : String(err)); | ||
| } | ||
| } | ||
|
|
||
| function validateWriteTarget(path: unknown): { realRoot: string; target: string } | FilesFailure { | ||
| if (typeof path !== "string" || !path.trim()) return fail("Invalid path."); | ||
| const realRoot = validateRoot(); | ||
| if (typeof realRoot !== "string") return realRoot; | ||
| const target = resolve(path); | ||
| try { | ||
| assertPathAllowed(target, [realRoot]); | ||
| if (existsSync(target)) { | ||
| const realTarget = realpathSync(target); | ||
| if (statSync(realTarget).isDirectory()) return fail("Cannot write to a directory."); | ||
| } | ||
| return { realRoot, target }; | ||
| } catch (err) { | ||
| if (err instanceof Error && err.message === "Path not allowed") { | ||
| return fail("Write target is outside the workspace."); | ||
| } | ||
| return fail(err instanceof Error ? err.message : String(err)); | ||
| } | ||
| } | ||
|
|
||
| function listEntry(parent: string, name: string, realRoot: string): FileEntry { | ||
| const path = join(parent, name); | ||
| try { | ||
| const lst = lstatSync(path); | ||
| if (lst.isSymbolicLink()) { | ||
| const real = realpathSync(path); | ||
| if (!isPathUnderRoot(real, realRoot)) { | ||
| return { name, isDir: false, path, error: "Outside workspace" }; | ||
| } | ||
| return { name, isDir: statSync(real).isDirectory(), path }; | ||
| } | ||
| return { name, isDir: lst.isDirectory(), path }; | ||
| } catch (err) { | ||
| return { | ||
| name, | ||
| isDir: false, | ||
| path, | ||
| error: err instanceof Error ? err.message : String(err), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| function containsBinaryBytes(buffer: Buffer): boolean { | ||
| return buffer.includes(0); | ||
| } | ||
|
|
||
| export function registerFilesHandlers(): void { | ||
| ipcMain.handle("files-get-workspace-root", (): FilesResult<{ root: string | null }> => { | ||
| if (isRemoteOnlyMode()) return unsupported(); | ||
| return { success: true, data: { root: readWorkspaceRoot() } }; | ||
| }); | ||
|
|
||
| ipcMain.handle("files-set-workspace-root", (_event, dir: string): FilesResult<{ root: string }> => { | ||
| if (isRemoteOnlyMode()) return unsupported(); | ||
| if (typeof dir !== "string" || !dir.trim()) return fail("Invalid workspace folder."); | ||
| try { | ||
| const realRoot = realpathSync(dir); | ||
| if (!statSync(realRoot).isDirectory()) return fail("Workspace root must be a directory."); | ||
| writeWorkspaceRoot(realRoot); | ||
| return { success: true, data: { root: realRoot } }; | ||
| } catch (err) { | ||
| return fail(err instanceof Error ? err.message : String(err)); | ||
| } | ||
| }); | ||
|
|
||
| ipcMain.handle("files-list-dir", (_event, dir: string): FilesResult<ListResult> => { | ||
| if (isRemoteOnlyMode()) return unsupported(); | ||
| const target = validateExistingTarget(dir); | ||
| if (isFilesFailure(target)) return target; | ||
| if (!statSync(target.realTarget).isDirectory()) return fail("Path is not a directory."); | ||
|
|
||
| const entries = readdirSync(target.realTarget) | ||
| .map((name) => listEntry(target.realTarget, name, target.realRoot)) | ||
| .sort((a, b) => { | ||
| if (a.isDir !== b.isDir) return a.isDir ? -1 : 1; | ||
| return a.name.localeCompare(b.name); | ||
| }); | ||
|
|
||
| return { | ||
| success: true, | ||
| data: { root: target.realRoot, cwd: target.realTarget, entries }, | ||
| }; | ||
| }); | ||
|
|
||
| ipcMain.handle("files-read", (_event, path: string): FilesResult<{ text: string }> => { | ||
| if (isRemoteOnlyMode()) return unsupported(); | ||
| const target = validateExistingTarget(path); | ||
| if (isFilesFailure(target)) return target; | ||
| const st = statSync(target.realTarget); | ||
| if (!st.isFile()) return fail("Path is not a file."); | ||
| if (st.size > MAX_READ_BYTES) return fail("File is too large to open."); | ||
| const raw = readFileSync(target.realTarget); | ||
| if (containsBinaryBytes(raw)) return fail("Binary files are not supported."); | ||
| return { success: true, data: { text: raw.toString("utf-8") } }; | ||
| }); | ||
|
|
||
| ipcMain.handle("files-write", (_event, path: string, content: string): FilesResult => { | ||
| if (isRemoteOnlyMode()) return unsupported(); | ||
| if (typeof content !== "string") return fail("File content must be text."); | ||
| if (Buffer.byteLength(content, "utf-8") > MAX_WRITE_BYTES) { | ||
| return fail("File is too large to save."); | ||
| } | ||
| const target = validateWriteTarget(path); | ||
| if (isFilesFailure(target)) return target; | ||
| safeWriteFile(target.target, content); | ||
| return { success: true }; | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
files-set-workspace-rootaccepts any valid directory path — including/,/etc,/home/other-user, etc. — and stores it verbatim. The PR description explicitly claims "Onlyhomedir()andprocess.cwd()roots —/etc,/tmpoutside roots are rejected," but no such check exists in the code. Because this IPC channel is exposed to the renderer via the preload bridge, any renderer-side code (e.g., via XSS or a compromised dependency) can callfilesSetWorkspaceRoot('/')and immediately usefilesRead/filesListDirto exfiltrate any file the Electron process can access. The downstream per-operation sandbox (assertPathAllowed) is only as strong as the workspace root that was last set — and that value is now unconstrained.