-
Notifications
You must be signed in to change notification settings - Fork 577
fix(commands): Improve phone number input validation of OCC commands #17173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nickvergessen
merged 3 commits into
main
from
bugfix/noid/improve-phonenumber-validation
Mar 30, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Talk\Service; | ||
|
|
||
| use OCP\IConfig; | ||
| use OCP\IPhoneNumberUtil; | ||
|
|
||
| class PhoneNumberValidation { | ||
|
|
||
| public function __construct( | ||
| protected IPhoneNumberUtil $phoneNumberUtil, | ||
| protected IConfig $config, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Validate input as a phone number | ||
| * | ||
| * - Local number: allow | ||
| * - International number | ||
| * a. If valid, strip + and allow | ||
| * b. If invalid, throw | ||
| * @throws \InvalidArgumentException When the number is invalid | ||
| */ | ||
| public function validateNumber(string $phoneNumber): string { | ||
|
|
||
| if ( | ||
| // Not an internation number | ||
| !str_starts_with($phoneNumber, '0') | ||
| // And matches a local number or dial-through | ||
| && preg_match('/^[0-9]{1,20}$/', $phoneNumber) | ||
| ) { | ||
| return $phoneNumber; | ||
| } | ||
|
|
||
| $defaultRegion = $this->config->getSystemValueString('default_phone_region') ?: null; | ||
| $standardPhoneNumber = $this->phoneNumberUtil->convertToStandardFormat($phoneNumber, $defaultRegion); | ||
|
|
||
| if ($standardPhoneNumber === null) { | ||
| throw new \InvalidArgumentException(); | ||
| } | ||
|
|
||
| if (str_starts_with($standardPhoneNumber, '+')) { | ||
| return substr($standardPhoneNumber, 1); | ||
| } | ||
|
|
||
| throw new \InvalidArgumentException(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fancycode is the free pass for local numbers okay? Or should e.g.
0711be replaced with49711if DE is specified as default phone prefix. I assume it would be bad, as it would prevent specifying 0711… even if that would be what your SIP gate receivesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An internal extension should never start with a
0, so you could add the international prefix if configured (i.e.071112345becomes+4971112345) or just reject such numbers.To make things worse, the
00prefix for international numbers is only if calling from Germany to another country. For example calling from the US to another country needs011as prefix, from Australia it's0011. See https://en.wikipedia.org/wiki/List_of_international_call_prefixes for other prefixes. So replacing00with a+will fail for Australian people trying to configure00114971112345as German number in Stuttgart.You could try to guess based on the default phone prefix what the user might try to do, but maybe the easiest is to simply reject numbers that start with a
0.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the only thing I can do is look for a leading 0 or +
try to convert to standard and strip of the +, if there was no + return it's invalid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think that would be the best, though libphonenumber supports detecting valid numbers with a leading
0if a country is configured.Some testcases (assuming DE is configured as default phone country):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is striping off a leading 1, but sure why. But the normalizing returns:
+12345678901so only a single1not 2 as you posted.