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

fix: Unauthenticated api throttling #20993

Merged
merged 6 commits into from
Mar 19, 2024
Merged

Conversation

benjackwhite
Copy link
Contributor

@benjackwhite benjackwhite commented Mar 19, 2024

Problem

We were throttling certain requests such as email signup / password reset based on the request which is a bit loose given we almost always have something we can identify the intended user with (email or uuid).

Also we didn't set a scope which means that one throttler could be affecting the other. We have really harsh rate limits on password resets which then in turn affect anything else scoped to that user.

Changes

2024-03-19 10 02 18

  • Adds a custom throttler that tries to find the authed user or email|uuid in the payload, hashes it and falls back to the ident.

👉 Stay up-to-date with PostHog coding conventions for a smoother review.

Does this work well for both Cloud and self-hosted?

How did you test this code?

@@ -190,6 +190,7 @@ class LoginViewSet(NonCreatingViewSetMixin, viewsets.GenericViewSet):
queryset = User.objects.none()
serializer_class = LoginSerializer
permission_classes = (permissions.AllowAny,)
# NOTE: Throttling is handled by the `axes` package
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a note here as I wasn't sure why we had no throttler for logging in

Copy link
Member

@pauldambra pauldambra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple of questions but 👍

@@ -434,6 +434,23 @@ def test_cant_reset_more_than_six_times(self):
# Three emails should be sent, fourth should not
self.assertEqual(len(mail.outbox), 6)

def test_is_rate_limited_on_email_not_ip(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm lazily assuming there's a test in here that shows the reverse (if we have the same IP but varying addresses then we don't limit (or we're slower to limit))

@@ -222,6 +223,26 @@ def get_bucket_key(self, request):
return ident


class UserOrEmailRateThrottle(SimpleRateThrottle):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've only got a vague understanding these throttles...

if i'm a bad actor on a single IP stuffing email addresses into the login form will I get rate limited at some point?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. Only per email after this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a fallback rate limit?

It's relatively common for attackers to find sites with poor rate limits and use them to check email/common password combos to get around rate limits on the site they really want to attack...

Copy link
Member

@pauldambra pauldambra Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(can be a follow up since we want to test this really fixes the issue folk are experiencing sooner rather than later)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Login is protected differently (axes). This only applies to email validation / password reset requests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 someone could still stuff emails to find valid accounts but relatively low risk :)

@benjackwhite benjackwhite merged commit a6eb82c into master Mar 19, 2024
73 checks passed
@benjackwhite benjackwhite deleted the fix/password-throttling branch March 19, 2024 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants