File size: 4,601 Bytes
5c2a829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
 * Tests approval auth helper decisions and implicit same-chat authorization markers.
 */
import { describe, expect, it } from "vitest";
import {
  createChannelApprovalAuth,
  createResolvedApproverActionAuthAdapter,
  isImplicitSameChatApprovalAuthorization,
} from "./approval-auth-helpers.js";

describe("createChannelApprovalAuth", () => {
  it("shares resolution, sender checks, and action authorization", () => {
    const auth = createChannelApprovalAuth({
      channelLabel: "Example",
      resolveInputs: () => ({ allowFrom: [" OWNER ", "owner"], defaultTo: "backup" }),
      normalizeApprover: (value) => String(value).trim().toLowerCase() || undefined,
    });

    expect(auth.resolveApprovers({ cfg: {} })).toEqual(["owner", "backup"]);
    expect(auth.isAuthorizedSender({ cfg: {}, senderId: "OWNER" })).toBe(true);
    expect(
      auth.approvalAuth.authorizeActorAction({
        cfg: {},
        senderId: "attacker",
        action: "approve",
        approvalKind: "plugin",
      }),
    ).toEqual({
      authorized: false,
      reason: "❌ You are not authorized to approve plugin requests on Example.",
    });
  });

  it("supports channel-owned wildcard authorization without broadcasting a wildcard target", () => {
    const auth = createChannelApprovalAuth({
      channelLabel: "Example",
      resolveInputs: () => ({ allowFrom: ["*"] }),
      normalizeApprover: (value) => (value === "*" ? undefined : String(value)),
      isWildcardAuthorized: ({ purpose, senderId, inputs }) =>
        purpose === "sender" && Boolean(senderId) && inputs.allowFrom?.includes("*") === true,
    });

    expect(auth.resolveApprovers({ cfg: {} })).toEqual([]);
    expect(auth.isAuthorizedSender({ cfg: {}, senderId: "anyone" })).toBe(true);
    expect(auth.isAuthorizedSender({ cfg: {} })).toBe(false);
    expect(
      auth.approvalAuth.authorizeActorAction({
        cfg: {},
        senderId: "anyone",
        action: "approve",
        approvalKind: "exec",
      }),
    ).toEqual({ authorized: true });
  });
});

describe("createResolvedApproverActionAuthAdapter", () => {
  it.each([
    {
      name: "falls back to generic same-chat auth when no approvers resolve",
      channelLabel: "Slack",
      resolveApprovers: () => [],
      normalizeSenderId: undefined,
      cases: [
        {
          senderId: "U_OWNER",
          approvalKind: "exec" as const,
          expected: { authorized: true },
        },
      ],
    },
    {
      name: "allows matching normalized approvers and rejects others",
      channelLabel: "Signal",
      resolveApprovers: () => ["uuid:owner"],
      normalizeSenderId: (value: string) => value.trim().toLowerCase(),
      cases: [
        {
          senderId: " UUID:OWNER ",
          approvalKind: "plugin" as const,
          expected: { authorized: true },
        },
        {
          senderId: "uuid:attacker",
          approvalKind: "plugin" as const,
          expected: {
            authorized: false,
            reason: "❌ You are not authorized to approve plugin requests on Signal.",
          },
        },
      ],
    },
  ])("$name", ({ channelLabel, resolveApprovers, normalizeSenderId, cases }) => {
    const auth = createResolvedApproverActionAuthAdapter({
      channelLabel,
      resolveApprovers,
      normalizeSenderId,
    });

    for (const testCase of cases) {
      expect(
        auth.authorizeActorAction({
          cfg: {},
          senderId: testCase.senderId,
          action: "approve",
          approvalKind: testCase.approvalKind,
        }),
      ).toEqual(testCase.expected);
    }
  });

  it("marks empty-approver fallback auth as implicit", () => {
    const auth = createResolvedApproverActionAuthAdapter({
      channelLabel: "Signal",
      resolveApprovers: () => [],
    });
    const result = auth.authorizeActorAction({
      cfg: {},
      senderId: "uuid:attacker",
      action: "approve",
      approvalKind: "exec",
    });

    expect(result).toEqual({ authorized: true });
    expect(isImplicitSameChatApprovalAuthorization(result)).toBe(true);
  });

  it("does not mark configured-approver auth as implicit", () => {
    const auth = createResolvedApproverActionAuthAdapter({
      channelLabel: "Signal",
      resolveApprovers: () => ["uuid:owner"],
    });
    const result = auth.authorizeActorAction({
      cfg: {},
      senderId: "uuid:owner",
      action: "approve",
      approvalKind: "exec",
    });

    expect(result).toEqual({ authorized: true });
    expect(isImplicitSameChatApprovalAuthorization(result)).toBe(false);
  });
});