Repository navigation
fix(Middleware): reject reserved all-lowercase query parameters with 400 - #262
Merged
Merged
Conversation
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>
This was referenced Oct 8, 2026
Merged
8 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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=1was silently ignored.SchemaErrorsmiddleware, which everyEndpointconstructor 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 ownqueryschema declares. It reads the encoded AST (structs, unions, andfilter: true'sfilter[rest), caches the result per endpoint, and stands aside for a schema that admits any name, such as aRecord(String, …).status: "400",code: "bad_request",title: "Bad Request", a detail naming the parameter and explaining the reservation, andsource: { 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 inmeta#107 / feat(ApiError): set source.parameter on the query-parameter errors #231). It also emits the existingSpan.schemaError, taggedjsonapi.request_part: "Query".?fooBar=1,?x-trace=1,?foo_bar=1,?fooBar[x]=1,?ext:name=1). Tests pin these.@since 0.17.0):Middleware.schemaErrorsLayer(options?)builds the liveSchemaErrors;SchemaErrorsLiveis nowschemaErrorsLayer().Middleware.layerWithalso takesallowedQueryParameters.Middleware.QueryParameterOptionsandMiddleware.QueryParameterCheck.Middleware.checkQueryParameters(names, { declared, allowedQueryParameters })is the rule standalone, for a framework hook that decodes requests itself. It returns the 400 document orundefined, likeMiddleware.negotiate.spec/query/unknown-query-parameteris off theKNOWN_FAILURESlist in all threeexamples/*/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).Middlewaremodule,SchemaErrors,SchemaErrorsLiveandlayerWithdocs are updated.src/Middleware.query.test.tswith 25 tests. I wrote the behavioural tests first: against unfixedtrunk, 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:
?include=/?sort=on an endpoint that does not support them is ignored, though the spec makes it a 400 #261:?include=/?sort=on an endpoint that doesn't support them is still ignored, though §Inclusion and §Sorting make it a 400. The spec families are deliberately passed through here, per Unknown query parameters are ignored rather than rejected, though the spec makes an all-lowercase unrecognised name a 400 #148's "the spec does not define".?x=1&x[y]=2is a 500 #260: an endpoint whoseQuery.schemadeclares nothing passes every parameter through to its handler, and?x=1&x[y]=2is a 500 fromnest. This predates this change.Verification
All run locally, in this order, on the head commit:
pnpm install --frozen-lockfile: passvp run build: passvp 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,| undefinedoptions/returns asnegotiatedoes and thetitleconditional spread asschemaErrorDocumentdoes)vp run check(sources, type-level tests, examples, emitted declarations): passvp run -r test:unit: pass (root 2080 tests; examples 39 / 42 / 52)vp run docgen(every JSDoc@examplecompiled and executed): pass (224 examples)fallow audit --base origin/trunk: pass ("No issues in 8 changed files"; one inheritedsourceOfCRAP finding, excluded by the gate). The agent gate (fallow audit --format json --quiet --explain --gate-marker agent) verdict ispass.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=1or?foo[bar]=1. This applies underMiddleware.layer,layerWith,layerHostNegotiated(theRegistrydefault) andSchemaErrorsLive. 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 ownqueryschema declares (e.g. a customq) keeps working.Owner decision: opt-out vs always on. Not delegated. This PR implements a proposal, also recorded on #148:
allowedQueryParameters(family base names), onMiddleware.layerWith/Middleware.schemaErrorsLayer. The docs recommend renaming first.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]=1is rejected. The issue text said "the check is on the whole key", which would letfoo[bar]through. I followed the spec text, and #148's comment records this.Precedence. The
Endpointconstructors attachSchemaErrorsafterContentNegotiation, 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 onschemaErrorsLayerand pinned by a test.Declaration shape.
SchemaErrorsLive's type is unchanged (Layer.Layer<SchemaErrors>). It is now built withLayer.succeedover anEffect.fnUntracedmiddleware instead ofHttpApiMiddleware.layerSchemaErrorTransform, and its response-encode handling (#208) is unchanged.🤖 Generated with Claude Code
Generated by Claude Code