Skip to content

Commit

Permalink
feat(settings): add setting to limit query depth
Browse files Browse the repository at this point in the history
This should avoid overly comples GraphQL queries which might overload
the backend. Note: We do not yet have experience with the exact useful
value of this settings, so the default is zero (which implies the limit
is disabled)
  • Loading branch information
winged committed Jan 31, 2024
1 parent 8289ba3 commit dde9c9a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 13 additions & 3 deletions caluma/caluma_user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.http.response import HttpResponse
from django.utils.encoding import force_bytes, smart_str
from django.utils.module_loading import import_string
from graphene.validation import DisableIntrospection
from graphene.validation import DisableIntrospection, depth_limit_validator
from graphene_django.views import GraphQLView, HttpError
from rest_framework.authentication import get_authorization_header

Expand All @@ -19,9 +19,19 @@ class HttpResponseUnauthorized(HttpResponse):
status_code = 401


custom_validation_rules = []
if settings.DISABLE_INTROSPECTION: # pragma: no cover
custom_validation_rules.append(DisableIntrospection)

if settings.QUERY_DEPTH_LIMIT: # pragma: no cover
custom_validation_rules.append(
depth_limit_validator(max_depth=settings.QUERY_DEPTH_LIMIT)
)


class AuthenticationGraphQLView(GraphQLView):
if settings.DISABLE_INTROSPECTION: # pragma: no cover
validation_rules = (DisableIntrospection,)
if custom_validation_rules: # pragma: no cover
validation_rules = tuple(custom_validation_rules)

def get_bearer_token(self, request):
auth = get_authorization_header(request).split()
Expand Down
4 changes: 4 additions & 0 deletions caluma/settings/caluma.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def default(default_dev=env.NOTSET, default_prod=env.NOTSET):
# the source).
DISABLE_INTROSPECTION = env.bool("DISABLE_INTROSPECTION", default=default(False, True))

# DOS protection: Limit query depth to a given level. Default is 0, which means
# it is disabled
QUERY_DEPTH_LIMIT = env.int("QUERY_DEPTH_LIMIT", default=0)

# OpenID connect

OIDC_USERINFO_ENDPOINT = env.str("OIDC_USERINFO_ENDPOINT", default=None)
Expand Down

0 comments on commit dde9c9a

Please sign in to comment.