Skip to content

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

companion: malicious-relay suite against the real sidecar, WIP

milind-sonia7a21994f558
diff --git a/companion/test/e2e/fake-relay.ts b/companion/test/e2e/fake-relay.ts
index d08f25e9cf..eb44e3aba7 100644
--- a/companion/test/e2e/fake-relay.ts
+++ b/companion/test/e2e/fake-relay.ts
@@ -32,6 +32,8 @@ export interface FakeRelay {
closes: Array<{ kind: string; code: number }>;
/** Token refreshes accepted on a live control socket. */
refreshes: number;
+ /** Plays a malicious relay on every session spliced from now on; null passes bytes through. */
+ tamper: FakeRelayOptions["tamper"] | null;
admitted(hid: string): Set<string>;
restart(): void;
/** Drops every socket without a close frame, as a crashed process would. */
@@ -44,7 +46,7 @@ const toBuffer = (data: RawData): Buffer => (Array.isArray(data) ? Buffer.concat
export async function startFakeRelay(options: FakeRelayOptions): Promise<FakeRelay> {
const hosts = new Map<string, Host>();
- const relay = { refreshes: 0 } as FakeRelay;
+ const relay = { refreshes: 0, tamper: options.tamper ?? null } as FakeRelay;
const closes: FakeRelay["closes"] = [];
const server = createServer((_req, res) => res.writeHead(404).end());
const wss = new WebSocketServer({
@@ -65,12 +67,12 @@ export async function startFakeRelay(options: FakeRelayOptions): Promise<FakeRel
let toPhone = 0;
phone.on("message", (raw, isBinary) => {
if (!isBinary) return close(phone, "phone", 4400);
- const out = options.tamper ? options.tamper("toHost", toBuffer(raw), toHost++) : toBuffer(raw);
+ const out = relay.tamper ? relay.tamper("toHost", toBuffer(raw), toHost++) : toBuffer(raw);
if (out && data.readyState === WebSocket.OPEN) data.send(out);
});
data.on("message", (raw, isBinary) => {
if (!isBinary) return close(data, "data", 4400);
- const out = options.tamper ? options.tamper("toPhone", toBuffer(raw), toPhone++) : toBuffer(raw);
+ const out = relay.tamper ? relay.tamper("toPhone", toBuffer(raw), toPhone++) : toBuffer(raw);
if (out && phone.readyState === WebSocket.OPEN) phone.send(out);
});
const end = (other: WebSocket) => (code: number) => {
diff --git a/companion/test/e2e/sidecar.test.ts b/companion/test/e2e/sidecar.test.ts
index bbf7134a99..4fa42ae74b 100644
--- a/companion/test/e2e/sidecar.test.ts
+++ b/companion/test/e2e/sidecar.test.ts
@@ -10,7 +10,7 @@ import { fileURLToPath } from "node:url";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { admission, admissionHash, b64u, credentialHash, ed25519RawPublicKey, fromB64u, keyId, mintRelayToken, psk } from "../../src/e2e/credentials.ts";
-import { TRANSPORT_DIRECT, TRANSPORT_RELAY } from "../../src/e2e/noise.ts";
+import { generateKeyPair, NoiseError, TRANSPORT_DIRECT, TRANSPORT_RELAY } from "../../src/e2e/noise.ts";
import { dial } from "../../src/e2e/phone.ts";
import { SessionClosed, type E2ESession } from "../../src/e2e/session.ts";
import { ATTACHMENT_BYTES, attachmentByte, startFakeHarness, type FakeHarness } from "./fake-harness.ts";
@@ -315,6 +315,81 @@ describe("the sidecar's end-to-end connection", () => {
await waitFor(() => relay.admitted(hid).has(adm));
});
+ describe("against a malicious relay, every attack ends in a closed session and nothing delivered", () => {
+ const flip = (data: Buffer) => {
+ const out = Buffer.from(data);
+ out[out.length - 1] ^= 1;
+ return out;
+ };
+ /** One upload through a tampered relay: it must never reach the harness. */
+ async function attempt(tamper: NonNullable<FakeRelay["tamper"]>): Promise<"refused" | "delivered"> {
+ relay.tamper = tamper;
+ try {
+ const session = await viaRelay(await control("GET", "/state"), deviceToken);
+ const reply = await http(session, "POST", "/api/files", { token: deviceToken, body: randomBytes(300_000), headers: { "content-type": "application/octet-stream" } });
+ session.shutdown();
+ return reply.status === 201 ? "delivered" : "refused";
+ } catch {
+ return "refused";
+ } finally {
+ relay.tamper = null;
+ }
+ }
+
+ it("key swap: a message 2 the computer didn't write is a hard stop", async () => {
+ relay.tamper = (direction, data, index) =>
+ direction === "toPhone" && index === 0 ? Buffer.concat([generateKeyPair().publicKey, randomBytes(data.length - 65)]) : data;
+ await expect(viaRelay(await control("GET", "/state"), deviceToken)).rejects.toBeInstanceOf(NoiseError);
+ relay.tamper = null;
+ });
+
+ it("posing as the phone: a message 1 without the psk is refused with 4001", async () => {
+ const forged = (await import("../../src/e2e/noise.ts")).phoneHello(
+ { remoteStatic: transportKey(await control("GET", "/state")), psk: randomBytes(32), transport: TRANSPORT_RELAY },
+ Buffer.from(JSON.stringify({ v: 1, conn: b64u(randomBytes(16)) })),
+ "",
+ ).message1;
+ expect(await attempt((direction, data, index) => (direction === "toHost" && index === 0 ? forged : data))).toBe("refused");
+ });
+
+ it("replaying, dropping, truncating or flipping a record", async () => {
+ let previous: Buffer | null = null;
+ const replay: NonNullable<FakeRelay["tamper"]> = (direction, data, index) => {
+ if (direction !== "toHost" || index < 1) return data;
+ const out = index === 3 && previous ? previous : data;
+ previous = data;
+ return out;
+ };
+ expect(await attempt(replay)).toBe("refused");
+ expect(await attempt((direction, data, index) => (direction === "toHost" && index === 2 ? null : data))).toBe("refused");
+ expect(await attempt((direction, data, index) => (direction === "toHost" && index === 2 ? data.subarray(0, data.length - 1) : data))).toBe("refused");
+ expect(await attempt((direction, data, index) => (direction === "toHost" && index === 2 ? flip(data) : data))).toBe("refused");
+ });
+
+ it("reflection: the computer's own record sent back to it", async () => {
+ let fromComputer: Buffer | null = null;
+ let reflected = false;
+ expect(await attempt((direction, data, index) => {
+ if (direction === "toPhone" && index >= 1) fromComputer ??= data;
+ if (direction !== "toHost" || index < 2 || !fromComputer || reflected) return data;
+ reflected = true;
+ return fromComputer;
+ })).toBe("refused");
+ });
+
+ it("a stale message 2 from an earlier session", async () => {
+ let old: Buffer | null = null;
+ relay.tamper = (direction, data, index) => {
+ if (direction === "toPhone" && index === 0) old = data;
+ return data;
+ };
+ (await viaRelay(await control("GET", "/state"), deviceToken)).shutdown();
+ relay.tamper = (direction, data, index) => (direction === "toPhone" && index === 0 && old ? old : data);
+ await expect(viaRelay(await control("GET", "/state"), deviceToken)).rejects.toBeInstanceOf(NoiseError);
+ relay.tamper = null;
+ });
+ });
+
it("closes a removed device's sessions within a second, with 4001", async () => {
const state = await control("GET", "/state");
const session = await direct(state, deviceToken);