Skip to content

Commit 235a5c4

Browse files
fix(imessage): recognize bare hex group chat identifiers as chat targets (#99525)
* fix(imessage): recognize bare hex group chat identifiers as chat targets A 32-char hex iMessage group chat identifier (as returned by `imsg` for group chats) with no explicit `chat_identifier:` prefix fell through to E.164 phone-number normalization. normalizeE164 strips non-digit characters and prepends `+`, turning `7d5297154d5f436d83dbbdf03fcc8fdd` into `+75297154543683038` -- a bogus phone number. Cron/announce delivery then reported `delivered: true` while silently sending to a nonexistent recipient. Detect bare 32-char hex strings in both the send-path target parser (extensions/imessage/src/targets.ts) and the outbound delivery-target normalizer (extensions/imessage/src/normalize.ts) and route them as chat_identifier targets, consistent with how an explicit `chat_identifier:<id>` prefix already resolves. Fixes #89235. * refactor(imessage): centralize bare chat identifiers * refactor(imessage): centralize bare chat identifiers --------- Co-authored-by: Peter Steinberger <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent ecc486b commit 235a5c4

5 files changed

Lines changed: 93 additions & 1 deletion

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ describe("normalizeIMessageMessagingTarget", () => {
1616
expect(normalizeIMessageMessagingTarget("imessage:CHAT_GUID:abc")).toBe("chat_guid:abc");
1717
expect(normalizeIMessageMessagingTarget("auto:ChatIdentifier:foo")).toBe("chatidentifier:foo");
1818
});
19+
20+
it("treats a bare 32-char hex group chat identifier as a chat_identifier, not a phone number", () => {
21+
const hex = "7d5297154d5f436d83dbbdf03fcc8fdd";
22+
expect(normalizeIMessageMessagingTarget(hex)).toBe(`chat_identifier:${hex}`);
23+
expect(normalizeIMessageMessagingTarget(hex.toUpperCase())).toBe(`chat_identifier:${hex}`);
24+
});
1925
});
2026

2127
describe("looksLikeIMessageTargetId", () => {
@@ -25,5 +31,6 @@ describe("looksLikeIMessageTargetId", () => {
2531
expect(looksLikeIMessageTargetId("[email protected]")).toBe(true);
2632
expect(looksLikeIMessageTargetId("+15555550123")).toBe(true);
2733
expect(looksLikeIMessageTargetId("")).toBe(false);
34+
expect(looksLikeIMessageTargetId("7d5297154d5f436d83dbbdf03fcc8fdd")).toBe(true);
2835
});
2936
});

‎extensions/imessage/src/normalize.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
normalizeLowercaseStringOrEmpty,
55
normalizeOptionalString,
66
} from "openclaw/plugin-sdk/string-coerce-runtime";
7+
import { normalizeBareIMessageChatIdentifier } from "./target-identifiers.js";
78

