HTTP 500 error resulting from MaxTokens UnexpectedErrorProcessingRequest hook
I'm running into an issue where apollo-armor max-tokens plugin is returning a 500 error unexpectedly when graphql validation fails. It seems similar to the issue described in #571, or #605 and #191.
I've set up a test repo here using the apollo server 4 demo. If you send a request with an invalid graphql document like the following, it will return a 500 error as the plugin throws in the unexpectedErrorProcessingRequest hook.
Here is the demo repo I put together.
Making a query with an invalid document when the max-tokens plugin is enabled returns a GRAPHQL_VALIDATION_ERROR with a 500 status code:
curl -i 'http://localhost:4000/graphql' \
-H 'content-type: application/json' \
--data-raw $'{"query":"query books / { books { title } }"}'
HTTP/1.1 500 Internal Server Error
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 1235
ETag: W/"4d3-GtVzf7AAjpOMJcFTYs+VSj0jwu8"
Date: Sun, 15 Dec 2024 23:35:15 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"errors":[{"message":"Syntax Error: Unexpected character: \"/\".\n\nGraphQL request:1:13\n1 | query books / { books { title } }\n | ^","extensions":{"code":"GRAPHQL_VALIDATION_FAILED","stacktrace":["GraphQLError: Syntax Error: Unexpected character: \"/\".","","GraphQL request:1:13","1 | query books / { books { title } }"," | ^"," at Object.unexpectedErrorProcessingRequest (/project/workspace/node_modules/@escape.tech/graphql-armor/dist/graphql-armor.cjs.dev.js:184:13)"," at file:///project/workspace/node_modules/@apollo/server/dist/esm/ApolloServer.js:618:108"," at Array.map (<anonymous>)"," at internalExecuteOperation (file:///project/workspace/node_modules/@apollo/server/dist/esm/ApolloServer.js:618:45)"," at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"," at async runHttpQuery (file:///project/workspace/node_modules/@apollo/server/dist/esm/runHttpQuery.js:129:29)"," at async runPotentiallyBatchedHttpQuery (file:///project/workspace/node_modules/@apollo/server/dist/esm/httpBatching.js:34:16)"," at async ApolloServer.executeHTTPGraphQLRequest (file:///project/workspace/node_modules/@apollo/server/dist/esm/ApolloServer.js:525:20)"]}}]}
Whereas if I do the same query with the plugin disabled I get a 400 response, which is what I would expect.
curl -i 'http://localhost:4000/graphql' \
-H 'content-type: application/json' \
--data-raw $'{"query":"query books / { books { title } }"}'
HTTP/1.1 400 Bad Request
X-Powered-By: Express
Access-Control-Allow-Origin: *
cache-control: no-store
Content-Type: application/json; charset=utf-8
Content-Length: 1174
ETag: W/"496-4EKlcvmH/fuFUbbQ8k2g2o7cWHw"
Date: Sun, 15 Dec 2024 23:37:07 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"errors":[{"message":"Syntax Error: Unexpected character: \"/\".","locations":[{"line":1,"column":13}],"extensions":{"code":"GRAPHQL_PARSE_FAILED","stacktrace":["GraphQLError: Syntax Error: Unexpected character: \"/\"."," at syntaxError (/project/workspace/node_modules/graphql/error/syntaxError.js:15:10)"," at readNextToken (/project/workspace/node_modules/graphql/language/lexer.js:420:40)"," at Lexer.lookahead (/project/workspace/node_modules/graphql/language/lexer.js:84:29)"," at Lexer.advance (/project/workspace/node_modules/graphql/language/lexer.js:67:38)"," at Parser.advanceLexer (/project/workspace/node_modules/graphql/language/parser.js:1545:31)"," at Parser.expectToken (/project/workspace/node_modules/graphql/language/parser.js:1402:12)"," at Parser.parseName (/project/workspace/node_modules/graphql/language/parser.js:117:24)"," at Parser.parseOperationDefinition (/project/workspace/node_modules/graphql/language/parser.js:248:19)"," at Parser.parseDefinition (/project/workspace/node_modules/graphql/language/parser.js:211:23)"," at Parser.many (/project/workspace/node_modules/graphql/language/parser.js:1520:26)"]}}]}
I was looking at the unexpectedErrorProcessingRequest documentation and it says,
"Unexpected" errors don't include common client data errors such as validation, parsing, or GraphQL execution errors.
Which is interesting, because it seems to run that hook on parsing errors. Maybe I'm not understanding that hook properly.
I'm wondering if a http status should be explicitly passed back in the error that is thrown from the plugin so that it returns a 400 http status code. Maybe there is a better option. Or maybe I'm doing something wrong in the setup 😅. Either way I'd love to hear what you think.
Cheers
Hi @WenInCode,
Apollo has a limitation.
To quickly stop a request, it sends a 500 error.
To get a 400 error, you need to complete the parsing or validation process, which might involve handling a large document. You can disable the plugin or use onReject callbacks with propagateOnRejection: false to manage this.
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.