Skip to content

Commit 9ee4987

Browse files
authored
test(tlon): exercise the real channel authorization resolver (#134393)
1 parent c76feea commit 9ee4987

1 file changed

Lines changed: 50 additions & 40 deletions

File tree

‎extensions/tlon/src/security.test.ts‎

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import { describe, expect, it, vi } from "vitest";
12+
import { resolveChannelAuthorization } from "./monitor/authorization.js";
1213
import {
1314
extractCites,
1415
resolveTlonCommandAuthorizationWithIngress,
@@ -349,53 +350,62 @@ describe("Security: Message Text Extraction", () => {
349350
});
350351

351352
describe("Security: Channel Authorization Logic", () => {
352-
/**
353-
* These tests document the expected behavior of channel authorization.
354-
* The actual resolveChannelAuthorization function is internal to monitor/index.ts
355-
* but these tests verify the building blocks and expected invariants.
356-
*/
353+
const channelNest = "chat/~zod/test";
357354

358-
it("default mode should be restricted (not open)", () => {
359-
// This is a critical security invariant: if no mode is specified,
360-
// channels should default to RESTRICTED, not open.
361-
// If this test fails, someone may have changed the default unsafely.
362-
363-
// The logic in resolveChannelAuthorization is:
364-
// const mode = rule?.mode ?? "restricted";
365-
// We verify this by checking undefined rule gives restricted
366-
type ModeRule = { mode?: "restricted" | "open" };
367-
const rule = undefined as ModeRule | undefined;
368-
const mode = rule?.mode ?? "restricted";
369-
expect(mode).toBe("restricted");
355+
it("defaults an unconfigured channel to restricted with no authorized ships", () => {
356+
expect(resolveChannelAuthorization({}, channelNest)).toEqual({
357+
mode: "restricted",
358+
allowedShips: [],
359+
});
370360
});
371361

372-
it("empty allowedShips with restricted mode should block all", () => {
373-
const allowedShips: string[] = [];
374-
const sender = "~random-ship";
375-
376-
const isAllowed = allowedShips.some((ship) => normalizeShip(ship) === normalizeShip(sender));
377-
expect(isAllowed).toBe(false);
362+
it("keeps an explicit empty channel allowlist instead of inheriting defaults", () => {
363+
expect(
364+
resolveChannelAuthorization(
365+
{
366+
channels: {
367+
tlon: {
368+
defaultAuthorizedShips: ["~zod"],
369+
authorization: { channelRules: { [channelNest]: { allowedShips: [] } } },
370+
},
371+
},
372+
},
373+
channelNest,
374+
),
375+
).toEqual({ mode: "restricted", allowedShips: [] });
378376
});
379377

380-
it("open mode should not check allowedShips", () => {
381-
// In open mode, any ship can send regardless of allowedShips
382-
const mode: "open" | "restricted" = "open";
383-
// The check in monitor/index.ts is:
384-
// if (mode === "restricted") { /* check ships */ }
385-
// So open mode skips the ship check entirely
386-
expect(mode).not.toBe("restricted");
378+
it("preserves explicit open channel policy", () => {
379+
expect(
380+
resolveChannelAuthorization(
381+
{
382+
channels: {
383+
tlon: {
384+
authorization: { channelRules: { [channelNest]: { mode: "open" } } },
385+
},
386+
},
387+
},
388+
channelNest,
389+
),
390+
).toEqual({ mode: "open", allowedShips: [] });
387391
});
388392

389-
it("settings should override file config for channel rules", () => {
390-
// Documented behavior: settingsRules[nest] ?? fileRules[nest]
391-
// This means settings take precedence
392-
type ChannelRule = { mode: "restricted" | "open" };
393-
const fileRules: Record<string, ChannelRule> = { "chat/~zod/test": { mode: "restricted" } };
394-
const settingsRules: Record<string, ChannelRule> = { "chat/~zod/test": { mode: "open" } };
395-
const nest = "chat/~zod/test";
396-
397-
const effectiveRule = settingsRules[nest] ?? fileRules[nest];
398-
expect(effectiveRule?.mode).toBe("open"); // settings wins
393+
it("prefers settings channel rules over conflicting file configuration", () => {
394+
expect(
395+
resolveChannelAuthorization(
396+
{
397+
channels: {
398+
tlon: {
399+
authorization: {
400+
channelRules: { [channelNest]: { mode: "restricted", allowedShips: ["~zod"] } },
401+
},
402+
},
403+
},
404+
},
405+
channelNest,
406+
{ channelRules: { [channelNest]: { mode: "open", allowedShips: ["~bus"] } } },
407+
),
408+
).toEqual({ mode: "open", allowedShips: ["~bus"] });
399409
});
400410
});
401411

0 commit comments

Comments
 (0)