-
Notifications
You must be signed in to change notification settings - Fork 23
Fix bug in access attempts. Add burst and daily rate limits #4794
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,16 @@ defmodule Sanbase.Accounts.CouponAttempt do | |
| @impl true | ||
| def config do | ||
| %{ | ||
| interval_in_minutes: 10, | ||
| allowed_user_attempts: 30, | ||
| allowed_ip_attempts: 60 | ||
| # Burst limits (short-term protection) | ||
| burst_interval_in_minutes: 5, | ||
| allowed_user_burst_attempts: 30, | ||
| allowed_ip_burst_attempts: 60, | ||
|
|
||
| # Daily limits (long-term protection) | ||
| # 24 hours | ||
| daily_interval_in_minutes: 24 * 60, | ||
| allowed_user_daily_attempts: 200, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the use case where a user would need 200 login emails per day? That's like one login every 8 minutes. I think we can push this down to 10?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, this is the coupon limit.. Isn't this also very, very high? |
||
| allowed_ip_daily_attempts: 500 | ||
| } | ||
| end | ||
|
|
||
|
|
@@ -20,6 +27,10 @@ defmodule Sanbase.Accounts.CouponAttempt do | |
| AccessAttempt.check_attempt_limit(type(), user, remote_ip) | ||
| end | ||
|
|
||
| def check_ip_attempt_limit(remote_ip) do | ||
| AccessAttempt.check_ip_attempt_limit(type(), remote_ip) | ||
| end | ||
|
|
||
| @impl true | ||
| def create(user, remote_ip) do | ||
| AccessAttempt.create(type(), user, remote_ip) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,8 +87,11 @@ defmodule SanbaseWeb.Graphql.Resolvers.AuthResolver do | |
|
|
||
| with true <- allowed_email_domain?(email), | ||
| true <- allowed_origin?(origin_host_parts, origin_url), | ||
| # Check IP limits before creating/finding user | ||
| :ok <- EmailLoginAttempt.check_ip_attempt_limit(remote_ip), | ||
| {:ok, %{first_login: first_login} = user} <- | ||
| User.find_or_insert_by(:email, email, %{username: args[:username]}), | ||
| # Check user-specific limits after user creation | ||
| :ok <- EmailLoginAttempt.check_attempt_limit(user, remote_ip), | ||
| {:ok, user} <- User.Email.update_email_token(user, args[:consent]), | ||
| {:ok, _res} <- User.Email.send_login_email(user, first_login, origin_host_parts, args), | ||
|
|
@@ -106,6 +109,20 @@ defmodule SanbaseWeb.Graphql.Resolvers.AuthResolver do | |
|
|
||
| {:error, message: message} | ||
|
|
||
| {:error, :too_many_burst_attempts} -> | ||
| Logger.info( | ||
| "Login failed: too many burst attempts. Email: #{email}, IP Address: #{remote_ip}, Origin URL: #{origin_url}" | ||
| ) | ||
|
|
||
| {:error, message: "Too many login attempts, try again after a few minutes"} | ||
|
|
||
| {:error, :too_many_daily_attempts} -> | ||
| Logger.info( | ||
| "Login failed: too many daily attempts. Email: #{email}, IP Address: #{remote_ip}, Origin URL: #{origin_url}" | ||
| ) | ||
|
|
||
| {:error, message: "Too many login attempts, try again tomorrow"} | ||
|
|
||
| {:error, :too_many_attempts} -> | ||
|
||
| Logger.info( | ||
| "Login failed: too many login attempts. Email: #{email}, IP Address: #{remote_ip}, Origin URL: #{origin_url}" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can call the functions from within cond:
This way it will stop computing the rest if it finds an offence.
Alternatively, we could also probably compute all of the counts in one SQL query to avoid multiple DB calls. It would be a fragment with
COUNT(CASE ... THEN 1 ELSE 0 END)like here: https://github.com/santiment/sanbase2/blob/master/lib/sanbase/accounts/interaction/interaction.ex#L166-L172