Rejecting costly or malicious queries
See original GitHub issueIs there currently a way to determine how costly a query will be to run? I have been looking at the code and I think there isn’t, correct me and close this issue if I’m wrong!
It would be important to add some protections against this kind of thing. When thinking of ways to do this I found that I settled on a way that sangria is already doing. i.e. assign a complexity score to each field and add up the scores, rejecting the query if it’s too complex.
With this in mind I’d propose the following:
- Add an optional cost/complexity function to the GraphQLFieldDefinition and its inner Builder class. The interface would be something like:
public interface ComplexityFunction {
Double calculate(ValidationContext validationContext, List<GraphQLArgument> args, Double childScore);
}
with java 8 usage like
newFieldDefinition()
.name("field")
.dataFetcher(env -> "result")
.complexity((validationContext, fieldDef, childScore) -> 2.0 * getArgument("size", args) * childScore)
- Add a way to specify the complexity limit on queries to run. This will likely take the form of adding a List of validations as an argument to the
graphql.execute
method. It would be nice to be able to use the RulesVisitor and another validation for this but I’m not sure it can be done that way yet.
What do you think? I’m willing to implement this feature myself, so I’d like to know if this is wanted and if this is a good approach.
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (8 by maintainers)
Top Results From Across the Web
Securing Your GraphQL API from Malicious Queries
One harmful aspect of the malicious query above is the nesting, classified by its depth, which makes the query exponentially more expensive.
Read more >A Principled Approach to GraphQL Query Cost Analysis
Server-side query enforcement can reject queries and update rate limits based on provider-defined policies. This is a brief for the research paper A...
Read more >How to Secure Your GraphQL API From Malicious Queries
It will run for each request that's coming into your graphql server, validate them all, and reject any query which is too long....
Read more >What is SQL Injection? Attack Examples & Prevention Tips
The classic method is the most common. The attacker directly assaults the database, submitting malevolent data as part of a query. For example, ......
Read more >What Is an SQL Injection? Cheatsheet and Examples
A Structured Query Language (SQL) injection is a cybersecurity attack technique or vulnerability where malicious variants of SQL statements ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
FYI, Github v4 api is GraphQL based and it’s interesting to see how they enforce limits [1].
They have a node limit and a rate limit:
[1] https://developer.github.com/v4/guides/resource-limitations/
I would like to see some kind of solution for malicious queries in the next version: the question what to do about it comes up all the time and I think graphql-java needs to have some kind of answer to that.