Skip to content

construction fails for depth around > 200 / resource exhaustion? #216

Done

Description

@devkral

Reporting issues with GraphQL-core 3

In my tests the construction of a deep request tree fails with recursion problems.
The problem is a recursive approach in the generation of the graphql request tree (this is why I created the test).

Next to denial of service it is most probably possible to cause resource exhaustion attacks by passing big graphs.

There should be two changes:

I am not sure if the cost spec ( https://ibm.github.io/graphql-specs/cost-spec.html ) can fix this. The changes must take place while generating the requested input graph

Activity

  1. devkral commented on Mar 17, 2024

    @devkral
    Author

    Working example code for simply transform recursive function in an "stack free" call tree:

    from functools import partial
    
    
    def _foo(level=0, *, get_result):
        if level > 100000:
            yield level
            return
        print("1", level)
        yield partial(_foo, level=level + 1)
        print("2", get_result())
        yield level
    
    
    def foo():
        result_stack = []
    
        fn_stack = [_foo(get_result=result_stack.pop)]
    
        while fn_stack:
            cur_el = fn_stack[-1]
            try:
                next_el = next(cur_el)
                if isinstance(next_el, partial):
                    fn_stack.append(next_el(get_result=result_stack.pop))
                else:
                    result_stack.append(next_el)
            except StopIteration:
                fn_stack.pop()
    foo()
  2. erikwrede commented on Mar 17, 2024

    @erikwrede
    Member

    Hey there, it's best to report any particular issues with graphql-core in the corresponding repository. Please note that graphql core closely follows the reference implementation written in typescript in order to be as up to date as possible given our available maintainer time.

    That's why I deem it unlikely to see any changes to the execution logic on just the python level. If you're interested in bringing this into graphql-js, it might be worthwhile to open up an RFC there and follow the process.

    The limits functionality of graphene-protector look amazing. Are you interested in contributing them to strawberry-graphql or graphene directly so they're even easier to use? Feel free to contact me about both, I'm a core maintainer on both teams 😊

  3. Cito commented on Mar 17, 2024

    @Cito
    Member

    What Erik wrote. Completely changing the execution or parsing logic is out of the scope of this project.

    If it really would be a significant improvement, consider discussing this in the upstream GraphQL-js project.

    Also, please note that a max_tokens parameter has been added to GraphQL-js and already ported to GraphQL-core. This could also help to avoid denial of service via memory exhaustion. Have you seen that?

  4. devkral commented on Mar 18, 2024

    @devkral
    Author

    thanks for the hint. Then only the first issue is valid.
    It isn't really much effort. Generators are a good way to save the current progress

  5. devkral commented on Mar 18, 2024

    @devkral
    Author

    Hey there, it's best to report any particular issues with graphql-core in the corresponding repository. Please note that graphql core closely follows the reference implementation written in typescript in order to be as up to date as possible given our available maintainer time.

    That's why I deem it unlikely to see any changes to the execution logic on just the python level. If you're interested in bringing this into graphql-js, it might be worthwhile to open up an RFC there and follow the process.

    The limits functionality of graphene-protector look amazing. Are you interested in contributing them to strawberry-graphql or graphene directly so they're even easier to use? Feel free to contact me about both, I'm a core maintainer on both teams 😊

    if you like to, we can move it. I proposed it a few years ago if I remember right. I would really like to develop this in a team.

  6. devkral commented on Mar 20, 2024

    @devkral
    Author

    What Erik wrote. Completely changing the execution or parsing logic is out of the scope of this project.

    If it really would be a significant improvement, consider discussing this in the upstream GraphQL-js project.

    Also, please note that a max_tokens parameter has been added to GraphQL-js and already ported to GraphQL-core. This could also help to avoid denial of service via memory exhaustion. Have you seen that?

    I had some time to look through the code of the upstream project:

    they seem to not to use a recursive pattern for parsing inputs, so they should be safe.
    The max_token parameter is nice but doesn't prevent depths around 200 except it is configured very restrictive

    They seem to be affected the same. I misread the code.

  7. Cito commented on Mar 20, 2024

    @Cito
    Member

    Maybe, instead of or in addition to max_tokens, there should be something like max_level? Is that something we should suggest?

  8. devkral commented on Mar 20, 2024

    @devkral
    Author

    Maybe, instead of or in addition to max_tokens, there should be something like max_level? Is that something we should suggest?

    This solves a symptom and not the problem: Level > 200 depth is simply not supported.
    An easy fix would just handle the crash and return an appropriate error (already done implicitly but without a nice error)

  9. devkral commented on May 6, 2024

    @devkral
    Author

    Note: currently there is no use in having 200 level depths of recursion but this can change if someone abuses strawberry-django filters for building really big filters.

  10. unshorn commented on Aug 26, 2026

    @unshorn

    Maybe, instead of or in addition to max_tokens, there should be something like max_level? Is that something we should suggest?

    Yes, measured this. max_tokens=500 rejects a 2,000-field wide query but not a 400-level deep one, even though the deep query has ~800 tokens.

  11. Cito commented on Aug 27, 2026

    @Cito
    Member

    @unshorn With max_tokens=500 a query that has ~800 tokens should be rejected. If your test query contained comments, then this could explain it - these have not been counted before 3.2.12/3.3.0rc1.

  12. Cito commented on Nov 6, 2026

    @Cito
    Member

    3.3.2 and 3.2.14 no longer let the RecursionError escape to the caller, and they add a fixed limit: documents, fragment chains and variable values nested deeper than 200 levels are now rejected with a normal GraphQL error.

    • parse and the other parse functions raise a GraphQLSyntaxError with the message "Syntax Error: Document is nested too deeply. Parsing aborted.", and graphql and graphql_sync return it in the result.
    • validate returns "Validation aborted." as the last error for very long chains of fragments, and keeps the errors found before.
    • Variable values nested too deeply give the request error "Variable values are nested too deeply."

    What this means for a depth around 200: a query with up to 199 nested fields below the root still parses. A query that really goes that deep into your data can still run out of stack during execution on 3.3, with the default recursion limit somewhere between 150 and 190 levels on CPython 3.14. As before, you get a field error for this in the result, with the rest of the data. On 3.2, 199 levels worked in the same test. These numbers depend on the interpreter, on sys.setrecursionlimit and on your resolvers.

    The limit is not configurable yet, and GraphQL.js has no such limit. Calling the methods of Parser directly can still raise the RecursionError, as can utilities outside of the request path such as separate_operations.

  13. Cito commented on Nov 29, 2026

    @Cito
    Member

    Closing this, since 3.3.2 and 3.2.14 return a normal GraphQL error instead of crashing, as described above.

    Making the limit configurable, or removing the recursion altogether, would have to start in GraphQL.js, which currently has no such limit. If it gets one, we will follow it here.

    If you still get a RecursionError out of graphql or graphql_sync with 3.3.2 / 3.2.14 or later, please open a new issue with the query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions