|
| 1 | +import { describe, expect, mock, test } from "bun:test"; |
| 2 | +import { createInboundMessage } from "@intx/mime"; |
| 3 | +import type { InboundMessage } from "@intx/types/runtime"; |
| 4 | +import { configureSync, getConfig, resetSync } from "@intx/log"; |
| 5 | + |
| 6 | +import { |
| 7 | + createApprovalCorrelationValidator, |
| 8 | + createApprovalProvenanceGate, |
| 9 | + isForeignApprovalIngress, |
| 10 | + LOCAL_APPROVAL_FROM, |
| 11 | + LOCAL_APPROVAL_MAILBOX, |
| 12 | +} from "./approval-provenance.js"; |
| 13 | + |
| 14 | +interface MessageInput { |
| 15 | + from: string; |
| 16 | + mailbox?: string; |
| 17 | +} |
| 18 | + |
| 19 | +function approvalMessage( |
| 20 | + args: MessageInput = { |
| 21 | + from: LOCAL_APPROVAL_FROM, |
| 22 | + mailbox: LOCAL_APPROVAL_MAILBOX, |
| 23 | + }, |
| 24 | + outcome: "approved" | "rejected" = "approved", |
| 25 | +): InboundMessage { |
| 26 | + const message = createInboundMessage({ |
| 27 | + from: args.from, |
| 28 | + to: "agent@local", |
| 29 | + correlationId: "corr-1", |
| 30 | + content: JSON.stringify({ outcome }), |
| 31 | + ...(args.mailbox === undefined ? {} : { ref: { mailbox: args.mailbox } }), |
| 32 | + }); |
| 33 | + message.headers.interchangeType = |
| 34 | + outcome === "approved" ? "approval.granted" : "approval.denied"; |
| 35 | + return message; |
| 36 | +} |
| 37 | + |
| 38 | +describe("isForeignApprovalIngress", () => { |
| 39 | + test("the local approval-delivery path is not foreign", () => { |
| 40 | + expect(isForeignApprovalIngress(approvalMessage())).toBe(false); |
| 41 | + }); |
| 42 | + test("a foreign sender is foreign even on the local mailbox", () => { |
| 43 | + expect( |
| 44 | + isForeignApprovalIngress( |
| 45 | + approvalMessage({ from: "watchdog@foreign", mailbox: "approval" }), |
| 46 | + ), |
| 47 | + ).toBe(true); |
| 48 | + }); |
| 49 | + test("mailbox ingress is foreign even with the local sender", () => { |
| 50 | + expect( |
| 51 | + isForeignApprovalIngress( |
| 52 | + approvalMessage({ from: LOCAL_APPROVAL_FROM, mailbox: "INBOX" }), |
| 53 | + ), |
| 54 | + ).toBe(true); |
| 55 | + }); |
| 56 | + test("a typed approval with no mailbox fails closed as foreign", () => { |
| 57 | + // The declared MessageRef always names a mailbox, but the gate must still |
| 58 | + // fail closed when a ref arrives without one (upstream transport parses |
| 59 | + // external bytes before this type guarantees anything). |
| 60 | + const { mailbox: _omitted, ...refWithoutMailbox } = approvalMessage().ref; |
| 61 | + const message = { |
| 62 | + ...approvalMessage(), |
| 63 | + ref: refWithoutMailbox, |
| 64 | + } as unknown as InboundMessage; |
| 65 | + expect(isForeignApprovalIngress(message)).toBe(true); |
| 66 | + }); |
| 67 | + test("a typed approval with no ref at all fails closed as foreign", () => { |
| 68 | + // What fail-closed is for: a type-illegal shape the declared ref cannot |
| 69 | + // produce must not flip the default to trusted merely by existing. |
| 70 | + const message = { |
| 71 | + ...approvalMessage({ from: "webhook@foreign" }), |
| 72 | + ref: undefined, |
| 73 | + } as unknown as InboundMessage; |
| 74 | + expect(isForeignApprovalIngress(message)).toBe(true); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +describe("createApprovalCorrelationValidator", () => { |
| 79 | + test("authenticates only local approval-delivery provenance", async () => { |
| 80 | + const validator = createApprovalCorrelationValidator(); |
| 81 | + expect(await validator.validate({} as never, approvalMessage())).toBe(true); |
| 82 | + expect( |
| 83 | + await validator.validate( |
| 84 | + {} as never, |
| 85 | + approvalMessage({ from: "watchdog@foreign", mailbox: "INBOX" }), |
| 86 | + ), |
| 87 | + ).toBe(false); |
| 88 | + }); |
| 89 | + test("authenticates foreign ingress for denied decisions too", async () => { |
| 90 | + const validator = createApprovalCorrelationValidator(); |
| 91 | + expect( |
| 92 | + await validator.validate( |
| 93 | + {} as never, |
| 94 | + approvalMessage({ from: "user@local", mailbox: "INBOX" }, "rejected"), |
| 95 | + ), |
| 96 | + ).toBe(false); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe("createApprovalProvenanceGate", () => { |
| 101 | + test("drops a foreign typed approval before the agent can correlate it", () => { |
| 102 | + const deliver = mock((_message: InboundMessage): void => undefined); |
| 103 | + const gate = createApprovalProvenanceGate({ deliver }); |
| 104 | + gate.deliver( |
| 105 | + approvalMessage({ from: "webhook@foreign", mailbox: "INBOX" }), |
| 106 | + ); |
| 107 | + expect(deliver).not.toHaveBeenCalled(); |
| 108 | + }); |
| 109 | + test("passes the local approval-delivery path through unchanged", () => { |
| 110 | + const deliver = mock((_message: InboundMessage): void => undefined); |
| 111 | + const gate = createApprovalProvenanceGate({ deliver }); |
| 112 | + const message = approvalMessage(); |
| 113 | + gate.deliver(message); |
| 114 | + expect(deliver).toHaveBeenCalledWith(message); |
| 115 | + }); |
| 116 | + test("leaves nonapproval inbound routing untouched", () => { |
| 117 | + const deliver = mock((_message: InboundMessage): void => undefined); |
| 118 | + const gate = createApprovalProvenanceGate({ deliver }); |
| 119 | + const message = createInboundMessage({ |
| 120 | + from: "user@local", |
| 121 | + to: "agent@local", |
| 122 | + content: "hello", |
| 123 | + }); |
| 124 | + gate.deliver(message); |
| 125 | + expect(deliver).toHaveBeenCalledWith(message); |
| 126 | + }); |
| 127 | + test("a type-illegal ref-less foreign approval is discarded, not thrown", () => { |
| 128 | + // The gate must not convert a forge probe into a deliver() throw: the |
| 129 | + // receipt path dereferences the same optional ref the policy checked. |
| 130 | + const deliver = mock((_message: InboundMessage): void => undefined); |
| 131 | + const gate = createApprovalProvenanceGate({ deliver }); |
| 132 | + const message = { |
| 133 | + ...approvalMessage({ from: "webhook@foreign" }), |
| 134 | + ref: undefined, |
| 135 | + } as unknown as InboundMessage; |
| 136 | + gate.deliver(message); |
| 137 | + expect(deliver).not.toHaveBeenCalled(); |
| 138 | + }); |
| 139 | + test("discarded forgeries get a structured receipt naming provenance", () => { |
| 140 | + const records: { level: string; text: string }[] = []; |
| 141 | + const previous = getConfig(); |
| 142 | + configureSync({ |
| 143 | + reset: true, |
| 144 | + sinks: { |
| 145 | + test: (record) => { |
| 146 | + records.push({ |
| 147 | + level: record.level, |
| 148 | + text: record.message.map(String).join(""), |
| 149 | + }); |
| 150 | + }, |
| 151 | + }, |
| 152 | + loggers: [ |
| 153 | + { category: [], lowestLevel: "warning", sinks: ["test"] }, |
| 154 | + { |
| 155 | + category: ["logtape", "meta"], |
| 156 | + lowestLevel: "error", |
| 157 | + sinks: ["test"], |
| 158 | + }, |
| 159 | + ], |
| 160 | + }); |
| 161 | + try { |
| 162 | + const deliver = mock((_message: InboundMessage): void => undefined); |
| 163 | + const gate = createApprovalProvenanceGate({ deliver }); |
| 164 | + gate.deliver( |
| 165 | + approvalMessage({ |
| 166 | + from: "watchdog@foreign", |
| 167 | + mailbox: "INBOX", |
| 168 | + }), |
| 169 | + ); |
| 170 | + // The same probe, but with the ref missing entirely: the receipt must |
| 171 | + // render the missing mailbox as <none> instead of throwing. |
| 172 | + gate.deliver({ |
| 173 | + ...approvalMessage({ from: "webhook@foreign" }), |
| 174 | + ref: undefined, |
| 175 | + } as unknown as InboundMessage); |
| 176 | + const receipts = records.filter( |
| 177 | + (record) => |
| 178 | + record.level === "warning" && |
| 179 | + record.text.includes("forged approval discarded"), |
| 180 | + ); |
| 181 | + expect(receipts).toHaveLength(2); |
| 182 | + expect(receipts[0]?.text).toContain("correlationId=corr-1"); |
| 183 | + expect(receipts[0]?.text).toContain("from=watchdog@foreign"); |
| 184 | + expect(receipts[0]?.text).toContain("mailbox=INBOX"); |
| 185 | + expect(receipts[0]?.text).toContain("type=approval.granted"); |
| 186 | + expect(receipts[1]?.text).toContain("from=webhook@foreign"); |
| 187 | + expect(receipts[1]?.text).toContain("mailbox=<none>"); |
| 188 | + expect(deliver).not.toHaveBeenCalled(); |
| 189 | + } finally { |
| 190 | + if (previous === null) resetSync(); |
| 191 | + else configureSync({ ...previous, reset: true }); |
| 192 | + } |
| 193 | + }); |
| 194 | + test("wraps deliver without disturbing the rest of the surface", async () => { |
| 195 | + const deliver = mock((_message: InboundMessage): void => undefined); |
| 196 | + const send = mock(async (_content: string) => undefined); |
| 197 | + const gate = createApprovalProvenanceGate({ deliver, send }); |
| 198 | + gate.deliver(approvalMessage({ from: "watchdog@foreign" })); |
| 199 | + expect(deliver).not.toHaveBeenCalled(); |
| 200 | + await gate.send("continue"); |
| 201 | + expect(send).toHaveBeenCalledWith("continue"); |
| 202 | + }); |
| 203 | +}); |
0 commit comments