Skip to content

fix(smtp): require public SMTP hosts for project and messaging SMTP - #14811

Merged
ChiragAgg5k merged 4 commits into
mainfrom
fix/smtp-public-hosts
Oct 22, 2026
Merged

ChiragAgg5k merged 4 commits into
mainfrom
fix/smtp-public-hosts

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Project SMTP and messaging SMTP providers are dialled on a host the project chooses, which was only checked for its format. This applies the same public-address rules (publicHostname, _APP_ALLOWED_INTERNAL_ADDRESSES) that webhooks and migration sources already use. Operator _APP_SMTP_* configuration is not affected.

  • New Appwrite\SMTP\Validator\Host reads a host the way the SMTP adapter does (;-separated servers, ssl:///tls:// prefixes, :port, bracketed IPv6) and checks every server.
  • Project SMTP update: dials the checked address (certificate still verified against the host name) and returns a generic, step-specific error instead of the server's reply text.
  • Project SMTP test endpoint: refuses a private host with 400.
  • Messaging SMTP provider create/update: refuses a host that resolves to a private or reserved address with 400. A host that does not resolve yet is still accepted, since it is checked again at send time.
  • Mails and messaging workers: check the stored host again before every send (covering configurations saved before this change) and dial the checked addresses. The messaging SMTP adapter gains an addresses option for that.
  • appwrite-worker-mails now receives _APP_DNS_EXTERNAL and _APP_ALLOWED_INTERNAL_ADDRESSES like the other workers.

Messaging deliveryErrors keep the per-recipient SMTP replies (for example 550 5.1.1 User unknown): with the host checks in place they only ever come from a public server the project configured, and they are what makes a failed send actionable.

Self-hosted installs that point project SMTP at a private relay need to list it in _APP_ALLOWED_INTERNAL_ADDRESSES, as for webhooks.

Test plan

  • Unit: tests/unit/SMTP/Validator/HostTest.php, tests/unit/Network/Validators/PublicHostnameTest.php
  • bin/monorepo check messaging / bin/monorepo test messaging (new SMTPAddressesTest)
  • Pint, PHPStan and Rector on touched files
  • E2E: testCreateSMTPProviderPrivateHost, testUpdateSMTPProviderPrivateHost (Messaging); testUpdateSMTPPrivateHost, testUpdateSMTPRefusedCredentialsDoNotEchoServer, testCreateSMTPTestPrivateHost (Project); existing maildev-based SMTP tests keep passing

Related: appwrite-labs/cloud#6674

Project SMTP and messaging SMTP providers are dialled from the API and
the workers on a host the project chooses, which was only checked for
its format. Check every server in the host against the same public
address rules webhooks and migrations use, dial the address checked
(the certificate is still verified against the host), and stop
repeating the server's own reply in the project SMTP error. Operator
_APP_SMTP_* configuration is unchanged.

Adds address pinning to the messaging SMTP adapter and a lenient
PublicHostname::addresses() for hosts stored before they resolve.
@luxor-code-review

luxor-code-review Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Review complete. 🟠 1 high · 🟡 2 medium

💬 Inline comments (2)

📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — Host errors bypass the InvalidArgumentException catch · Mails.php:251–267 · unchanged line

// src/Appwrite/Platform/Workers/Mails.php
251	                addresses: new Host($publicHostname)->addresses($smtp['host']),
252	            );
253	
254	            $send = static function (EmailAdapter $adapter) use ($emailMessage, $telemetry): array {
255	                $adapter->setTelemetry($telemetry);
256	
257	                return $adapter->send($emailMessage);
258	            };
259	
260	            $result = $adapter instanceof EmailAdapter ? $send($adapter) : $register->get('smtp')->use($send);
261	        } catch (InvalidArgumentException $error) {
262	            // The address or name can never be delivered, so a retry cannot help.

The adapter construction at src/Appwrite/Platform/Workers/Mails.php:251 throws PHP's global InvalidArgumentException from Host::addresses()/PublicHostname::address(), but the catch at Mails.php:261 binds Utopia\Messaging\Exception\InvalidArgumentException (imported at line 15), so host errors escape it.

They land in the generic catch (\Throwable) at line 268 and are classified as generic send failures: the intended skipped-with-error-span path never fires for unresolved or refused hosts, the failure spans carry the wrong status, and on the last attempt the mail is acknowledged via abandon() with the message 'Error sending mail: Hostname X does not resolve.' rather than the designed skip.

Impact: observability and end-state classification for project-SMTP host failures diverge from the design the pre-check at line 169 documents.


This PR blocks private, reserved, and internal address targets for project SMTP and messaging SMTP providers by introducing a new Host validator built on an extended PublicHostname that resolves hosts and refuses private/reserved addresses. Provider create/update endpoints now reject unreachable or internal hosts while still allowing hosts that fail to resolve at save time, deferring that check to send time.

Files Change
src/Appwrite/SMTP/Validator/Host.php, src/Appwrite/Network/Validator/PublicHostname.php New SMTP host parser/validator and hardened hostname resolution with private-address refusal
src/Appwrite/Platform/Modules/Messaging/Http/Providers/Smtp/*, src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/* Create/update endpoints validate SMTP hosts (unresolved allowed, private refused)
src/Appwrite/Platform/Workers/Mails.php, src/Appwrite/Platform/Workers/Messaging.php, src/Appwrite/Messaging/Provider.php, packages/messaging/src/Adapter/Email/SMTP.php Workers re-check the dial target at send time and the adapter exposes parsed host/address data
app/init/resources.php, docker-compose.yml, app/config/variables.php Register the publicHostname resource and wire DNS/allowlist env vars to the split workers
tests/unit/*, tests/e2e/Services/Messaging/MessagingBase.php, tests/e2e/Services/Project/SMTPBase.php Unit and e2e coverage for the new validation branches

Main risks center on eager host resolution aborting messaging sends (a transient DNS failure dead-letters the whole message), host errors bypassing the intended catch path in the mails worker, and split-worker compose services missing the _APP_DNS fallback resolver.

Reviewed commit: cf0088d

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 22, 2026
@ghost

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The new end-to-end test covers the previously open late-DNS-failure behavior, and I found no remaining issues in the incremental changes.

Project-configured and messaging SMTP hosts are parsed and checked against public-address policy, with connections pinned to checked addresses while preserving hostname-based TLS verification. Project and messaging APIs reject private hosts, SMTP workers recheck stored hosts during delivery, and the PR adds configuration, validation tests, and API coverage for these behaviors.

Latest changes: The newest commit adds an E2E case that stores an unresolved SMTP host, sends a message through it, and verifies the message reaches a failed state with a recipient delivery error.

Verdict New comments Fixed Still open
✅ Approved 0 1 0
📂 Walkthrough · 19
File Change
app/config/variables.php Documents the DNS and allowed-internal-address settings used by project SMTP.
app/init/resources.php Registers the public-host validator dependency for messaging providers.
docker-compose.yml Passes DNS and internal-address configuration to SMTP-capable workers, including the mail worker.
packages/messaging/src/Adapter/Email/SMTP.php Supports dialing pre-checked SMTP addresses while retaining hostname-based certificate verification.
packages/messaging/tests/Adapter/Email/SMTPAddressesTest.php Covers SMTP dialing with checked addresses and the hostname fallback.
src/Appwrite/Messaging/Provider.php Validates messaging SMTP hosts and passes resolved addresses to the adapter.
src/Appwrite/Network/Validator/PublicHostname.php Resolves public addresses and supports accepting unresolved hostnames for later revalidation.
src/Appwrite/Platform/Modules/Messaging/Http/Providers/Smtp/Create.php Rejects private messaging SMTP hosts while allowing unresolved names to be stored.
src/Appwrite/Platform/Modules/Messaging/Http/Providers/Smtp/Update.php Applies the same host policy when updating messaging SMTP providers.
src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Tests/Create.php Rejects private hosts at the project SMTP test endpoint.
src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php Checks SMTP addresses before connecting and returns generic step-specific errors.
src/Appwrite/Platform/Workers/Mails.php Rechecks project SMTP hosts before delivery and dials the checked addresses.
src/Appwrite/Platform/Workers/Messaging.php Rechecks messaging SMTP hosts during delivery and records validation failures for recipients.
src/Appwrite/SMTP/Validator/Host.php Parses SMTP server lists and validates each host, including producing checked address mappings.
tests/e2e/Services/Messaging/MessagingBase.php Covers rejection of private hosts and verifies unresolved SMTP hosts fail message delivery with an observable error.
tests/e2e/Services/Project/SMTPBase.php Covers private-host rejection and sanitized project SMTP errors.
tests/unit/Network/Validators/PublicHostnameTest.php Tests public-address resolution, including unresolved hostnames.
tests/unit/Platform/Workers/MailsTest.php Provides the host-validator dependency and verifies private project SMTP hosts are skipped.
tests/unit/SMTP/Validator/HostTest.php Tests SMTP host parsing, public/private validation, and server-to-address mapping.
✅ Fixed since the last review · 1
  • Cover SMTP DNS failures during message delivery · src/Appwrite/Platform/Workers/Messaging.php:319

Reviewed the commits since b2e618a · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds SSRF protection for user-supplied SMTP hosts by resolving and validating them at provider create/update and again at send time in the mails and messaging workers.

Key findings

Comment thread src/Appwrite/Messaging/Provider.php Resolved
Comment thread docker-compose.yml Resolved
A refused or unresolved SMTP host threw out of the provider loop and
failed the whole message, including recipients on other providers.
Record it against that provider's recipients instead. Also pass _APP_DNS
to the separate mails and messaging workers, which the public hostname
check falls back to.
@ChiragAgg5k

Copy link
Copy Markdown
Member Author

On the tenki finding outside the diff (Mails.php host errors bypassing the InvalidArgumentException catch): this is intentional. A host that resolves to a private address is caught by the lenient pre-check and skipped, since a retry cannot fix it. A host that does not resolve at send time may be a transient DNS failure, so it goes through the generic retry path and is abandoned only on the last attempt, the same as a connection failure.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/smtp-public-hosts (after).

Metric Before After Change
🚀 Requests/sec 158.72 183.7 🟢 +15.7%
⏱️ Latency P50 107.43 ms 93.64 ms 🟢 -12.8%
⏱️ Latency P95 263.59 ms 224.51 ms 🟢 -14.8%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 93.64 224.51 11,457 183.7 -39.07
Account 171.58 332.23 603 9.97 -42.69
TablesDB 90.61 168.9 6,231 101.26 -24.63
Storage 86.69 206.17 3,015 51.3 -67.08
Functions 131.53 269.07 1,608 28.12 -27.02

Top API waits (after)

API request Max wait (ms)
account.prefs.update 560.45
account.name.update 489.87
storage.buckets.create 396.67
functions.create 381.12
tablesdb.rows.create 373.35

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@ChiragAgg5k
ChiragAgg5k merged commit 1c3636d into main Oct 22, 2026
71 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/smtp-public-hosts branch October 22, 2026 17:57
JadeCong pushed a commit to CloudEngineHub/appwrite that referenced this pull request Oct 23, 2026
The private-host test changes the host after an earlier test stored a
password, so the write-only password rule (appwrite#14795) rejected it before the
public-host check (appwrite#14811) ran.
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…MTP (backport #14811)

Project SMTP and messaging SMTP provider hosts are checked against public addresses on save, on the SMTP test endpoint, and again by the mails and messaging workers before each send. Project SMTP verification dials the checked address of a single server and no longer echoes the server reply. Adds _APP_ALLOWED_INTERNAL_ADDRESSES (introduced in 1.9.10) so an internal relay can still be allowed. Pinning worker connections to the checked address needs the messaging library's SMTP addresses option, which 1.9.x's vendored utopia-php/messaging does not have.

(cherry picked from commit 1c3636d)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…MTP (backport #14811)

Project SMTP and messaging SMTP provider hosts are checked against public addresses on save, on the SMTP test endpoint, and again by the mails and messaging workers before each send. Project SMTP verification dials the checked address of a single server and no longer echoes the server reply. Adds _APP_ALLOWED_INTERNAL_ADDRESSES (introduced in 1.9.10) so an internal relay can still be allowed. Pinning worker connections to the checked address needs the messaging library's SMTP addresses option, which 1.9.x's vendored utopia-php/messaging does not have.

(cherry picked from commit 1c3636d)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…MTP (backport #14811)

Project SMTP and messaging SMTP provider hosts are checked against public addresses on save, on the SMTP test endpoint, and again by the mails and messaging workers before each send. Project SMTP verification dials the checked address of a single server and no longer echoes the server reply. Adds _APP_ALLOWED_INTERNAL_ADDRESSES (introduced in 1.9.10) so an internal relay can still be allowed. Pinning worker connections to the checked address needs the messaging library's SMTP addresses option, which 1.9.x's vendored utopia-php/messaging does not have.

(cherry picked from commit 1c3636d)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…MTP (backport #14811)

Project SMTP and messaging SMTP provider hosts are checked against public addresses on save, on the SMTP test endpoint, and again by the mails and messaging workers before each send. Project SMTP verification dials the checked address of a single server and no longer echoes the server reply. Adds _APP_ALLOWED_INTERNAL_ADDRESSES (introduced in 1.9.10) so an internal relay can still be allowed. Pinning worker connections to the checked address needs the messaging library's SMTP addresses option, which 1.9.x's vendored utopia-php/messaging does not have.

(cherry picked from commit 1c3636d)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…MTP (backport #14811)

Project SMTP and messaging SMTP provider hosts are checked against public addresses on save, on the SMTP test endpoint, and again by the mails and messaging workers before each send. Project SMTP verification dials the checked address of a single server and no longer echoes the server reply. Adds _APP_ALLOWED_INTERNAL_ADDRESSES (introduced in 1.9.10) so an internal relay can still be allowed. Pinning worker connections to the checked address needs the messaging library's SMTP addresses option, which 1.9.x's vendored utopia-php/messaging does not have.

(cherry picked from commit 1c3636d)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…MTP (backport #14811)

Project SMTP and messaging SMTP provider hosts are checked against public addresses on save, on the SMTP test endpoint, and again by the mails and messaging workers before each send. Project SMTP verification dials the checked address of a single server and no longer echoes the server reply. Adds _APP_ALLOWED_INTERNAL_ADDRESSES (introduced in 1.9.10) so an internal relay can still be allowed. Pinning worker connections to the checked address needs the messaging library's SMTP addresses option, which 1.9.x's vendored utopia-php/messaging does not have.

(cherry picked from commit 1c3636d)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…MTP (backport #14811)

Project SMTP and messaging SMTP provider hosts are checked against public addresses on save, on the SMTP test endpoint, and again by the mails and messaging workers before each send. Project SMTP verification dials the checked address of a single server and no longer echoes the server reply. Adds _APP_ALLOWED_INTERNAL_ADDRESSES (introduced in 1.9.10) so an internal relay can still be allowed. Pinning worker connections to the checked address needs the messaging library's SMTP addresses option, which 1.9.x's vendored utopia-php/messaging does not have.

(cherry picked from commit 1c3636d)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…MTP (backport #14811)

Project SMTP and messaging SMTP provider hosts are checked against public addresses on save, on the SMTP test endpoint, and again by the mails and messaging workers before each send. Project SMTP verification dials the checked address of a single server and no longer echoes the server reply. Adds _APP_ALLOWED_INTERNAL_ADDRESSES (introduced in 1.9.10) so an internal relay can still be allowed. Pinning worker connections to the checked address needs the messaging library's SMTP addresses option, which 1.9.x's vendored utopia-php/messaging does not have.

(cherry picked from commit 1c3636d)
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
fix(smtp): require public SMTP hosts for project and messaging SMTP

(cherry picked from commit 1c3636d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant