fix: reject zero and negative page sizes with a rendered error - #476
Merged
Merged
Conversation
A `first`, `last` or `limit` of zero or less passed schema validation and reached Ash, whose page option validation failed with a `Spark.Options.ValidationError` wrapped in `Ash.Error.Unknown`, so the client saw "Something went wrong". A negative value never got that far: the complexity analyzer returned a negative number and Absinthe raised `Absinthe.AnalysisError` before resolution. The page option validation now returns an `Ash.Error.Query.InvalidQuery` for a page size below one, on both the offset and keyset strategies, and a negative `limit` on a relationship field adds the same error to the nested query instead of being passed to Ash, which would silently return nothing. The complexity callbacks clamp the page size at zero so analysis never raises. Closes ash-project#471 Closes ash-project#474
Contributor
|
🚀 Thank you for your contribution! 🚀 |
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.
Closes #471 and #474.
Both issues are one gap at the GraphQL boundary.
first,lastandlimitare plainIntarguments, so zero and negative values pass schema validation:Ash.read/2, whose page option validation failed with aSpark.Options.ValidationErrorwrapped inAsh.Error.Unknown, so the client saw "Something went wrong" (first: 0 and last: 0 are an unrendered error #474).query_complexity/3multiplied it into a negative complexity and Absinthe raisedAbsinthe.AnalysisErrorbefore resolution (A negative page size makes complexity analysis raise #471). Clamping the complexity alone just turns A negative page size makes complexity analysis raise #471 into first: 0 and last: 0 are an unrendered error #474.Changes in
AshGraphql.Graphql.Resolver:validate_offset_opts/3andvalidate_keyset_opts/3return anAsh.Error.Query.InvalidQueryfor alimit,firstorlastbelow one, in the same shape as the existingfirst/last/before/aftercombination errors, so the client gets a renderedinvalid_queryerror.apply_load_arguments/5adds the same error to the nested query for a negativelimiton a relationship field. Ash accepts any integer there, so this previously returned an empty list without complaint. Alimitof zero on a relationship still returns an empty list. This is the one behaviour change that is a judgement call; happy to drop it if you would rather leave relationship arguments to Ash.query_complexity/3clauses clamp the page size at zero, so complexity analysis never returns a negative value.Tests: new
test/page_size_test.exscovers zero and negativelimit(offset),firstandlast(keyset), a negative page size underanalyze_complexity: true, and the relationshiplimitcases. Onmain, the offset and keyset cases return the unrendered Unknown error, the complexity case raisesAbsinthe.AnalysisError, and the relationship case returns an empty list. Full suite 444/444 onmainand 429/429 with the patch applied to v1.11.0.mix format --check-formatted,mix credo --strict,mix sobelowandmix dialyzerclean.Found by an AI agent working with a human fuzz-testing their own application; the reproductions outside this repo are https://github.com/grempe/ash-fuzz-repros/blob/main/test/ash_graphql/negative_page_size_complexity_test.exs and https://github.com/grempe/ash-fuzz-repros/blob/main/test/ash_graphql/zero_page_size_test.exs.
Contributor checklist
Leave anything that you believe does not apply unchecked.