Skip to content

fix(Middleware): reject reserved all-lowercase query parameters with 400 - #262

Merged
thomasfosterau merged 1 commit into
trunkfrom
issue-148-reject-unknown-query
Oct 8, 2026
Merged

thomasfosterau merged 1 commit into
trunkfrom
issue-148-reject-unknown-query

Conversation

@thomasfosterau

Copy link
Copy Markdown
Owner

Summary

Closes #148.

JSON:API 1.1 §Query Parameters reserves query-parameter family base names made only of a-z for the specification: "If a server encounters a query parameter that does not follow the naming conventions above, or the server does not know how to process it as a query parameter from this specification, it MUST return 400 Bad Request." Until now an unknown name like ?unknown=1 was silently ignored.

  • Where it runs: in the SchemaErrors middleware, which every Endpoint constructor attaches. That makes it the one layer that covers every endpoint, including those that declare no query schema (e.g. DELETE). Before the endpoint decodes anything, the middleware reads the request's parameter names (ParsedSearchParams) and checks them against two lists: the spec's families (include, fields, sort, page, filter) and the names the endpoint's own query schema declares. It reads the encoded AST (structs, unions, and filter: true's filter[ rest), caches the result per endpoint, and stands aside for a schema that admits any name, such as a Record(String, …).
  • The 400: one error object per offending parameter (a repeated key once). Each has status: "400", code: "bad_request", title: "Bad Request", a detail naming the parameter and explaining the reservation, and source: { parameter: "<the key>" }. That's the shape every other query 400 already has (Ship the standard query-parameter errors (unsupported include path / include depth / sort field), carrying what *is* supported in meta #107 / feat(ApiError): set source.parameter on the query-parameter errors #231). It also emits the existing Span.schemaError, tagged jsonapi.request_part: "Query".
  • Still ignored: implementation-specific names, i.e. any base name with a non-a-z character (?fooBar=1, ?x-trace=1, ?foo_bar=1, ?fooBar[x]=1, ?ext:name=1). Tests pin these.
  • New public API (@since 0.17.0):
    • Middleware.schemaErrorsLayer(options?) builds the live SchemaErrors; SchemaErrorsLive is now schemaErrorsLayer().
    • Middleware.layerWith also takes allowedQueryParameters.
    • Middleware.QueryParameterOptions and Middleware.QueryParameterCheck.
    • Middleware.checkQueryParameters(names, { declared, allowedQueryParameters }) is the rule standalone, for a framework hook that decodes requests itself. It returns the 400 document or undefined, like Middleware.negotiate.
  • Conformance: spec/query/unknown-query-parameter is off the KNOWN_FAILURES list in all three examples/*/test/conformance.test.ts, and the kit now passes it against each example. docs/conformance.md §8 drops the row and updates the counts (five → four).
  • Docs: README gets a new "Reserved query parameters" section plus a bullet under "Every endpoint automatically". The Middleware module, SchemaErrors, SchemaErrorsLive and layerWith docs are updated.
  • Tests: a new src/Middleware.query.test.ts with 25 tests. I wrote the behavioural tests first: against unfixed trunk, 7 of them failed (200 / 204 where a 400 was expected), and the "still ignored" and spec-family cases passed.

Nothing is left over from #148's acceptance criteria. Two neighbouring problems I found while verifying are filed rather than fixed here:

Verification

All run locally, in this order, on the head commit:

  • pnpm install --frozen-lockfile: pass
  • vp run build: pass
  • vp check: pass (0 errors; warnings are the existing staged-rollout debt; the warn-tier sites left on new lines follow the module's own conventions, | undefined options/returns as negotiate does and the title conditional spread as schemaErrorDocument does)
  • vp run check (sources, type-level tests, examples, emitted declarations): pass
  • vp run -r test:unit: pass (root 2080 tests; examples 39 / 42 / 52)
  • vp run docgen (every JSDoc @example compiled and executed): pass (224 examples)
  • fallow audit --base origin/trunk: pass ("No issues in 8 changed files"; one inherited sourceOf CRAP finding, excluded by the gate). The agent gate (fallow audit --format json --quiet --explain --gate-marker agent) verdict is pass.

Changeset

  • .changeset/reject-reserved-query-parameters.md, minor. Pre-1.0, new public API is a minor, and this also changes default behaviour. The changeset opens with a bold Behaviour change line.

Notes

Behaviour change. Requests that used to succeed now answer 400: any request with a parameter whose base name is all a-z and that the spec doesn't define and the endpoint doesn't declare, e.g. GET /articles/1?unknown=1 or ?foo[bar]=1. This applies under Middleware.layer, layerWith, layerHostNegotiated (the Registry default) and SchemaErrorsLive. Consuming apps whose own URL space passes lowercase parameters (?lang=, ?ref=, ?q=…) through to the api must rename them or allow-list them on upgrade. A lowercase parameter the endpoint's own query schema declares (e.g. a custom q) keeps working.

Owner decision: opt-out vs always on. Not delegated. This PR implements a proposal, also recorded on #148:

  • Proposed default (implemented): rejection is on by default, as the spec's MUST, with an explicit allow-list, allowedQueryParameters (family base names), on Middleware.layerWith / Middleware.schemaErrorsLayer. The docs recommend renaming first.
  • Alternative: always on with no allow-list. Simpler and stricter, but a host mounted under a framework would have no way to keep an existing lowercase parameter working while it migrates.

If you prefer the alternative, the change is to drop the option and schemaErrorsLayer's parameter.

Base name, not whole key. JSON:API 1.1 §Implementation-Specific Query Parameters says names "MUST come from a family whose base name is a legal member name and also contains at least one non a-z character". So the test is on the base name (everything before the first [), and ?foo[bar]=1 is rejected. The issue text said "the check is on the whole key", which would let foo[bar] through. I followed the spec text, and #148's comment records this.

Precedence. The Endpoint constructors attach SchemaErrors after ContentNegotiation, which makes it the outer middleware, so this check runs first. A request that both carries a reserved parameter and fails §5 negotiation now gets the 400 rather than the 406/415. The spec's §Processing Errors lets a server stop at the first problem. This is documented on schemaErrorsLayer and pinned by a test.

Declaration shape. SchemaErrorsLive's type is unchanged (Layer.Layer<SchemaErrors>). It is now built with Layer.succeed over an Effect.fnUntraced middleware instead of HttpApiMiddleware.layerSchemaErrorTransform, and its response-encode handling (#208) is unchanged.

🤖 Generated with Claude Code


Generated by Claude Code

JSON:API 1.1 §Query Parameters reserves query parameter family base names
made only of a-z for the specification, and a server that does not know how
to process one MUST answer 400 Bad Request. The SchemaErrors middleware every
Endpoint constructor attaches now checks each request's parameter names,
before the endpoint decodes anything, against the specification's families
and the names the endpoint's query schema declares, and answers an offender
with one error object per parameter naming it in source.parameter. Names
carrying any other character (fooBar, x-trace) are still ignored.

Rejection is on by default. Middleware.layerWith and the new
Middleware.schemaErrorsLayer take allowedQueryParameters, by family base
name, for a host that must pass its own lowercase parameters through;
Middleware.checkQueryParameters is the rule standalone.

spec/query/unknown-query-parameter comes off the three examples'
known-failure lists, and docs/conformance.md §8 drops it.

Closes #148

Claude-Session: https://claude.ai/code/session_0187g2QpzaHqEzX8GJxiJBVc
Co-Authored-By: Claude <noreply@anthropic.com>
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.

Unknown query parameters are ignored rather than rejected, though the spec makes an all-lowercase unrecognised name a 400

2 participants