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.

11.2 KB
Raw Blame
import {
  API_KEY_HEADER_NAME,
  CLIENT_COUNTRY_TOKEN_HEADER_NAME,
  CLIENT_IP_TOKEN_HEADER_NAME,
  IMPERSONATION_HEADER_NAME,
  VERCEL_COUNTRY_HEADER_NAME,
} from "@/lib/constants";
import { getClientIPToken } from "@/lib/auth/client-ip-token";
import { getCountryToken } from "@/lib/auth/country-token";
import { getServerAuthToken } from "@/lib/auth/server/getServerAuthToken";
import { environment } from "@/services/environment";
import { NextRequest, NextResponse } from "next/server";

import {
  buildSafeWorkspaceDownloadHeaders,
  fetchWorkspaceDownloadWithRetry,
  getResponseStartTimeoutMs,
  getSafePrivateMediaRange,
  shouldBufferPrivateMedia,
  getWorkspaceDownloadErrorMessage,
  isPrivateStoreMediaRequest,
  isPrivateStoreVideoRequest,
  isStoreSearchRequest,
  isWorkspaceDownloadRequest,
  watchResponseStart,
} 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";

// Hard ceiling on the backend hop so a stalled connection becomes a definite
// 504 instead of an eternal spinner. Sized just under `maxDuration` so it never
// interrupts a legitimate large upload/download that fits the function budget —
// it only converts a true stall (which would otherwise hit Vercel's opaque
// function timeout) into a clean error response.
const BACKEND_FETCH_TIMEOUT_MS = 290_000;

function isTimeoutError(error: unknown): boolean {
  return (
    typeof error === "object" &&
    error !== null &&
    "name" in error &&
    (error as { name: unknown }).name === "TimeoutError"
  );
}

const FORWARDED_REQUEST_HEADERS: ReadonlyArray<string> = [
  "content-type",
  "content-length",
  "accept",
  "accept-language",
  IMPERSONATION_HEADER_NAME.toLowerCase(),
  API_KEY_HEADER_NAME.toLowerCase(),
  "sentry-trace",
  "baggage",
  "x-datafast-visitor-id",
  "x-datafast-session-id",
  "x-posthog-session-id",
];

// Browsers advertise `zstd` in Accept-Encoding, but undici (Node 22's fetch)
// only auto-decompresses gzip/deflate/br. If we forwarded the browser value,
// Cloudflare in front of the backend would pick zstd, Node would hand us raw
// zstd bytes, the response handler would strip Content-Encoding, and Vercel's
// edge would then brotli-wrap those bytes — leaving the browser to decode br
// over zstd and fail JSON.parse on a 200 response.
const BACKEND_ACCEPT_ENCODING = "gzip, deflate, br";

// 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.
// `set-cookie` is stripped defensively so any backend cookie without an
// explicit Domain attribute can't attach to the frontend origin.
const STRIPPED_RESPONSE_HEADERS: ReadonlySet<string> = new Set([
  "connection",
  "keep-alive",
  "proxy-authenticate",
  "proxy-authorization",
  "te",
  "trailer",
  "transfer-encoding",
  "upgrade",
  "content-encoding",
  "content-length",
  "authorization",
  "set-cookie",
]);

function buildBackendUrl(path: string[], queryString: string): string {
  return `${environment.getAGPTServerBaseUrl()}/${path.join("/")}${queryString}`;
}

async function signCountry(country: string) {
  try {
    return await getCountryToken(country);
  } catch (error) {
    console.error("[proxy] Could not sign the client country", error);
    return null;
  }
}

