Repository navigation
fix(databases): answer bad query input with 4xx, not 500 or warnings - #14538
Conversation
…p warnings The query-lib migration routes every user filter through packages/query, which DB 7.4.2 on main never called. Malformed query values from the public API then reached code that assumed a well-formed shape: - geometryToWkt read $p[1] / $point[1] from points with one ordinate and fabricated a POINT(x 0) from the result, so a malformed geometry raised "Undefined array key" warnings and silently matched the wrong shape, or produced WKT the database rejects with a 500. - SpatialDistanceFilter::fromTuple read missing tuple keys and passed a non-boolean meters flag into a bool parameter (TypeError, 500). - groupByType ran intval() on limit/offset values beyond the int range, which PHP 8.5 reports as a warning before the validator rejects them. Malformed geometry and distance tuples now throw ValidationException, which the database adapter already turns into a Query exception, so the API answers 400 general_query_invalid. Limit and offset values PHP cannot hold read as absent instead of warning. A point with extra ordinates keeps its x and y, as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Aggregation queries are new with the query-lib migration. Summing or taking the deviation of large doubles overflows: MySQL raises 1690 and PostgreSQL 22003, which the adapter maps to a Limit exception, and the list action let it escape as a 500. An engine that returns the overflow as infinity instead makes the response unencodable. Both now answer 400 with the new database_query_out_of_range type, since the query is valid but its result cannot be represented. Co-Authored-By: Claude Opus 5.5 <[email protected]>
utopia-php/database 8.x maps an unknown column (MySQL 1054, PostgreSQL
42703) to NotFound('Attribute not found'). The documents/rows list and the
files list catch every NotFound as "the backing table is missing", so
equal("s.x") on a plain column answered 404 table_not_found or
collection_not_found, and list endpoints with no NotFound catch, /users
among them, answered 500.
A NotFound whose driver cause is an unknown column is now classified by
Appwrite\Databases\Rejection from its SQLSTATE and driver code, not its
message. The HTTP error hook maps it to 400 general_query_invalid for every
endpoint, and the two lists that catch NotFound check it first. The
documents list also falls back to looking the table up, so only a table
that is really missing still answers 404.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟡 2 medium 💬 Inline comments (2)
This PR hardens how list endpoints handle malformed queries and how driver errors are surfaced to clients. Geometry values passed to spatial filters are now validated (shape, finite numeric coordinates, WKT strings) before compilation, and distance tuples validate type and the
Two medium-severity items remain open: inconsistent rejection of 3-element coordinates in Reviewed commit: 855ffdf |
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
🟢 Tier S · Ready to mergeThis PR hardens query handling by validating malformed spatial and distance inputs, treating out-of-range pagination values as absent, and mapping aggregate overflow to a structured 400 response. It also updates database and file list error handling and adds unit and E2E coverage for rejected query values. Latest changes: The latest commit removes the unknown-column classifier and its dotted-path E2E case, while changing the document-list catches to treat every NotFound as a missing collection or table.
Couldn't attach to a lineThese findings could not be placed on a changed line, so they were not posted as inline comments.
📂 Walkthrough · 18
Reviewed the commits since |
There was a problem hiding this comment.
Adds strict validation of user-supplied geometry in spatial queries, a driver-error rejection mapper that turns unknown-column DB errors into 400s, and a new database_query_out_of_range error type covered by unit and e2e tests.
Key findings
- 🟡 Flat
[x,y,z]point silently truncated, nested one rejected — Spatial.php:19 - 🟡 Global
Rejectionmapping turns server-side schema faults into 400s — general.php:1306
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
A polygon ring with fewer than four points or whose last point differs from its first passed validation and compiled to WKT the spatial parser refuses, so the engine raised it as a 500. A flat point with a z ordinate was cut down to its x and y and silently matched a different shape, while the same point nested inside a line was already refused. Both now throw ValidationException, which the API answers with 400 general_query_invalid. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…sent queries The error hook turned every unknown-column driver error into 400 general_query_invalid on every route. A server fault, such as schema drift on a platform collection or an internal query naming a column that was never created, then read as the caller's mistake and skipped the >= 500 reporting path. The hook now classifies the error only on a route that takes queries and only when the request sent some. Anything else stays a 500. MongoDB has no column list, so a nested path on a plain attribute matches nothing there; the users test now expects an empty 200 on schemaless engines instead of the 400 the SQL engines answer. Co-Authored-By: Claude Opus 5.5 <[email protected]>
MongoDB stores documents, not columns, so a nested path on a plain attribute is a valid path that matches nothing and answers 200, as it did before the query-lib migration. Only the SQL engines raise an unknown column, which is the 404 and 500 regression these tests guard. The attribute-support flag is true on MongoDB, so the tests now key on the adapter instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php # tests/e2e/Services/Databases/DatabasesBase.php
utopia-php/database 8.3.0 refuses a nested path on an attribute that holds no nested values before the query reaches the engine, so the unknown-column driver error this mapped to 400 no longer happens for caller input. The classifier, its error-hook wiring and the documents and files list fallbacks go, and the lists treat NotFound as a missing table again. The dotted-path end-to-end tests move to the database bump, where every adapter answers them with 400 through QueryException. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Release blocker for #11649, found by staging QA of appwrite-labs/cloud#5410.
Problem
Staging QA of the query-lib migration (#11649, build c3f599d) found bad input answering 500 or raising PHP warnings, in code paths the migration made live:
stddev/sumover a float column holding 1e308 answers 500. MySQL raises 1690 / PostgreSQL 22003, the adapter maps it toUtopia\Database\Exception\Limit, and the documents/rows list never caught it. Aggregations are new with the migration.packages/query: malformed spatial values hitUndefined array keywarnings inBuilder/Trait/Spatial.php(and silently fabricatedPOINT(x 0)), a malformed distance tuple raised a TypeError (500), andlimit/offsetvalues beyond the int range warned inQuery::groupByTypeon PHP 8.5. On main (DB 7.4.2) none of this code ran: 7.4.2 never calledutopia-php/query.Fix
packages/query: malformed geometry (including a polygon ring with fewer than 4 points or not closed, and a flat point with more than two ordinates) and distance tuples throwValidationException, which the adapter already turns into a Query exception, so the API answers 400general_query_invalid. Out-of-range limit/offset values read as absent instead of warning.database_query_out_of_range(400) for an aggregate the database cannot represent: aLimitexception from the engine, or a non-finite result.equal("s.x")) is fixed at the root in fix(query): refuse a nested path on an attribute that holds no nested values utopia-php/database#1022 (8.3.0): the validator refuses it before it reaches the engine. The database bump PR adds the end-to-end assertions for it.Tests
packages/query):HelperTest,Builder/Feature/SpatialTest,Builder/SpatialDistanceFilterTest. Each new case collects PHP warnings and fails on BASE 5204588: 3, 28 and 9 failing cases respectively.DatabasesBase):Queries/RejectedValuescovers malformed geometry and aggregate overflow.bin/monorepo check querypass locally. The local unit-suite failures (yaml, maxminddb, Imagick fonts, JWT key) are environmental and identical on BASE.E2E not run locally (no Docker); CI is the first run.
🤖 Generated with Claude Code