Reviewer · Database
Bound and minimize query results
Every data-access call must explicitly bound how much it reads and avoid pulling more than the caller actually needs. 1. Validate (don't silently clamp) any caller-supplied page/row size against a hard maximum before issuing the query, and reject out-of-range values with an error instead of truncating silently. Clamping a `perPage`/`size` value can make a...
Every data-access call must explicitly bound how much it reads and avoid pulling more than the caller actually needs.
- Validate (don’t silently clamp) any caller-supplied page/row size against a hard maximum before issuing the query, and reject out-of-range values with an error instead of truncating silently. Clamping a
perPage/sizevalue can make a caller mistake a short, truncated page for the last page, and passing an unbounded value straight into an Elasticsearchsize(or similar) risks exceedingindex.max_result_windowand failing the whole request.
const MAX_DOCS_PER_PAGE = 10_000;
if (perPage < 1 || perPage > MAX_DOCS_PER_PAGE) {
throw Boom.badRequest(`perPage must be between 1 and ${MAX_DOCS_PER_PAGE}`);
}
-
When streaming or batching large results (cursors, scroll/search_after, SQL cursors), cap both row count and response byte size, and read in small batches so a handful of oversized rows/documents can’t be buffered in full before the cap applies. Also guard against a deadline/timeout for the whole read, not just per-batch.
-
Never fetch a whole document/row when only a few fields are required. Use field-level projections (
_source.includes,sourceIncludes, selecting specific columns) instead of loading the full payload and discarding the rest — this reduces both query time and transferred/held data, especially important for large text/JSON blobs. -
Double check truncation/edge-case handling around these limits (e.g., don’t let a “drop last item to signal truncation” strategy wipe out an already-small result set), and keep paging/sizing limits in sync across the API schema and the service layer that executes the query.</description>
</invoke>