From 4859ea4e0b819133c838ceb47f5c82ac13ba228a Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 11 Sep 2026 17:23:50 +0530 Subject: [PATCH 1/6] fix(messaging): accept alphanumeric Twilio sender IDs on create Create checked `from` with the phone validator while update accepted any text up to 256 characters, so an alphanumeric sender ID could only be set through update. Migrations re-create providers through create and hit the same check. Create now uses the same Text(256) rule as update, and both describe `from` as a phone number or an alphanumeric sender ID. Twilio also accepts short codes and Messaging Service SIDs as the sender, and rejects an invalid sender when the message is sent. --- .../Modules/Messaging/Http/Providers/Twilio/Create.php | 3 +-- .../Modules/Messaging/Http/Providers/Twilio/Update.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Create.php b/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Create.php index e453bb054c..2908b495d7 100644 --- a/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Create.php +++ b/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Create.php @@ -2,7 +2,6 @@ namespace Appwrite\Platform\Modules\Messaging\Http\Providers\Twilio; -use Appwrite\Auth\Validator\Phone; use Appwrite\Event\Event; use Appwrite\Extend\Exception; use Appwrite\SDK\AuthType; @@ -56,7 +55,7 @@ class Create extends Action )) ->param('providerId', '', fn (Database $dbForProject) => new CustomId(false, $dbForProject->getAdapter()->getMaxUIDLength()), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.', false, ['dbForProject']) ->param('name', '', new Text(128), 'Provider name.') - ->param('from', '', new Phone(), 'Sender Phone number. Format this number with a leading \'+\' and a country code, e.g., +16175551212.', true) + ->param('from', '', new Text(256), 'Sender phone number or alphanumeric sender ID. Format phone numbers with a leading \'+\' and a country code, e.g., +16175551212.', true) ->param('accountSid', '', new Text(0), 'Twilio account secret ID.', true) ->param('authToken', '', new Text(0), 'Twilio authentication token.', true) ->param('enabled', null, new Nullable(new Boolean()), 'Set as enabled.', true) diff --git a/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Update.php b/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Update.php index 0f60946613..83b63222e1 100644 --- a/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Update.php +++ b/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Update.php @@ -55,7 +55,7 @@ class Update extends Action ->param('enabled', null, new Nullable(new Boolean()), 'Set as enabled.', true) ->param('accountSid', '', new Text(0), 'Twilio account secret ID.', true) ->param('authToken', '', new Text(0), 'Twilio authentication token.', true) - ->param('from', '', new Text(256), 'Sender number.', true) + ->param('from', '', new Text(256), 'Sender phone number or alphanumeric sender ID. Format phone numbers with a leading \'+\' and a country code, e.g., +16175551212.', true) ->inject('queueForEvents') ->inject('dbForProject') ->inject('response') From e6da051a82643163bf470ded5415f7768c37c509 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 11 Sep 2026 17:23:50 +0530 Subject: [PATCH 2/6] fix(messaging): require a full Messaging Service SID in _APP_SMS_FROM Any _APP_SMS_FROM starting with MG was also sent to Twilio as a Messaging Service SID, so an alphanumeric sender ID such as "MGM Resorts" went out as an invalid MessagingServiceSid. Only a value in Twilio's SID format, MG followed by 32 hex characters, is treated as one now. The variable's description mentions alphanumeric sender IDs. --- app/config/variables.php | 2 +- src/Appwrite/Platform/Workers/Messaging.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/config/variables.php b/app/config/variables.php index ef208047a6..ee06bec7d1 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -873,7 +873,7 @@ return [ ], [ 'name' => '_APP_SMS_FROM', - 'description' => 'Phone number used for sending out messages. If using Twilio, this may be a Messaging Service SID, starting with MG. Otherwise, the number must start with a leading \'+\' and maximum of 15 digits without spaces (+123456789). ', + 'description' => 'Phone number, or an alphanumeric sender ID where the provider supports it, used for sending out messages. A phone number must start with a leading \'+\' and have a maximum of 15 digits without spaces (+123456789). If using Twilio, this may also be a Messaging Service SID, starting with MG.', 'introduction' => '0.15.0', 'default' => '', 'required' => false, diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index 4e53ecd759..d8b09e1068 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -1278,9 +1278,9 @@ class Messaging extends Action 'twilio' => [ 'accountSid' => $user, 'authToken' => $password, - // Twilio Messaging Service SIDs always start with MG - // https://www.twilio.com/docs/messaging/services - 'messagingServiceSid' => \str_starts_with($from, 'MG') ? $from : null + // Alphanumeric sender IDs can also start with MG, so require the full Messaging Service SID format + // https://www.twilio.com/docs/messaging/api/service-resource + 'messagingServiceSid' => \preg_match('/^MG[0-9a-fA-F]{32}$/', $from) ? $from : null ], 'textmagic' => [ 'username' => $user, @@ -1313,7 +1313,7 @@ class Messaging extends Action }, 'options' => match ($host) { 'twilio' => [ - 'from' => \str_starts_with($from, 'MG') ? null : $from + 'from' => \preg_match('/^MG[0-9a-fA-F]{32}$/', $from) ? null : $from ], default => [ 'from' => $from From 6820221ebd2d621ad3b07f0a9f68fd22d4822259 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 11 Sep 2026 17:23:50 +0530 Subject: [PATCH 3/6] test(messaging): cover alphanumeric Twilio senders on create and update Before the create change, creating a Twilio provider with an alphanumeric sender returned 400. --- .../e2e/Services/Messaging/MessagingBase.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/e2e/Services/Messaging/MessagingBase.php b/tests/e2e/Services/Messaging/MessagingBase.php index 35e8b933c5..d0a4f59140 100644 --- a/tests/e2e/Services/Messaging/MessagingBase.php +++ b/tests/e2e/Services/Messaging/MessagingBase.php @@ -1022,6 +1022,49 @@ trait MessagingBase $this->assertEquals("Invalid `serviceAccountJSON` param: FCM service account JSON must include a non-empty 'client_email' field, which identifies the service account used for authentication. or null", $response['body']['message']); } + public function testCreateTwilioProviderAlphanumericSender(): void + { + $response = $this->client->call(Client::METHOD_POST, '/messaging/providers/twilio', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'providerId' => ID::unique(), + 'name' => 'Twilio', + 'accountSid' => 'my-accountSid', + 'authToken' => 'my-authToken', + 'from' => 'Appwrite', + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertEquals('Appwrite', $response['body']['options']['from']); + } + + public function testUpdateTwilioProviderAlphanumericSender(): void + { + $provider = $this->client->call(Client::METHOD_POST, '/messaging/providers/twilio', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'providerId' => ID::unique(), + 'name' => 'Twilio', + ]); + + $this->assertEquals(201, $provider['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_PATCH, '/messaging/providers/twilio/' . $provider['body']['$id'], [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'from' => 'Appwrite', + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('Appwrite', $response['body']['options']['from']); + } + public function testUpdateProviderMissingCredentialsThrows(): void { // Create new FCM provider with no serviceAccountJSON From 33fadf8f93e634de02c63182c9078067c0ea7547 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Sun, 13 Sep 2026 01:15:34 +0530 Subject: [PATCH 4/6] refactor(messaging): detect Messaging Service SIDs by prefix and length A Messaging Service SID is always MG followed by 32 characters, and an alphanumeric sender ID is at most 11, so the MG prefix plus a length of 34 tells them apart without a regular expression. --- src/Appwrite/Platform/Workers/Messaging.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index d8b09e1068..ce012272cb 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -1278,9 +1278,9 @@ class Messaging extends Action 'twilio' => [ 'accountSid' => $user, 'authToken' => $password, - // Alphanumeric sender IDs can also start with MG, so require the full Messaging Service SID format + // Messaging Service SIDs are always 34 characters; alphanumeric sender IDs, at most 11, can also start with MG // https://www.twilio.com/docs/messaging/api/service-resource - 'messagingServiceSid' => \preg_match('/^MG[0-9a-fA-F]{32}$/', $from) ? $from : null + 'messagingServiceSid' => \str_starts_with($from, 'MG') && \strlen($from) === 34 ? $from : null ], 'textmagic' => [ 'username' => $user, @@ -1313,7 +1313,7 @@ class Messaging extends Action }, 'options' => match ($host) { 'twilio' => [ - 'from' => \preg_match('/^MG[0-9a-fA-F]{32}$/', $from) ? null : $from + 'from' => \str_starts_with($from, 'MG') && \strlen($from) === 34 ? null : $from ], default => [ 'from' => $from From 71951303ae875456d4118d602cdbcb25f87ecd55 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Sun, 13 Sep 2026 01:15:34 +0530 Subject: [PATCH 5/6] chore: discourage regular expressions in AGENTS.md and Greptile rules Regular expressions are hard to review and easy to get subtly wrong. AGENTS.md now asks for string functions or an existing validator instead, and a Greptile rule flags new ones in PHP files. --- .greptile/config.json | 10 ++++++++++ AGENTS.md | 1 + 2 files changed, 11 insertions(+) create mode 100644 .greptile/config.json diff --git a/.greptile/config.json b/.greptile/config.json new file mode 100644 index 0000000000..bc8a608f63 --- /dev/null +++ b/.greptile/config.json @@ -0,0 +1,10 @@ +{ + "rules": [ + { + "id": "no-regex", + "rule": "Flag every new regular expression (preg_match, preg_replace, preg_split, or a regex pattern passed to a validator). Ask for plain string functions instead, such as str_starts_with, str_ends_with, str_contains, strlen, substr, explode or ctype_*, or an existing validator. Regex is hard to review and easy to get subtly wrong. Accept one only when no string-function alternative exists and the PR explains why.", + "scope": ["**/*.php"], + "severity": "high" + } + ] +} diff --git a/AGENTS.md b/AGENTS.md index da9c7beadc..dd25856331 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -222,6 +222,7 @@ Actions should read like a story. The `action()` method is the plot: [`Teams/Htt ## Conventions - PSR-12 (Pint), PSR-4 autoloading. Avoid dependencies outside the `utopia-php` ecosystem. Never hardcode credentials — use env vars. Code changes may require a container restart; logs live on the relevant container. +- Do not add regular expressions (`preg_*`, regex patterns passed to validators). Use string functions (`str_starts_with`, `str_ends_with`, `str_contains`, `strlen`, `substr`, `explode`, `ctype_*`) or an existing validator instead; regex is hard to review and easy to get subtly wrong. If nothing else works, explain why in the PR. - When updating documents, pass only changed attributes as a sparse Document: ```php From 29c257a7024e5178c42f00156746a7e0343bc36f Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Sun, 13 Sep 2026 01:17:10 +0530 Subject: [PATCH 6/6] chore: remove the Greptile config The regex guidance stays in AGENTS.md. --- .greptile/config.json | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 .greptile/config.json diff --git a/.greptile/config.json b/.greptile/config.json deleted file mode 100644 index bc8a608f63..0000000000 --- a/.greptile/config.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "rules": [ - { - "id": "no-regex", - "rule": "Flag every new regular expression (preg_match, preg_replace, preg_split, or a regex pattern passed to a validator). Ask for plain string functions instead, such as str_starts_with, str_ends_with, str_contains, strlen, substr, explode or ctype_*, or an existing validator. Regex is hard to review and easy to get subtly wrong. Accept one only when no string-function alternative exists and the PR explains why.", - "scope": ["**/*.php"], - "severity": "high" - } - ] -}