Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(api): build exchange proxy paths from validated segments (#5331)

David Tippett910fd466aa55
diff --git a/apps/api/src/__tests__/routes/exchange-connectors.routes.test.ts b/apps/api/src/__tests__/routes/exchange-connectors.routes.test.ts
index 37dc5a19b..ca66dda56 100644
--- a/apps/api/src/__tests__/routes/exchange-connectors.routes.test.ts
+++ b/apps/api/src/__tests__/routes/exchange-connectors.routes.test.ts
@@ -241,6 +241,34 @@ describe("Alexandria connector routes on the Exchange proxy", () => {
);
});
+ it("re-encodes path segments before forwarding", async () => {
+ keyResolvesAs({});
+ await request(app)
+ .patch("/exchange/team-skills/item%201")
+ .set("authorization", `Bearer ${KEY}`)
+ .send({ name: "Digest" });
+ expect(mocks.upstreamFetch).toHaveBeenCalledTimes(1);
+ expect(upstream().url).toBe(
+ "https://exchange.test/v1/team-skills/item%201",
+ );
+ });
+
+ it.each([
+ "/exchange/supply/..%2Fretrieve",
+ "/exchange/supply/%2e%2e%2Fretrieve",
+ "/exchange/supply/x%5C..%5Cretrieve",
+ "/exchange/supply/a//b",
+ "/exchange/supply/%E0%A4%A",
+ ])("refuses %s before reaching Exchange", async path => {
+ keyResolvesAs({ exchangeRetrieve: true });
+ const response = await request(app)
+ .post(path)
+ .set("authorization", `Bearer ${KEY}`)
+ .send({});
+ expect(response.status).toBe(400);
+ expect(mocks.upstreamFetch).not.toHaveBeenCalled();
+ });
+
it("never forwards a caller's own x-exchange-connectors header", async () => {
keyResolvesAs({});
// A route open without the flag: the header must still not reach Exchange.
diff --git a/apps/api/src/routes/exchange.ts b/apps/api/src/routes/exchange.ts
index 8615a3268..7352b41c4 100644
--- a/apps/api/src/routes/exchange.ts
+++ b/apps/api/src/routes/exchange.ts
@@ -25,6 +25,23 @@ const INGEST_TIMEOUT_MS = 50_000;
const FORWARDED_REQUEST_HEADERS = ["accept", "x-request-id"];
const FORWARDED_RESPONSE_HEADERS = ["content-type", "x-request-id"];
+const FORWARDED_PATH_ROOTS = new Set([
+ "analytics",
+ "applications",
+ "claims",
+ "connectors",
+ "discover",
+ "enrichment",
+ "ingest",
+ "platform",
+ "provider-terms",
+ "publisher",
+ "rates",
+ "records",
+ "skills",
+ "supply",
+ "team-skills",
+]);
function dispatcherFor(timeout: number) {
return new Agent({
@@ -43,6 +60,32 @@ function upstreamBase(): string | null {
return config.FIRE_EXCHANGE_URL.replace(/\/+$/, "");
}
+// Rebuilds the upstream path from validated, re-encoded segments so it always
+// names the route that authorized the request.
+function upstreamPath(req: Request): string | null {
+ const segments = req.path.split("/").slice(1);
+ if (segments.length > 1 && segments.at(-1) === "") segments.pop();
+
+ const decoded: string[] = [];
+ for (const segment of segments) {
+ let value: string;
+ try {
+ value = decodeURIComponent(segment);
+ } catch {
+ return null;
+ }
+ if (!value || value === "." || value === ".." || /[/\\]/.test(value)) {
+ return null;
+ }
+ decoded.push(value);
+ }
+ if (!FORWARDED_PATH_ROOTS.has(decoded[0])) return null;
+
+ const queryStart = req.originalUrl.indexOf("?");
+ const query = queryStart === -1 ? "" : req.originalUrl.slice(queryStart);
+ return "/v1/" + decoded.map(encodeURIComponent).join("/") + query;
+}
+
function exchangeProxy(
timeout: number,
options: {
@@ -67,6 +110,11 @@ function exchangeProxy(
return exchangeError(res, 503, "This endpoint is not available.");
}
+ const path = upstreamPath(req);
+ if (path === null) {
+ return exchangeError(res, 400, "Invalid request path.");
+ }
+
if (requiresRetrieveFlag && !authedReq.acuc?.flags?.exchangeRetrieve) {
return exchangeError(
res,
@@ -77,7 +125,6 @@ function exchangeProxy(
const hasBody = req.method !== "GET";
const billingCountry = await billingCountryForTeam(authedReq.auth.team_id);
- const path = req.originalUrl.replace(/^\/exchange/, "/v1");
try {
const upstream = await fetch(base + path, {