Skip to content

fix(migrations): validate source endpoints - #14035

Closed
abnegate wants to merge 11 commits into
2.0.xfrom
fix/migration-endpoint-ssrf-2.0.x
Closed

abnegate wants to merge 11 commits into
2.0.xfrom
fix/migration-endpoint-ssrf-2.0.x

Conversation

@abnegate

@abnegate abnegate commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

What

GET /v1/migrations/appwrite/report and POST /v1/migrations/appwrite checked the source endpoint only with Utopia\Validator\URL. They now use a migrations Endpoint validator built on a new PublicURL validator (ported from main): http or https only, a known public domain or public IP, and every resolved address public.

The migrations worker validates a stored Appwrite-source endpoint again before it builds the source, so migrations queued earlier or retried follow the same rule. The internal endpoint the worker writes itself (http://{_APP_MIGRATION_HOST}/v1) is exempt.

Connection pinning

Validation alone leaves a window between the DNS lookup and the requests the worker sends, and the source SDK followed redirects anywhere. The worker now pins every request the Appwrite source makes:

  • PublicHostname::getResolve(int $port) and PublicURL::getResolve() are ported from main. After a successful check they return CURLOPT_RESOLVE entries (host:port:address[,address]) for the addresses that were checked; IP literals and allowlisted hostnames return none.
  • The migrations Endpoint validator gains resolve(string $url), which validates the URL and returns getResolve(), or throws general_argument_invalid with the same fixed description.
  • GET /v1/migrations/appwrite/report is pinned the same way: the action calls setResolver((new Endpoint())->resolve(...)) on the source it builds, before report(). Without this, report() resolves the host again, so a rebinding host could pass the param validator and then point the report requests at a private address.
  • In processSource(), after the Appwrite source is built and before report() runs, the worker calls $source->setResolver((new Endpoint())->resolve(...)) unless the endpoint is the internal http://{_APP_MIGRATION_HOST}/v1. Each SDK and Target::call() request URL is validated again and cURL connects only to the checked addresses. The internal endpoint is computed in one getInternalEndpoint() helper shared with processMigration(), which keeps its early validation.
  • The resolver runs per request inside the worker's hooked coroutines, so main's coroutine DNS fix is ported as well (see below for the unhooked case).

Dependency

This relies on utopia-php/migration#230, the 2.0.x backport of utopia-php/migration#229. It adds setResolver() and refuses redirects (3xx raises an exception with the status as its code) on the Appwrite source, through an SDK 27 Client::call() override. composer.json requires dev-feat/source-request-controls-2.0.x as 2.0.9 until utopia-php/migration 2.0.9 is tagged, and the lock change covers only that package. This needs switching to ^2.0.9 before merge. Because the branch is cut from 2.0.8, it also brings in the 2.0.7 and 2.0.8 OAuth2 provider allow-list additions (TikTok, Kakao, Webflow).

Validator corrections this relies on

  • PublicHostname treats IPv4-compatible IPv6 (::/96, e.g. ::7f00:1) and site-local (fec0::/10) addresses as reserved.
  • PublicURL refuses URLs with userinfo or backslashes, where URL parsers and curl can disagree on the host.
  • Numeric IPv4 spellings (2130706433, 0x7f.1, 127.1) are never treated as allowed hostnames.
  • Endpoint reports one fixed description, so a rejection does not reveal whether a host exists or what it resolves to.

Opt-in allowlist

_APP_MIGRATIONS_ALLOWED_HOSTS takes comma-separated hostnames and IPv4/IPv6 CIDR ranges for sources that are deliberately private, such as another instance on the same network. A listed hostname, an IP in a listed range, or a hostname whose resolved addresses are all public or listed is accepted. Hostnames match exactly: example.com does not allow evil-example.com or db.example.com. The default is empty, which allows public sources only. The development .env lists appwrite and the compose network 172.16.238.0/24 so the local stack can migrate from itself.

Effect on other callers

PublicHostname takes an optional Allowlist that defaults to empty, and its lookup is late-static-bound so tests can substitute it. Its other callers (avatars favicon/image/screenshot, webhooks worker) now also reject ::/96 and fec0::/10 addresses. PublicURL is new on this branch and is used only by the migrations Endpoint validator.

This is the backport of #14034.

Lookups in unhooked coroutines

PublicHostname::resolve() uses Swoole\Coroutine\System::getaddrinfo() only when the coroutine hooks network functions (SWOOLE_HOOK_NET_FUNCTION), the only case where Swoole 6.2's dns_get_record() retains ~140 KiB per lookup. Elsewhere it keeps the blocking dns_get_record(). getaddrinfo() always yields, and the 1.9.x and 2.0.x HTTP servers set no hook_flags, so GraphQL's Promise::then(), which waits in an unhooked usleep() loop, never let a yielded avatars lookup resume: every GraphQL avatar request that fetches a URL hung (seven 120 s timeouts in the GraphQL E2E job). Main's HTTP server and the workers enable SWOOLE_HOOK_ALL and keep the native lookup.

Tests

  • Unit: AllowlistTest, SubnetTest, PublicHostnameTest, PublicURLTest (exact host, IPv4 CIDR, IPv6 CIDR, non-match, lookalike hosts, numeric spellings, IPv6 forms, userinfo, backslashes), and EndpointTest. All the new cases fail without the change.
  • Pinning: PublicHostnameTest sends real requests through the getResolve() entries for IPv4, IPv6 and trailing-dot hostnames, checks that entries are empty for IP literals and reset after a rejection, and covers coroutine resolution, including a per-lookup memory bound. PublicURLTest checks the default and explicit ports in the entries. EndpointTest covers resolve() returning entries and throwing general_argument_invalid for every rejected endpoint. All of these fail without the change. The route and worker wiring is covered end to end below, since AGENTS.md keeps route actions and workers out of the unit suite.
  • E2E: testGetAppwriteReportWithSubnetEndpoint and testCreateAppwriteMigrationWithSubnetEndpoint report and migrate from http://appwrite.test/v1. The development .env no longer lists appwrite.test by name, so the validator resolves it to traefik's address, admits it because .env lists the compose network 172.16.238.0/24, and the source connects to that address through the CURLOPT_RESOLVE entry. appwrite.test is _APP_DOMAIN, so CI's router protection accepts it, and every other Appwrite migration E2E test takes the same pinned path. Each uses its own source project, so the user counts are exact. testGetAppwriteReport reports from the shared source project, and the rejection test now also refuses http://localhost/v1.
  • Unit: PublicHostnameTest::testResolvesWithoutYieldingInsideUnhookedCoroutine fails if a lookup in an unhooked coroutine yields.
  • E2E: testAppwriteMigrationRejectsPrivateEndpoints checks that report and create return general_argument_invalid for link-local, loopback, IPv6 loopback and non-http endpoints.
  • E2E: testRetryAppwriteMigrationWithStoredPrivateEndpoint covers the worker validating a stored endpoint again when a migration is retried.

Locally: lint, PHPStan and the network, migrations-validator and migrations-worker unit tests pass.

🤖 Generated with Claude Code

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your trial has ended. Reactivate Greptile to resume code reviews.

@ghost

ghost commented Oct 1, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

Limited by an open minor finding: Keep the new generic networking primitives in Utopia

Adds public HTTP(S) endpoint validation and an opt-in hostname/CIDR allowlist for Appwrite migrations. Migration reports and workers pin source requests to validated DNS addresses, while the worker's trusted internal endpoint remains exempt. The PR also adds validator and API regression coverage, local avatar fixtures, and dependency/configuration updates.

Latest changes: The newest commits remove appwrite.test from the development hostname allowlist and use the shared appwrite.test endpoint in subnet migration tests, exercising DNS resolution and CIDR admission instead of hostname exemption.

Verdict New comments Fixed Still open
✅ Approved 0 0 1
Fix with agent prompt
### Issue 1
src/Appwrite/Network/Validator/PublicURL.php:14-19
**Keep the new generic networking primitives in Utopia**

AGENTS.md's Libraries rule explicitly requires generic validators and networking utilities to live in the `utopia-php` ecosystem rather than `src/Appwrite`. `PublicURL`, `Allowlist`, and `Subnet` are generic networking primitives; please move those into a Utopia dependency while retaining the migration-specific `Endpoint` adapter here.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
📂 Walkthrough · 28
File Change
.env; app/config/variables.php Configures and documents the migration allowlist; appwrite.test now relies on the development network CIDR.
composer.json; composer.lock Selects and locks the migration dependency branch providing source request controls.
docker-compose.yml Passes the migration allowlist to API and worker services.
docker-compose.override.yml Adds local screenshot fixture networking and updates the development S3 fixture.
src/Appwrite/Network/Allowlist.php; src/Appwrite/Network/Subnet.php Adds exact hostname matching and IPv4/IPv6 CIDR admission.
src/Appwrite/Network/Validator/PublicHostname.php Adds allowlist support, reserved-address checks, DNS pinning entries, and hook-aware coroutine resolution.
src/Appwrite/Network/Validator/PublicURL.php Validates public HTTP(S) URLs and exposes checked DNS addresses.
src/Appwrite/Platform/Modules/Migrations/Validator/Endpoint.php Applies the migration allowlist and fixed endpoint rejection errors.
src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Appwrite/Create.php Validates migration source endpoints with the Endpoint validator.
src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Appwrite/Report/Get.php Validates report endpoints and installs source request resolution.
src/Appwrite/Platform/Workers/Migrations.php Revalidates stored endpoints and pins source requests, exempting the trusted internal endpoint.
src/Appwrite/SDK/Specification/Format/OpenAPI3.php Preserves URL schema metadata for migration endpoint parameters.
tests/e2e/Services/Avatars/AvatarsBase.php; tests/e2e/Services/GraphQL/AvatarsTest.php Uses local screenshot fixtures and updates image URLs.
tests/e2e/Services/Migrations/MigrationsBase.php Covers endpoint rejection, stored-endpoint retries, reports, and subnet-backed migrations using the shared web endpoint.
tests/resources/avatars/screenshot-example-com.png; tests/resources/avatars/screenshot.png; tests/resources/avatars/webpage/index.html; tests/resources/coredns/Corefile Provides reference images, a local screenshot page, and fixture DNS.
tests/unit/Network/AllowlistTest.php; tests/unit/Network/SubnetTest.php Tests exact hostname matching and IPv4/IPv6 subnet admission.
tests/unit/Network/Validators/LoopbackHostname.php; tests/unit/Network/Validators/PublicHostnameTest.php; tests/unit/Network/Validators/PublicURLTest.php Tests URL/address restrictions, resolve entries, and isolated coroutine DNS behavior.
tests/unit/Platform/Modules/Migrations/Validator/EndpointTest.php Tests endpoint admission, resolve entries, and fixed rejection errors.
.env Allows local development migration source hostnames.
app/config/variables.php Documents the migration source hostname/CIDR allowlist.
composer.json Selects the migration dependency branch providing source request controls.
composer.lock Locks the migration request-controls branch and updates Mongo dependencies.
src/Appwrite/Network/Allowlist.php Parses exact hostnames and permitted IP ranges.
src/Appwrite/Network/Subnet.php Implements IPv4/IPv6 CIDR parsing and membership checks.
tests/resources/coredns/Corefile Provides a public-address DNS answer for the screenshot fixture.
tests/unit/Network/Validators/LoopbackHostname.php Provides deterministic loopback, IPv6, and trailing-dot resolution cases.
tests/unit/Network/Validators/PublicHostnameTest.php Tests address restrictions, curl resolve entries, and isolated coroutine lookup behavior.
tests/unit/Network/Validators/PublicURLTest.php Tests URL rejection, allowlist behavior, resolve state, and coroutine resolution.
⏳ Still open from earlier reviews · 1

Reviewed the commits since 6ddf0ba · 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 A · Looks good to merge. Summary

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing 2.0.x (before) → fix/migration-endpoint-ssrf-2.0.x (after).

Metric Before After Change
🚀 Requests/sec 410.05 412.31 ⚪ +0.5%
⏱️ Latency P50 43.43 ms 43.3 ms ⚪ -0.3%
⏱️ Latency P95 98.01 ms 96.49 ms ⚪ -1.6%
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 43.3 96.49 25,194 412.31 -1.52
Account 71.27 148.22 1,326 22.13 -0.39
TablesDB 43.26 80.44 13,702 226.1 -1.08
Storage 37.08 84.53 6,630 111.41 -0.64
Functions 58.59 117.79 3,536 59.97 -1.17

Top API waits (after)

API request Max wait (ms)
account.name.update 224.9
account.get 221.9
account.prefs.update 210.41
functions.variables.delete 209.1
functions.variables.update 205.07

@abnegate
abnegate force-pushed the fix/migration-endpoint-ssrf-2.0.x branch from 0d25ece to b4f72c3 Compare October 1, 2026 04:33

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your trial has ended. Reactivate Greptile to resume code reviews.

abnegate and others added 2 commits October 1, 2026 17:37
Some callers need to reach destinations that are deliberately private,
such as another service on the same network, while still refusing every
other private or reserved address. PublicHostname now takes an optional
Allowlist of exact hostnames and IPv4/IPv6 subnets. A listed hostname is
accepted without a lookup, an IP in a listed subnet is accepted, and a
hostname is accepted when every address it resolves to is public or
listed. Hostnames never match by suffix, and numeric spellings such as
2130706433 or 127.1 are never treated as listed hostnames.

The allowlist defaults to empty, so existing callers keep their current
behaviour. Resolution is late-static-bound so tests can substitute it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Appwrite migration report and create endpoints accepted any URL for
the source endpoint, so the server could be pointed at loopback, private,
link-local or other reserved addresses, and the migrations worker used
whatever endpoint had been stored. Both params now use an Endpoint
validator built on a new PublicURL validator: http or https only, a known
public domain or public IP, and every resolved address public. The worker
validates a stored endpoint again before it builds the source, so
migrations queued earlier or retried are held to the same rule; the
internal endpoint the worker writes itself is exempt.

PublicHostname now also treats IPv4-compatible IPv6 addresses (::/96,
e.g. ::7f00:1) and the deprecated site-local range (fec0::/10) as
reserved, and PublicURL refuses URLs with userinfo or backslashes, where
URL parsers and curl can disagree on the host. The Endpoint validator
reports one fixed description, so a rejection does not reveal whether a
host exists or what it resolves to.

_APP_MIGRATIONS_ALLOWED_HOSTS takes comma-separated hostnames and IPv4 or
IPv6 CIDR ranges for sources that are deliberately private, such as an
instance on the same network. Hostnames match exactly. It is empty by
default, which allows public sources only; the development .env lists
appwrite and appwrite.test so the local stack can migrate from itself.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@abnegate
abnegate force-pushed the fix/migration-endpoint-ssrf-2.0.x branch from b4f72c3 to dc233ea Compare October 1, 2026 04:39

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your trial has ended. Reactivate Greptile to resume code reviews.

abnegate and others added 4 commits October 1, 2026 22:36
Worker behaviour is covered by e2e tests, not worker unit tests. The create
route now refuses private endpoints, so the e2e test stores an Appwrite
migration directly, the way a job created before validation would look,
retries it through the API and checks that the real worker records the
endpoint failure.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
utopia-php/mongo 1.5.3 pinned mongodb/mongodb to 2.1.*, which is affected
by GHSA-65fr-j4p9-vc33 (fixed in 2.4.1), so composer audit failed. 1.5.4
removes that library, which it never used, and only needs ext-mongodb. It
is the version main already runs, and utopia-php/database still accepts it
through its 1.* constraint.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Docker Hub no longer serves minio/minio, so the S3 e2e job failed at image
pull. Use the Silo image main already uses (6cc2997, 9efdb62): the
maintained MinIO fork, pinned by digest and pulled from the Appwrite GHCR
mirror, keeping the MINIO_* variables and mc.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
appwrite.io no longer serves /images/open-graph/website.avif, and
example.com was redesigned, so testGetImage, the GraphQL image test and
the screenshot pixel comparison failed on every run. Port main's fixtures
(8f2c97d, 4193dc8): fetch public/images/github.png from
cloud.appwrite.io, and screenshot a static page served by an nginx
container that the browser reaches through a network alias.

On this branch the API container resolves fixture names through CoreDNS,
not main's unbound resolver, so screenshot.webapp.com is added to the
webapp.com zone with the same public address.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Validating the endpoint before a migration leaves a window between that
DNS lookup and the requests the worker sends, so a host can pass
validation and then resolve to a private address, or the source can
follow a redirect somewhere the check never saw.

utopia-php/migration's 2.0.x backport adds setResolver() and refuses
redirects on the Appwrite source. The worker now passes
Endpoint::resolve(), which validates each request URL and returns the
CURLOPT_RESOLVE entries for the addresses it checked, to every Appwrite
source except the internal _APP_MIGRATION_HOST endpoint. The internal
endpoint is computed in one helper shared with processMigration().

PublicHostname::getResolve() and PublicURL::getResolve() are ported from
main to produce those entries. Main's coroutine DNS fix is ported too:
the resolver runs per request inside the worker's hooked coroutines,
where Swoole 6.2's dns_get_record() retains ~140 KiB per lookup.

The migration dependency points at the 2.0.x backport branch until
utopia-php/migration 2.0.9 is tagged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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

…dresses

The endpoint param validator resolves the host once, but report() lets
the source SDK resolve it again, so a rebinding host could pass
validation and then point the report requests at a private address.

The action now builds the source through createSource(), which passes
Endpoint::resolve() as the source's resolver, the same pinning the
migrations worker applies.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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

abnegate and others added 3 commits October 2, 2026 16:14
PublicHostname::resolve() switched to Swoole\Coroutine\System::getaddrinfo()
inside any coroutine, to avoid the ~140 KiB that Swoole 6.2 keeps for each
hooked dns_get_record() call. getaddrinfo() always yields, even when the
runtime hooks nothing.

On 1.9.x and 2.0.x the HTTP server sets no hook_flags, so usleep() blocks
there. GraphQL resolvers run each route in a child coroutine while
Promise::then() waits for it in a usleep() loop. Once the avatars
image, favicon and screenshot routes yielded inside the lookup, that loop
never let the reactor resume them, and every GraphQL avatar request that
fetches a URL hung until the client gave up (seven 120 s timeouts in the
GraphQL E2E job on both branches).

The retained memory only comes from the SWOOLE_HOOK_NET_FUNCTION hook, so
resolve natively only when that hook is active. Workers and main's HTTP
server, which enable SWOOLE_HOOK_ALL, keep the native lookup. Unhooked
coroutines make the blocking dns_get_record() call, which neither yields
nor retains memory without the hook. The regression test resolves inside a
child of an unhooked coroutine and fails if control returns to the parent
before the lookup finishes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… coverage

AGENTS.md keeps HTTP route actions and workers out of the unit suite.
Report/GetTest and the pinning cases in Workers/MigrationsTest also used
a closed loopback port, so they passed whether or not the resolver
returned any CURLOPT_RESOLVE entries.

Remove both. The report action builds its source inline again, as on
1.9.x, and getInternalEndpoint() is private, since no test subclass needs
them. Endpoint::resolve() keeps its unit test, now inside a hooked
coroutine like the other lookups, and asserts the exact entry format.

The E2E suite now lists the compose network (172.16.238.0/24) in the
development allowlist and reports and migrates from http://traefik/v1.
traefik is not a listed hostname, so the validator resolves it, admits
the address because it is inside the listed range, and the source
connects to that address through the CURLOPT_RESOLVE entry. Each test
uses its own source project so the user counts stay exact while other
migration tests run in parallel. It also reports from the allowlisted
appwrite.test, as 1.9.x does, and rejects http://localhost/v1, a hostname
that resolves outside every listed range.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI enables _APP_OPTIONS_ROUTER_PROTECTION, so requests to http://traefik/v1 reached the API through the pinned address and were refused with 401 because traefik is not a platform hostname.

Stop listing appwrite.test by name in the development allowlist instead. It is _APP_DOMAIN, so the router accepts it, and the validator now resolves it to traefik's address inside the listed 172.16.238.0/24 range. The subnet tests use it, and every other Appwrite migration E2E test now takes the resolved and pinned path too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@abnegate

Copy link
Copy Markdown
Member Author

Closing: 2.0.x backport no longer needed; main is on migration 3.x, and the main-line fix landed separately.

@abnegate abnegate closed this Oct 18, 2026
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