Skip to content

Commit b0526f7

Browse files
fix(buzz): bound relay information responses (#119182)
Reject malformed relay metadata and cancel oversized response streams at the existing 16 MiB provider boundary. Co-authored-by: Peter Steinberger <[email protected]>
1 parent 574f753 commit b0526f7

9 files changed

Lines changed: 76 additions & 33 deletions

‎config/assertion-safety-baseline.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ extensions/buzz/src/gateway.ts 1
133133
extensions/buzz/src/inbound.ts 1
134134
extensions/buzz/src/message-event.ts 2
135135
extensions/buzz/src/qa/adapter.runtime.ts 2
136-
extensions/buzz/src/relay-auth.ts 1
137136
extensions/buzz/src/room-membership.ts 2
138137
extensions/buzz/src/setup-core.ts 1
139138
extensions/buzz/src/setup-surface.ts 1

‎extensions/buzz/src/buzz-bus.archived-rooms.test.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,12 @@ describe("Buzz archived room lifecycle", () => {
109109
relayMocks.roomMetadataEvents = [];
110110
vi.stubGlobal(
111111
"fetch",
112-
vi.fn(async () => ({
113-
ok: true,
114-
json: async () => ({
112+
vi.fn(async () =>
113+
Response.json({
115114
self: RELAY_PUBLIC_KEY,
116115
software: "https://github.com/block/buzz",
117116
}),
118-
})),
117+
),
119118
);
120119
});
121120

‎extensions/buzz/src/buzz-bus.history-catchup.test.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,12 @@ describe("Buzz reconnect history catch-up", () => {
204204
relayMocks.connected = true;
205205
vi.stubGlobal(
206206
"fetch",
207-
vi.fn(async () => ({
208-
ok: true,
209-
json: async () => ({
207+
vi.fn(async () =>
208+
Response.json({
210209
self: RELAY_PUBLIC_KEY,
211210
software: "https://github.com/block/buzz",
212211
}),
213-
})),
212+
),
214213
);
215214
});
216215

‎extensions/buzz/src/buzz-bus.lifecycle.test.ts‎

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ const BOT_PUBLIC_KEY = getPublicKey(Uint8Array.from(Buffer.from(PRIVATE_KEY, "he
127127
const SENDER_PUBLIC_KEY = getPublicKey(Uint8Array.from(Buffer.from(SENDER_PRIVATE_KEY, "hex")));
128128
const SENDER_SECRET_KEY = Uint8Array.from(Buffer.from(SENDER_PRIVATE_KEY, "hex"));
129129
const RELAY_PUBLIC_KEY = "f".repeat(64);
130+
const BUZZ_RELAY_INFO_MAX_BYTES = 16 * 1024 * 1024;
130131
const tempDirs = new Set<string>();
131132
let previousStateDir: string | undefined;
132133
let stateDir: string;
@@ -209,13 +210,12 @@ describe("Buzz bus lifecycle", () => {
209210
relayMocks.stallRoomEoseChannelId = undefined;
210211
vi.stubGlobal(
211212
"fetch",
212-
vi.fn(async () => ({
213-
ok: true,
214-
json: async () => ({
213+
vi.fn(async () =>
214+
Response.json({
215215
self: RELAY_PUBLIC_KEY,
216216
software: "https://github.com/block/buzz",
217217
}),
218-
})),
218+
),
219219
);
220220
});
221221

@@ -291,6 +291,58 @@ describe("Buzz bus lifecycle", () => {
291291
vi.useRealTimers();
292292
});
293293

294+
it("cancels oversized NIP-11 relay information before consuming the entire response", async () => {
295+
relayMocks.auth.mockResolvedValue("ok");
296+
const cancel = vi.fn();
297+
const chunk = new Uint8Array(1024 * 1024).fill("x".charCodeAt(0));
298+
let emittedChunks = 0;
299+
const body = new ReadableStream<Uint8Array>({
300+
pull(controller) {
301+
if (emittedChunks === 0) {
302+
controller.enqueue(
303+
new TextEncoder().encode(`{"self":"${RELAY_PUBLIC_KEY}","description":"`),
304+
);
305+
} else if (emittedChunks <= 17) {
306+
controller.enqueue(chunk);
307+
} else {
308+
controller.enqueue(new TextEncoder().encode('"}'));
309+
controller.close();
310+
}
311+
emittedChunks += 1;
312+
},
313+
cancel,
314+
});
315+
vi.stubGlobal(
316+
"fetch",
317+
vi.fn<typeof fetch>(async () => new Response(body)),
318+
);
319+
320+
await expect(startTestBus()).rejects.toThrow(
321+
`Buzz relay information: JSON response exceeds ${BUZZ_RELAY_INFO_MAX_BYTES} bytes`,
322+
);
323+
324+
expect(cancel).toHaveBeenCalledOnce();
325+
expect(emittedChunks).toBeLessThan(19);
326+
expect(relayMocks.close).toHaveBeenCalledOnce();
327+
});
328+
329+
it.each([
330+
["truncated JSON", '{"self":'],
331+
["null", "null"],
332+
["an array", "[]"],
333+
["a primitive", "true"],
334+
])("rejects malformed NIP-11 relay information containing %s", async (_label, body) => {
335+
relayMocks.auth.mockResolvedValue("ok");
336+
vi.stubGlobal(
337+
"fetch",
338+
vi.fn<typeof fetch>(async () => new Response(body)),
339+
);
340+
341+
await expect(startTestBus()).rejects.toThrow("Buzz relay information: malformed JSON response");
342+
343+
expect(relayMocks.close).toHaveBeenCalledOnce();
344+
});
345+
294346
it("publishes and closes a standalone authenticated send", async () => {
295347
relayMocks.auth.mockResolvedValue("ok");
296348

‎extensions/buzz/src/buzz-bus.mentions.test.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,12 @@ describe("Buzz mention delivery", () => {
118118
relayMocks.send.mockResolvedValue();
119119
vi.stubGlobal(
120120
"fetch",
121-
vi.fn(async () => ({
122-
ok: true,
123-
json: async () => ({
121+
vi.fn(async () =>
122+
Response.json({
124123
self: RELAY_PUBLIC_KEY,
125124
software: "https://github.com/block/buzz",
126125
}),
127-
})),
126+
),
128127
);
129128
});
130129

‎extensions/buzz/src/directory.test.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,12 @@ describe("Buzz live directory", () => {
8888
gatewayMocks.activeBus = undefined;
8989
vi.stubGlobal(
9090
"fetch",
91-
vi.fn(async () => ({
92-
ok: true,
93-
json: async () => ({
91+
vi.fn(async () =>
92+
Response.json({
9493
self: RELAY_PUBLIC_KEY,
9594
software: "https://github.com/block/buzz",
9695
}),
97-
})),
96+
),
9897
);
9998
relayMocks.subscribe.mockImplementation(
10099
(

‎extensions/buzz/src/qa/relay-client.test.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,12 @@ describe("Buzz QA relay driver", () => {
8989
relayMocks.replayedMessage = undefined;
9090
vi.stubGlobal(
9191
"fetch",
92-
vi.fn(async () => ({
93-
ok: true,
94-
json: async () => ({
92+
vi.fn(async () =>
93+
Response.json({
9594
self: RELAY_PUBLIC_KEY,
9695
software: "https://github.com/block/buzz",
9796
}),
98-
})),
97+
),
9998
);
10099
});
101100

‎extensions/buzz/src/relay-auth.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type EventTemplate, finalizeEvent, Relay, type VerifiedEvent } from "nostr-tools";
2+
import { readProviderJsonObjectResponse } from "openclaw/plugin-sdk/provider-http";
23
import {
34
fetchWithSsrFGuard,
45
ssrfPolicyFromHttpBaseUrlAllowedOrigin,
@@ -91,10 +92,7 @@ async function resolveBuzzRelayPublicKey(params: {
9192
await response.body?.cancel().catch(() => undefined);
9293
throw new Error(`Buzz relay information request failed with HTTP ${response.status}`);
9394
}
94-
const document = (await response.json()) as {
95-
self?: unknown;
96-
software?: unknown;
97-
};
95+
const document = await readProviderJsonObjectResponse(response, "Buzz relay information");
9896
const relayPublicKey =
9997
typeof document.self === "string" ? document.self.trim().toLowerCase() : "";
10098
if (HEX_PUBLIC_KEY_PATTERN.test(relayPublicKey)) {

‎extensions/buzz/src/room-discovery.test.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,12 @@ describe("discoverBuzzRooms", () => {
6868
relayMocks.subscribe.mockReset();
6969
vi.stubGlobal(
7070
"fetch",
71-
vi.fn(async () => ({
72-
ok: true,
73-
json: async () => ({
71+
vi.fn(async () =>
72+
Response.json({
7473
self: RELAY_PUBLIC_KEY,
7574
software: "https://github.com/block/buzz",
7675
}),
77-
})),
76+
),
7877
);
7978
});
8079

0 commit comments

Comments
 (0)