diff --git a/lib/utils/normalize-path.ts b/lib/utils/normalize-path.ts index debc468..7b8c4af 100644 --- a/lib/utils/normalize-path.ts +++ b/lib/utils/normalize-path.ts @@ -1,6 +1,6 @@ export function normalizePath(path: string): string { if (!path || path === "/") return "" - let normalized = path.replace(/\\+/g, "/").replace(/\/\/+/, "/") + let normalized = path.replace(/\\+/g, "/").replace(/\/\/+/g, "/") if (normalized.startsWith("/")) { normalized = normalized.slice(1) } diff --git a/lib/utils/resolve-file-proxy.ts b/lib/utils/resolve-file-proxy.ts index 3145b4e..71109e9 100644 --- a/lib/utils/resolve-file-proxy.ts +++ b/lib/utils/resolve-file-proxy.ts @@ -1,6 +1,6 @@ import type { FileProxy } from "../db/schema" import { readFile } from "node:fs/promises" -import { join } from "node:path" +import { join, resolve } from "node:path" import { normalizePath } from "./normalize-path" export async function resolveFileProxy( @@ -15,6 +15,11 @@ export async function resolveFileProxy( const prefix = pattern.slice(0, -1) // Remove "*" to get "prefix/" const relativePath = normalizedPath.slice(prefix.length) + // Prevent path traversal outside proxy root + if (relativePath.split("/").includes("..")) { + return new Response("Forbidden: Path traversal detected", { status: 403 }) + } + if (proxy.proxy_type === "disk") { return resolveDiskProxy(proxy.disk_path, relativePath) } else { @@ -26,7 +31,15 @@ async function resolveDiskProxy( diskPath: string, relativePath: string, ): Promise { - const fullPath = join(diskPath, relativePath) + const resolvedDiskRoot = resolve(diskPath) + const fullPath = resolve(diskPath, relativePath) + + if ( + !fullPath.startsWith(resolvedDiskRoot + "/") && + fullPath !== resolvedDiskRoot + ) { + return new Response("Forbidden: Path traversal detected", { status: 403 }) + } try { const content = await readFile(fullPath) diff --git a/tests/routes/security-and-normalization.test.ts b/tests/routes/security-and-normalization.test.ts new file mode 100644 index 0000000..75ce7c2 --- /dev/null +++ b/tests/routes/security-and-normalization.test.ts @@ -0,0 +1,50 @@ +import { test, expect } from "bun:test" +import { normalizePath } from "lib/utils/normalize-path" +import { getTestServer } from "tests/fixtures/get-test-server" +import { mkdir, writeFile, rm } from "node:fs/promises" +import { join } from "node:path" + +test("normalizePath is idempotent across multiple repeated slash groups", () => { + expect(normalizePath("a///b///c.txt")).toBe("a/b/c.txt") + expect(normalizePath("a//b//c//d")).toBe("a/b/c/d") + expect(normalizePath("//a//b//c//")).toBe("a/b/c") + expect(normalizePath(normalizePath("a///b///c.txt"))).toBe( + normalizePath("a///b///c.txt"), + ) +}) + +test("disk proxy blocks path traversal attempts (CWE-22)", async () => { + const { axios } = await getTestServer() + + const testDir = "/tmp/test-file-server-proxy-traversal" + const secretFile = "/tmp/test-file-server-secret.txt" + + await mkdir(testDir, { recursive: true }) + await writeFile(join(testDir, "allowed.txt"), "allowed content") + await writeFile(secretFile, "secret content outside proxy") + + try { + await axios.post("/file_proxies/create", { + matching_pattern: "sandbox/*", + proxy_type: "disk", + disk_path: testDir, + }) + + // Allowed file should succeed + const allowedRes = await axios.get("/files/download/sandbox/allowed.txt") + expect(allowedRes.status).toBe(200) + expect(allowedRes.data).toBe("allowed content") + + // Path traversal via query param should be rejected with 403 Forbidden + expect( + axios.get("/files/download", { + params: { file_path: "sandbox/../test-file-server-secret.txt" }, + }), + ).rejects.toMatchObject({ + status: 403, + }) + } finally { + await rm(testDir, { recursive: true, force: true }) + await rm(secretFile, { force: true }) + } +})