|
| 1 | +--- |
| 2 | +title: "Limits" |
| 3 | +date: 2024-11-24T10:00:00+10:00 |
| 4 | +description: Configuring limits to prevent DoS attacks |
| 5 | +--- |
| 6 | +# Limits in graphql-java to Prevent DoS Attacks |
| 7 | + |
| 8 | +graphql-java provides configurable options to limit the potential for Denial of Service (DoS) attacks through excessive query complexity or size. Defaults for all these limits have been already set. |
| 9 | + |
| 10 | +## Parser Level Limits |
| 11 | +At the parser level, graphql-java offers several options to restrict the amount of work the query parser will perform. See `graphql.parser.ParserOptions`. |
| 12 | + |
| 13 | +**Maximum Query Characters:** Limits the number of characters in a query to prevent excessive parsing time and memory usage. The default is set to 1MB. |
| 14 | + |
| 15 | +```java |
| 16 | +public static final int MAX_QUERY_CHARACTERS = 1024 * 1024; // 1 MB |
| 17 | +``` |
| 18 | + |
| 19 | +**Maximum Query Tokens:** Restricts the number of tokens in a query to prevent excessive CPU usage. The default is set to 15,000 tokens. |
| 20 | + |
| 21 | +```java |
| 22 | +public static final int MAX_QUERY_TOKENS = 15_000; |
| 23 | +``` |
| 24 | + |
| 25 | +**Maximum Whitespace Tokens:** Limits the amount of whitespace in a query to prevent unnecessary parsing overhead. The default is set to 200,000 whitespace tokens. |
| 26 | + |
| 27 | +```java |
| 28 | +public static final int MAX_WHITESPACE_TOKENS = 200_000; |
| 29 | +``` |
| 30 | + |
| 31 | +**Maximum Rule Depth:** Restricts the depth of grammar rules in a query to prevent stack overflow exceptions. The default is set to 500. |
| 32 | + |
| 33 | +```java |
| 34 | +public static final int MAX_RULE_DEPTH = 500; |
| 35 | +``` |
| 36 | + |
| 37 | +## Introspection Query Limits |
| 38 | +graphql-java includes measures to limit introspection queries: |
| 39 | + |
| 40 | +**GoodFaithIntrospection:** Ensures introspection queries remain under a reasonable size. |
| 41 | + |
| 42 | +```java |
| 43 | +public static final int GOOD_FAITH_MAX_FIELDS_COUNT = 500; |
| 44 | +public static final int GOOD_FAITH_MAX_DEPTH_COUNT = 20; |
| 45 | +``` |
| 46 | + |
| 47 | +## Instrumentation |
| 48 | +Beyond parser level limits, graphql-java provides instrumentation to manage query complexity: |
| 49 | + |
| 50 | +**MaxQueryComplexityInstrumentation:** Limits the complexity of a query to prevent excessive resource usage. [See example on the Instrumentation page](/documentation/master/instrumentation#query-complexity-instrumentation). |
| 51 | + |
| 52 | +**MaxQueryDepthInstrumentation:** Limits the depth of a query to prevent overly complex queries. [See example on the Instrumentation page](/documentation/master/instrumentation#query-depth-instrumentation) |
0 commit comments