Skip to content

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

Teammate calls: refuse resolved addresses with the egress address classes

Bugfixer 2914aca83d10d
diff --git a/server/remote-teammates.test.ts b/server/remote-teammates.test.ts
index b48b0f6db2..8f0daa9de5 100644
--- a/server/remote-teammates.test.ts
+++ b/server/remote-teammates.test.ts
@@ -8,7 +8,7 @@ import { afterEach, describe, expect, it } from "vitest";
import { BotApiManager } from "./bot-api.ts";
import { createBotApiHost, type BotApiHostDeps } from "./bot-api-host.ts";
import { json, readBody } from "./harness/http.ts";
-import { checkRemote, linkLocalAddress, linkLocalRefusingLookup, readRemote, remoteAddress, RemoteTeammates, sendToRemote, type RemoteTeammate } from "./remote-teammates.ts";
+import { checkRemote, notAServerAddress, serverOnlyLookup, readRemote, remoteAddress, RemoteTeammates, sendToRemote, type RemoteTeammate } from "./remote-teammates.ts";
import type { RequestAuth } from "./request-auth.ts";
import { createRemoteTeammateRoutes } from "./routes/remote-teammates.ts";
import { dispatchRoutes } from "./routes/table.ts";
@@ -189,22 +189,22 @@ describe("calling a teammate's Bot API", () => {
expect((result as { detail?: string }).detail).toMatch(/ECONNREFUSED/);
});
- it("never connects to a name that resolves to a link-local or metadata address", async () => {
- for (const address of ["169.254.169.254", "fe80::1", "fd00:ec2::254", "::ffff:169.254.169.254", "64:ff9b::a9fe:a9fe", "::a9fe:a9fe"]) expect(linkLocalAddress(address)).toBe(true);
- for (const address of ["127.0.0.1", "100.64.1.2", "192.168.1.5", "::1", "not an address"]) expect(linkLocalAddress(address)).toBe(false);
+ it("never connects to a name that resolves to a link-local, metadata or reserved address", async () => {
+ for (const address of ["169.254.169.254", "fe80::1", "fd00:ec2::254", "::ffff:169.254.169.254", "64:ff9b::a9fe:a9fe", "::a9fe:a9fe", "100.100.100.200", "fe80::1%eth0"]) expect(notAServerAddress(address)).toBe(true);
+ for (const address of ["127.0.0.1", "100.64.1.2", "192.168.1.5", "::1", "not an address"]) expect(notAServerAddress(address)).toBe(false);
const lookupWith = (answer: Array<{ address: string; family: number }>) => {
const resolve = ((_host: string, options: { all?: boolean }, callback: (...args: unknown[]) => void) => {
if (options.all) callback(null, answer);
else callback(null, answer[0]!.address, answer[0]!.family);
}) as unknown as LookupFunction;
- const lookup = linkLocalRefusingLookup(resolve);
+ const lookup = serverOnlyLookup(resolve);
return (all: boolean) => new Promise<{ error: NodeJS.ErrnoException | null; address: unknown }>((done) => {
lookup("far.example.com", { all }, (error, address) => done({ error, address }));
});
};
const metadata = await lookupWith([{ address: "169.254.169.254", family: 4 }])(false);
- expect(metadata.error).toMatchObject({ code: "ELINKLOCAL", message: "far.example.com resolves to 169.254.169.254, a link-local address, not another server" });
- expect((await lookupWith([{ address: "100.64.1.2", family: 4 }, { address: "fe80::1", family: 6 }])(true)).error).toMatchObject({ code: "ELINKLOCAL" });
+ expect(metadata.error).toMatchObject({ code: "ENOTSERVER", message: "far.example.com resolves to 169.254.169.254, a link-local, metadata or reserved address, not another server" });
+ expect((await lookupWith([{ address: "100.64.1.2", family: 4 }, { address: "fe80::1", family: 6 }])(true)).error).toMatchObject({ code: "ENOTSERVER" });
expect(await lookupWith([{ address: "100.64.1.2", family: 4 }])(false)).toEqual({ error: null, address: "100.64.1.2" });
});
diff --git a/server/remote-teammates.ts b/server/remote-teammates.ts
index 14a9bb5755..fc4add20ad 100644
--- a/server/remote-teammates.ts
+++ b/server/remote-teammates.ts
@@ -9,6 +9,7 @@ import { dirname, join } from "node:path";
import type { Agent } from "undici";
import { z } from "zod";
+import { addressClass, type AddressClass } from "../electron/egress-addresses.mjs";
import { allowedServerOrigin } from "../electron/server-address.mjs";
import { botApiEndpoint, type BotApiReply } from "../shared/bot-api.ts";
import { writeFileAtomic } from "./atomic.ts";
@@ -32,36 +33,32 @@ const MAX_BASE_CHARS = 300;
/** Link-local hosts (169.254/16, fe80::/10) and the cloud metadata service: never another OpenMausBot server, and a key
* must not open a way from this server to them. */
const LINK_LOCAL = /^(?:169\.254\.|\[fe[89ab][0-9a-f]:|\[::ffff:a9fe:|\[fd00:ec2::254\]$)/i;
-/** The same ranges as addresses, for what a name resolves to when a call connects. */
-const LINK_LOCAL_ADDRESSES = new BlockList();
-LINK_LOCAL_ADDRESSES.addSubnet("169.254.0.0", 16, "ipv4");
-LINK_LOCAL_ADDRESSES.addSubnet("fe80::", 10, "ipv6");
-LINK_LOCAL_ADDRESSES.addAddress("fd00:ec2::254", "ipv6");
-// 169.254/16 inside IPv6: NAT64's 64:ff9b::/96 and the old IPv4-compatible ::/96.
-LINK_LOCAL_ADDRESSES.addSubnet("64:ff9b::a9fe:0", 112, "ipv6");
-LINK_LOCAL_ADDRESSES.addSubnet("::a9fe:0", 112, "ipv6");
-
-export function linkLocalAddress(address: string): boolean {
- const family = isIP(address);
- return family !== 0 && LINK_LOCAL_ADDRESSES.check(address, family === 6 ? "ipv6" : "ipv4");
+/** No other server answers on these, whatever a name resolves to. */
+const NOT_A_SERVER = new Set<AddressClass>(["link-local", "metadata", "reserved"]);
+/** This server's own addresses count as any other: a teammate may run on this computer. */
+const NONE_OWN = new BlockList();
+
+/** Whether a resolved address is link-local, a cloud metadata service or reserved (also inside NAT64 or 6to4). */
+export function notAServerAddress(address: string): boolean {
+ return isIP(address.split("%", 1)[0]!) !== 0 && NOT_A_SERVER.has(addressClass(address, NONE_OWN));
}
-/** Resolves a name as the system does, then refuses it when any address it gives is link-local or the metadata service,
+/** Resolves a name as the system does, then refuses it when any address it gives is link-local, metadata or reserved,
* so a DNS name can't carry a teammate key where a typed address may not go. Runs for every connection a call makes. */
-export function linkLocalRefusingLookup(resolve: LookupFunction = dnsLookup as LookupFunction): LookupFunction {
+export function serverOnlyLookup(resolve: LookupFunction = dnsLookup as LookupFunction): LookupFunction {
return (hostname, options, callback) => resolve(hostname, options, (error, address, family) => {
const found = typeof address === "string" ? [address] : (address ?? []).map((entry) => entry.address);
- const refused = error ? undefined : found.find(linkLocalAddress);
+ const refused = error ? undefined : found.find(notAServerAddress);
if (refused === undefined) return (callback as (...args: unknown[]) => void)(error, address, family);
- callback(Object.assign(new Error(`${hostname} resolves to ${refused}, a link-local address, not another server`), { code: "ELINKLOCAL" }), "", 0);
+ callback(Object.assign(new Error(`${hostname} resolves to ${refused}, a link-local, metadata or reserved address, not another server`), { code: "ENOTSERVER" }), "", 0);
});
}
let teammateAgent: Agent | undefined;
-/** fetch whose every connection goes through {@link linkLocalRefusingLookup}. */
+/** fetch whose every connection goes through {@link serverOnlyLookup}. */
async function teammateFetch(url: string, init: RequestInit): Promise<Response> {
const { Agent: UndiciAgent, fetch: undiciFetch } = await import("undici");
- teammateAgent ??= new UndiciAgent({ connect: { lookup: linkLocalRefusingLookup() } });
+ teammateAgent ??= new UndiciAgent({ connect: { lookup: serverOnlyLookup() } });
return undiciFetch(url, { ...init, dispatcher: teammateAgent } as Parameters<typeof undiciFetch>[1]) as unknown as Promise<Response>;
}