Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(settings): add setting to limit query depth #2138

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading