Skip to content

Commit 8016c43

Browse files
committed
fix: tighten imessage reply authorization trust
Keep inferred iMessage reply targets sender-authorized only when the target actually used for delivery matches the current channel, and ignore gateway-supplied requester identities for sender-based group allowlist checks. Preserve sender authorization for internally consumed implicit reply chunks while still rejecting malformed reply ids that sanitize away. Validation: - node scripts/run-vitest.mjs extensions/imessage/src/actions.test.ts extensions/imessage/src/send.test.ts - node scripts/run-vitest.mjs extensions/imessage/src/send.test.ts extensions/imessage/src/actions.test.ts extensions/imessage/src/channel.runtime.test.ts extensions/imessage/src/monitor/deliver.test.ts extensions/imessage/src/test-plugin.test.ts extensions/imessage/src/monitor.gating.test.ts src/infra/outbound/deliver.test.ts src/infra/outbound/message.channels.test.ts extensions/imessage/src/targets.test.ts src/infra/outbound/message-action-normalization.test.ts - pnpm exec oxlint extensions/imessage/src/actions.ts extensions/imessage/src/actions.test.ts extensions/imessage/src/send.ts extensions/imessage/src/send.test.ts - git diff --check
1 parent f528b0f commit 8016c43

4 files changed

Lines changed: 105 additions & 4 deletions

File tree

‎extensions/imessage/src/actions.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,54 @@ describe("imessage message actions", () => {
690690
expect(runtimeMock.sendRichMessage).not.toHaveBeenCalled();
691691
});
692692

