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.

fix(frontend): stream proxy bodies and skip middleware auth on /api/proxy (#13019)

Ubbe431730ccd5a1
diff --git a/autogpt_platform/frontend/src/app/api/mutators/__tests__/custom-mutator.test.ts b/autogpt_platform/frontend/src/app/api/mutators/__tests__/custom-mutator.test.ts
index 89b17866c3..6f6a28e590 100644
--- a/autogpt_platform/frontend/src/app/api/mutators/__tests__/custom-mutator.test.ts
+++ b/autogpt_platform/frontend/src/app/api/mutators/__tests__/custom-mutator.test.ts
@@ -183,3 +183,83 @@ describe("customMutator — Sentry trace propagation", () => {
(Sentry as { getTraceData?: unknown }).getTraceData = mockGetTraceData;
});
});
+
+describe("customMutator — empty body handling", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mockIsClientSide.mockReturnValue(true);
+ mockGetSystemHeaders.mockReturnValue({});
+ mockGetTraceData.mockReturnValue({});
+ });
+
+ it("returns null data for 204 No Content even when Content-Type is application/json", async () => {
+ // FastAPI sets Content-Type: application/json on 204 by default; the proxy
+ // forwards it. Calling .json() on the empty body would throw — verify we
+ // short-circuit and return null instead.
+ const jsonSpy = vi.fn();
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ status: 204,
+ headers: new Headers({ "content-type": "application/json" }),
+ json: jsonSpy,
+ }),
+ );
+
+ const result = await customMutator<{
+ data: unknown;
+ status: number;
+ headers: Headers;
+ }>("/api/executions/abc", { method: "DELETE" });
+
+ expect(result.status).toBe(204);
+ expect(result.data).toBeNull();
+ expect(jsonSpy).not.toHaveBeenCalled();
+ });
+
+ it("returns null data when Content-Length is 0", async () => {
+ const jsonSpy = vi.fn();
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ status: 200,
+ headers: new Headers({
+ "content-type": "application/json",
+ "content-length": "0",
+ }),
+ json: jsonSpy,
+ }),
+ );
+
+ const result = await customMutator<{
+ data: unknown;
+ status: number;
+ headers: Headers;
+ }>("/api/foo", { method: "DELETE" });
+
+ expect(result.data).toBeNull();
+ expect(jsonSpy).not.toHaveBeenCalled();
+ });
+
+ it("still parses JSON body for non-empty 200 responses", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ status: 200,
+ headers: new Headers({ "content-type": "application/json" }),
+ json: () => Promise.resolve({ ok: true }),
+ }),
+ );
+
+ const result = await customMutator<{
+ data: unknown;
+ status: number;
+ headers: Headers;
+ }>("/api/foo", { method: "GET" });
+
+ expect(result.data).toEqual({ ok: true });
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/api/mutators/custom-mutator.ts b/autogpt_platform/frontend/src/app/api/mutators/custom-mutator.ts
index 019e911fbf..f4c6656f29 100644
--- a/autogpt_platform/frontend/src/app/api/mutators/custom-mutator.ts
+++ b/autogpt_platform/frontend/src/app/api/mutators/custom-mutator.ts
@@ -21,7 +21,18 @@ const getBaseUrl = (): string => {
}
};
-const getBody = <T>(c: Response | Request): Promise<T> => {
+const getBody = async <T>(c: Response | Request): Promise<T> => {
+ // 204 No Content responses (and 200s with Content-Length: 0) have no body.
+ // Calling .json() on them throws "Unexpected end of JSON input" because the
+ // backend may still set Content-Type: application/json on 204s. Short-circuit
+ // to null so callers see a normal success rather than a parse error.
+ if (
+ "status" in c &&
+ (c.status === 204 || c.headers.get("Content-Length") === "0")
+ ) {
+ return null as T;
+ }
+
const contentType = c.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
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 605d4cf16c..002f912c7d 100644
--- a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts
+++ b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts
@@ -1,36 +1,90 @@
import {
- ApiError,
- getServerAuthToken,
- makeAuthenticatedFileUpload,
- makeAuthenticatedRequest,
-} from "@/lib/autogpt-server-api/helpers";
+ API_KEY_HEADER_NAME,
+ IMPERSONATION_HEADER_NAME,
+} from "@/lib/constants";
+import { getServerAuthToken } from "@/lib/autogpt-server-api/helpers";
import { environment } from "@/services/environment";
import { NextRequest, NextResponse } from "next/server";
-// Increase body size limit to 256MB to match backend file upload limit
-export const maxDuration = 300; // 5 minutes timeout for large uploads
-export const dynamic = "force-dynamic";
-
import {
fetchWorkspaceDownloadWithRetry,
getWorkspaceDownloadErrorMessage,
isWorkspaceDownloadRequest,
} from "./route.helpers";
+// 5 minutes for large uploads. Most calls finish in well under a second.
+export const maxDuration = 300;
+export const dynamic = "force-dynamic";
+
+const FORWARDED_REQUEST_HEADERS: ReadonlyArray<string> = [
+ "content-type",
+ "content-length",
+ "accept",
+ "accept-language",
+ "accept-encoding",
+ IMPERSONATION_HEADER_NAME.toLowerCase(),
+ API_KEY_HEADER_NAME.toLowerCase(),
+ "sentry-trace",
+ "baggage",
+];
+
+// Headers that must not be forwarded from the backend to the client.
+// Hop-by-hop entries are listed in RFC 7230 §6.1; content-encoding and
+// content-length are stripped because Node's fetch auto-decompresses the
+// backend body when reading response.body, so the streamed payload's actual
+// byte length and encoding no longer match what the backend advertised.
+// Letting Node recompute framing (via Transfer-Encoding: chunked) keeps
+// HTTP parsing on the client side correct.
+const STRIPPED_RESPONSE_HEADERS: ReadonlySet<string> = new Set([
+ "connection",
+ "keep-alive",
+ "proxy-authenticate",
+ "proxy-authorization",
+ "te",
+ "trailer",
+ "transfer-encoding",
+ "upgrade",
+ "content-encoding",
+ "content-length",
+]);
+
function buildBackendUrl(path: string[], queryString: string): string {
- const backendPath = path.join("/");
- return `${environment.getAGPTServerBaseUrl()}/${backendPath}${queryString}`;
+ return `${environment.getAGPTServerBaseUrl()}/${path.join("/")}${queryString}`;
}
-/**
- * Handle workspace file download requests with proper binary response streaming.
- */
+function buildForwardHeaders(req: NextRequest, token: string | null): Headers {
+ const headers = new Headers();
+ for (const name of FORWARDED_REQUEST_HEADERS) {
+ const value = req.headers.get(name);
+ if (value) headers.set(name, value);
+ }
+ if (token && token !== "no-token-found") {
+ headers.set("authorization", `Bearer ${token}`);
+ }
+ return headers;
+}
+
+function filterResponseHeaders(src: Headers): Headers {
+ const out = new Headers();
+ src.forEach((value, key) => {
+ if (!STRIPPED_RESPONSE_HEADERS.has(key.toLowerCase())) {
+ out.set(key, value);
+ }
+ });
+ return out;
+}
+
+const METHODS_WITHOUT_BODY: ReadonlySet<string> = new Set([
+ "GET",
+ "HEAD",
+ "DELETE",
+ "OPTIONS",
+]);
+
async function handleWorkspaceDownload(
- req: NextRequest,
backendUrl: string,
+ token: string | null,
): Promise<NextResponse> {
- const token = await getServerAuthToken();
-
const headers: Record<string, string> = {};
if (token && token !== "no-token-found") {
headers["Authorization"] = `Bearer ${token}`;
@@ -47,7 +101,7 @@ async function handleWorkspaceDownload(
return await createWorkspaceDownloadErrorResponse(response);
}
- // Fully buffer the response before forwarding. Passing response.body as a
+ // Fully buffer the response before forwarding. Passing response.body as a
// ReadableStream causes silent truncation in Next.js / Vercel — the last
// ~10 KB of larger files are dropped, corrupting PNGs and truncating CSVs.
const buffer = await response.arrayBuffer();
@@ -99,152 +153,16 @@ async function createWorkspaceDownloadErrorResponse(
}
}
-async function handleJsonRequest(
- req: NextRequest,
- method: string,
- backendUrl: string,
-): Promise<any> {
- let payload;
-
- try {
- payload = await req.json();
- } catch (error) {
- // Handle cases where request body is empty, invalid JSON, or already consumed
- console.warn("Failed to parse JSON from request body:", error);
- payload = null;
- }
-
- return await makeAuthenticatedRequest(
- method,
- backendUrl,
- payload,
- "application/json",
- req,
- );
-}
-
-async function handleFormDataRequest(
- req: NextRequest,
- backendUrl: string,
-): Promise<any> {
- const formData = await req.formData();
- return await makeAuthenticatedFileUpload(backendUrl, formData, req);
-}
-
-async function handleUrlEncodedRequest(
- req: NextRequest,
- method: string,
- backendUrl: string,
-): Promise<any> {
- const textPayload = await req.text();
- const params = new URLSearchParams(textPayload);
- const payload = Object.fromEntries(params.entries());
- return await makeAuthenticatedRequest(
- method,
- backendUrl,
- payload,
- "application/x-www-form-urlencoded",
- req,
- );
-}
-
-async function handleGetDeleteRequest(
- method: string,
- backendUrl: string,
- req: NextRequest,
-): Promise<any> {
- return await makeAuthenticatedRequest(
- method,
- backendUrl,
- undefined,
- "application/json",
- req,
- );
-}
-
-function createUnsupportedContentTypeResponse(
- contentType: string | null,
-): NextResponse {
- return NextResponse.json(
- {
- error:
- "Unsupported Content-Type for proxying with authentication helpers.",
- receivedContentType: contentType,
- supportedContentTypes: [
- "application/json",
- "multipart/form-data",
- "application/x-www-form-urlencoded",
- ],
- },
- { status: 415 },
- );
-}
-
-function createResponse(
- responseBody: any,
- responseStatus: number,
- responseHeaders: Record<string, string>,
-): NextResponse {
- if (responseStatus === 204) {
- return new NextResponse(null, { status: responseStatus });
- } else {
- return NextResponse.json(responseBody, {
- status: responseStatus,
- headers: responseHeaders,
- });
- }
-}
-
-function createErrorResponse(
- error: unknown,
- path: string,
- method: string,
-): NextResponse {
- if (
- error &&
- typeof error === "object" &&
- "status" in error &&
- [401, 403].includes(error.status as number)
- ) {
- // Log this since it indicates a potential frontend bug
- console.warn(
- `Authentication error in API proxy for ${method} ${path}:`,
- "message" in error ? error.message : error,
- );
- }
-
- // If it's our custom ApiError, preserve the original status and response
- if (error instanceof ApiError) {
- return NextResponse.json(error.response || { error: error.message }, {
- status: error.status,
- });
- }
-
- // For JSON parsing errors, provide more context
- if (error instanceof SyntaxError && error.message.includes("JSON")) {
- return NextResponse.json(
- {
- error: "Invalid response from backend",
- detail: error.message ?? "Backend returned non-JSON response",
- },
- { status: 502 },
- );
- }
-
- // For other errors, use generic response
- const detail =
- error instanceof Error ? error.message : "An unknown error occurred";
- return NextResponse.json(
- { error: "Proxy request failed", detail },
- { status: 500 },
- );
-}
-
/**
- * A simple proxy route that forwards requests to the backend API.
- * It injects the server-side authentication token into the Authorization header.
- * It uses the makeAuthenticatedRequest and makeAuthenticatedFileUpload helpers
- * to handle request body parsing and authentication.
+ * Forwards browser API requests to the backend with the JWT pulled from
+ * httpOnly cookies attached to the Authorization header.
+ *
+ * Body and response are streamed end-to-end so JSON / FormData / SSE bodies
+ * pass through without parse → re-serialise round-trips. Status codes and
+ * non-hop-by-hop response headers are preserved.
+ *
+ * Workspace file downloads have a dedicated buffering path because Vercel's
+ * stream forwarding silently truncates large binary responses.
*/
async function handler(
req: NextRequest,
@@ -254,39 +172,41 @@ async function handler(
const url = new URL(req.url);
const queryString = url.search;
const backendUrl = buildBackendUrl(path, queryString);
-
const method = req.method;
- const contentType = req.headers.get("Content-Type");
-
- let responseBody: any;
- const responseHeaders: Record<string, string> = {
- "Content-Type": "application/json",
- };
try {
- // Handle workspace file downloads separately (binary response)
+ const token = await getServerAuthToken();
+
if (method === "GET" && isWorkspaceDownloadRequest(path)) {
- return await handleWorkspaceDownload(req, backendUrl);
+ return await handleWorkspaceDownload(backendUrl, token);
}
- if (method === "GET" || method === "DELETE") {
- responseBody = await handleGetDeleteRequest(method, backendUrl, req);
- } else if (contentType?.includes("application/json")) {
- responseBody = await handleJsonRequest(req, method, backendUrl);
- } else if (contentType?.includes("multipart/form-data")) {
- responseBody = await handleFormDataRequest(req, backendUrl);
- } else if (contentType?.includes("application/x-www-form-urlencoded")) {
- responseBody = await handleUrlEncodedRequest(req, method, backendUrl);
- } else {
- return createUnsupportedContentTypeResponse(contentType);
- }
+ const headers = buildForwardHeaders(req, token);
+ const hasBody = !METHODS_WITHOUT_BODY.has(method);
- return createResponse(responseBody, 200, responseHeaders);
- } catch (error) {
- return createErrorResponse(
- error,
- path.map((s) => `/${s}`).join(""),
+ const backendResponse = await fetch(backendUrl, {
method,
+ headers,
+ body: hasBody ? req.body : undefined,
+ // `duplex: "half"` is required by Node's fetch when sending a
+ // ReadableStream body. Cast because TS lib types haven't caught up.
+ ...(hasBody ? ({ duplex: "half" } as RequestInit) : {}),
+ redirect: "manual",
+ });
+
+ return new NextResponse(backendResponse.body, {
+ status: backendResponse.status,
+ statusText: backendResponse.statusText,
+ headers: filterResponseHeaders(backendResponse.headers),
+ });
+ } catch (error) {
+ console.error(`Proxy error for ${method} /${path.join("/")}:`, error);
+ return NextResponse.json(
+ {
+ error: "Proxy request failed",
+ detail: error instanceof Error ? error.message : "Unknown error",
+ },
+ { status: 502 },
);
}
}
diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/client.test.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/client.test.ts
index 524064f6e1..ccbed9b2e8 100644
--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/client.test.ts
+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/__tests__/client.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it, vi } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import BackendAPI, { buildOAuthLoginQuery } from "../client";
describe("BackendAPI.oAuthLogin", () => {
@@ -71,3 +71,50 @@ describe("buildOAuthLoginQuery", () => {
});
});
});
+
+describe("BackendAPI._makeClientRequest 204 handling", () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ function mockFetch(response: Response) {
+ return vi.spyOn(globalThis, "fetch").mockResolvedValue(response);
+ }
+
+ it("returns null for 204 No Content without parsing body", async () => {
+ const api = new BackendAPI("http://test", "ws://test");
+ mockFetch(new Response(null, { status: 204 }));
+
+ const result = await (api as any)._makeClientRequest(
+ "DELETE",
+ "/library/agents/abc",
+ );
+
+ expect(result).toBeNull();
+ });
+
+ it("returns null when Content-Length is 0", async () => {
+ const api = new BackendAPI("http://test", "ws://test");
+ mockFetch(
+ new Response("", { status: 200, headers: { "Content-Length": "0" } }),
+ );
+
+ const result = await (api as any)._makeClientRequest("DELETE", "/x");
+
+ expect(result).toBeNull();
+ });
+
+ it("parses JSON body for non-empty 200 responses", async () => {
+ const api = new BackendAPI("http://test", "ws://test");
+ mockFetch(
+ new Response(JSON.stringify({ ok: true }), {
+ status: 200,
+ headers: { "Content-Type": "application/json" },
+ }),
+ );
+
+ const result = await (api as any)._makeClientRequest("GET", "/x");
+
+ expect(result).toEqual({ ok: true });
+ });
+});
diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts
index 22169d1c4d..93a4484ee9 100644
--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts
+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts
@@ -932,6 +932,12 @@ export default class BackendAPI {
throw error;
}
+ if (
+ response.status === 204 ||
+ response.headers.get("Content-Length") === "0"
+ ) {
+ return null;
+ }
return await response.json();
}
diff --git a/autogpt_platform/frontend/src/middleware.ts b/autogpt_platform/frontend/src/middleware.ts
index 411e2fe498..b98280a3a2 100644
--- a/autogpt_platform/frontend/src/middleware.ts
+++ b/autogpt_platform/frontend/src/middleware.ts
@@ -23,11 +23,14 @@ export const config = {
* - /_next/image (image optimization files)
* - /favicon.ico (favicon file)
* - /auth/callback (OAuth callback - needs to work without auth)
+ * - /api/proxy (backend API proxy - the route handler authenticates
+ * itself via httpOnly cookies; running supabase.auth.getUser() here
+ * adds a Supabase auth-server round-trip to every API call)
* Feel free to modify this pattern to include more paths.
*
* Note: /auth/authorize and /auth/integrations/* ARE protected and need
* middleware to run for authentication checks.
*/
- "/((?!_next/static|_next/image|favicon.ico|auth/callback|auth/integrations/mcp_callback|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
+ "/((?!_next/static|_next/image|favicon.ico|auth/callback|auth/integrations/mcp_callback|api/proxy|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};
diff --git a/autogpt_platform/frontend/src/playwright/utils/signup.ts b/autogpt_platform/frontend/src/playwright/utils/signup.ts
index c83c760102..7a2a3abd6b 100644
--- a/autogpt_platform/frontend/src/playwright/utils/signup.ts
+++ b/autogpt_platform/frontend/src/playwright/utils/signup.ts
@@ -32,8 +32,10 @@ export async function signupTestUser(
const confirmPasswordInput = page.locator("#confirmPassword");
await confirmPasswordInput.fill(userPassword);
- // Agree to terms and submit
- await getRole("checkbox").click();
+ // Agree to terms and submit. Scope to the Terms checkbox by accessible
+ // name — in dev/local the AgentationDevtool renders extra checkboxes
+ // globally, so a bare getByRole("checkbox") trips Playwright strict mode.
+ await getRole("checkbox", /agree to the terms/i).click();
const signupButton = getButton("Sign up");
await signupButton.click();