fix: reject undefined input object fields supplied via variables - #765
Open
derekadams wants to merge 1 commit into
Open
Conversation
An input object value supplied through a variable was accepted even when it contained entries the input object type does not define; the undefined entries were silently discarded and the request succeeded. validateValue's *ast.InputObject case iterated the type's declared fields and looked each one up in the supplied map, so an entry present in the map but absent from the type was never visited. The literal path (validateValueType) iterates the other direction -- provided fields, looked up in the type -- and already reports `Field "x" is not defined by type "Y"`. This aligns the variable path with it. The spec requires this: section 3.10 Input Coercion states that an input object literal "or unordered map supplied by a variable ... must not contain any entries with names not defined by a field of this input object type, otherwise a request error must be raised". graphql-js rejects this case in coerceInputValue. Undefined entries are collected and sorted before being reported so that the errors do not vary between runs with Go's randomized map iteration order. This is the unknown-key counterpart to the missing-key direction fixed in graph-gophers#292. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Problem
An input object value supplied through a variable is accepted even when it contains entries the input object type does not define. The undefined entries are silently discarded and the request succeeds. The same value written as a literal in the query is correctly rejected.
This is easy to hit in practice: a client that misnames a field gets a success response and a partially-applied input, with nothing to indicate a value was dropped.
Cause
validateValue's*ast.InputObjectcase iterates the type's declared fields and looks each one up in the supplied map:An entry present in the map but absent from the type is never visited.
validateValueType(the literal path) iterates the other direction — provided fields, looked up in the type — and already reportsField "x" is not defined by type "Y", which is why literals are caught.Spec
Section 3.10, Input Coercion, requires the error, and names the variable case explicitly:
Section 6.1.2 routes variable values through those same coercion rules. graphql-js rejects this case in
coerceInputValue.Change
Walk the supplied map as well, reporting entries not defined by the type, and reuse the existing
makeSuggestionhelper so the message matches the literal path. Undefined entries are collected and sorted before being reported, so Go's randomized map iteration order does not make the error output vary between runs.Two cases added to
TestQueryVariablesValidation, covering the rejection and the did-you-mean suggestion. The full test suite passes (go test -count=1 ./...).Relationship to #292
#292 ("Query variables are not validated") fixed the missing-required-key direction of this same literal-vs-variable asymmetry. This is the unknown-key counterpart, which that change did not cover.
It is also structurally invisible to the graphql-js parity work in #708: graphql-js catches this at execution-time coercion rather than in a validation rule, so importing the validation rules could not surface it.
Note, not addressed here
The
@oneOfexactly-one-key constraint appears to have the same asymmetry — enforced for literals, not for values arriving via variables. I left it out to keep this diff focused, but I'm happy to follow up if you'd like it in the same PR.🤖 Generated with Claude Code