Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): allow local media uploads through CSP (#16301)

Tobias Wochinger411291a7385c
diff --git a/web/next.config.mjs b/web/next.config.mjs
index 929a2dce12..0785fa6f1e 100644
--- a/web/next.config.mjs
+++ b/web/next.config.mjs
@@ -14,7 +14,8 @@ import { renamedRouteRedirects } from "./redirects.mjs";
// Dataset attachments PUT media directly to presigned storage URLs, so
// connect-src must allow AWS S3, Azure Blob Storage, GCS, and the configured
// S3-compatible endpoint. The endpoint env var is only present at runtime in
-// official Docker images, so static wildcards cover the common providers too.
+// official Docker images, so static wildcards cover the common providers and
+// the local Docker Compose MinIO endpoint too.
const mediaUploadConnectSrc = (() => {
const endpoint = env.LANGFUSE_S3_MEDIA_UPLOAD_ENDPOINT;
if (!endpoint) return "";
@@ -26,6 +27,10 @@ const mediaUploadConnectSrc = (() => {
return "";
}
})();
+const localStorageConnectSrc =
+ env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION === undefined
+ ? "http://localhost:* "
+ : "";
const cspHeader = `
default-src 'self' https://*.langfuse.com https://*.langfuse.dev https://*.posthog.com https://*.sentry.io;
script-src 'self' 'unsafe-eval' 'unsafe-inline' https://*.langfuse.com https://*.langfuse.dev https://challenges.cloudflare.com https://*.sentry.io https://static.cloudflareinsights.com https://*.stripe.com https://login.microsoftonline.com https://login.microsoft.com https://*.microsoftonline.com;
@@ -38,7 +43,7 @@ const cspHeader = `
base-uri 'self';
form-action 'self' https://login.microsoftonline.com https://login.microsoft.com https://*.microsoftonline.com;
frame-ancestors 'none';
- connect-src 'self' ${mediaUploadConnectSrc}https://*.langfuse.com https://*.langfuse.dev https://*.ingest.us.sentry.io https://*.sentry.io https://chat.uk.plain.com https://*.amazonaws.com https://*.blob.core.windows.net https://storage.googleapis.com https://prod-uk-services-attachm-attachmentsuploadbucket2-1l2e4906o2asm.s3.eu-west-2.amazonaws.com https://login.microsoftonline.com https://login.microsoft.com https://*.microsoftonline.com https://graph.microsoft.com;
+ connect-src 'self' ${localStorageConnectSrc}${mediaUploadConnectSrc}https://*.langfuse.com https://*.langfuse.dev https://*.ingest.us.sentry.io https://*.sentry.io https://chat.uk.plain.com https://*.amazonaws.com https://*.blob.core.windows.net https://storage.googleapis.com https://prod-uk-services-attachm-attachmentsuploadbucket2-1l2e4906o2asm.s3.eu-west-2.amazonaws.com https://login.microsoftonline.com https://login.microsoft.com https://*.microsoftonline.com https://graph.microsoft.com;
media-src 'self' https: http://localhost:*;
${env.LANGFUSE_CSP_ENFORCE_HTTPS === "true" ? "upgrade-insecure-requests; block-all-mixed-content;" : ""}
${env.SENTRY_CSP_REPORT_URI ? `report-uri ${env.SENTRY_CSP_REPORT_URI}; report-to csp-endpoint;` : ""}
@@ -324,6 +329,6 @@ const sentryConfig = withSentryConfig(nextConfig, {
removeDebugLogging: true,
},
},
- });
+});
export default sentryConfig;
diff --git a/web/src/__tests__/server/unit/csp.servertest.ts b/web/src/__tests__/server/unit/csp.servertest.ts
new file mode 100644
index 0000000000..22bdc73185
--- /dev/null
+++ b/web/src/__tests__/server/unit/csp.servertest.ts
@@ -0,0 +1,36 @@
+import { execFileSync } from "node:child_process";
+import { describe, expect, it } from "vitest";
+
+const getCsp = (cloudRegion?: string) =>
+ execFileSync(
+ process.execPath,
+ [
+ "--input-type=module",
+ "--eval",
+ `
+ const config = (await import("./next.config.mjs")).default;
+ const headers = await config.headers();
+ console.log(headers.flatMap((rule) => rule.headers).find((header) => header.key === "Content-Security-Policy").value);
+ `,
+ ],
+ {
+ cwd: process.cwd(),
+ env: {
+ ...process.env,
+ DOCKER_BUILD: "1",
+ LANGFUSE_S3_MEDIA_UPLOAD_ENDPOINT: "",
+ NEXT_PUBLIC_LANGFUSE_CLOUD_REGION: cloudRegion,
+ },
+ encoding: "utf8",
+ },
+ );
+
+describe("Content Security Policy", () => {
+ it("allows the local MinIO endpoint in self-hosted Docker builds", () => {
+ expect(getCsp()).toContain("connect-src 'self' http://localhost:*");
+ });
+
+ it("does not allow local connections in Cloud", () => {
+ expect(getCsp("US")).not.toContain("connect-src 'self' http://localhost:*");
+ });
+});