Bug: Cannot limit number of results using auth directive to prevent malicious queries

We are trying to prevent users from requesting too many documents by setting a big number in first argument.

What edition and version of Dgraph are you using?

v21.03.1

Have you tried reproducing the issue with the latest release?

Yes, the bug can be reproduced in Dgraph Cloud too.

Steps to reproduce the issue (paste the query/schema if possible)

Try this schema:

type Todo
  @auth(
    query: {
      rule: """
      query {
          queryTodo(first: 2) {
              id
          }
      }
      """
    }
  ) {
  id: ID!
  text: String! @search(by: [term])
  owner: String! @search(by: [hash])
}

And after that add more than 2 Todo.
After running this query: query { queryTodo { id } }
The result is :

 "queryTodo": [
      {
        "id": "0x4c6cd2ed"
      },
      {
        "id": "0x4c6cd2ee"
      },
      {
        "id": "0x4c6cd2ef"
      },
      {
        "id": "0x4c6cd2f0"
      }
    ]

Expected behavior and actual result.

As we have used first: 2 in auth directive we expect only 2 results:

   "queryTodo": [
      {
        "id": "0x4c6cd2ed"
      },
      {
        "id": "0x4c6cd2ee"
      }
    ]

You can checkout this bug here:
https://green-pine.us-east-1.aws.cloud.dgraph.io/graphql

1 Like

I think this does not work for a somewhat simple reason. If you put a limit in the auth rule you might be actually blocking data you want to get.

Consider your example above. The auth rule also controls the getTodo. So if you always limit it to two you will only ever be able to read 0x4c6cd2ed and 0x4c6cd2ee. So if you tried to do getToDo(id: "0x4c6cd2ef") { id } you would be blocked.

So the solution is not this in an auth rule, but a new feature to put a max limit somewhere for a generated query*

Thanks Anthony. You are right but even this method is not working and even by using that auth directive I get all results.
We need this feature. right now what we did to prevent malicious queries is using a timeout limit in alpha servers and also setting a request size and response size limit to prevent crawling our data.

2 Likes

Interesting. That makes sense. I guess @auth is just really a filter. Agreed, there needs to be a way to stop this. We can create a feature request for this, but I don’t know that Dgraph cares about backend security at this point.

J