Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/utils/normalize-path.ts
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
17 changes: 15 additions & 2 deletions lib/utils/resolve-file-proxy.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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 {
Expand All @@ -26,7 +31,15 @@ async function resolveDiskProxy(
diskPath: string,
relativePath: string,
): Promise<Response> {
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)
Expand Down
50 changes: 50 additions & 0 deletions tests/routes/security-and-normalization.test.ts
Original file line number Diff line number Diff line change
@@ -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 })
}
})
Loading