Skip to content

Commit

Permalink
#684 Added check for if ip None
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivareh committed Nov 19, 2024
1 parent 8c0392a commit 686bd73
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/backend_api/app/api/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,12 @@ def get_username_by_request(request: Request) -> str:


def get_user_ip_with_context(request: Request) -> str: # noqa: ARG001
return context.data["X-Forwarded-For"]
local_host = request.client.host if request.client else "127.0.0.1"
return (
context.data["X-Forwarded-For"]
if context.data["X-Forwarded-For"]
else local_host
)


def get_rate_limit_amount_by_tier(tier: int) -> int:
Expand Down
8 changes: 7 additions & 1 deletion src/backend_api/app/api/routes/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@ async def get_plot_data(
cooldown_seconds=rate_limit_settings.PLOT_RATE_LIMIT_COOLDOWN_SECONDS,
)

client_ip = (
context.data["X-Forwarded-For"]
if context.data["X-Forwarded-For"]
else "127.0.0.1"
)

async with apply_custom_rate_limit(
unique_key="plot_" + get_username_by_request(request),
rate_spec=rate_spec,
prefix=plot_prefix,
), apply_custom_rate_limit(
unique_key="plot_" + context.data["X-Forwarded-For"],
unique_key="plot_" + client_ip,
rate_spec=rate_spec,
prefix=plot_prefix,
):
Expand Down
6 changes: 5 additions & 1 deletion src/backend_api/app/api/routes/turnstile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ async def get_turnstile_validation(
)

# Use a fallback IP if request.client is None (e.g., during testing)
client_ip = context.data["X-Forwarded-For"] if request.client else "127.0.0.1"
client_ip = (
context.data["X-Forwarded-For"]
if context.data["X-Forwarded-For"]
else "127.0.0.1"
)

async with apply_custom_rate_limit(
unique_key="turnstile_" + client_ip,
Expand Down

0 comments on commit 686bd73

Please sign in to comment.