Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Introduce status code regression mitigation #7465

Merged
merged 12 commits into from Mar 27, 2023

Conversation

trevor-scheer
Copy link
Member

@trevor-scheer trevor-scheer commented Mar 23, 2023

Apollo Server v4 introduced a regression with respect to invalid variables and http status codes. AS4 incorrectly started responding with a 200 status code, where AS3 would respond with a 400 when the provided variables object failed variable coercion (during graphql-js execute).

Providing the following config to your AS4 constructor options will opt-in to the regression mitigation:

new ApolloServer({
  // ...
  status400WithErrorsAndNoData: true,
})

Fixes #7462
Related discussion #7460

TODO:

  • check docs pages / links

Apollo Server v4 introduced a regression with respect to invalid
`variables` and http status codes. AS4 incorrectly started responding
with a 200 status code, where AS3 would respond with a 400.

Providing the following config to your AS4 constructor options will
opt-in to the regression mitigation:
```
new ApolloServer({
  // ...
  status400WithErrorsAndNoData: true,
})
```
@netlify
Copy link

netlify bot commented Mar 23, 2023

Deploy Preview for apollo-server-docs ready!

Name Link
🔨 Latest commit 4fa3401
🔍 Latest deploy log https://app.netlify.com/sites/apollo-server-docs/deploys/641e211d51df000008894da9
😎 Deploy Preview https://deploy-preview-7465--apollo-server-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 23, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 4fa3401:

Sandbox Source
Apollo Server Typescript Configuration
Apollo Server Configuration

@trevor-scheer trevor-scheer marked this pull request as ready for review March 23, 2023 21:06
Copy link
Contributor

@StephenBarlow StephenBarlow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with suggestions!

docs/source/api/apollo-server.mdx Outdated Show resolved Hide resolved
docs/source/data/errors.mdx Outdated Show resolved Hide resolved
docs/source/migration.mdx Show resolved Hide resolved
docs/source/migration.mdx Outdated Show resolved Hide resolved
docs/source/migration.mdx Outdated Show resolved Hide resolved
docs/source/migration.mdx Outdated Show resolved Hide resolved
trevor-scheer and others added 5 commits March 23, 2023 14:42
Co-authored-by: Stephen Barlow <stephen@apollographql.com>
Co-authored-by: Stephen Barlow <stephen@apollographql.com>
Co-authored-by: Stephen Barlow <stephen@apollographql.com>
Copy link
Member

@glasser glasser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops i forgot to send these!


### Appropriate 400 status codes

Apollo Server v4 will respond to an invalid `variables` object with a _200_ status code, where v3 would correctly respond with a 400 status code. This regression was introduced in [PR #6502]https://github.com/apollographql/apollo-server/pull/6502) and brought to our attention in [Issue #7462](https://github.com/apollographql/apollo-server/issues/7462).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe give examples of what invalid means (I think the big ones are "surprisingly null/surprisingly missing fields on input objects" and "scalar function whatever it's called throws"). Maybe in the changeset too?

Might be worth noting that v4 does 400 on, eg, variables key that doesn't map to an object, idk.

docs/source/migration.mdx Outdated Show resolved Hide resolved
packages/server/src/requestPipeline.ts Outdated Show resolved Hide resolved
@korinne korinne requested a review from clenfest March 24, 2023 14:43
Copy link
Member

@glasser glasser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I think this suggestion is fixing something but I might be misinterpreting.)

.changeset/purple-paws-yell.md Outdated Show resolved Hide resolved
docs/source/migration.mdx Outdated Show resolved Hide resolved
Co-authored-by: David Glasser <glasser@apollographql.com>
@trevor-scheer trevor-scheer merged commit 1e80814 into main Mar 27, 2023
6 checks passed
@trevor-scheer trevor-scheer deleted the trevor/fix-7462-400-status branch March 27, 2023 17:46
@github-actions github-actions bot mentioned this pull request Mar 27, 2023
trevor-scheer pushed a commit that referenced this pull request Apr 3, 2023
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @apollo/server@4.6.0

### Minor Changes

