Skip to content

Commit f6b5cf6

Browse files
klopez4212codex
andcommitted
Merge main to include session placement recovery fix
Co-authored-by: Codex <[email protected]> Signed-off-by: klopez4212 <[email protected]>
2 parents 1951abc + cb3e437 commit f6b5cf6

195 files changed

Lines changed: 9208 additions & 3833 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Cargo.lock‎

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[workspace]
2-
members = ["crates/pairing", "crates/plugin-manager", "crates/agent-controller", "crates/credential-store", "crates/ifc-core", "crates/buzz-ifc", "src-tauri"]
2+
members = ["crates/pairing", "crates/plugin-manager", "crates/agent-controller", "crates/credential-store", "crates/ifc-core", "crates/buzz-ifc", "crates/nsec-backup", "src-tauri"]
33
default-members = ["crates/plugin-manager"]
44
resolver = "2"

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ controls are included. Keep Buzz running for existing agents to answer selected
342342
- [Contextual identity names: cross-client spec and fixtures](src/bundled/identity-naming/README.md)
343343
- [Mention rules: cross-client spec and fixtures](src/bundled/mentions/README.md)
344344
- [Contributing](docs/contributing.md)
345+
- [Buzz feedback community](https://feedback.communities.buzz.xyz/invite/v2.yC8ZZIz5vwWCZYo2RRheuY1a0IKZRTXvxTmK7vIFIIY)
346+
- [@buzzdotxyz on X](https://x.com/buzzdotxyz)
345347
- [Project leads](CODEOWNERS)
346348
- [Governance](GOVERNANCE.md)
347349
- [Apache-2.0 license](LICENSE)

‎browser-host/relay-broker-api.test.mjs‎

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getLogger, setLogLevel } from "../src/features/developer/logging.ts";
77
import { brokerSocket, openBrokerSocket } from "../tests/broker-socket.mjs";
88
import { fixtureRelayUrl, fixtureAliases } from "../tests/relay-config.ts";
99
import { createRelayReader } from "../src/features/relay/reader.ts";
10-
import { createServer, get } from "node:http";
10+
import { createServer, get, request } from "node:http";
1111
import { createHash, createHmac } from "node:crypto";
1212
import { schnorr } from "@noble/curves/secp256k1.js";
1313
import { ReadableStream } from "node:stream/web";
@@ -106,6 +106,7 @@ async function harness(
106106
const base = `http://127.0.0.1:${server.address().port}`;
107107
return {
108108
base,
109+
server,
109110
event,
110111
calls,
111112
publications: socket.publications,
@@ -1088,6 +1089,121 @@ test("foreground profile publication starts while background I/O is outstanding;
10881089
}
10891090
});
10901091

1092+
// Delivers bytes in two HTTP chunks, the second only after the broker has read the first.
1093+
async function postSplit(h, path, bytes, at) {
1094+
const read = new Promise((resolve) =>
1095+
h.server.once("request", (req) => resolve(vi.spyOn(req, "read"))),
1096+
);
1097+
const client = request(`${h.base}${path}`, {
1098+
method: "POST",
1099+
headers: { "Content-Type": "application/json" },
1100+
});
1101+
const response = new Promise((resolve, reject) =>
1102+
client.once("response", resolve).once("error", reject),
1103+
);
1104+
client.write(bytes.subarray(0, at));
1105+
const spy = await read;
1106+
await vi.waitFor(() =>
1107+
expect(spy.mock.results.some(({ value }) => value?.length)).toBe(true),
1108+
);
1109+
client.end(bytes.subarray(at));
1110+
const res = await response;
1111+
const chunks = [];
1112+
for await (const chunk of res) chunks.push(chunk);
1113+
return {
1114+
status: res.statusCode,
1115+
body: JSON.parse(Buffer.concat(chunks).toString("utf8")),
1116+
};
1117+
}
1118+
1119+
const profileName = "café €🛩";
1120+
const profileBody = Buffer.from(
1121+
JSON.stringify({ name: profileName, picture: "" }),
1122+
);
1123+
// Every interior byte of each 2-, 3- and 4-byte UTF-8 sequence.
1124+
const profileSplits = [...profileName]
1125+
.filter((char) => Buffer.byteLength(char) > 1)
1126+
.flatMap((char) =>
1127+
Array.from({ length: Buffer.byteLength(char) - 1 }, (_, byte) => [
1128+
char,
1129+
byte + 1,
1130+
profileBody.indexOf(char) + byte + 1,
1131+
]),
1132+
);
1133+
1134+
test.each(profileSplits)(
1135+
"signed profile text survives %s split after byte %i across HTTP chunks",
1136+
async (_char, _byte, at) => {
1137+
const h = await harness(success);
1138+
try {
1139+
const response = await postSplit(
1140+
h,
1141+
"/api/relay/profile",
1142+
profileBody,
1143+
at,
1144+
);
1145+
expect(response.status).toBe(200);
1146+
const [published] = h.calls.filter((call) =>
1147+
call.url.endsWith("/events"),
1148+
);
1149+
expect(published.body.kind).toBe(0);
1150+
expect(verifyEvent(published.body)).toBe(true);
1151+
expect(JSON.parse(published.body.content).name).toBe(profileName);
1152+
} finally {
1153+
await h.close();
1154+
}
1155+
},
1156+
);
1157+
1158+
test.each([
1159+
["/api/builderlab/delete", 4096, "chars", "Request too large"],
1160+
["/api/relay/register", 4096, "chars", "Relay URL too large"],
1161+
[
1162+
"/api/relay/prepare-remote-agent-authorization",
1163+
4096,
1164+
"bytes",
1165+
"Authorization request is too large",
1166+
],
1167+
["/api/relay/archive", 4096, "bytes", "Archive request too large"],
1168+
["/api/relay/stream-presence", 256, "bytes", "Live control too large"],
1169+
["/api/relay/stream", 300000, "bytes", "Live interests too large"],
1170+
["/api/relay/query", 65536, "chars", "Filter body too large"],
1171+
["/api/relay/presence-snapshot", 20 * 1024, "bytes", "Filter body too large"],
1172+
])(
1173+
"%s measures a split multibyte body exactly at its %i %s limit",
1174+
async (path, limit, unit, tooLarge) => {
1175+
const size = (body) =>
1176+
unit === "bytes" ? Buffer.byteLength(body) : body.length;
1177+
const padded = (extra) =>
1178+
Buffer.from(
1179+
JSON.stringify({
1180+
pad: `é${"a".repeat(limit - size(JSON.stringify({ pad: "é" })) + extra)}`,
1181+
}),
1182+
);
1183+
const at = padded(0).indexOf("é") + 1;
1184+
const h = await harness(success);
1185+
try {
1186+
const whole = await fetch(`${h.base}${path}`, {
1187+
method: "POST",
1188+
headers: { "Content-Type": "application/json" },
1189+
body: padded(0),
1190+
});
1191+
const split = await postSplit(h, path, padded(0), at);
1192+
expect(split.status).not.toBe(413);
1193+
expect(split).toEqual({
1194+
status: whole.status,
1195+
body: await whole.json(),
1196+
});
1197+
expect(await postSplit(h, path, padded(1), at)).toEqual({
1198+
status: 413,
1199+
body: { error: tooLarge },
1200+
});
1201+
} finally {
1202+
await h.close();
1203+
}
1204+
},
1205+
);
1206+
10911207
test("local capacity is explicitly unsent, not relay quota; unknown upstream publication is never resent", async () => {
10921208
let release;
10931209
const held = new Promise((resolve) => {
@@ -1211,6 +1327,29 @@ test("each request mints fresh auth at dispatch after wall time advances", async
12111327
}
12121328
});
12131329

1330+
test("query keeps relay 403 refusals but turns an edge 403 page into a gateway failure", async () => {
1331+
let edge = false;
1332+
const h = await harness(() =>
1333+
edge
1334+
? new Response("<html><body>Forbidden</body></html>", {
1335+
status: 403,
1336+
headers: { "Content-Type": "text/html" },
1337+
})
1338+
: Response.json({ error: "restricted: not a member" }, { status: 403 }),
1339+
);
1340+
try {
1341+
const refused = await h.post("query", filters);
1342+
expect(refused.status).toBe(403);
1343+
expect(await refused.json()).toMatchObject({ denied: true });
1344+
edge = true;
1345+
const page = await h.post("query", filters);
1346+
expect(page.status).toBe(502);
1347+
expect(await page.json()).toEqual({ error: "Relay request failed (502)" });
1348+
} finally {
1349+
await h.close();
1350+
}
1351+
});
1352+
12141353
test("reaction sign and publish preserve kind 7 and reject malformed targets before upstream I/O", async () => {
12151354
const h = await harness((call) =>
12161355
Response.json({ accepted: true, event_id: call.body.id }),
@@ -2208,7 +2347,7 @@ test("memory reads use captured relay and owner, not submitted identity/filter a
22082347
]);
22092348
expect(call.auth.pubkey).toBe(viewer);
22102349
return forbidden
2211-
? new Response("denied", { status: 403 })
2350+
? Response.json({ error: "restricted: not the owner" }, { status: 403 })
22122351
: Response.json([memory]);
22132352
});
22142353
try {

‎browser-host/relay-broker.mjs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ async function relayAuthority(fetch, relay) {
307307
relayAuthor: author,
308308
channelCreation:
309309
Array.isArray(nip11.supported_nips) && nip11.supported_nips.includes(29),
310+
// NIP-CL: the relay keeps channel labels and answers label lookups.
311+
channelIdentity:
312+
Array.isArray(nip11.supported_extensions) &&
313+
nip11.supported_extensions.includes("nip-cl"),
310314
...(readSnapshotCommunity(nip11.read_state_snapshot)
311315
? { readStateCommunity: readSnapshotCommunity(nip11.read_state_snapshot) }
312316
: {}),
@@ -739,6 +743,8 @@ export function relayBrokerPlugin({
739743
agentLibrary = readAgentLibrary,
740744
builderlab: builderlabOptions = {},
741745
archiveFile,
746+
// Tests that hold EOSE on purpose lengthen this real-time deadline.
747+
liveSetupTimeoutMs,
742748
} = {}) {
743749
const aliases = parseCommunityAliases(communityAliases);
744750
const defaultRelay = relayUrl?.trim() ? relayOrigin(relayUrl) : undefined;
@@ -900,6 +906,7 @@ export function relayBrokerPlugin({
900906
if (req.method !== "POST")
901907
return json(res, 404, { error: "Unknown Builderlab route" });
902908
let raw = "";
909+
req.setEncoding("utf8");
903910
for await (const part of req) {
904911
raw += part;
905912
if (raw.length > 4096)
@@ -931,6 +938,7 @@ export function relayBrokerPlugin({
931938
}
932939
if (url.pathname === "/api/relay/register" && req.method === "POST") {
933940
let raw = "";
941+
req.setEncoding("utf8");
934942
for await (const part of req) {
935943
raw += part;
936944
if (raw.length > 4096)
@@ -959,6 +967,7 @@ export function relayBrokerPlugin({
959967
req.method === "POST"
960968
) {
961969
let raw = "";
970+
req.setEncoding("utf8");
962971
for await (const part of req) {
963972
raw += part;
964973
if (Buffer.byteLength(raw) > 4096)
@@ -1320,6 +1329,7 @@ export function relayBrokerPlugin({
13201329
}
13211330
if (route === "/api/relay/archive" && req.method === "POST") {
13221331
let raw = "";
1332+
req.setEncoding("utf8");
13231333
for await (const part of req) {
13241334
raw += part;
13251335
if (Buffer.byteLength(raw) > 4096)
@@ -1409,6 +1419,7 @@ export function relayBrokerPlugin({
14091419
channelActivity: true,
14101420
sidebarMuteWrites: true,
14111421
channelKit: true,
1422+
channelIdentity: (await getAuthority(relay)).channelIdentity,
14121423
readState: true,
14131424
sidebarPreferenceWrites: true,
14141425
sidebarSectionRemoval: true,
@@ -1440,6 +1451,7 @@ export function relayBrokerPlugin({
14401451
const observing = route === "/api/relay/stream-observer";
14411452
const updating = route === "/api/relay/stream-interests";
14421453
let raw = "";
1454+
req.setEncoding("utf8");
14431455
for await (const part of req) {
14441456
raw += part;
14451457
if (
@@ -1559,6 +1571,7 @@ export function relayBrokerPlugin({
15591571
}
15601572
if (route === "/api/relay/stream" && req.method === "POST") {
15611573
let raw = "";
1574+
req.setEncoding("utf8");
15621575
for await (const part of req) {
15631576
raw += part;
15641577
if (Buffer.byteLength(raw) > 300000)
@@ -1738,6 +1751,8 @@ export function relayBrokerPlugin({
17381751
},
17391752
socketFactory,
17401753
principal.live,
1754+
undefined,
1755+
liveSetupTimeoutMs,
17411756
);
17421757
principal.streams++;
17431758
traffic.observe(observer);
@@ -1977,6 +1992,7 @@ export function relayBrokerPlugin({
19771992
? setTimeout(() => req.destroy(), 10000)
19781993
: undefined;
19791994
try {
1995+
req.setEncoding("utf8");
19801996
for await (const part of req) {
19811997
raw += part;
19821998
if (

‎browser-host/session-commands.mjs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { sessionMetadata } from "../src/features/sessions/metadata.ts";
2+
import { validChannelLabel } from "../src/features/relay/channel-labels.ts";
23
// Host signing allowlist. No arbitrary kinds, roles or metadata edits.
34
const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
45
export function validChannelCommand(event) {
@@ -26,6 +27,19 @@ export function validChannelCommand(event) {
2627
if (h?.length !== 2 || h[0] !== "h" || !uuid.test(h[1])) return false;
2728
if (event.kind === 9007) {
2829
const expected = ["h", "name", "visibility", "channel_type"];
30+
// NIP-CL: up to 8 distinct label `t` tags end the command.
31+
const firstLabel = tags.findIndex(([name]) => name === "t");
32+
const labels = firstLabel < 0 ? [] : tags.slice(firstLabel);
33+
if (
34+
labels.length > 8 ||
35+
labels.some(
36+
(tag) =>
37+
tag.length !== 2 || tag[0] !== "t" || !validChannelLabel(tag[1]),
38+
) ||
39+
new Set(labels.map(([, label]) => label)).size !== labels.length
40+
)
41+
return false;
42+
if (labels.length) tags = tags.slice(0, firstLabel);
2943
const optional = tags.slice(expected.length);
3044
const optionalNames = optional.map(([name]) => name);
3145
const validOptionalOrder = [[], ["about"], ["ttl"], ["about", "ttl"]].some(

‎browser-host/session-commands.test.mjs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,44 @@ it("rejects plugin placement metadata in channel creation", () => {
238238
}),
239239
).toBe(false);
240240
});
241+
242+
it("admits NIP-CL labels at the end of a channel creation, before the client id", () => {
243+
const create = (labels) => ({
244+
kind: 9007,
245+
created_at: 1,
246+
content: "",
247+
tags: [
248+
["h", id],
249+
["name", "Records"],
250+
["visibility", "private"],
251+
["channel_type", "stream"],
252+
["about", "For You records"],
253+
...labels,
254+
["client-id", "22222222-2222-4222-8222-222222222222"],
255+
],
256+
});
257+
expect(validChannelCommand(create([]))).toBe(true);
258+
expect(
259+
validChannelCommand(
260+
create([
261+
["t", "for-you"],
262+
["t", "app:records"],
263+
]),
264+
),
265+
).toBe(true);
266+
for (const labels of [
267+
[["t", "system"]],
268+
[["t", "Upper"]],
269+
[["t", ""]],
270+
[
271+
["t", "a"],
272+
["t", "a"],
273+
],
274+
[
275+
["t", "a"],
276+
["about", "late"],
277+
],
278+
Array.from({ length: 9 }, (_, n) => ["t", `l${n}`]),
279+
])
280+
expect(validChannelCommand(create(labels))).toBe(false);
281+
});

0 commit comments

Comments
 (0)