693+
it("does not trust inferred-target markers when the delivered alias differs", async () => {
694+
await expect(
695+
imessageMessageActions.handleAction?.({
696+
action: "reply",
697+
cfg: cfg(undefined, {
698+
groupPolicy: "allowlist",
699+
groupAllowFrom: ["+15551230000"],
700+
}),
701+
params: {
702+
target: "chat_identifier:team-thread",
703+
to: "chat_identifier:blocked-thread",
704+
__openclawInferredTargetFromCurrentChannel: true,
705+
messageId: "message-guid",
706+
text: "reply",
707+
},
708+
toolContext: { currentChannelId: "chat_identifier:team-thread" },
709+
requesterSenderId: "+15551230000",
710+
} as never),
711+
).rejects.toThrow(
712+
"iMessage outbound blocked: target is not in channels.imessage.groupAllowFrom",
713+
);
714+
expect(runtimeMock.resolveChatGuidForTarget).not.toHaveBeenCalled();
715+
expect(runtimeMock.sendRichMessage).not.toHaveBeenCalled();
716+
});
717+
718+
it("does not trust gateway-supplied requester sender for group reply authorization", async () => {
719+
await expect(
720+
imessageMessageActions.handleAction?.({
721+
action: "reply",
722+
cfg: cfg(undefined, {
723+
groupPolicy: "allowlist",
724+
groupAllowFrom: ["+15551230000"],
725+
}),
726+
params: {
727+
messageId: "message-guid",
728+
text: "reply",
729+
},
730+
toolContext: { currentChannelId: "chat_identifier:team-thread" },
731+
requesterSenderId: "+15551230000",
732+
gatewayClientScopes: ["operator.write"],
733+
} as never),
734+
).rejects.toThrow(
735+
"iMessage outbound blocked: target is not in channels.imessage.groupAllowFrom",
736+
);
737+
expect(runtimeMock.resolveChatGuidForTarget).not.toHaveBeenCalled();
738+
expect(runtimeMock.sendRichMessage).not.toHaveBeenCalled();
739+
});
740+
693741
it("does not use trusted requester sender for explicit group reply targets", async () => {
694742
await expect(
695743
imessageMessageActions.handleAction?.({

‎extensions/imessage/src/actions.ts‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,25 @@ function readOutboundActionTarget(params: {
257257
return rawTarget ? parseIMessageTarget(rawTarget) : null;
258258
}
259259

260+
function readRawOutboundActionTarget(params: {
261+
actionParams: Record<string, unknown>;
262+
currentChannelId?: string;
263+
}): string | undefined {
264+
return (
265+
readStringParam(params.actionParams, "chatGuid")?.trim() ||
266+
(typeof readPositiveIntegerParam(params.actionParams, "chatId") === "number"
267+
? `chat_id:${readPositiveIntegerParam(params.actionParams, "chatId")}`
268+
: undefined) ||
269+
(readStringParam(params.actionParams, "chatIdentifier")?.trim()
270+
? `chat_identifier:${readStringParam(params.actionParams, "chatIdentifier")?.trim()}`
271+
: undefined) ||
272+
readStringParam(params.actionParams, "to")?.trim() ||
273+
readStringParam(params.actionParams, "target")?.trim() ||
274+
params.currentChannelId?.trim() ||
275+
undefined
276+
);
277+
}
278+
260279
function hasExplicitOutboundActionTarget(
261280
params: Record<string, unknown>,
262281
currentChannelId?: string,
@@ -267,7 +286,7 @@ function hasExplicitOutboundActionTarget(
267286
const targetWasInferred =
268287
params[MESSAGE_ACTION_INFERRED_TARGET_PARAM] === true &&
269288
Boolean(currentTarget) &&
270-
(targetParam === currentTarget || toParam === currentTarget);
289+
readRawOutboundActionTarget({ actionParams: params, currentChannelId }) === currentTarget;
271290
return (
272291
Boolean(readStringParam(params, "chatGuid")?.trim()) ||
273292
typeof readPositiveIntegerParam(params, "chatId") === "number" ||
@@ -447,7 +466,15 @@ export const imessageMessageActions: ChannelMessageActionAdapter = {
447466
leaveGroup: { aliases: ["chatGuid", "chatIdentifier", "chatId"] },
448467
},
449468
extractToolSend: ({ args }) => extractToolSend(args, "sendMessage"),
450-
handleAction: async ({ action, params, cfg, accountId, requesterSenderId, toolContext }) => {
469+
handleAction: async ({
470+
action,
471+
params,
472+
cfg,
473+
accountId,
474+
requesterSenderId,
475+
toolContext,
476+
gatewayClientScopes,
477+
}) => {
451478
const runtime = await loadIMessageActionsRuntime();
452479
const account = resolveIMessageAccount({
453480
cfg,
@@ -524,7 +551,9 @@ export const imessageMessageActions: ChannelMessageActionAdapter = {
524551
toolContext?.currentChannelId,
525552
)
526553
? undefined
527-
: assertOpts?.replyRequesterSender;
554+
: gatewayClientScopes
555+
? undefined
556+
: assertOpts?.replyRequesterSender;
528557
await assertIMessageOutboundAllowed({
529558
cfg,
530559
account,

‎extensions/imessage/src/send.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,30 @@ describe("sendMessageIMessage receipts", () => {
461461
expect(getClientMocks(client).request).not.toHaveBeenCalled();
462462
});
463463

464+
it("preserves sender-based group allowance after an implicit reply id is consumed", async () => {
465+
const client = createClient({ guid: "p:0/group-consumed-reply-id" });
466+
467+
await sendMessageIMessage("chat_id:42", "hello", {
468+
config: {
469+
channels: {
470+
imessage: {
471+
groupPolicy: "allowlist",
472+
groupAllowFrom: ["+15551230000"],
473+
},
474+
},
475+
},
476+
client,
477+
replyToIdSource: "implicit",
478+
replyRequesterSender: "+15551230000",
479+
});
480+
481+
expect(getClientMocks(client).request).toHaveBeenCalledWith(
482+
"send",
483+
expect.objectContaining({ chat_id: 42 }),
484+
expect.any(Object),
485+
);
486+
});
487+
464488
it("blocks sender-based group allowance when the reply id sanitizes empty", async () => {
465489
const client = createClient({ guid: "p:0/group-empty-reply-id" });
466490

‎extensions/imessage/src/send.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ export async function sendMessageIMessage(
858858
account,
859859
target,
860860
replyRequesterSender:
861-
resolvedReplyToId && opts.replyToIdSource === "implicit"
861+
opts.replyToIdSource === "implicit" && (opts.replyToId == null || resolvedReplyToId)
862862
? opts.replyRequesterSender
863863
: undefined,
864864
});

0 commit comments

Comments
 (0)