Repository navigation
fix(settings): Don't fail when a stored domain is added again #13976
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
Open
DerDreschner
wants to merge
3
commits into
main
Choose a base branch
from
fix/domain-duplicate-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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,19 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Mail\Exception; | ||
|
|
||
| use OCP\AppFramework\Http; | ||
|
|
||
| class InvalidAddressException extends ClientException { | ||
| #[\Override] | ||
| public function getHttpCode(): int { | ||
| return Http::STATUS_UNPROCESSABLE_ENTITY; | ||
| } | ||
| } |
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,128 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Mail\Service; | ||
|
|
||
| use Horde_Mail_Exception; | ||
| use Horde_Mail_Rfc822; | ||
| use Horde_Mail_Rfc822_Address; | ||
| use OCA\Mail\Exception\InvalidAddressException; | ||
|
|
||
| class AddressOrDomainNormalizer { | ||
| private const TYPE_INDIVIDUAL = 'individual'; | ||
| private const TYPE_DOMAIN = 'domain'; | ||
|
|
||
| /** RFC 5321, section 4.5.3.1.3: a path has at most 256 octets, including the angle brackets */ | ||
| private const MAX_LENGTH = 254; | ||
|
|
||
| /** Stricter than RFC 5322 and 6532, but some writing systems need the joiners U+200C and U+200D */ | ||
| private const DISALLOWED_CHARACTERS = '/(?![\x{200C}\x{200D}])[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u'; | ||
|
|
||
| private const IDNA_OPTIONS = IDNA_NONTRANSITIONAL_TO_ASCII | IDNA_USE_STD3_RULES | IDNA_CHECK_BIDI | IDNA_CHECK_CONTEXTJ; | ||
|
|
||
| /** | ||
| * @throws InvalidAddressException | ||
| */ | ||
| public function normalize(string $value, string $type): string { | ||
| $normalized = match ($type) { | ||
| self::TYPE_INDIVIDUAL => $this->normalizeAddress($value), | ||
| self::TYPE_DOMAIN => $this->normalizeDomain($value), | ||
| default => throw new InvalidAddressException('Invalid type'), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. throws InvalidAddressException('Invalid type'), which reports a programming mistake as bad user input. An enum, or reusing the constants the mappers already have, would make that case impossible. |
||
| }; | ||
|
|
||
| // IDN decoding can lengthen the value, and Oracle's columns count bytes | ||
| if (strlen($normalized) > self::MAX_LENGTH) { | ||
| throw new InvalidAddressException('Email address or domain too long'); | ||
| } | ||
|
|
||
| return $normalized; | ||
| } | ||
|
|
||
| /** | ||
| * @throws InvalidAddressException | ||
| */ | ||
| private function normalizeAddress(string $value): string { | ||
| if (strlen($value) > self::MAX_LENGTH | ||
| || !mb_check_encoding($value, 'UTF-8') | ||
| || preg_match(self::DISALLOWED_CHARACTERS, $value) === 1) { | ||
| throw new InvalidAddressException('Invalid email address'); | ||
| } | ||
|
|
||
| try { | ||
| // RFC 5322, with the UTF-8 addresses of RFC 6532 | ||
| $addresses = (new Horde_Mail_Rfc822())->parseAddressList($value, ['validate' => 'eai']); | ||
| } catch (Horde_Mail_Exception) { | ||
| throw new InvalidAddressException('Invalid email address'); | ||
| } | ||
|
|
||
| $address = $addresses->first(); | ||
| $separator = strrpos($value, '@'); | ||
| // Horde accepts a trailing dot, over-long labels and any address literal | ||
| $host = $separator === false ? null : $this->toAsciiHost(substr($value, $separator + 1)); | ||
| if (count($addresses) !== 1 | ||
| || !($address instanceof Horde_Mail_Rfc822_Address) | ||
| || $host === null) { | ||
| throw new InvalidAddressException('Invalid email address'); | ||
| } | ||
|
|
||
| $address->host = $host; | ||
| return $address->bare_address; | ||
| } | ||
|
|
||
| /** | ||
| * @throws InvalidAddressException | ||
| */ | ||
| private function normalizeDomain(string $domain): string { | ||
| $host = $this->toAsciiHost($domain); | ||
| if ($host === null) { | ||
| throw new InvalidAddressException('Invalid domain'); | ||
| } | ||
|
|
||
| // Same form as the host of a parsed sender: IDN-decoded and lowercased by Horde | ||
| $address = new Horde_Mail_Rfc822_Address(); | ||
| $address->host = $host; | ||
| return $address->host; | ||
| } | ||
|
|
||
| private function toAsciiHost(string $domain): ?string { | ||
| if (str_starts_with($domain, '[') && str_ends_with($domain, ']')) { | ||
| $literal = substr($domain, 1, -1); | ||
| return $this->isValidAddressLiteral($literal) ? $literal : null; | ||
| } | ||
|
|
||
| if ($domain === '') { | ||
| return null; | ||
| } | ||
|
|
||
| $asciiDomain = idn_to_ascii($domain, self::IDNA_OPTIONS, INTL_IDNA_VARIANT_UTS46); | ||
| // UTS #46 accepts a trailing dot, RFC 5322 doesn't | ||
| if ($asciiDomain === false || str_ends_with($asciiDomain, '.')) { | ||
| return null; | ||
| } | ||
|
|
||
| // RFC 3696, section 2: only an IPv4 address ends with an all-numeric label | ||
| if (preg_match('/(^|\.)[0-9]+$/', $asciiDomain) === 1 | ||
| && filter_var($asciiDomain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { | ||
| return null; | ||
| } | ||
|
|
||
| return $asciiDomain; | ||
| } | ||
|
|
||
| /** | ||
| * RFC 5321, section 4.1.3 | ||
| */ | ||
| private function isValidAddressLiteral(string $literal): bool { | ||
| if (stripos($literal, 'IPv6:') === 0) { | ||
| return filter_var(substr($literal, 5), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false; | ||
| } | ||
|
|
||
| return filter_var($literal, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false; | ||
| } | ||
| } | ||
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
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.
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.
Rename it after what it produces, not what it accepts, such as TrustedEntryNormalizer, or something that matches the individual/domain type values.