Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(bot-box): keep a broker grant inside the upstream path it names

DeryFerd57837c9aa894
diff --git a/server/bot-box/broker.test.ts b/server/bot-box/broker.test.ts
index 9a6d0a1528..6b555baa86 100644
--- a/server/bot-box/broker.test.ts
+++ b/server/bot-box/broker.test.ts
@@ -161,5 +161,12 @@ describe("the upstream address", () => {
expect(upstreamUrl("https://api.example", "/v1/messages", "")?.href).toBe("https://api.example/v1/messages");
expect(upstreamUrl("https://hq.example/mcp?key=abc", "", "?session=2")?.href).toBe("https://hq.example/mcp?key=abc&session=2");
expect(upstreamUrl("not a url", "", "")).toBeNull();
+ // A client path may not climb out of the grant's own prefix; the URL parser resolves both spellings of `..`.
+ expect(upstreamUrl("https://api.example/v1", "/../../organization/admins", "")).toBeNull();
+ expect(upstreamUrl("https://api.example/v1", "/%2e%2e/%2e%2e/organization/admins", "")).toBeNull();
+ expect(upstreamUrl("https://api.example/v1", "/models", "")?.href).toBe("https://api.example/v1/models");
+ expect(upstreamUrl("https://api.example/v1", "/", "")?.href).toBe("https://api.example/v1/");
+ // A grant that names only a host still reaches that host's paths.
+ expect(upstreamUrl("https://api.example", "/anything", "")?.href).toBe("https://api.example/anything");
});
});
diff --git a/server/bot-box/broker.ts b/server/bot-box/broker.ts
index 4de9b2492f..5c1ec06608 100644
--- a/server/bot-box/broker.ts
+++ b/server/bot-box/broker.ts
@@ -116,7 +116,14 @@ export function brokerHeaderUrl(port: number, grant: string): string {
export function upstreamUrl(upstream: string, path: string, query: string): URL | null {
let url: URL;
try { url = new URL(upstream); } catch { return null; }
- if (path) url.pathname = `${url.pathname.replace(/\/+$/, "")}${path}`;
+ const base = url.pathname.replace(/\/+$/, "");
+ if (path) {
+ url.pathname = `${base}${path}`;
+ // Security: assigning a pathname resolves `.` and `..` (and their `%2e` spellings), so without this a client
+ // path climbs out of the grant's own prefix and reaches a sibling endpoint on the same host, carrying the
+ // grant's credential. A grant names one upstream, not a host.
+ if (url.pathname !== base && !url.pathname.startsWith(`${base}/`)) return null;
+ }
if (query.length > 1) url.search = url.search ? `${url.search}&${query.slice(1)}` : query;
return url;
}