RFC 3 / Accepted

Fix CoerceArgumentValues() hasValue

Opened on
Merged on

At a glance

Project
GraphQL Spec
Identifier
#1056
Stage
RFC 3 / Accepted
Champion
@benjie
Updated
2025-07-03
Latest activity
Pull request merged on
Source

Pull request description

Fixes a bug discovered whilst carefully evaluating CoerceArgumentValues() that leads to "undefined value leakage" and potential null pointer exception if strictly following the spec. GraphQL.js does not suffer this, so this is a spec bug rather than an implementation bug.

Consider the following schema:

type Query {
  field(arg: String! = "defaultValue"): String
}

And the following GraphQL query:

query ($var: String) {
  field(arg: $var)
}

Imagine that we send an empty object ({}) as the variable values.

Coercing the variableValues according to https://spec.graphql.org/draft/#CoerceVariableValues() we get an empty object ({}).

Fast-forward to https://spec.graphql.org/draft/#CoerceArgumentValues():

  • Let {coercedValues} be an empty unordered Map. coercedValues = {}
  • Let {argumentValues} be the argument values provided in {field}. argumentValues = { arg: $var }
  • Let {fieldName} be the name of {field}. fieldName = 'field'
  • Let {argumentDefinitions} be the arguments defined by {objectType} for the field named {fieldName}. argumentDefinitions = { arg: String! = "defaultValue" }
  • For each {argumentDefinition} in {argumentDefinitions}:
    • Let {argumentName} be the name of {argumentDefinition}. argumentName = 'arg'
    • Let {argumentType} be the expected type of {argumentDefinition}. argumentType = String!
    • Let {defaultValue} be the default value for {argumentDefinition}. defaultValue = 'defaultValue'
    • Let {hasValue} be {true} if {argumentValues} provides a value for the name {argumentName}. :bug: !!!BUG!!! hasValue = true because argumentValues does provide the variable $var as the value for the argument 'arg'
    • Let {argumentValue} be the value provided in {argumentValues} for the name {argumentName}. argumentValue = $var
    • If {argumentValue} is a {Variable}: Yes, $var is a variable
      • Let {variableName} be the name of {argumentValue}. variableName = 'var'
      • Let {hasValue} be {true} if {variableValues} provides a value for the name {variableName}. :bug: !!!BUG!!! This does not fire, but hasValue is already {true} by the above.
      • Let {value} be the value provided in {variableValues} for the name {variableName}. :bug: !!!BUG!!! value = undefined
    • Otherwise, let {value} be {argumentValue}. NOT TRIGGERED
    • If {hasValue} is not {true} and {defaultValue} exists (including {null}): NOT TRIGGERED since hasValue is true
      • Add an entry to {coercedValues} named {argumentName} with the value {defaultValue}.
    • Otherwise if {argumentType} is a Non-Nullable type, and either {hasValue} is not {true} or {value} is {null}, raise a field error. NOT TRIGGERED because hasValue is {true} and value is not {null} (it is undefined!)
    • Otherwise if {hasValue} is true: Yes, it is
      • If {value} is {null}: It is not, it is undefined
        • Add an entry to {coercedValues} named {argumentName} with the value {null}.
      • Otherwise, if {argumentValue} is a {Variable}: It is!
        • Add an entry to {coercedValues} named {argumentName} with the value {value}. coercedValues[argumentName] = undefined (since value is undefined)
      • Otherwise:
        • If {value} cannot be coerced according to the input coercion rules of {argumentType}, raise a field error.
        • Let {coercedValue} be the result of coercing {value} according to the input coercion rules of {argumentType}.
        • Add an entry to {coercedValues} named {argumentName} with the value {coercedValue}.
  • Return {coercedValues}.

Expectation: coercedValues = { arg: "defaultValue" } Actual result: coercedValues = { arg: undefined }

arg is non-null string -> NPE! :boom:

Essentially the phrase "Let {hasValue} be {true} if {argumentValues} provides a value for the name {argumentName}" is at best ambiguous and at worst plain wrong, since the next two lines get the "value" for {argumentName} and then check to see if this {value} is a variable.

