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
18 changes: 13 additions & 5 deletions lib/utils/resolve-file-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ async function resolveHttpProxy(
httpTargetUrl: string,
relativePath: string,
): Promise<Response> {
// Ensure the URL doesn't have double slashes
const baseUrl = httpTargetUrl.endsWith("/")
? httpTargetUrl.slice(0, -1)
: httpTargetUrl
const targetUrl = `${baseUrl}/${relativePath}`
const targetUrl = buildHttpProxyUrl(httpTargetUrl, relativePath)

try {
const response = await fetch(targetUrl)
Expand Down Expand Up @@ -91,6 +87,18 @@ async function resolveHttpProxy(
}
}

export function buildHttpProxyUrl(
httpTargetUrl: string,
relativePath: string,
): URL {
const baseUrl = new URL(httpTargetUrl)
if (!baseUrl.pathname.endsWith("/")) {
baseUrl.pathname += "/"
}
baseUrl.pathname += relativePath
return baseUrl
}

function getContentType(fileName: string): string {
const ext = fileName.split(".").pop()?.toLowerCase()
const mimeTypes: Record<string, string> = {
Expand Down
34 changes: 34 additions & 0 deletions tests/routes/file-proxy03.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { test, expect } from "bun:test"
import { getTestServer } from "tests/fixtures/get-test-server"
import { Buffer } from "node:buffer"
import { buildHttpProxyUrl } from "lib/utils/resolve-file-proxy"

test("http proxy URL preserves special characters as path data", () => {
const targetUrl = buildHttpProxyUrl(
"https://example.com/files/static/source?token=abc",
"folder name/report?final#1.txt",
)

expect(targetUrl.toString()).toBe(
"https://example.com/files/static/source/folder%20name/report%3Ffinal%231.txt?token=abc",
)
})

test("http proxy file resolution", async () => {
const { axios, url } = await getTestServer()
Expand Down Expand Up @@ -78,6 +90,28 @@ test("http proxy with query param download", async () => {
expect(downloadRes.data).toBe("Query param HTTP proxy test")
})

test("http proxy downloads paths containing URL delimiters", async () => {
const { axios, url } = await getTestServer()

await axios.post("/files/upsert", {
file_path: "/special-source/report?final#1.txt",
text_content: "Special path HTTP proxy test",
})

await axios.post("/file_proxies/create", {
proxy_type: "http",
http_target_url: `${url}/files/static/special-source`,
matching_pattern: "http-special/*",
})

const downloadRes = await axios.get("/files/download", {
params: { file_path: "/http-special/report?final#1.txt" },
})

expect(downloadRes.status).toBe(200)
expect(downloadRes.data).toBe("Special path HTTP proxy test")
})

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

Expand Down
Loading