Skip to content

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

fix(relay): match the page origin after parsing; guard catches returned fetch

Bugfixer 292647cc69dbe
diff --git a/src/lib/relay-server-callers.test.ts b/src/lib/relay-server-callers.test.ts
index 97f6befec5..40f5df51b5 100644
--- a/src/lib/relay-server-callers.test.ts
+++ b/src/lib/relay-server-callers.test.ts
@@ -16,8 +16,8 @@ function appFiles(): string[] {
}
/** A call to fetch, whitespace or a line break before its `(` too, or fetch handed on as a value (`fetchImpl = fetch`,
- * `run(fetch)`, `impl ?? fetch`, `custom || fetch`); not `.fetch(` or a `fetch:` key. */
-const BARE_FETCH = /(?<![\w$.])fetch\s*\(|(?:[=(,:]|\?\?|\|\||&&)\s*fetch\b(?![\w$.(]|\s*[:(])/g;
+ * `run(fetch)`, `impl ?? fetch`, `custom || fetch`, `return fetch`, `() => fetch`); not `.fetch(` or a `fetch:` key. */
+const BARE_FETCH = /(?<![\w$.])fetch\s*\(|(?:[=(,:]|=>|\?\?|\|\||&&|\breturn)\s*fetch\b(?![\w$.(]|\s*[:(])/g;
/** The 1-based lines of `source` with a bare fetch, `//` comments left out. */
function bareFetchLines(source: string): number[] {
@@ -38,6 +38,7 @@ describe("app requests to its own server", () => {
expect(bareFetchLines('const a = 1;\nawait fetch\n ("/api/bots");')).toEqual([2]);
expect(bareFetchLines("const run = fetch;\nuse(fetch)")).toEqual([1, 2]);
expect(bareFetchLines('const impl = options.fetchImpl ?? fetch;\nconst alt = custom || fetch;')).toEqual([1, 2]);
+ expect(bareFetchLines("function impl() { return fetch; }\nconst get = () => fetch;")).toEqual([1, 2]);
expect(bareFetchLines('link.fetch("/api/bots");\nconst x = { fetch: go };\n// fetch("/api/bots")\nserverFetch("/api/bots");')).toEqual([]);
expect(bareFetchLines("const UNREACHABLE = /Load failed|fetch failed$/i;")).toEqual([]);
});
diff --git a/src/lib/relay-server.test.ts b/src/lib/relay-server.test.ts
index bda70c739c..d139ae1a36 100644
--- a/src/lib/relay-server.test.ts
+++ b/src/lib/relay-server.test.ts
@@ -124,9 +124,10 @@ describe("serverFetch", () => {
const { link, sent } = fakeLink(() => new Response(null, { status: 204 }));
setRelayLink(link);
await serverFetch("https://app.example/api/bots/b1/browser/upload?chooser=c1&token=t", { method: "POST", body: new FormData() });
+ await serverFetch("https://APP.example:443/api/bots");
await serverFetch("https://other.example/api/bots");
await serverFetch("https://app.example/apiary");
- expect(sent.map((one) => one.path)).toEqual(["/api/bots/b1/browser/upload?chooser=c1&token=t"]);
+ expect(sent.map((one) => one.path)).toEqual(["/api/bots/b1/browser/upload?chooser=c1&token=t", "/api/bots"]);
expect(plain.mock.calls.map(([path]) => path)).toEqual(["https://other.example/api/bots", "https://app.example/apiary"]);
});
diff --git a/src/lib/relay-server.ts b/src/lib/relay-server.ts
index 5d0052b35e..a3da346d68 100644
--- a/src/lib/relay-server.ts
+++ b/src/lib/relay-server.ts
@@ -40,8 +40,14 @@ const API = /^\/(?:api(?:[/?#]|$)|\.well-known\/openmausbot\/environment(?:[?#]|
function ownApiPath(path: string): string | null {
if (API.test(path)) return path;
const origin = (globalThis as { location?: { origin?: string } }).location?.origin;
- if (!origin || !path.startsWith(`${origin}/`)) return null;
- const url = new URL(path);
+ if (!origin) return null;
+ // Parsed, so the same origin written another way (an explicit default port, upper case) still counts.
+ let url: URL;
+ try {
+ url = new URL(path);
+ } catch {
+ return null;
+ }
return url.origin === origin && API.test(url.pathname) ? url.pathname + url.search : null;
}