Repository navigation
security(file-share): sandbox filesystem access to home/upload dirs (#498) #508
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import type { IncomingMessage, ServerResponse } from "node:http" | ||
| import { randomUUID } from "node:crypto" | ||
| import fs from "node:fs" | ||
| import os from "node:os" | ||
| import path from "node:path" | ||
| import { json, requireAuth } from "../utils.ts" | ||
| import logger from "../../../utils/logger.ts" | ||
|
|
@@ -216,6 +217,27 @@ export async function handleFileUpload( | |
| } | ||
|
|
||
| /** GET /api/files/browse?path=<dirPath> — list host files and directories */ | ||
| /** | ||
| * Returns true when targetPath resolves inside the user's home directory or the | ||
| * configured upload directory. Used to sandbox filesystem browsing/downloads. | ||
| * On Windows, paths are compared case-insensitively to avoid bypasses. | ||
| */ | ||
| function isPathSafe(targetPath: string): boolean { | ||
| const resolved = path.resolve(targetPath) | ||
| const home = path.resolve(os.homedir()) | ||
| const upload = path.resolve(getUploadDir()) | ||
|
|
||
| const normalize = (p: string) => | ||
| process.platform === "win32" ? p.toLowerCase() : p | ||
|
|
||
| const r = normalize(resolved) | ||
| const within = (base: string) => { | ||
| const b = normalize(base) | ||
| return r === b || r.startsWith(b + path.sep) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve the separator when the allowed directory is a drive root. If Append a separator only when the base does not already end with one. Keep the equality check and sibling-prefix rejection.
|
||
| } | ||
| return within(home) || within(upload) | ||
| } | ||
|
|
||
| export async function handleFileBrowse( | ||
| req: IncomingMessage, | ||
| res: ServerResponse, | ||
|
|
@@ -228,6 +250,10 @@ export async function handleFileBrowse( | |
|
|
||
| let targetPath = reqPath ? path.resolve(reqPath) : defaultDir | ||
|
|
||
| if (!isPathSafe(targetPath)) { | ||
| targetPath = defaultDir | ||
| } | ||
|
|
||
| if (!fs.existsSync(targetPath)) { | ||
| targetPath = defaultDir | ||
| } | ||
|
|
@@ -313,6 +339,11 @@ export function handleFileDownload( | |
| if (filePathParam) { | ||
| const targetPath = path.resolve(filePathParam) | ||
|
|
||
| if (!isPathSafe(targetPath)) { | ||
| json(res, 403, { error: "Forbidden" }) | ||
| return | ||
| } | ||
|
|
||
| if (!fs.existsSync(targetPath) || !fs.statSync(targetPath).isFile()) { | ||
| json(res, 404, { error: "File not found" }) | ||
| return | ||
|
|
||
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.
Resolve symbolic links before checking containment.
If
/home/alice/externalpoints to/etc,/home/alice/external/passwdpasses this check. The download handler then streams a file outside both allowed roots. Browsing the linked directory also escapes the sandbox.path.resolve()performs lexical normalization; filesystem operations follow symbolic links. (nodejs.org)Canonicalize both roots and the target with
fs.realpath()before comparing containment. Use the verified destination for filesystem access. Preserve the browse fallback and download 403 for escaped targets. (nodejs.org)Based on learnings, “validate and canonicalize file paths before using them for file system access.”
Source: Learnings