@openclaw/tlon 2026.5.2 → 2026.5.3-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/dist/api.js +4 -0
  2. package/dist/channel-B4LtNGeZ.js +221 -0
  3. package/dist/channel-plugin-api.js +2 -0
  4. package/dist/channel.runtime-v-_0sHkJ.js +3584 -0
  5. package/dist/doctor-contract-W99wMbar.js +7 -0
  6. package/dist/doctor-contract-api.js +2 -0
  7. package/dist/index.js +18 -0
  8. package/dist/runtime-5FiW9E50.js +8 -0
  9. package/dist/runtime-api-C0hr1Rtn.js +4 -0
  10. package/dist/runtime-api.js +2 -0
  11. package/dist/setup-api.js +3 -0
  12. package/dist/setup-core-BUkZJ-MU.js +386 -0
  13. package/dist/setup-entry.js +11 -0
  14. package/dist/setup-surface-FxNMaluC.js +72 -0
  15. package/dist/test-api.js +2 -0
  16. package/package.json +15 -6
  17. package/api.ts +0 -16
  18. package/channel-plugin-api.ts +0 -1
  19. package/doctor-contract-api.ts +0 -1
  20. package/index.ts +0 -183
  21. package/runtime-api.ts +0 -17
  22. package/setup-api.ts +0 -2
  23. package/setup-entry.ts +0 -9
  24. package/src/account-fields.ts +0 -31
  25. package/src/channel.runtime.ts +0 -248
  26. package/src/channel.ts +0 -173
  27. package/src/config-schema.ts +0 -53
  28. package/src/core.test.ts +0 -278
  29. package/src/doctor-contract.ts +0 -9
  30. package/src/doctor.test.ts +0 -40
  31. package/src/doctor.ts +0 -10
  32. package/src/logger-runtime.ts +0 -1
  33. package/src/monitor/approval-runtime.ts +0 -363
  34. package/src/monitor/approval.test.ts +0 -33
  35. package/src/monitor/approval.ts +0 -286
  36. package/src/monitor/authorization.ts +0 -30
  37. package/src/monitor/cites.ts +0 -54
  38. package/src/monitor/discovery.ts +0 -68
  39. package/src/monitor/history.ts +0 -226
  40. package/src/monitor/index.ts +0 -1524
  41. package/src/monitor/media.test.ts +0 -91
  42. package/src/monitor/media.ts +0 -172
  43. package/src/monitor/processed-messages.test.ts +0 -58
  44. package/src/monitor/processed-messages.ts +0 -89
  45. package/src/monitor/settings-helpers.test.ts +0 -113
  46. package/src/monitor/settings-helpers.ts +0 -158
  47. package/src/monitor/utils.ts +0 -372
  48. package/src/runtime.ts +0 -9
  49. package/src/security.test.ts +0 -622
  50. package/src/session-route.ts +0 -40
  51. package/src/settings.ts +0 -391
  52. package/src/setup-core.ts +0 -228
  53. package/src/setup-surface.ts +0 -103
  54. package/src/targets.ts +0 -102
  55. package/src/tlon-api.test.ts +0 -589
  56. package/src/tlon-api.ts +0 -402
  57. package/src/types.ts +0 -160
  58. package/src/urbit/auth.ssrf.test.ts +0 -45
  59. package/src/urbit/auth.ts +0 -48
  60. package/src/urbit/base-url.test.ts +0 -48
  61. package/src/urbit/base-url.ts +0 -61
  62. package/src/urbit/channel-ops.ts +0 -145
  63. package/src/urbit/context.ts +0 -50
  64. package/src/urbit/errors.ts +0 -51
  65. package/src/urbit/fetch.ts +0 -42
  66. package/src/urbit/foreigns.ts +0 -49
  67. package/src/urbit/send.test.ts +0 -38
  68. package/src/urbit/send.ts +0 -176
  69. package/src/urbit/sse-client.test.ts +0 -203
  70. package/src/urbit/sse-client.ts +0 -484
  71. package/src/urbit/story.ts +0 -332
  72. package/src/urbit/upload.test.ts +0 -154
  73. package/src/urbit/upload.ts +0 -60
  74. package/test-api.ts +0 -1
  75. package/tsconfig.json +0 -16
