question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rejecting costly or malicious queries

See original GitHub issue

Is 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:

  1. 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)
  1. 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:closed
  • Created 7 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
ieugencommented, May 29, 2017

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:

Clients must supply a first or last argument on any connection. Values of first and last must be within 1-100. Individual calls cannot request more than 11,250 total nodes.

[1] https://developer.github.com/v4/guides/resource-limitations/

2reactions
andimarekcommented, May 20, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found