Laravel Rust✓ Human-reviewed

Allowlisted API filters and sorting in Laravel Rust

Translate a focused Query Builder contract into typed request input and model-column matches, keeping authorization, literal search, and stable pagination explicit.

Approved 2026-12-04T12:03:48.509731Z · Submission 24

Allowlisted API filters and sorting in Laravel Rust

Source, target, and choice of approach

PHP reference: Spatie's MIT-licensed spatie/laravel-query-builder 7.3.10, commit dee20864308f99258e462ebcf9b6eec2a1aafbb8. Read src/Concerns/{FiltersQuery,SortsQuery}.php, src/Filters/{FiltersExact,FiltersPartial}.php, src/QueryBuilderRequest.php, and their tests. Preserve upstream MIT notices for copied/adapted source. Target: Laravel Rust 0.1.3, commit 7837d40d8508957fd0e48a25d0087c851095fcc9; confirm installed APIs before editing the app.

Check get-recipe's current port catalog; if it lists a compatible Rust port, offer it as one option. This recipe is the application-owned alternative. Do not interpret a listing as proof of completeness or security.

Scope one endpoint: an exact enum filter, one literal substring filter, two allowed sort aliases with optional descending markers, and bounded stable pagination. Relationship includes, arbitrary operators, sparse fields, aggregate filters, and full JSON:API compatibility are out of scope. This guidance is untested in your app and is not a ready-made crate.

Establish the source contract

Upstream validates filter/sort names, defaults string filters to partial matching, binds values rather than interpolating them, and treats -name as descending. Array/exact and relation filters have additional behavior. Capture the current PHP endpoint's request examples and ordered result IDs; enumerate the subset being preserved and any intentional difference in errors or input encoding.

Rust adaptation

  1. Define a query input at the handler boundary. At the pinned framework, #[laravel::query] and request.query().await? parse query-only input. Optional/default fields are lenient on invalid input: security-relevant values must be explicitly validated after parsing or represented as required validated fields. Do not let an invalid status silently become “no filter.”
  2. Preserve existing filter[status]/filter[name] wire names if clients depend on them; if choosing a flatter Rust endpoint, document that as an API change. Reject unsupported filter keys and structured values rather than ignoring them.
  3. Start from the authorized tenant/owner query, then add fixed model conditions. For example, User::name.contains(&name) implements escaped literal substring matching at this framework revision. like(format!("%{name}%")) does not have the same wildcard semantics. Column text comparisons on SQLite only provide ASCII case-insensitivity by default; verify production collation and Unicode requirements.
  4. Parse a bounded sort token list into an application enum. Apply fixed model columns with a match; never turn the token into a raw SQL expression. This is a fragment assuming a real app model and an already-scoped builder:
query = match token {
    "name" => query.order_by(User::name.asc()),
    "-name" => query.order_by(User::name.desc()),
    "created_at" => query.order_by(User::created_at.asc()),
    "-created_at" => query.order_by(User::created_at.desc()),
    _ => laravel::abort!(422, "Unsupported sort"),
};
  1. Apply the app's default order only when no sort was requested, then append User::id.asc() as a unique tie-breaker. Keep a strict page-size ceiling and explicit output/resource fields. Do not let include, fields, or tenant_id inputs widen the query or expose private columns.
  2. Keep the query logic at the endpoint owner; a general dynamic filter registry would recreate the package. Reuse existing app pagination and validation instead of adding a compatibility layer.

Discriminating trial cases

Seed two tenants; in the allowed tenant use names Alex, Alex, and A_100%, different statuses, and duplicate timestamps. Put a matching row in the forbidden tenant.

  • Check exact status results and tenant isolation together, not only separately.
  • Search _100%, a quote, and backslash: special characters are literal; injected sort SQL is rejected.
  • Verify multi-sort precedence and stable page boundaries by asserting ordered IDs, not just counts.
  • Invalid enums, repeated/array filters, unknown names, unsupported includes, excessive sort counts, and oversized pagination fail with the documented client error.
  • Exercise Unicode/case behavior on the real production driver. A SQLite-only green suite is not PostgreSQL collation proof.

Run cargo fmt --all --check, strict Clippy, and the endpoint's integration tests, then compare the same fixtures with the PHP implementation's declared subset. Record exact wire-contract changes and test output with the change. Do not remove any dependency until every affected endpoint and background consumer has been inventoried.