async function buildForwardHeaders(
  req: NextRequest,
  token: string | null,
  path: string[],
): Promise<Headers> {
  const headers = new Headers();
  for (const name of FORWARDED_REQUEST_HEADERS) {
    const value = req.headers.get(name);
    if (value) headers.set(name, value);
  }
  headers.set("accept-encoding", BACKEND_ACCEPT_ENCODING);
  // The visitor's country as the edge saw it, signed so the backend can tell
  // it from one a caller typed. Without it the country is unknown, which the
  // trial offer treats as "no offer", so a failed mint fails closed.
  const country = req.headers.get(VERCEL_COUNTRY_HEADER_NAME);
  const countryToken = country ? await signCountry(country) : null;
  if (countryToken) headers.set(CLIENT_COUNTRY_TOKEN_HEADER_NAME, countryToken);
  if (isStoreSearchRequest(path, req.nextUrl.searchParams)) {
    const ipToken = await getClientIPToken(req.headers);
    if (ipToken) headers.set(CLIENT_IP_TOKEN_HEADER_NAME, ipToken);
  }
  if (token) {
    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;
}

function hardenPrivateMediaResponseHeaders(headers: Headers): Headers {
  headers.set("Cache-Control", "private, no-store");
  headers.set("X-Content-Type-Options", "nosniff");
  return headers;
}

const METHODS_WITHOUT_BODY: ReadonlySet<string> = new Set([
  "GET",
  "HEAD",
  "DELETE",
  "OPTIONS",
]);

async function handleWorkspaceDownload(
  backendUrl: string,
  token: string | null,
): Promise<NextResponse> {
  const headers: Record<string, string> = {};
  if (token) {
    headers["Authorization"] = `Bearer ${token}`;
  }

  const response = await fetchWorkspaceDownloadWithRetry(
    backendUrl,
    headers,
    2,
    500,
  );

  if (!response.ok) {
    return await createWorkspaceDownloadErrorResponse(response);
  }

  // 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();

  const responseHeaders = buildSafeWorkspaceDownloadHeaders(
    response.headers.get("Content-Type"),
    response.headers.get("Content-Disposition"),
    buffer.byteLength,
  );

  return new NextResponse(buffer, {
    status: 200,
    headers: responseHeaders,
  });
}

async function createWorkspaceDownloadErrorResponse(
  response: Response,
): Promise<NextResponse> {
  const contentType = response.headers.get("Content-Type")?.toLowerCase() ?? "";

  try {
    if (contentType.includes("application/json")) {
      const body = await response.json();
      return NextResponse.json(body, { status: response.status });
    }

    const text = await response.text();
    const detail =
      getWorkspaceDownloadErrorMessage(text) ||
      response.statusText ||
      "Failed to download file";

    return NextResponse.json({ detail }, { status: response.status });
  } catch {
    return NextResponse.json(
      {
        detail: response.statusText || "Failed to download file",
      },
      { status: response.status },
    );
  }
}

/**
 * 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 and private store media are buffered because
 * Vercel's stream forwarding silently truncates large binary responses.
 */
async function handler(
  req: NextRequest,
  { params }: { params: Promise<{ path: string[] }> },
) {
  const { path } = await params;
  const url = new URL(req.url);
  const queryString = url.search;
  const backendUrl = buildBackendUrl(path, queryString);
  const method = req.method;
  const isPrivateMedia = isPrivateStoreMediaRequest(path);

  try {
    const token = await getServerAuthToken();

    if (method === "GET" && isWorkspaceDownloadRequest(path)) {
      return await handleWorkspaceDownload(backendUrl, token);
    }

    const headers = await buildForwardHeaders(req, token, path);
    if (isPrivateMedia) {
      headers.set("cache-control", "no-store");
    }
    if (method === "GET" && isPrivateStoreVideoRequest(path)) {
      const range = getSafePrivateMediaRange(req.headers.get("range"));
      if (range) {
        headers.set("range", range);
        headers.set("accept-encoding", "identity");
      }
    }
    const hasBody = !METHODS_WITHOUT_BODY.has(method);

    // Two-phase timeout: the overall ceiling covers the whole hop, while the
    // much tighter response-start watch arms only once the request body has
    // been fully uploaded — a multi-minute upload is legitimate, a backend
    // that stays silent after receiving everything is stalled.
    const responseStart = watchResponseStart(
      hasBody ? req.body : null,
      getResponseStartTimeoutMs(path, method),
    );

    let backendResponse: Response;
    try {
      backendResponse = await fetch(backendUrl, {
        method,
        headers,
        body: responseStart.body,
        // `duplex: "half"` is required by Node's fetch when sending a
        // ReadableStream body. Cast because TS lib types haven't caught up.
        ...(responseStart.body ? ({ duplex: "half" } as RequestInit) : {}),
        redirect: "manual",
        ...(isPrivateMedia ? { cache: "no-store" } : {}),
        signal: AbortSignal.any([
          AbortSignal.timeout(BACKEND_FETCH_TIMEOUT_MS),
          responseStart.signal,
        ]),
      });
    } finally {
      responseStart.clear();
    }

    const responseHeaders = filterResponseHeaders(backendResponse.headers);
    if (isPrivateMedia) {
      hardenPrivateMediaResponseHeaders(responseHeaders);
      // Vercel silently drops the tail of large streamed binary bodies, so
      // private media is buffered like workspace downloads. The backend
      // redirects larger full responses to a short-lived signed object URL.
      if (shouldBufferPrivateMedia(backendResponse.headers)) {
        const body = await backendResponse.arrayBuffer();
        responseHeaders.set("content-length", String(body.byteLength));
        return new NextResponse(body, {
          status: backendResponse.status,
          statusText: backendResponse.statusText,
          headers: responseHeaders,
        });
      }
    }

    return new NextResponse(backendResponse.body, {
      status: backendResponse.status,
      statusText: backendResponse.statusText,
      headers: responseHeaders,
    });
  } catch (error) {
    const timedOut = isTimeoutError(error);
    const errorMessage = timedOut
      ? "Proxy request timed out"
      : "Proxy request failed";
    if (isPrivateMedia) {
      console.error(`Private media proxy ${method} failed: ${errorMessage}`);
      const response = NextResponse.json(
        { error: errorMessage },
        { status: timedOut ? 504 : 502 },
      );
      hardenPrivateMediaResponseHeaders(response.headers);
      return response;
    }

    console.error(`Proxy error for ${method} /${path.join("/")}:`, error);
    return NextResponse.json(
      {
        error: errorMessage,
        detail: error instanceof Error ? error.message : "Unknown error",
      },
      { status: timedOut ? 504 : 502 },
    );
  }
}

export {
  handler as DELETE,
  handler as GET,
  handler as PATCH,
  handler as POST,
  handler as PUT,
};