Skip to content

Commit f1a374a

Browse files
morlutosteipete
andauthored
fix(channels): normalize phone identities with stray plus signs (#100467)
* fix(channels): canonicalize phone identity digits * test(channels): cover phone identity normalization * test(whatsapp): use shared phone normalizer * fix(channels): reject invalid normalized phone identities * fix(signal): preserve strict account input validation * fix(signal): preserve prefixed allowlist input * docs(changelog): stabilize phone identity entry * chore(changelog): defer phone identity entry --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 3c2316c commit f1a374a

7 files changed

Lines changed: 54 additions & 22 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ describe("normalizeIMessageMessagingTarget", () => {
99

1010
it("preserves service prefixes for handles", () => {
1111
expect(normalizeIMessageMessagingTarget("sms:+1 (555) 222-3333")).toBe("sms:+15552223333");
12+
expect(normalizeIMessageMessagingTarget("sms:++1 (555) 222-3333")).toBe("sms:+15552223333");
13+
});
14+
15+
it("preserves non-phone handles instead of collapsing them to a plus sign", () => {
16+
expect(normalizeIMessageMessagingTarget("auto:Alice Smith")).toBe("auto:AliceSmith");
1217
});
1318

1419
it("drops service prefixes for chat targets", () => {

‎extensions/signal/src/core.test.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as clientModule from "./client-adapter.js";
1616
import { classifySignalCliLogLine } from "./daemon.js";
1717
import {
1818
looksLikeUuid,
19+
normalizeSignalAllowRecipient,
1920
resolveSignalPeerId,
2021
resolveSignalRecipient,
2122
resolveSignalSender,
@@ -73,6 +74,22 @@ describe("signal sender identity", () => {
7374
});
7475
});
7576

77+
it("falls back to sourceUuid when sourceNumber has no digits", () => {
78+
const sender = resolveSignalSender({
79+
sourceNumber: "not a phone number",
80+
sourceUuid: "123e4567-e89b-12d3-a456-426614174000",
81+
});
82+
expect(sender).toEqual({
83+
kind: "uuid",
84+
raw: "123e4567-e89b-12d3-a456-426614174000",
85+
});
86+
});
87+
88+
it("normalizes noisy allowlist numbers and rejects digit-free entries", () => {
89+
expect(normalizeSignalAllowRecipient("signal:++1 (555) 000-1111")).toBe("+15550001111");
90+
expect(normalizeSignalAllowRecipient("signal:not a phone number")).toBeUndefined();
91+
});
92+
7693
it("maps uuid senders to recipient and peer ids", () => {
7794
const sender = { kind: "uuid", raw: "123e4567-e89b-12d3-a456-426614174000" } as const;
7895
expect(resolveSignalRecipient(sender)).toBe("123e4567-e89b-12d3-a456-426614174000");
@@ -843,7 +860,9 @@ describe("signal setup parsing", () => {
843860

844861
it("parses e164, uuid and wildcard entries", () => {
845862
expect(
846-
parseSignalAllowFromEntries("+15555550123, uuid:123e4567-e89b-12d3-a456-426614174000, *"),
863+
parseSignalAllowFromEntries(
864+
"signal:+15555550123, uuid:123e4567-e89b-12d3-a456-426614174000, *",
865+
),
847866
).toEqual({
848867
entries: ["+15555550123", "uuid:123e4567-e89b-12d3-a456-426614174000", "*"],
849868
});

‎extensions/signal/src/identity.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ export function resolveSignalSender(params: {
2424
}): SignalSender | null {
2525
const sourceNumber = params.sourceNumber?.trim();
2626
if (sourceNumber) {
27-
return {
28-
kind: "phone",
29-
raw: sourceNumber,
30-
e164: normalizeE164(sourceNumber),
31-
};
27+
const e164 = normalizeE164(sourceNumber);
28+
if (e164) {
29+
return { kind: "phone", raw: sourceNumber, e164 };
30+
}
3231
}
3332
const sourceUuid = params.sourceUuid?.trim();
3433
if (sourceUuid) {
@@ -83,7 +82,8 @@ function parseSignalAllowEntry(entry: string): SignalAllowEntry | null {
8382
return { kind: "uuid", raw: stripped };
8483
}
8584

86-
return { kind: "phone", e164: normalizeE164(stripped) };
85+
const e164 = normalizeE164(stripped);
86+
return e164 ? { kind: "phone", e164 } : null;
8787
}
8888

8989
export function normalizeSignalAllowRecipient(entry: string): string | undefined {

‎extensions/signal/src/setup-core.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ export function normalizeSignalAccountInput(value: string | null | undefined): s
4141
if (!trimmed) {
4242
return null;
4343
}
44-
const normalized = normalizeE164(trimmed);
44+
const phoneInput = trimmed.replace(/^signal:/i, "").trim();
45+
// Setup accepts formatting punctuation, but embedded or duplicate pluses are invalid input.
46+
const plusCount = phoneInput.match(/\+/g)?.length ?? 0;
47+
if (plusCount > 1 || (plusCount === 1 && !phoneInput.startsWith("+"))) {
48+
return null;
49+
}
50+
const normalized = normalizeE164(phoneInput);
4551
const digits = normalized.slice(1);
4652
if (!DIGITS_ONLY.test(digits)) {
4753
return null;

‎extensions/whatsapp/src/channel.setup.test.ts‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,6 @@ vi.mock("openclaw/plugin-sdk/setup", async () => {
5858
const actual = await vi.importActual<typeof import("openclaw/plugin-sdk/setup")>(
5959
"openclaw/plugin-sdk/setup",
6060
);
61-
const normalizeE164 = (value?: string | null) => {
62-
const raw = (value ?? "").trim();
63-
if (!raw) {
64-
return "";
65-
}
66-
const digits = raw.replace(/[^\d+]/g, "");
67-
return digits.startsWith("+") ? digits : `+${digits}`;
68-
};
6961
return {
7062
...actual,
7163
DEFAULT_ACCOUNT_ID,
@@ -80,7 +72,6 @@ vi.mock("openclaw/plugin-sdk/setup", async () => {
8072
}
8173
return [...normalized];
8274
},
83-
normalizeE164,
8475
splitSetupEntries: splitSetupEntriesForMock,
8576
setSetupChannelEnabled: (cfg: OpenClawConfig, channel: string, enabled: boolean) => ({
8677
...cfg,

‎src/utils.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { withEnv } from "./test-utils/env.js";
88
import {
99
CONFIG_DIR,
1010
ensureDir,
11+
normalizeE164,
1112
pinConfigDir,
1213
resolveConfigDir,
1314
resolveHomeDir,
@@ -56,6 +57,19 @@ describe("sleep", () => {
5657
});
5758
});
5859

60+
describe("normalizeE164", () => {
61+
it.each([
62+
["+1234567890", "+1234567890"],
63+
["++1234567890", "+1234567890"],
64+
["1+234+567", "+1234567"],
65+
["whatsapp:+1 (234) 567-8900", "+12345678900"],
66+
["signal: 1 234 567", "+1234567"],
67+
["not a phone number", ""],
68+
])("normalizes %s", (input, expected) => {
69+
expect(normalizeE164(input)).toBe(expected);
70+
});
71+
});
72+
5973
describe("resolveConfigDir", () => {
6074
it("prefers ~/.openclaw when legacy dir is missing", async () => {
6175
await withTempDir({ prefix: "openclaw-config-dir-" }, async (root) => {

‎src/utils.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,8 @@ export function isRecord(value: unknown): value is Record<string, unknown> {
5555
/** Normalizes phone-like input into the loose E.164 shape used by channel helpers. */
5656
export function normalizeE164(number: string): string {
5757
const withoutPrefix = number.replace(/^[a-z][a-z0-9-]*:/i, "").trim();
58-
const digits = withoutPrefix.replace(/[^\d+]/g, "");
59-
if (digits.startsWith("+")) {
60-
return `+${digits.slice(1)}`;
61-
}
62-
return `+${digits}`;
58+
const digits = withoutPrefix.replace(/\D/g, "");
59+
return digits ? `+${digits}` : "";
6360
}
6461

6562
// Surrogate-safe slicing helpers live in a node-free leaf module so browser/UI

0 commit comments

Comments
 (0)