This PR fixes this issue by only setting hasValue to true when the value is explicitly resolved via the two branches: variable and non-variable.

There is no need for a GraphQL.js PR for this since GraphQL.js already follows the expected behavior; reproduction:

import { GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString, graphqlSync, printSchema, validateSchema } from "graphql";

const Query = new GraphQLObjectType({
  name: "Query",
  fields: {
    field: {
      args: {
        arg: {
          type: new GraphQLNonNull(GraphQLString),
          defaultValue: "defaultValue",
        },
      },
      type: GraphQLString,
      resolve(_, { arg }) {
        return arg;
      },
    },
  },
});

const schema = new GraphQLSchema({
  query: Query,
});

const result = graphqlSync({
  schema,
  source: /* GraphQL */ `
    query ($var: String) {
      field(arg: $var)
    }
  `,
  variables: {
    /* EXPLICITLY DO NOT PASS "var" */
  },
});

const errors = validateSchema(schema);
if (errors.length) {
  console.dir(errors);
  process.exit(1);
}

console.log(printSchema(schema));
console.log(JSON.stringify(result, null, 2));

Timeline

July 2025

  • Pull request merged on by leebyron
  • @benjie committed "Merge branch 'main' into fix-has-value" on
  • Advanced to RFC 3 on by leebyron
  • Discussed in GraphQL WG

    The GraphQL Working Group meeting began with Lee noting the thinner attendance due to the holiday week and introduced the agenda, which included updates on the spec, editorial changes, and several RFCs. Benjie highlighted the upcoming discount period for GraphQL Conference tickets and provided a summary of the previous meeting's progress, including merged items like executable descriptions and deprecation discussions. The group planned to discuss schema coordinates, argument values, list coercion algorithms, and incorporating Meta fields into execute collected fields, among other topics. Lee encouraged attendees to review the previous meeting's outcomes and emphasized the importance of highlighting upcoming dates to avoid scheduling conflicts.

    Zoom summary · GraphQL WG · 2025-07-03

    The meeting focused on discussing schema coordinates and meta fields, with Leebyron explaining that meta fields are not part of the schema and can be implementation-specific. Benjie highlighted the distinction between schema elements and introspection types, noting that introspection queries may include both schema and implementation-specific types. The group also discussed two RFCs related to argument value coercion in GraphQL, with Leebyron proposing a more significant rewrite of the algorithm to avoid relying on the "hasValue" concept. Benjie presented a coercion table for arrays and input objects, which aligns with GraphQL.js behavior and addresses underspecified areas in the current specification.

    Zoom summary · GraphQL WG · 2025-07-03
    Value Coercion Specification Discussion

    Leebyron and Benjie discussed the approach to handling value coercion in the specification, comparing it to the existing value completion algorithm. They agreed that a more algorithmic approach would be clearer than prose-based descriptions. Leebyron supported merging a fix for the "has value" bug into the current specification release, as it addressed an active issue rather than just clarifying ambiguity. They decided to mark this change as an RFC rather than an editorial update, acknowledging that it would change behavior in implementations.

    Zoom summary · GraphQL WG · 2025-07-03

June 2025

  • Discussed in GraphQL WG
    Next Steps

    Lee: Review Benjie's bug fix PR regarding spec text alignment with GraphQLJS implementation

    Zoom summary · GraphQL WG · 2025-06-26

    The group discusses two issues in the GraphQL specification. Benjie presents a longstanding bug in the spec that needs fixing, providing detailed spec text for Lee to review. They also address list coercion, which Benjie has rewritten to use a more algorithmic system and to better handle variables. Lee agrees to carefully review both issues, emphasizing the importance of aligning the spec with the reference implementation, GraphQL.js. The meeting concludes with Lee acknowledging the productivity of the session and the need to finalize the discussed changes.

    Zoom summary · GraphQL WG · 2025-06-26
  • Marked as Ready for Next Stage on by benjie

January 2025

December 2024

November 2024

December 2023

November 2023