@@ -1,622 +0,0 @@
1
- /**
2
- * Security Tests for Tlon Plugin
3
- *
4
- * These tests ensure that security-critical behavior cannot regress:
5
- * - DM allowlist enforcement
6
- * - Channel authorization rules
7
- * - Ship normalization consistency
8
- * - Bot mention detection boundaries
9
- */
10
-
11
- import { describe, expect, it, vi } from "vitest";
12
- import {
13
- extractCites,
14
- isDmAllowed,
15
- isGroupInviteAllowed,
16
- isBotMentioned,
17
- extractMessageText,
18
- resolveAuthorizedMessageText,
19
- } from "./monitor/utils.js";
20
- import { normalizeShip } from "./targets.js";
21
-
22
- describe("Security: DM Allowlist", () => {
23
- describe("isDmAllowed", () => {
24
- it("rejects DMs when allowlist is empty", () => {
25
- expect(isDmAllowed("~zod", [])).toBe(false);
26
- expect(isDmAllowed("~sampel-palnet", [])).toBe(false);
27
- });
28
-
29
- it("rejects DMs when allowlist is undefined", () => {
30
- expect(isDmAllowed("~zod", undefined)).toBe(false);
31
- });
32
-
33
- it("allows DMs from ships on the allowlist", () => {
34
- const allowlist = ["~zod", "~bus"];
35
- expect(isDmAllowed("~zod", allowlist)).toBe(true);
36
- expect(isDmAllowed("~bus", allowlist)).toBe(true);
37
- });
38
-
39
- it("rejects DMs from ships NOT on the allowlist", () => {
40
- const allowlist = ["~zod", "~bus"];
41
- expect(isDmAllowed("~nec", allowlist)).toBe(false);
42
- expect(isDmAllowed("~sampel-palnet", allowlist)).toBe(false);
43
- expect(isDmAllowed("~random-ship", allowlist)).toBe(false);
44
- });
45
-
46
- it("normalizes ship names (with/without ~ prefix)", () => {
47
- const allowlist = ["~zod"];
48
- expect(isDmAllowed("zod", allowlist)).toBe(true);
49
- expect(isDmAllowed("~zod", allowlist)).toBe(true);
50
-
51
- const allowlistWithoutTilde = ["zod"];
52
- expect(isDmAllowed("~zod", allowlistWithoutTilde)).toBe(true);
53
- expect(isDmAllowed("zod", allowlistWithoutTilde)).toBe(true);
54
- });
55
-
56
- it("handles galaxy, star, planet, and moon names", () => {
57
- const allowlist = [
58
- "~zod", // galaxy
59
- "~marzod", // star
60
- "~sampel-palnet", // planet
61
- "~dozzod-dozzod-dozzod-dozzod", // moon
62
- ];
63
-
64
- expect(isDmAllowed("~zod", allowlist)).toBe(true);
65
- expect(isDmAllowed("~marzod", allowlist)).toBe(true);
66
- expect(isDmAllowed("~sampel-palnet", allowlist)).toBe(true);
67
- expect(isDmAllowed("~dozzod-dozzod-dozzod-dozzod", allowlist)).toBe(true);
68
-
69
- // Similar but different ships should be rejected
70
- expect(isDmAllowed("~nec", allowlist)).toBe(false);
71
- expect(isDmAllowed("~wanzod", allowlist)).toBe(false);
72
- expect(isDmAllowed("~sampel-palned", allowlist)).toBe(false);
73
- });
74
-
75
- // NOTE: Ship names in Urbit are always lowercase by convention.
76
- // This test documents current behavior - strict equality after normalization.
77
- // If case-insensitivity is desired, normalizeShip should lowercase.
78
- it("uses strict equality after normalization (case-sensitive)", () => {
79
- const allowlist = ["~zod"];
80
- expect(isDmAllowed("~zod", allowlist)).toBe(true);
81
- // Different case would NOT match with current implementation
82
- expect(isDmAllowed("~Zod", ["~Zod"])).toBe(true); // exact match works
83
- });
84
-
85
- it("does not allow partial matches", () => {
86
- const allowlist = ["~zod"];
87
- expect(isDmAllowed("~zod-extra", allowlist)).toBe(false);
88
- expect(isDmAllowed("~extra-zod", allowlist)).toBe(false);
89
- });
90
-
91
- it("handles whitespace in ship names (normalized)", () => {
92
- // Ships with leading/trailing whitespace are normalized by normalizeShip
93
- const allowlist = [" ~zod ", "~bus"];
94
- expect(isDmAllowed("~zod", allowlist)).toBe(true);
95
- expect(isDmAllowed(" ~zod ", allowlist)).toBe(true);
96
- });
97
- });
98
- });
99
-
100
- describe("Security: Group Invite Allowlist", () => {
101
- describe("isGroupInviteAllowed", () => {
102
- it("rejects invites when allowlist is empty (fail-safe)", () => {
103
- // CRITICAL: Empty allowlist must DENY, not accept-all
104
- expect(isGroupInviteAllowed("~zod", [])).toBe(false);
105
- expect(isGroupInviteAllowed("~sampel-palnet", [])).toBe(false);
106
- expect(isGroupInviteAllowed("~malicious-actor", [])).toBe(false);
107
- });
108
-
109
- it("rejects invites when allowlist is undefined (fail-safe)", () => {
110
- // CRITICAL: Undefined allowlist must DENY, not accept-all
111
- expect(isGroupInviteAllowed("~zod", undefined)).toBe(false);
112
- expect(isGroupInviteAllowed("~sampel-palnet", undefined)).toBe(false);
113
- });
114
-
115
- it("accepts invites from ships on the allowlist", () => {
116
- const allowlist = ["~nocsyx-lassul", "~malmur-halmex"];
117
- expect(isGroupInviteAllowed("~nocsyx-lassul", allowlist)).toBe(true);
118
- expect(isGroupInviteAllowed("~malmur-halmex", allowlist)).toBe(true);
119
- });
120
-
121
- it("rejects invites from ships NOT on the allowlist", () => {
122
- const allowlist = ["~nocsyx-lassul", "~malmur-halmex"];
123
- expect(isGroupInviteAllowed("~random-attacker", allowlist)).toBe(false);
124
- expect(isGroupInviteAllowed("~malicious-ship", allowlist)).toBe(false);
125
- expect(isGroupInviteAllowed("~zod", allowlist)).toBe(false);
126
- });
127
-
128
- it("normalizes ship names (with/without ~ prefix)", () => {
129
- const allowlist = ["~nocsyx-lassul"];
130
- expect(isGroupInviteAllowed("nocsyx-lassul", allowlist)).toBe(true);
131
- expect(isGroupInviteAllowed("~nocsyx-lassul", allowlist)).toBe(true);
132
-
133
- const allowlistWithoutTilde = ["nocsyx-lassul"];
134
- expect(isGroupInviteAllowed("~nocsyx-lassul", allowlistWithoutTilde)).toBe(true);
135
- });
136
-
137
- it("does not allow partial matches", () => {
138
- const allowlist = ["~zod"];
139
- expect(isGroupInviteAllowed("~zod-moon", allowlist)).toBe(false);
140
- expect(isGroupInviteAllowed("~pinser-botter-zod", allowlist)).toBe(false);
141
- });
142
-
143
- it("handles whitespace in allowlist entries", () => {
144
- const allowlist = [" ~nocsyx-lassul ", "~malmur-halmex"];
145
- expect(isGroupInviteAllowed("~nocsyx-lassul", allowlist)).toBe(true);
146
- });
147
- });
148
- });
149
-
150
- describe("Security: Bot Mention Detection", () => {
151
- describe("isBotMentioned", () => {
152
- const botShip = "~sampel-palnet";
153
- const nickname = "nimbus";
154
-
155
- it("detects direct ship mention", () => {
156
- expect(isBotMentioned("hey ~sampel-palnet", botShip)).toBe(true);
157
- expect(isBotMentioned("~sampel-palnet can you help?", botShip)).toBe(true);
158
- expect(isBotMentioned("hello ~sampel-palnet how are you", botShip)).toBe(true);
159
- });
160
-
161
- it("detects @all mention", () => {
162
- expect(isBotMentioned("@all please respond", botShip)).toBe(true);
163
- expect(isBotMentioned("hey @all", botShip)).toBe(true);
164
- expect(isBotMentioned("@ALL uppercase", botShip)).toBe(true);
165
- });
166
-
167
- it("detects nickname mention", () => {
168
- expect(isBotMentioned("hey nimbus", botShip, nickname)).toBe(true);
169
- expect(isBotMentioned("nimbus help me", botShip, nickname)).toBe(true);
170
- expect(isBotMentioned("hello NIMBUS", botShip, nickname)).toBe(true);
171
- });
172
-
173
- it("does NOT trigger on random messages", () => {
174
- expect(isBotMentioned("hello world", botShip)).toBe(false);
175
- expect(isBotMentioned("this is a normal message", botShip)).toBe(false);
176
- expect(isBotMentioned("hey everyone", botShip)).toBe(false);
177
- });
178
-
179
- it("does NOT trigger on partial ship matches", () => {
180
- expect(isBotMentioned("~sampel-palnet-extra", botShip)).toBe(false);
181
- expect(isBotMentioned("my~sampel-palnetfriend", botShip)).toBe(false);
182
- });
183
-
184
- it("does NOT trigger on substring nickname matches", () => {
185
- // "nimbus" should not match "nimbusy" or "animbust"
186
- expect(isBotMentioned("nimbusy", botShip, nickname)).toBe(false);
187
- expect(isBotMentioned("prenimbus", botShip, nickname)).toBe(false);
188
- });
189
-
190
- it("handles empty/null inputs safely", () => {
191
- expect(isBotMentioned("", botShip)).toBe(false);
192
- expect(isBotMentioned("test", "")).toBe(false);
193
- // @ts-expect-error testing null input
194
- expect(isBotMentioned(null, botShip)).toBe(false);
195
- });
196
-
197
- it("requires word boundary for nickname", () => {
198
- expect(isBotMentioned("nimbus, hello", botShip, nickname)).toBe(true);
199
- expect(isBotMentioned("hello nimbus!", botShip, nickname)).toBe(true);
200
- expect(isBotMentioned("nimbus?", botShip, nickname)).toBe(true);
201
- });
202
- });
203
- });
204
-
205
- describe("Security: Ship Normalization", () => {
206
- describe("normalizeShip", () => {
207
- it("adds ~ prefix if missing", () => {
208
- expect(normalizeShip("zod")).toBe("~zod");
209
- expect(normalizeShip("sampel-palnet")).toBe("~sampel-palnet");
210
- });
211
-
212
- it("preserves ~ prefix if present", () => {
213
- expect(normalizeShip("~zod")).toBe("~zod");
214
- expect(normalizeShip("~sampel-palnet")).toBe("~sampel-palnet");
215
- });
216
-
217
- it("trims whitespace", () => {
218
- expect(normalizeShip(" ~zod ")).toBe("~zod");
219
- expect(normalizeShip(" zod ")).toBe("~zod");
220
- });
221
-
222
- it("handles empty string", () => {
223
- expect(normalizeShip("")).toBe("");
224
- expect(normalizeShip(" ")).toBe("");
225
- });
226
- });
227
- });
228
-
229
- describe("Security: Message Text Extraction", () => {
230
- describe("extractMessageText", () => {
231
- it("extracts plain text", () => {
232
- const content = [{ inline: ["hello world"] }];
233
- expect(extractMessageText(content)).toBe("hello world");
234
- });
235
-
236
- it("extracts @all mentions from sect null", () => {
237
- const content = [{ inline: [{ sect: null }] }];
238
- expect(extractMessageText(content)).toContain("@all");
239
- });
240
-
241
- it("extracts ship mentions", () => {
242
- const content = [{ inline: [{ ship: "~zod" }] }];
243
- expect(extractMessageText(content)).toContain("~zod");
244
- });
245
-
246
- it("handles malformed input safely", () => {
247
- expect(extractMessageText(null)).toBe("");
248
- expect(extractMessageText(undefined)).toBe("");
249
- expect(extractMessageText([])).toBe("");
250
- expect(extractMessageText([{}])).toBe("");
251
- expect(extractMessageText("not an array")).toBe("");
252
- });
253
-
254
- it("does not execute injected code in inline content", () => {
255
- // Ensure malicious content doesn't get executed
256
- const maliciousContent = [{ inline: ["<script>alert('xss')</script>"] }];
257
- const result = extractMessageText(maliciousContent);
258
- expect(result).toBe("<script>alert('xss')</script>");
259
- // Just a string, not executed
260
- });
261
- });
262
- });
263
-
264
- describe("Security: Channel Authorization Logic", () => {
265
- /**
266
- * These tests document the expected behavior of channel authorization.
267
- * The actual resolveChannelAuthorization function is internal to monitor/index.ts
268
- * but these tests verify the building blocks and expected invariants.
269
- */
270
-
271
- it("default mode should be restricted (not open)", () => {
272
- // This is a critical security invariant: if no mode is specified,
273
- // channels should default to RESTRICTED, not open.
274
- // If this test fails, someone may have changed the default unsafely.
275
-
276
- // The logic in resolveChannelAuthorization is:
277
- // const mode = rule?.mode ?? "restricted";
278
- // We verify this by checking undefined rule gives restricted
279
- type ModeRule = { mode?: "restricted" | "open" };
280
- const rule = undefined as ModeRule | undefined;
281
- const mode = rule?.mode ?? "restricted";
282
- expect(mode).toBe("restricted");
283
- });
284
-
285
- it("empty allowedShips with restricted mode should block all", () => {
286
- // If a channel is restricted but has no allowed ships,
287
- // no one should be able to send messages
288
- const _mode = "restricted";
289
- const allowedShips: string[] = [];
290
- const sender = "~random-ship";
291
-
292
- const isAllowed = allowedShips.some((ship) => normalizeShip(ship) === normalizeShip(sender));
293
- expect(isAllowed).toBe(false);
294
- });
295
-
296
- it("open mode should not check allowedShips", () => {
297
- // In open mode, any ship can send regardless of allowedShips
298
- const mode: "open" | "restricted" = "open";
299
- // The check in monitor/index.ts is:
300
- // if (mode === "restricted") { /* check ships */ }
301
- // So open mode skips the ship check entirely
302
- expect(mode).not.toBe("restricted");
303
- });
304
-
305
- it("settings should override file config for channel rules", () => {
306
- // Documented behavior: settingsRules[nest] ?? fileRules[nest]
307
- // This means settings take precedence
308
- type ChannelRule = { mode: "restricted" | "open" };
309
- const fileRules: Record<string, ChannelRule> = { "chat/~zod/test": { mode: "restricted" } };
310
- const settingsRules: Record<string, ChannelRule> = { "chat/~zod/test": { mode: "open" } };
311
- const nest = "chat/~zod/test";
312
-
313
- const effectiveRule = settingsRules[nest] ?? fileRules[nest];
314
- expect(effectiveRule?.mode).toBe("open"); // settings wins
315
- });
316
- });
317
-
318
- describe("Security: Authorization Edge Cases", () => {
319
- it("empty strings are not valid ships", () => {
320
- expect(isDmAllowed("", ["~zod"])).toBe(false);
321
- expect(isDmAllowed("~zod", [""])).toBe(false);
322
- });
323
-
324
- it("handles very long ship-like strings", () => {
325
- const longName = "~" + "a".repeat(1000);
326
- expect(isDmAllowed(longName, ["~zod"])).toBe(false);
327
- });
328
-
329
- it("handles special characters that could break regex", () => {
330
- // These should not cause regex injection
331
- const maliciousShip = "~zod.*";
332
- expect(isDmAllowed("~zodabc", [maliciousShip])).toBe(false);
333
-
334
- const allowlist = ["~zod"];
335
- expect(isDmAllowed("~zod.*", allowlist)).toBe(false);
336
- });
337
-
338
- it("protects against prototype pollution-style keys", () => {
339
- const suspiciousShip = "__proto__";
340
- expect(isDmAllowed(suspiciousShip, ["~zod"])).toBe(false);
341
- expect(isDmAllowed("~zod", [suspiciousShip])).toBe(false);
342
- });
343
- });
344
-
345
- describe("Security: Cite Resolution Authorization Ordering", () => {
346
- async function resolveAllCitesForPoC(
347
- content: unknown,
348
- api: { scry: (path: string) => Promise<unknown> },
349
- ): Promise<string> {
350
- const cites = extractCites(content);
351
- if (cites.length === 0) {
352
- return "";
353
- }
354
-
355
- const resolved: string[] = [];
356
- for (const cite of cites) {
357
- if (cite.type !== "chan" || !cite.nest || !cite.postId) {
358
- continue;
359
- }
360
- const data = (await api.scry(`/channels/v4/${cite.nest}/posts/post/${cite.postId}.json`)) as {
361
- essay?: { content?: unknown };
362
- };
363
- const text = data?.essay?.content ? extractMessageText(data.essay.content) : "";
364
- if (text) {
365
- resolved.push(`> ${cite.author || "unknown"} wrote: ${text}`);
366
- }
367
- }
368
-
369
- return resolved.length > 0 ? resolved.join("\n") + "\n\n" : "";
370
- }
371
-
372
- function buildCitedMessage(
373
- secretNest = "chat/~private-ship/ops",
374
- postId = "1701411845077995094",
375
- ) {
376
- return [
377
- {
378
- block: {
379
- cite: {
380
- chan: {
381
- nest: secretNest,
382
- where: `/msg/~victim-ship/${postId}`,
383
- },
384
- },
385
- },
386
- },
387
- { inline: ["~bot-ship please summarize this"] },
388
- ];
389
- }
390
-
391
- it("does not resolve channel cites for unauthorized senders", async () => {
392
- const content = buildCitedMessage();
393
- const rawText = extractMessageText(content);
394
- const api = {
395
- scry: vi.fn(async () => ({
396
- essay: { content: [{ inline: ["TOP-SECRET"] }] },
397
- })),
398
- };
399
-
400
- const messageText = await resolveAuthorizedMessageText({
401
- rawText,
402
- content,
403
- authorizedForCites: false,
404
- resolveAllCites: (nextContent) => resolveAllCitesForPoC(nextContent, api),
405
- });
406
-
407
- expect(messageText).toBe(rawText);
408
- expect(api.scry).not.toHaveBeenCalled();
409
- });
410
-
411
- it("resolves channel cites after sender authorization passes", async () => {
412
- const secretNest = "chat/~private-ship/ops";
413
- const postId = "170141184507799509469114119040828178432";
414
- const content = buildCitedMessage(secretNest, postId);
415
- const rawText = extractMessageText(content);
416
- const api = {
417
- scry: vi.fn(async (path: string) => {
418
- expect(path).toBe(`/channels/v4/${secretNest}/posts/post/${postId}.json`);
419
- return {
420
- essay: { content: [{ inline: ["TOP-SECRET: migration key is rotate-me"] }] },
421
- };
422
- }),
423
- };
424
-
425
- const messageText = await resolveAuthorizedMessageText({
426
- rawText,
427
- content,
428
- authorizedForCites: true,
429
- resolveAllCites: (nextContent) => resolveAllCitesForPoC(nextContent, api),
430
- });
431
-
432
- expect(api.scry).toHaveBeenCalledTimes(1);
433
- expect(messageText).toContain("TOP-SECRET: migration key is rotate-me");
434
- expect(messageText).toContain("> ~victim-ship wrote: TOP-SECRET: migration key is rotate-me");
435
- });
436
-
437
- it("does not resolve DM cites before a deny path", async () => {
438
- const content = buildCitedMessage("chat/~secret-dm/ops", "1701411845077995095");
439
- const rawText = extractMessageText(content);
440
- const senderShip = "~attacker-ship";
441
- const allowlist = ["~trusted-ship"];
442
- const api = {
443
- scry: vi.fn(async () => ({
444
- essay: { content: [{ inline: ["DM-SECRET"] }] },
445
- })),
446
- };
447
-
448
- const senderAllowed = allowlist
449
- .map((ship) => normalizeShip(ship))
450
- .includes(normalizeShip(senderShip));
451
- expect(senderAllowed).toBe(false);
452
-
453
- const messageText = await resolveAuthorizedMessageText({
454
- rawText,
455
- content,
456
- authorizedForCites: senderAllowed,
457
- resolveAllCites: (nextContent) => resolveAllCitesForPoC(nextContent, api),
458
- });
459
-
460
- expect(messageText).toBe(rawText);
461
- expect(api.scry).not.toHaveBeenCalled();
462
- });
463
-
464
- it("does not resolve DM cites before owner approval command handling", async () => {
465
- const content = [
466
- {
467
- block: {
468
- cite: {
469
- chan: {
470
- nest: "chat/~private-ship/admin",
471
- where: "/msg/~victim-ship/1701411845077995096",
472
- },
473
- },
474
- },
475
- },
476
- { inline: ["/approve 1"] },
477
- ];
478
- const rawText = extractMessageText(content);
479
- const api = {
480
- scry: vi.fn(async () => ({
481
- essay: { content: [{ inline: ["ADMIN-SECRET"] }] },
482
- })),
483
- };
484
-
485
- const messageText = await resolveAuthorizedMessageText({
486
- rawText,
487
- content,
488
- authorizedForCites: false,
489
- resolveAllCites: (nextContent) => resolveAllCitesForPoC(nextContent, api),
490
- });
491
-
492
- expect(rawText).toContain("/approve 1");
493
- expect(messageText).toBe(rawText);
494
- expect(messageText).not.toContain("ADMIN-SECRET");
495
- expect(api.scry).not.toHaveBeenCalled();
496
- });
497
-
498
- it("resolves DM cites for allowed senders after authorization passes", async () => {
499
- const secretNest = "chat/~private-ship/dm";
500
- const postId = "1701411845077995097";
501
- const content = buildCitedMessage(secretNest, postId);
502
- const rawText = extractMessageText(content);
503
- const api = {
504
- scry: vi.fn(async (path: string) => {
505
- expect(path).toBe(`/channels/v4/${secretNest}/posts/post/${postId}.json`);
506
- return {
507
- essay: { content: [{ inline: ["ALLOWED-DM-SECRET"] }] },
508
- };
509
- }),
510
- };
511
-
512
- const messageText = await resolveAuthorizedMessageText({
513
- rawText,
514
- content,
515
- authorizedForCites: true,
516
- resolveAllCites: (nextContent) => resolveAllCitesForPoC(nextContent, api),
517
- });
518
-
519
- expect(api.scry).toHaveBeenCalledTimes(1);
520
- expect(messageText).toContain("ALLOWED-DM-SECRET");
521
- expect(messageText).toContain("> ~victim-ship wrote: ALLOWED-DM-SECRET");
522
- });
523
- });
524
-
525
- describe("Security: Sender Role Identification", () => {
526
- /**
527
- * Tests for sender role identification (owner vs user).
528
- * This prevents impersonation attacks where an approved user
529
- * tries to claim owner privileges through prompt injection.
530
- *
531
- * SECURITY.md Section 9: Sender Role Identification
532
- */
533
-
534
- // Helper to compute sender role (mirrors logic in monitor/index.ts)
535
- function getSenderRole(senderShip: string, ownerShip: string | null): "owner" | "user" {
536
- if (!ownerShip) {
537
- return "user";
538
- }
539
- return normalizeShip(senderShip) === normalizeShip(ownerShip) ? "owner" : "user";
540
- }
541
-
542
- describe("owner detection", () => {
543
- it("identifies owner when ownerShip matches sender", () => {
544
- expect(getSenderRole("~nocsyx-lassul", "~nocsyx-lassul")).toBe("owner");
545
- expect(getSenderRole("nocsyx-lassul", "~nocsyx-lassul")).toBe("owner");
546
- expect(getSenderRole("~nocsyx-lassul", "nocsyx-lassul")).toBe("owner");
547
- });
548
-
549
- it("identifies user when ownerShip does not match sender", () => {
550
- expect(getSenderRole("~random-user", "~nocsyx-lassul")).toBe("user");
551
- expect(getSenderRole("~malicious-actor", "~nocsyx-lassul")).toBe("user");
552
- });
553
-
554
- it("identifies everyone as user when ownerShip is null", () => {
555
- expect(getSenderRole("~nocsyx-lassul", null)).toBe("user");
556
- expect(getSenderRole("~zod", null)).toBe("user");
557
- });
558
-
559
- it("identifies everyone as user when ownerShip is empty string", () => {
560
- // Empty string should be treated like null (no owner configured)
561
- expect(getSenderRole("~nocsyx-lassul", "")).toBe("user");
562
- });
563
- });
564
-
565
- describe("label format", () => {
566
- // Helper to compute fromLabel (mirrors logic in monitor/index.ts)
567
- function getFromLabel(
568
- senderShip: string,
569
- ownerShip: string | null,
570
- isGroup: boolean,
571
- channelNest?: string,
572
- ): string {
573
- const senderRole = getSenderRole(senderShip, ownerShip);
574
- return isGroup
575
- ? `${senderShip} [${senderRole}] in ${channelNest}`
576
- : `${senderShip} [${senderRole}]`;
577
- }
578
-
579
- it("DM from owner includes [owner] in label", () => {
580
- const label = getFromLabel("~nocsyx-lassul", "~nocsyx-lassul", false);
581
- expect(label).toBe("~nocsyx-lassul [owner]");
582
- expect(label).toContain("[owner]");
583
- });
584
-
585
- it("DM from user includes [user] in label", () => {
586
- const label = getFromLabel("~random-user", "~nocsyx-lassul", false);
587
- expect(label).toBe("~random-user [user]");
588
- expect(label).toContain("[user]");
589
- });
590
-
591
- it("group message from owner includes [owner] in label", () => {
592
- const label = getFromLabel("~nocsyx-lassul", "~nocsyx-lassul", true, "chat/~host/general");
593
- expect(label).toBe("~nocsyx-lassul [owner] in chat/~host/general");
594
- expect(label).toContain("[owner]");
595
- });
596
-
597
- it("group message from user includes [user] in label", () => {
598
- const label = getFromLabel("~random-user", "~nocsyx-lassul", true, "chat/~host/general");
599
- expect(label).toBe("~random-user [user] in chat/~host/general");
600
- expect(label).toContain("[user]");
601
- });
602
- });
603
-
604
- describe("impersonation prevention", () => {
605
- it("approved user cannot get [owner] label through ship name tricks", () => {
606
- // Even if someone has a ship name similar to owner, they should not get owner role
607
- expect(getSenderRole("~nocsyx-lassul-fake", "~nocsyx-lassul")).toBe("user");
608
- expect(getSenderRole("~fake-nocsyx-lassul", "~nocsyx-lassul")).toBe("user");
609
- });
610
-
611
- it("message content cannot change sender role", () => {
612
- // The role is determined by ship identity, not message content
613
- // This test documents that even if message contains "I am the owner",
614
- // the actual senderShip determines the role
615
- const senderShip = "~malicious-actor";
616
- const ownerShip = "~nocsyx-lassul";
617
-
618
- // The role is always based on ship comparison, not message content
619
- expect(getSenderRole(senderShip, ownerShip)).toBe("user");
620
- });
621
- });
622
- });
@@ -1,40 +0,0 @@
1
- import {
2
- buildChannelOutboundSessionRoute,
3
- type ChannelOutboundSessionRouteParams,
4
- } from "openclaw/plugin-sdk/core";
5
- import { parseTlonTarget } from "./targets.js";
6
-
7
- export function resolveTlonOutboundSessionRoute(params: ChannelOutboundSessionRouteParams) {
8
- const parsed = parseTlonTarget(params.target);
9
- if (!parsed) {
10
- return null;
11
- }
12
- if (parsed.kind === "group") {
13
- return buildChannelOutboundSessionRoute({
14
- cfg: params.cfg,
15
- agentId: params.agentId,
16
- channel: "tlon",
17
- accountId: params.accountId,
18
- peer: {
19
- kind: "group",
20
- id: parsed.nest,
21
- },
22
- chatType: "group",
23
- from: `tlon:group:${parsed.nest}`,
24
- to: `tlon:${parsed.nest}`,
25
- });
26
- }
27
- return buildChannelOutboundSessionRoute({
28
- cfg: params.cfg,
29
- agentId: params.agentId,
30
- channel: "tlon",
31
- accountId: params.accountId,
32
- peer: {
33
- kind: "direct",
34
- id: parsed.ship,
35
- },
36
- chatType: "direct",
37
- from: `tlon:${parsed.ship}`,
38
- to: `tlon:${parsed.ship}`,
39
- });
40
- }