89
const SERVICE_PREFIXES = ["imessage:", "sms:", "auto:"] as const;
910
const CHAT_TARGET_PREFIX_RE =
@@ -53,6 +54,10 @@ function normalizeIMessageHandle(raw: string): string {
5354
if (trimmed.includes("@")) {
5455
return normalizeLowercaseStringOrEmpty(trimmed);
5556
}
57+
const bareChatIdentifier = normalizeBareIMessageChatIdentifier(trimmed);
58+
if (bareChatIdentifier) {
59+
return `chat_identifier:${bareChatIdentifier}`;
60+
}
5661
const normalized = normalizeE164(trimmed);
5762
if (normalized) {
5863
return normalized;
@@ -93,6 +98,9 @@ export function looksLikeIMessageTargetId(raw: string): boolean {
9398
if (CHAT_TARGET_PREFIX_RE.test(trimmed)) {
9499
return true;
95100
}
101+
if (normalizeBareIMessageChatIdentifier(trimmed)) {
102+
return true;
103+
}
96104
return looksLikeHandleOrPhoneTarget({
97105
raw: trimmed,
98106
prefixPattern: /^(imessage:|sms:|auto:)/i,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Imessage helper module normalizes database-backed target identifiers.
2+
const BARE_CHAT_IDENTIFIER_RE = /^[0-9a-f]{32}$/i;
3+
4+
export function normalizeBareIMessageChatIdentifier(raw: string): string | undefined {
5+
const trimmed = raw.trim();
6+
if (!BARE_CHAT_IDENTIFIER_RE.test(trimmed)) {
7+
return undefined;
8+
}
9+
return trimmed.toLowerCase();
10+
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,44 @@ describe("imessage targets", () => {
157157
expect(looksLikeIMessageExplicitTargetId("sms:+15552223333")).toBe(true);
158158
expect(looksLikeIMessageExplicitTargetId("+15552223333")).toBe(false);
159159
expect(looksLikeIMessageExplicitTargetId("[email protected]")).toBe(false);
160+
expect(looksLikeIMessageExplicitTargetId("7d5297154d5f436d83dbbdf03fcc8fdd")).toBe(true);
160161
});
161162

162163
it("infers direct and group chat types from normalized targets", () => {
163164
expect(inferIMessageTargetChatType("+15552223333")).toBe("direct");
164165
expect(inferIMessageTargetChatType("chat_id:42")).toBe("group");
165166
});
167+
168+
it("treats bare 32-char hex strings as chat identifiers, not phone numbers", () => {
169+
const hex = "7d5297154d5f436d83dbbdf03fcc8fdd";
170+
expect(normalizeIMessageHandle(hex)).toBe(`chat_identifier:${hex}`);
171+
expect(normalizeIMessageHandle(hex.toUpperCase())).toBe(`chat_identifier:${hex}`);
172+
expect(parseIMessageTarget(hex)).toEqual({
173+
kind: "chat_identifier",
174+
chatIdentifier: hex,
175+
});
176+
expect(parseIMessageTarget(`imessage:${hex.toUpperCase()}`)).toEqual({
177+
kind: "chat_identifier",
178+
chatIdentifier: hex,
179+
});
180+
expect(inferIMessageTargetChatType(hex)).toBe("group");
181+
});
182+
183+
it.each(["7d5297154d5f436d83dbbdf03fcc8fd", "7d5297154d5f436d83dbbdf03fcc8fdg"])(
184+
"keeps non-hex or wrong-length value %s on the handle path",
185+
(value) => {
186+
expect(normalizeIMessageHandle(value)).not.toMatch(/^chat_identifier:/);
187+
expect(parseIMessageTarget(value)).toEqual({ kind: "handle", to: value, service: "auto" });
188+
},
189+
);
190+
191+
it("accepts the all-digit edge of the 32-hex identifier contract", () => {
192+
const identifier = "1".repeat(32);
193+
expect(parseIMessageTarget(identifier)).toEqual({
194+
kind: "chat_identifier",
195+
chatIdentifier: identifier,
196+
});
197+
});
166198
});
167199

168200
describe("imessage group policy", () => {

‎extensions/imessage/src/targets.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Imessage plugin module implements targets behavior.
22
import { normalizeE164 } from "openclaw/plugin-sdk/account-resolution";
33
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime";
4+
import { normalizeBareIMessageChatIdentifier } from "./target-identifiers.js";
45
import {
56
type ChatSenderAllowParams,
67
createAllowedChatSenderMatcher,
@@ -29,6 +30,22 @@ const SERVICE_PREFIXES: Array<{ prefix: string; service: IMessageService }> = [
2930
{ prefix: "auto:", service: "auto" },
3031
];
3132

33+
function parseServicePrefixedBareChatIdentifier(params: {
34+
trimmed: string;
35+
lower: string;
36+
}): IMessageTarget | undefined {
37+
for (const { prefix } of SERVICE_PREFIXES) {
38+
if (!params.lower.startsWith(prefix)) {
39+
continue;
40+
}
41+
const chatIdentifier = normalizeBareIMessageChatIdentifier(params.trimmed.slice(prefix.length));
42+
if (chatIdentifier) {
43+
return { kind: "chat_identifier", chatIdentifier };
44+
}
45+
}
46+
return undefined;
47+
}
48+
3249
export function normalizeIMessageHandle(raw: string): string {
3350
const trimmed = raw.trim();
3451
if (!trimmed) {
@@ -68,6 +85,10 @@ export function normalizeIMessageHandle(raw: string): string {
6885
if (trimmed.includes("@")) {
6986
return normalizeLowercaseStringOrEmpty(trimmed);
7087
}
88+
const bareChatIdentifier = normalizeBareIMessageChatIdentifier(trimmed);
89+
if (bareChatIdentifier) {
90+
return `chat_identifier:${bareChatIdentifier}`;
91+
}
7192
const normalized = normalizeE164(trimmed);
7293
if (normalized) {
7394
return normalized;
@@ -82,6 +103,14 @@ export function parseIMessageTarget(raw: string): IMessageTarget {
82103
}
83104
const lower = normalizeLowercaseStringOrEmpty(trimmed);
84105

106+
const servicePrefixedBareChatIdentifier = parseServicePrefixedBareChatIdentifier({
107+
trimmed,
108+
lower,
109+
});
110+
if (servicePrefixedBareChatIdentifier) {
111+
return servicePrefixedBareChatIdentifier;
112+
}
113+
85114
const servicePrefixed = resolveServicePrefixedChatTarget({
86115
trimmed,
87116
lower,
@@ -109,6 +138,11 @@ export function parseIMessageTarget(raw: string): IMessageTarget {
109138
return chatTarget;
110139
}
111140

141+
const bareChatIdentifier = normalizeBareIMessageChatIdentifier(trimmed);
142+
if (bareChatIdentifier) {
143+
return { kind: "chat_identifier", chatIdentifier: bareChatIdentifier };
144+
}
145+
112146
return { kind: "handle", to: trimmed, service: "auto" };
113147
}
114148

@@ -124,7 +158,8 @@ export function looksLikeIMessageExplicitTargetId(raw: string): boolean {
124158
return (
125159
CHAT_ID_PREFIXES.some((prefix) => lower.startsWith(prefix)) ||
126160
CHAT_GUID_PREFIXES.some((prefix) => lower.startsWith(prefix)) ||
127-
CHAT_IDENTIFIER_PREFIXES.some((prefix) => lower.startsWith(prefix))
161+
CHAT_IDENTIFIER_PREFIXES.some((prefix) => lower.startsWith(prefix)) ||
162+
Boolean(normalizeBareIMessageChatIdentifier(trimmed))
128163
);
129164
}
130165

0 commit comments

Comments
 (0)