- [#7465](#7465)
[`1e808146a`](1e80814)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Introduce
new opt-in configuration option to mitigate v4 status code regression

Apollo Server v4 accidentally started responding to requests with an
invalid `variables` object with a 200 status code, where v3 previously
responded with a 400. In order to not break current behavior
(potentially breaking users who have creatively worked around this
issue) and offer a mitigation, we've added the following configuration
option which we recommend for all users.

    ```ts
    new ApolloServer({
      // ...
      status400ForVariableCoercionErrors: true,
    });
    ```

Specifically, this regression affects cases where _input variable
coercion_ fails. Variables of an incorrect type (i.e. `String` instead
of `Int`) or unexpectedly `null` are examples that fail variable
coercion. Additionally, missing or incorrect fields on input objects as
well as custom scalars that throw during validation will also fail
variable coercion. For more specifics on variable coercion, see the
"Input Coercion" sections in the [GraphQL
spec](https://spec.graphql.org/June2018/#sec-Scalars).

This will become the default behavior in Apollo Server v5 and the
configuration option will be ignored / no longer needed.

### Patch Changes

- [#7454](#7454)
[`f6e3ae021`](f6e3ae0)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Start
building packages with TS 5.x, which should have no effect for users

- [#7433](#7433)
[`e0db95b96`](e0db95b)
Thanks [@KGAdamCook](https://github.com/KGAdamCook)! - Previously, when
users provided their own `documentStore`, Apollo Server used a random
prefix per schema in order to guarantee there was no shared state from
one schema to the next. Now Apollo Server uses a hash of the schema,
which enables the provided document store to be shared if you choose to
do so.

## @apollo/server-integration-testsuite@4.6.0

### Patch Changes

- [#7454](#7454)
[`f6e3ae021`](f6e3ae0)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Start
building packages with TS 5.x, which should have no effect for users

- Updated dependencies
\[[`1e808146a`](1e80814),
[`f6e3ae021`](f6e3ae0),
[`e0db95b96`](e0db95b)]:
    -   @apollo/server@4.6.0

## @apollo/server-plugin-response-cache@4.1.2

### Patch Changes

- [#7454](#7454)
[`f6e3ae021`](f6e3ae0)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Start
building packages with TS 5.x, which should have no effect for users

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@trevor-scheer
Copy link
Member Author

@jessemyers this is released in v4.6.0 via the status400ForVariableCoercionErrors constructor option.

trevor-scheer pushed a commit that referenced this pull request Apr 17, 2023
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @apollo/server@4.6.0

### Minor Changes

- [#7465](#7465)
[`1e808146a`](1e80814)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Introduce
new opt-in configuration option to mitigate v4 status code regression

Apollo Server v4 accidentally started responding to requests with an
invalid `variables` object with a 200 status code, where v3 previously
responded with a 400. In order to not break current behavior
(potentially breaking users who have creatively worked around this
issue) and offer a mitigation, we've added the following configuration
option which we recommend for all users.

    ```ts
    new ApolloServer({
      // ...
      status400ForVariableCoercionErrors: true,
    });
    ```

Specifically, this regression affects cases where _input variable
coercion_ fails. Variables of an incorrect type (i.e. `String` instead
of `Int`) or unexpectedly `null` are examples that fail variable
coercion. Additionally, missing or incorrect fields on input objects as
well as custom scalars that throw during validation will also fail
variable coercion. For more specifics on variable coercion, see the
"Input Coercion" sections in the [GraphQL
spec](https://spec.graphql.org/June2018/#sec-Scalars).

This will become the default behavior in Apollo Server v5 and the
configuration option will be ignored / no longer needed.

### Patch Changes

- [#7454](#7454)
[`f6e3ae021`](f6e3ae0)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Start
building packages with TS 5.x, which should have no effect for users

- [#7433](#7433)
[`e0db95b96`](e0db95b)
Thanks [@KGAdamCook](https://github.com/KGAdamCook)! - Previously, when
users provided their own `documentStore`, Apollo Server used a random
prefix per schema in order to guarantee there was no shared state from
one schema to the next. Now Apollo Server uses a hash of the schema,
which enables the provided document store to be shared if you choose to
do so.

## @apollo/server-integration-testsuite@4.6.0

### Patch Changes

- [#7454](#7454)
[`f6e3ae021`](f6e3ae0)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Start
building packages with TS 5.x, which should have no effect for users

- Updated dependencies
\[[`1e808146a`](1e80814),
[`f6e3ae021`](f6e3ae0),
[`e0db95b96`](e0db95b)]:
    -   @apollo/server@4.6.0

## @apollo/server-plugin-response-cache@4.1.2

### Patch Changes

- [#7454](#7454)
[`f6e3ae021`](f6e3ae0)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Start
building packages with TS 5.x, which should have no effect for users

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

regression in http status setting for certain errors (v3 to v4)
4 participants