Repository navigation
fix: confine migration and certificate check connections to public addresses - #14794
Conversation
…dresses The certificate health check and the Appwrite and NHost migration sources connect to hosts a request names. Validate those hosts, connect the certificate check to the address that was checked, and confine the migration sources' HTTP clients to public destinations so DNS cannot answer differently between the check and the connection. NHost subdomain and region must be DNS labels and the database name an identifier, since the source builds hostnames and a DSN from them. Internal export files now record the project that created them, and an import from the internal bucket requires the same project. The certificate check no longer returns socket error details.
|
Review complete. 🟠 1 high · 🟡 1 medium 💬 Inline comments (2)
🧹 Nitpicks (1) — 🟢 1 low
The change adds a fail-closed
Reviewed commit: d20ba34 |
🟢 Tier S · Ready to mergeThe certificate health check now resolves hostnames through the public-hostname policy and connects to the checked address while retaining the hostname for TLS verification. Migration exports record their owning project, and CSV/JSON imports enforce ownership for internal files while allowing the uploading Console user. The E2E coverage exercises unresolved certificate hosts, same-project and cross-project exports, and Console-upload imports.
📂 Walkthrough · 6
Reviewed |
There was a problem hiding this comment.
This PR hardens migration and health endpoints against SSRF/DNS rebinding via a shared publicInternet destination guard, pins the health certificate TLS check to a resolved IP, and restricts internal export files to the exporting project.
Key findings
- 🟠 Postgres PDO connections bypass destinations pinning — Migrations.php:328
- 🟡
databasenarrowed, rejects hyphenated Postgres names — Create.php:63
| ->param('subdomain', '', new Label(), 'Source\'s Subdomain') | ||
| ->param('region', '', new Label(), 'Source\'s Region') | ||
| ->param('adminSecret', '', new Text(512), 'Source\'s Admin Secret') | ||
| ->param('database', '', new Text(512), 'Source\'s Database Name') | ||
| ->param('database', '', new Identifier(63), 'Source\'s Database Name') |
There was a problem hiding this comment.
🟡 bug · medium
database narrowed, rejects hyphenated Postgres names
The database parameter changed from Text(512) to Identifier(63), which accepts only the C-identifier shape ^[A-Za-z_]\w*$ — no hyphens, dots, or empty string (src/Appwrite/Platform/Modules/Migrations/Http/Migrations/NHost/Create.php:66). Postgres database names may legitimately contain hyphens, and the NHost source connects with dbname=<databaseName> verbatim (packages/migration/src/Sources/NHost.php:132), so callers migrating a hyphenated database now get a 400 where the migration previously proceeded. The same narrowing appears in src/Appwrite/Platform/Modules/Migrations/Http/Migrations/NHost/Report/Get.php at line 59; the subdomain/region switch to Label (lines 63-64) is similarly a wire-contract narrowing, though those values form DNS labels so it likely matches reality.
📋 Prompt for AI Agents
In src/Appwrite/Platform/Modules/Migrations/Http/Migrations/NHost/Create.php line 66 and src/Appwrite/Platform/Modules/Migrations/Http/Migrations/NHost/Report/Get.php line 59, the database parameter uses new Identifier(63), whose regex ^[A-Za-z_]\w*$ rejects hyphens and dots — characters legal in Postgres database names that the PDO connection in packages/migration/src/Sources/NHost.php accepts. Replace it with a length-bounded validator that permits Postgres-legal database names (e.g. new Text(63)), and update the subdomain/region param descriptions on both endpoints to state the Label constraints so SDK consumers learn the new accepted format.
There was a problem hiding this comment.
Keeping this on purpose. The value goes straight into a PDO DSN, where ; separates key/value pairs that libpq accepts (a later host= would win), and the DSN has no way to escape it. So the name has to be restricted to a safe charset. NHost provisions the project database under the project subdomain, a lowercase alphanumeric string, so Identifier(63) (63 is the Postgres name limit) covers real NHost databases. If a hyphenated name ever turns up we can widen the allowed charset, but not to free text.
There was a problem hiding this comment.
That's a reasonable security rationale — DSN injection via a semicolon or other special characters is a real concern, and restricting to Identifier(63) does close that vector. The key question is whether it breaks existing legitimate users. Your point that NHost provisions databases under the project subdomain (lowercase alphanumeric) is a strong mitigating argument, since in practice no real NHost database name would contain a hyphen.
The finding stands as-is in the diff (still new Identifier(63)), but given your explanation the risk is lower than initially assessed. If you do encounter hyphenated names in the future, a regex-based validator that allows hyphens but blocks ;, =, and whitespace would be the right middle ground rather than reverting to Text(512).
The NHost source resolves its database hostname again when it connects, so a check made beforehand can be bypassed by a changing DNS answer. Extend the source app-side to connect through libpq hostaddr to the checked address, keeping the hostname for TLS, in the report route and the worker.
The findings that requested changes are resolved. See the summary comment for what is still open.
Migrations created before subdomain, region and database were validated can still reach the worker. Check them again before connecting, and put hostaddr last so no earlier value can override it.
The findings that requested changes are resolved. See the summary comment for what is still open.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
…-hostnames # Conflicts: # src/Appwrite/Platform/Modules/Migrations/Http/Migrations/NHost/Create.php # src/Appwrite/Platform/Modules/Migrations/Http/Migrations/NHost/Report/Get.php # src/Appwrite/Platform/Workers/Migrations.php # tests/e2e/Services/Migrations/MigrationsBase.php
# Conflicts: # src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Appwrite/Report/Get.php # src/Appwrite/Platform/Modules/Migrations/Http/Migrations/NHost/Report/Get.php # src/Appwrite/Platform/Workers/Migrations.php
…mport it The Console imports a local CSV by uploading it to the console bucket and importing it with internalFile. Those uploads carry uploaderId, not projectInternalId, so binding internal files to the exporting project alone refused every Console CSV import.
|
@hansi-codes review |
|
@hansi-codes review |
…-hostnames fix: confine migration and certificate check connections to public addresses (cherry picked from commit 74e4d5f)
What does this PR do?
Two endpoints let a request reach things it shouldn't. Each is shown below with real requests against a local stack, first on
mainand then on this branch.GET /v1/health/certificateopens a TLS connection to whatever the domain resolves to, including private and cloud-metadata addresses, and returns the raw socket error400 general_argument_invalidbefore connecting; connection errors are genericinternalFile: trueread any file in the shared internal bucket, so one project can import another project's export404 storage_file_not_found1. The certificate check reaches private addresses
The
domainparam was validated as a public domain name, but nothing checked what it resolves to. Any hostname that points at10.x,169.254.169.254or127.0.0.1passed, and the server then connected to it on port 443. The error text ((111) Connection refusedvs(110) Operation timed out) and the response time told the caller whether something was listening, which makes the endpoint a probe of the internal network.A host that does not resolve now returns
400too; it used to be a404after a failed connect.2. One project can import another project's export
CSV exports are written to the platform's internal bucket, which all projects share. The import endpoints read that bucket when
internalFileistrue, and they looked up the file by id only. Any project that knew an export's file id could import another tenant's data into its own collection.The uploader rule keeps the Console's "Import CSV from a local file" working: the Console uploads the file to the console project's
defaultbucket and imports it withinternalFile: true, and those uploads carryuploaderId, notprojectInternalId.Export files written before this change have no owner recorded, so they can no longer be imported through
internalFile. They expire with the export retention period anyway.Migrations source confinement
Destination confinement for migration sources (Appwrite report, NHost report, and the worker for remote Appwrite, Supabase and NHost sources) now comes from #14870 (
publicHostname->getDestinations(), withdeploymentHostnameexempting this deployment's own hosts). This PR no longer adds its ownpublicInternetresource or wiring.Changes
Health/Http/Health/Certificate/Get.php: resolve and check the host with the existingpublicHostnamevalidator, connect to the checked IP withpeer_nameset to the hostname, and return a generic connection error.Workers/Migrations.php: store the exporting project's internal id in the export file'smetadata.Migrations/Http/Migrations/{CSV,JSON}/Imports/Create.php: withinternalFile, require that id to match the calling project, or the file'suploaderIdto match the calling console user.Test Plan
testCertificateRefusesHostThatDoesNotResolvechecks the 400, the error type and that no socket error details come back. The existingdoesnotexist.comexpectation moves from 404 to 400.testCreateCSVExportnow imports the export file back into the exporting project (202, completes) and from another project (404storage_file_not_found).testCreateCSVImportFromConsoleUploadandtestCreateJSONImportFromConsoleUploadfollow the Console's local-file import: upload to the console bucket, then import withinternalFileas the uploader (202, 100 rows); the same file imported with an API key returns 404. Each fails without the uploader rule.tests/unit/Network.docker compose up), running the same requests onmain(d5ccf14) and on this branch (2faaf82). The hostnames use nip.io, which resolvesa.b.c.d.nip.iotoa.b.c.d.Related PRs and Issues
Checklist