Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Merge commit from fork

Nicholas Tindlee38c0a9431fe
diff --git a/autogpt_platform/backend/backend/api/features/chat/share.py b/autogpt_platform/backend/backend/api/features/chat/share.py
index fdac2c5256..6337a7e624 100644
--- a/autogpt_platform/backend/backend/api/features/chat/share.py
+++ b/autogpt_platform/backend/backend/api/features/chat/share.py
@@ -236,4 +236,4 @@ async def download_shared_chat_file(
file = await get_workspace_file_by_id(file_id)
if not file:
raise HTTPException(status_code=404, detail="Not found")
- return await create_file_download_response(file, inline=True)
+ return await create_file_download_response(file)
diff --git a/autogpt_platform/backend/backend/api/features/chat/share_test.py b/autogpt_platform/backend/backend/api/features/chat/share_test.py
index 231fc0fa10..551b0ea794 100644
--- a/autogpt_platform/backend/backend/api/features/chat/share_test.py
+++ b/autogpt_platform/backend/backend/api/features/chat/share_test.py
@@ -47,7 +47,12 @@ def _make_workspace_file(**overrides) -> WorkspaceFile:
def _mock_download_response():
async def _handler(file, *, inline=False):
- return Response(content=b"\x89PNG", media_type="image/png")
+ disposition = "inline" if inline else "attachment"
+ return Response(
+ content=b"\x89PNG",
+ media_type="image/png",
+ headers={"Content-Disposition": f'{disposition}; filename="image.png"'},
+ )
return _handler
@@ -304,7 +309,7 @@ class TestDownloadSharedChatFile:
)
assert response.status_code == 404
- def test_valid_token_returns_inline(self, client):
+ def test_valid_token_returns_attachment(self, client):
with (
patch(
"backend.api.features.chat.share.share_db.get_shared_chat_file",
@@ -326,3 +331,4 @@ class TestDownloadSharedChatFile:
)
assert response.status_code == 200
assert response.content == b"\x89PNG"
+ assert "attachment" in response.headers["Content-Disposition"]
diff --git a/autogpt_platform/backend/backend/api/features/v1.py b/autogpt_platform/backend/backend/api/features/v1.py
index 45e9aa95cc..a1c53f1a90 100644
--- a/autogpt_platform/backend/backend/api/features/v1.py
+++ b/autogpt_platform/backend/backend/api/features/v1.py
@@ -2546,7 +2546,7 @@ async def download_shared_file(
if not file:
raise HTTPException(status_code=404, detail="Not found")
- return await create_file_download_response(file, inline=True)
+ return await create_file_download_response(file)
########################################################
diff --git a/autogpt_platform/backend/backend/api/features/v1_share_test.py b/autogpt_platform/backend/backend/api/features/v1_share_test.py
index de5d14ad80..e9c1d7fd1b 100644
--- a/autogpt_platform/backend/backend/api/features/v1_share_test.py
+++ b/autogpt_platform/backend/backend/api/features/v1_share_test.py
@@ -65,7 +65,7 @@ class TestDownloadSharedFile:
def _client(self):
self.client = TestClient(app, raise_server_exceptions=False)
- def test_valid_token_and_file_returns_inline_content(self):
+ def test_valid_token_and_file_returns_attachment(self):
with (
patch(
"backend.api.features.v1.execution_db.get_shared_execution_file",
@@ -88,7 +88,7 @@ class TestDownloadSharedFile:
assert response.status_code == 200
assert response.content == b"\x89PNG"
- assert "inline" in response.headers["Content-Disposition"]
+ assert "attachment" in response.headers["Content-Disposition"]
def test_invalid_token_format_returns_422(self):
response = self.client.get(
diff --git a/autogpt_platform/backend/backend/api/features/workspace/routes.py b/autogpt_platform/backend/backend/api/features/workspace/routes.py
index 40c5bacfd1..d35f8c413f 100644
--- a/autogpt_platform/backend/backend/api/features/workspace/routes.py
+++ b/autogpt_platform/backend/backend/api/features/workspace/routes.py
@@ -81,6 +81,8 @@ def _create_streaming_response(
media_type=file.mime_type,
headers={
"Content-Disposition": disposition,
+ "Content-Security-Policy": "sandbox",
+ "X-Content-Type-Options": "nosniff",
"Content-Length": str(len(content)),
},
)
diff --git a/autogpt_platform/backend/backend/api/features/workspace/routes_test.py b/autogpt_platform/backend/backend/api/features/workspace/routes_test.py
index c6f07e7d7d..6b0a310024 100644
--- a/autogpt_platform/backend/backend/api/features/workspace/routes_test.py
+++ b/autogpt_platform/backend/backend/api/features/workspace/routes_test.py
@@ -1020,6 +1020,8 @@ class TestCreateStreamingResponse:
assert (
response.headers["Content-Disposition"] == 'attachment; filename="data.bin"'
)
+ assert response.headers["Content-Security-Policy"] == "sandbox"
+ assert response.headers["X-Content-Type-Options"] == "nosniff"
assert response.headers["Content-Type"] == "application/octet-stream"
assert response.headers["Content-Length"] == "11"
assert response.body == b"binary-data"
@@ -1030,6 +1032,8 @@ class TestCreateStreamingResponse:
file = _make_file(name="photo.png", mime_type="image/png")
response = _create_streaming_response(b"\x89PNG", file, inline=True)
assert response.headers["Content-Disposition"] == 'inline; filename="photo.png"'
+ assert response.headers["Content-Security-Policy"] == "sandbox"
+ assert response.headers["X-Content-Type-Options"] == "nosniff"
assert response.headers["Content-Type"] == "image/png"
def test_inline_sanitizes_filename(self):
diff --git a/autogpt_platform/frontend/playwright.security.config.ts b/autogpt_platform/frontend/playwright.security.config.ts
new file mode 100644
index 0000000000..4990423cd3
--- /dev/null
+++ b/autogpt_platform/frontend/playwright.security.config.ts
@@ -0,0 +1,21 @@
+import { defineConfig, devices } from "@playwright/test";
+
+export default defineConfig({
+ testDir: "./src/playwright",
+ testMatch: /shared-file-security-happy-path\.spec\.ts/,
+ fullyParallel: true,
+ forbidOnly: true,
+ retries: 0,
+ workers: 1,
+ reporter: "list",
+ use: {
+ bypassCSP: true,
+ trace: "retain-on-failure",
+ },
+ projects: [
+ {
+ name: "chromium",
+ use: { ...devices["Desktop Chrome"], channel: "chromium" },
+ },
+ ],
+});
diff --git a/autogpt_platform/frontend/src/app/api/proxy/[...path]/__tests__/route.helpers.test.ts b/autogpt_platform/frontend/src/app/api/proxy/[...path]/__tests__/route.helpers.test.ts
index 004b5b7735..3763e8d33c 100644
--- a/autogpt_platform/frontend/src/app/api/proxy/[...path]/__tests__/route.helpers.test.ts
+++ b/autogpt_platform/frontend/src/app/api/proxy/[...path]/__tests__/route.helpers.test.ts
@@ -1,5 +1,7 @@
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import {
+ buildSafeWorkspaceDownloadHeaders,
+ getSafeDownloadContentDisposition,
isWorkspaceDownloadRequest,
isRedirectStatus,
isTransientWorkspaceDownloadStatus,
@@ -740,6 +742,54 @@ describe("isWorkspaceDownloadRequest", () => {
});
});
+describe("getSafeDownloadContentDisposition", () => {
+ it("forces an inline response to download while preserving its filename", () => {
+ expect(
+ getSafeDownloadContentDisposition('inline; filename="payload.html"'),
+ ).toBe('attachment; filename="payload.html"');
+ });
+
+ it("keeps attachment filename parameters", () => {
+ expect(
+ getSafeDownloadContentDisposition(
+ "attachment; filename*=UTF-8''image.png",
+ ),
+ ).toBe("attachment; filename*=UTF-8''image.png");
+ });
+
+ it("supplies attachment when the upstream omits a disposition", () => {
+ expect(getSafeDownloadContentDisposition(null)).toBe("attachment");
+ });
+});
+
+describe("buildSafeWorkspaceDownloadHeaders", () => {
+ it("hardens an upstream inline active-content response", () => {
+ expect(
+ buildSafeWorkspaceDownloadHeaders(
+ "text/html; charset=utf-8",
+ 'inline; filename="payload.html"',
+ 42,
+ ),
+ ).toEqual({
+ "Content-Type": "text/html; charset=utf-8",
+ "Content-Length": "42",
+ "Content-Disposition": 'attachment; filename="payload.html"',
+ "Content-Security-Policy": "sandbox",
+ "X-Content-Type-Options": "nosniff",
+ });
+ });
+
+ it("hardens a redirected storage response with missing metadata", () => {
+ expect(buildSafeWorkspaceDownloadHeaders(null, null, 7)).toEqual({
+ "Content-Type": "application/octet-stream",
+ "Content-Length": "7",
+ "Content-Disposition": "attachment",
+ "Content-Security-Policy": "sandbox",
+ "X-Content-Type-Options": "nosniff",
+ });
+ });
+});
+
describe("isRedirectStatus", () => {
it.each([301, 302, 303, 307, 308])("returns true for %d", (status) => {
expect(isRedirectStatus(status)).toBe(true);
diff --git a/autogpt_platform/frontend/src/app/api/proxy/[...path]/__tests__/route.test.ts b/autogpt_platform/frontend/src/app/api/proxy/[...path]/__tests__/route.test.ts
index d8e6d9f7c2..3923849b6c 100644
--- a/autogpt_platform/frontend/src/app/api/proxy/[...path]/__tests__/route.test.ts
+++ b/autogpt_platform/frontend/src/app/api/proxy/[...path]/__tests__/route.test.ts
@@ -282,6 +282,40 @@ describe("proxy route — handler pass-through", () => {
`${BACKEND}/api/v1/items?page=2&size=20`,
);
});
+
+ it("hardens a public shared-file response after proxying storage", async () => {
+ vi.mocked(fetch).mockResolvedValue(
+ new Response("<script>window.name = 'executed'</script>", {
+ status: 200,
+ headers: {
+ "Content-Type": "text/html; charset=utf-8",
+ "Content-Disposition": 'inline; filename="payload.html"',
+ },
+ }),
+ );
+
+ const shareToken = "550e8400-e29b-41d4-a716-446655440000";
+ const fileID = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
+ const path = [
+ "api",
+ "public",
+ "shared",
+ shareToken,
+ "files",
+ fileID,
+ "download",
+ ];
+ const req = new NextRequest(`https://app.test/api/proxy/${path.join("/")}`);
+ const res = await GET(req, makeParams(path));
+
+ expect(res.status).toBe(200);
+ expect(res.headers.get("content-type")).toBe("text/html; charset=utf-8");
+ expect(res.headers.get("content-disposition")).toBe(
+ 'attachment; filename="payload.html"',
+ );
+ expect(res.headers.get("content-security-policy")).toBe("sandbox");
+ expect(res.headers.get("x-content-type-options")).toBe("nosniff");
+ });
});
describe("proxy route — response-start timeout", () => {
diff --git a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.ts b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.ts
index 7de65cf741..d02633d7b5 100644
--- a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.ts
+++ b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.ts
@@ -46,6 +46,32 @@ export function isWorkspaceDownloadRequest(path: string[]): boolean {
return false;
}
+export function getSafeDownloadContentDisposition(
+ contentDisposition: string | null,
+): string {
+ if (!contentDisposition) return "attachment";
+
+ const parametersStart = contentDisposition.indexOf(";");
+ return parametersStart === -1
+ ? "attachment"
+ : `attachment${contentDisposition.slice(parametersStart)}`;
+}
+
+export function buildSafeWorkspaceDownloadHeaders(
+ contentType: string | null,
+ contentDisposition: string | null,
+ contentLength: number,
+): Record<string, string> {
+ return {
+ "Content-Type": contentType || "application/octet-stream",
+ "Content-Length": String(contentLength),
+ "Content-Disposition":
+ getSafeDownloadContentDisposition(contentDisposition),
+ "Content-Security-Policy": "sandbox",
+ "X-Content-Type-Options": "nosniff",
+ };
+}
+
export function isRedirectStatus(status: number): boolean {
return [301, 302, 303, 307, 308].includes(status);
}
diff --git a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts
index 6f6e485435..3f6fae236b 100644
--- a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts
+++ b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts
@@ -7,6 +7,7 @@ import { environment } from "@/services/environment";
import { NextRequest, NextResponse } from "next/server";
import {
+ buildSafeWorkspaceDownloadHeaders,
fetchWorkspaceDownloadWithRetry,
getResponseStartTimeoutMs,
getWorkspaceDownloadErrorMessage,
@@ -137,18 +138,11 @@ async function handleWorkspaceDownload(
// ~10 KB of larger files are dropped, corrupting PNGs and truncating CSVs.
const buffer = await response.arrayBuffer();
- const contentType =
- response.headers.get("Content-Type") || "application/octet-stream";
- const contentDisposition = response.headers.get("Content-Disposition");
-
- const responseHeaders: Record<string, string> = {
- "Content-Type": contentType,
- "Content-Length": String(buffer.byteLength),
- };
-
- if (contentDisposition) {
- responseHeaders["Content-Disposition"] = contentDisposition;
- }
+ const responseHeaders = buildSafeWorkspaceDownloadHeaders(
+ response.headers.get("Content-Type"),
+ response.headers.get("Content-Disposition"),
+ buffer.byteLength,
+ );
return new NextResponse(buffer, {
status: 200,
diff --git a/autogpt_platform/frontend/src/playwright/shared-file-security-happy-path.spec.ts b/autogpt_platform/frontend/src/playwright/shared-file-security-happy-path.spec.ts
new file mode 100644
index 0000000000..6a373e0903
--- /dev/null
+++ b/autogpt_platform/frontend/src/playwright/shared-file-security-happy-path.spec.ts
@@ -0,0 +1,89 @@
+import { buildSafeWorkspaceDownloadHeaders } from "../app/api/proxy/[...path]/route.helpers";
+import { expect, test } from "./coverage-fixture";
+
+const PREVIEW_URL = "https://shared-files.test/preview.svg";
+const ACTIVE_CONTENT_URL = "https://shared-files.test/payload.html";
+const EXECUTION_MARKER_URL = "https://shared-files.test/executed";
+
+const SVG_PREVIEW = `
+ <svg xmlns="http://www.w3.org/2000/svg" width="20" height="10">
+ <rect width="20" height="10" fill="green" />
+ </svg>
+`;
+
+const ACTIVE_CONTENT = `
+ <script>
+ window.name = "payload-executed";
+ fetch("${EXECUTION_MARKER_URL}");
+ </script>
+`;
+
+function safeHeaders(
+ contentType: string,
+ contentDisposition: string,
+ body: string,
+) {
+ return buildSafeWorkspaceDownloadHeaders(
+ contentType,
+ contentDisposition,
+ Buffer.byteLength(body),
+ );
+}
+
+test.describe("shared workspace file delivery", () => {
+ test("renders an attached SVG as an image subresource", async ({ page }) => {
+ await page.route(PREVIEW_URL, async (route) => {
+ await route.fulfill({
+ status: 200,
+ headers: safeHeaders(
+ "image/svg+xml",
+ 'inline; filename="preview.svg"',
+ SVG_PREVIEW,
+ ),
+ body: SVG_PREVIEW,
+ });
+ });
+
+ await page.setContent(`<img id="preview" src="${PREVIEW_URL}" />`);
+
+ const preview = page.locator("#preview");
+ await expect
+ .poll(
+ async () =>
+ await preview.evaluate(
+ (image) => (image as HTMLImageElement).naturalWidth,
+ ),
+ )
+ .toBe(20);
+ });
+
+ test("downloads active content without executing it", async ({ page }) => {
+ let executionMarkerRequested = false;
+ page.on("request", (request) => {
+ if (request.url() === EXECUTION_MARKER_URL) {
+ executionMarkerRequested = true;
+ }
+ });
+
+ await page.route(ACTIVE_CONTENT_URL, async (route) => {
+ await route.fulfill({
+ status: 200,
+ headers: safeHeaders(
+ "text/html; charset=utf-8",
+ 'inline; filename="payload.html"',
+ ACTIVE_CONTENT,
+ ),
+ body: ACTIVE_CONTENT,
+ });
+ });
+
+ await page.goto("data:text/html,<script>window.name='safe-page'</script>");
+ const downloadPromise = page.waitForEvent("download");
+ await page.goto(ACTIVE_CONTENT_URL).catch(() => undefined);
+ const download = await downloadPromise;
+
+ expect(download.suggestedFilename()).toBe("payload.html");
+ expect(await page.evaluate(() => window.name)).toBe("safe-page");
+ expect(executionMarkerRequested).toBe(false);
+ });
+});