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

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

if (
pathFromRoot === ".." ||
pathFromRoot.startsWith(`..${sep}`) ||
isAbsolute(pathFromRoot)
) {
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 paths outside proxy root", async () => {
const { axios } = await getTestServer()

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

try {
await mkdir(proxyRoot)
await writeFile(join(proxyRoot, "allowed.txt"), "Allowed content")
await writeFile(join(tempDir, "secret.txt"), "Secret content")

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

const allowedRes = await axios.get("/files/download", {
params: { file_path: "/safe-disk/allowed.txt" },
})
expect(allowedRes.status).toBe(200)
expect(allowedRes.data).toBe("Allowed content")

await expect(
axios.get("/files/download", {
params: { file_path: "/safe-disk/../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