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
10 changes: 8 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 { relative, resolve, sep } from "node:path"
import { normalizePath } from "./normalize-path"

export async function resolveFileProxy(
Expand All @@ -26,7 +26,13 @@ async function resolveDiskProxy(
diskPath: string,
relativePath: string,
): Promise<Response> {
const fullPath = join(diskPath, relativePath)
const rootPath = resolve(diskPath)
const fullPath = resolve(rootPath, relativePath)
const pathFromRoot = relative(rootPath, fullPath)

if (pathFromRoot === ".." || pathFromRoot.startsWith(`..${sep}`)) {
return new Response("File not found", { status: 404 })
}

try {
const content = await readFile(fullPath)
Expand Down
36 changes: 36 additions & 0 deletions tests/routes/file-proxy02.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,42 @@ test("disk proxy with query param download", async () => {
}
})

test("disk proxy blocks parent directory traversal", async () => {
const { axios } = await getTestServer()

const tempDir = await mkdtemp(join(tmpdir(), "file-proxy-traversal-"))
const proxyRoot = join(tempDir, "root")

try {
await mkdir(proxyRoot)
await writeFile(join(proxyRoot, "safe.txt"), "Safe proxy content")
await writeFile(join(tempDir, "secret.txt"), "Secret sibling content")

await axios.post("/file_proxies/create", {
proxy_type: "disk",
disk_path: proxyRoot,
matching_pattern: "traversal/*",
})

const safeRes = await axios.get("/files/download", {
params: { file_path: "/traversal/safe.txt" },
})
expect(safeRes.status).toBe(200)
expect(safeRes.data).toBe("Safe proxy content")

await expect(
axios.get("/files/download", {
params: { file_path: "/traversal/../secret.txt" },
}),
).rejects.toMatchObject({
status: 404,
data: "File not found",
})
} finally {
await rm(tempDir, { recursive: true, force: true })
}
})

test("disk proxy binary file", async () => {
const { axios } = await getTestServer()

Expand Down
Loading