Skip to content

Conversation

ben-fornefeld
Copy link
Member

@ben-fornefeld ben-fornefeld commented Sep 1, 2025

This pr adds an ip based sign-up counter using Redis. all crucial values are configurable via environment variables.

The configuration for production is currently set to:

  SIGN_UP_LIMIT_PER_WINDOW=1
  SIGN_UP_WINDOW_HOURS=24

If the user has too many sign-up's, he might see rate limit alert like this:

Screenshot 2025-09-09 at 1 53 30 PM

Note

Introduces IP-based sign-up rate limiting using Vercel KV with configurable env vars and integrates checks into signUpAction.

  • Auth/Backend:
    • Add IP-based sign-up rate limiting in src/server/auth/auth-actions.ts using ipAddress and new helpers incrementAndCheckSignUpRateLimit/decrementSignUpRateLimit.
    • New module src/server/auth/ratelimit.ts implements atomic Redis Lua scripts for increment/decrement and TTL handling.
    • Extend error handling (email_address_invalid) and warn when no IP in production.
  • Config/Env:
    • New flag ENABLE_SIGN_UP_RATE_LIMITING in src/configs/flags.ts; add KV-backed KV_KEYS.RATE_LIMIT_SIGN_UP and defaults in src/configs/limits.ts.
    • Expand src/lib/env.ts schemas: add numeric-boolean support and new server vars ENABLE_SIGN_UP_RATE_LIMITING, SIGN_UP_LIMIT_PER_WINDOW, SIGN_UP_WINDOW_HOURS; tighten client NEXT_PUBLIC_* booleans.
    • .env.example: document optional Auth rate limiting envs.
  • Dependencies/Build:
    • Add @vercel/functions dependency.
    • tsconfig.json: set jsx to preserve.

Written by Cursor Bugbot for commit 0f99957. This will update automatically on new commits. Configure here.

@ben-fornefeld ben-fornefeld self-assigned this Sep 1, 2025
@ben-fornefeld ben-fornefeld added enhancement New feature or request feature labels Sep 1, 2025
Copy link

linear bot commented Sep 1, 2025

Copy link

vercel bot commented Sep 1, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
web Ready Ready Preview Comment Oct 7, 2025 1:18pm
web-juliett Ready Ready Preview Comment Oct 7, 2025 1:18pm

@mishushakov
Copy link
Member

Did you test it on staging?

@ben-fornefeld ben-fornefeld marked this pull request as draft September 8, 2025 08:51
- Introduced `@upstash/ratelimit` dependency for improved rate limiting functionality.
- Replaced manual increment logic with Upstash's rate limiting utility in the sign-up process.
- Updated sign-up rate limiting checks to enhance logging and error handling.
- Refactored related functions to streamline the rate limiting implementation.
…up process

- Renamed `TruethyOrFalsy` to `NumericBoolean` for clarity in environment schema.
- Added new fields for sign-up attempts limit and window in the environment schema.
- Updated rate limiting functions to improve clarity and logging for sign-up attempts.
- Refactored related code to enhance consistency in rate limiting checks.
…n for sign-up process

- Renamed `SIGN_UP_ATTEMPTS_LIMIT` to `SIGN_UP_ATTEMPTS_LIMIT_PER_WINDOW` for clarity.
- Added new environment variables for sign-up rate limiting configuration.
- Updated documentation and related code to reflect the changes in rate limiting logic.
cursor[bot]

This comment was marked as outdated.

- Added checks to ensure a valid IP address is retrieved in both the auth confirmation route and sign-up action.
- Returns an error response if the IP address is invalid, improving error handling and user feedback.
cursor[bot]

This comment was marked as outdated.

…lidation

- Introduced a custom string type for validating positive numbers in the environment schema.
- Updated rate limiting configuration to ensure that limits and windows are positive numbers.
- Improved documentation to reflect the new validation requirements for rate limiting parameters.
cursor[bot]

This comment was marked as outdated.

- Refactored IP address extraction in the auth confirmation route and sign-up action to use a dedicated utility function.
- Enhanced logging to capture errors when IP headers are missing in production environments.
- Updated documentation to clarify the handling of IP addresses in the rate limiting system, including support for development environments.
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

jakubno
jakubno previously approved these changes Oct 6, 2025
@jakubno jakubno dismissed mishushakov’s stale review October 6, 2025 13:43

All comments were addressed

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@ben-fornefeld
Copy link
Member Author

bugbot run

cursor[bot]

This comment was marked as outdated.

export const ENABLE_SIGN_UP_RATE_LIMITING =
process.env.ENABLE_SIGN_UP_RATE_LIMITING === '1' &&
process.env.KV_REST_API_URL &&
process.env.KV_REST_API_TOKEN
Copy link

Choose a reason for hiding this comment

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

Bug: Rate Limiting Flag Evaluates Incorrectly

The ENABLE_SIGN_UP_RATE_LIMITING flag's logic is flawed. It evaluates to a non-boolean string (e.g., KV_REST_API_TOKEN's value or an empty string) when enabled, which may cause unexpected conditional behavior. It also treats empty strings for KV_REST_API_URL and KV_REST_API_TOKEN as valid, potentially leading to silent rate limiting failures.

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants