-
Notifications
You must be signed in to change notification settings - Fork 36
Feature: Implement Sign-Up Rate Limiting #134
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
Draft
ben-fornefeld
wants to merge
21
commits into
main
Choose a base branch
from
feat-sign-up-request-limiting-per-ip-address-e2b-2967
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e788636
Feature: Implement Sign-Up Rate Limiting
ben-fornefeld 97d71d1
patch: Update Sign-Up Rate Limiting Flag Logic
ben-fornefeld 2aeb968
remove: manual reset since ttl resets
ben-fornefeld a6f4fa9
feat: Add Upstash Rate Limiting for Sign-Up Process
ben-fornefeld 1c8e1ed
refactor: Reorganize OTP verification logic in auth confirmation route
ben-fornefeld 89caac5
refactor: Update rate limiting logic and environment schema for sign-…
ben-fornefeld 424358e
refactor: Update environment variables and rate limiting configuratio…
ben-fornefeld 228ae45
fix: Validate IP address in auth confirmation and sign-up actions
ben-fornefeld 87fc2c3
refactor: Enhance rate limiting configuration with positive number va…
ben-fornefeld 2a92b5a
refactor: Improve IP address handling and logging in rate limiting
ben-fornefeld b17f560
Merge branch 'main' into feat-sign-up-request-limiting-per-ip-address…
ben-fornefeld ada193f
refactor: Simplify rate limiting logic and remove deprecated configur…
ben-fornefeld a950aa5
refactor: Remove rate limiting logic from sign-up confirmation route
ben-fornefeld b9adb0f
refactor: rate limit handling to simplify
ben-fornefeld 3f7d1d4
chore: clean up
ben-fornefeld 9fbb68b
wip: fix race condition
ben-fornefeld 2ac2a5c
remove: rate limit lib in favor of incr/decr
ben-fornefeld 2010cc0
refactor: use custom lua script to ensure atomic mutations
ben-fornefeld b837c7c
fix: missing await
ben-fornefeld 5b7ad4b
fix: ensure correct number passing + ensure ttl is always set
ben-fornefeld 0f99957
refactor: streamline sign-up rate limiting by using default values an…
ben-fornefeld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,9 @@ | |
| "@theguild/remark-mermaid": "^0.2.0", | ||
| "@types/mdx": "^2.0.13", | ||
| "@types/micromatch": "^4.0.9", | ||
| "@upstash/ratelimit": "^2.0.6", | ||
| "@vercel/analytics": "^1.5.0", | ||
| "@vercel/functions": "^3.1.0", | ||
| "@vercel/kv": "^3.0.0", | ||
| "@vercel/otel": "^1.13.0", | ||
| "@vercel/speed-insights": "^1.2.0", | ||
|
|
@@ -1162,12 +1164,20 @@ | |
|
|
||
| "@unrs/resolver-binding-win32-x64-msvc": ["@unrs/[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-lrW200hZdbfRtztbygyaq/6jP6AKE8qQN2KvPcJ+x7wiD038YtnYtZ82IMNJ69GJibV7bwL3y9FgK+5w/pYt6g=="], | ||
|
|
||
| "@upstash/core-analytics": ["@upstash/[email protected]", "", { "dependencies": { "@upstash/redis": "^1.28.3" } }, "sha512-7qJHGxpQgQr9/vmeS1PktEwvNAF7TI4iJDi8Pu2CFZ9YUGHZH4fOP5TfYlZ4aVxfopnELiE4BS4FBjyK7V1/xQ=="], | ||
|
|
||
| "@upstash/ratelimit": ["@upstash/[email protected]", "", { "dependencies": { "@upstash/core-analytics": "^0.0.10" }, "peerDependencies": { "@upstash/redis": "^1.34.3" } }, "sha512-Uak5qklMfzFN5RXltxY6IXRENu+Hgmo9iEgMPOlUs2etSQas2N+hJfbHw37OUy4vldLRXeD0OzL+YRvO2l5acg=="], | ||
|
|
||
| "@upstash/redis": ["@upstash/[email protected]", "", { "dependencies": { "uncrypto": "^0.1.3" } }, "sha512-hSjv66NOuahW3MisRGlSgoszU2uONAY2l5Qo3Sae8OT3/Tng9K+2/cBRuyPBX8egwEGcNNCF9+r0V6grNnhL+w=="], | ||
|
|
||
| "@vercel/analytics": ["@vercel/[email protected]", "", { "peerDependencies": { "@remix-run/react": "^2", "@sveltejs/kit": "^1 || ^2", "next": ">= 13", "react": "^18 || ^19 || ^19.0.0-rc", "svelte": ">= 4", "vue": "^3", "vue-router": "^4" }, "optionalPeers": ["@remix-run/react", "@sveltejs/kit", "next", "react", "svelte", "vue", "vue-router"] }, "sha512-MYsBzfPki4gthY5HnYN7jgInhAZ7Ac1cYDoRWFomwGHWEX7odTEzbtg9kf/QSo7XEsEAqlQugA6gJ2WS2DEa3g=="], | ||
|
|
||
| "@vercel/functions": ["@vercel/[email protected]", "", { "dependencies": { "@vercel/oidc": "3.0.0" }, "peerDependencies": { "@aws-sdk/credential-provider-web-identity": "*" }, "optionalPeers": ["@aws-sdk/credential-provider-web-identity"] }, "sha512-V+p8dO+sg1VjiJJUO5rYPp1KG17SzDcR74OWwW7Euyde6L8U5wuTMe9QfEOfLTiWPUPzN1MXZvLcYxqSYhKc4Q=="], | ||
|
|
||
| "@vercel/kv": ["@vercel/[email protected]", "", { "dependencies": { "@upstash/redis": "^1.34.0" } }, "sha512-pKT8fRnfyYk2MgvyB6fn6ipJPCdfZwiKDdw7vB+HL50rjboEBHDVBEcnwfkEpVSp2AjNtoaOUH7zG+bVC/rvSg=="], | ||
|
|
||
| "@vercel/oidc": ["@vercel/[email protected]", "", { "dependencies": { "@types/ms": "2.1.0", "ms": "2.1.3" } }, "sha512-XOoUcf/1VfGArUAfq0ELxk6TD7l4jGcrOsWjQibj4wYM74uNihzZ9gA46ywWegoqKWWdph4y5CKxGI9823deoA=="], | ||
|
|
||
| "@vercel/otel": ["@vercel/[email protected]", "", { "peerDependencies": { "@opentelemetry/api": ">=1.7.0 <2.0.0", "@opentelemetry/api-logs": ">=0.46.0 <0.200.0", "@opentelemetry/instrumentation": ">=0.46.0 <0.200.0", "@opentelemetry/resources": ">=1.19.0 <2.0.0", "@opentelemetry/sdk-logs": ">=0.46.0 <0.200.0", "@opentelemetry/sdk-metrics": ">=1.19.0 <2.0.0", "@opentelemetry/sdk-trace-base": ">=1.19.0 <2.0.0" } }, "sha512-esRkt470Y2jRK1B1g7S1vkt4Csu44gp83Zpu8rIyPoqy2BKgk4z7ik1uSMswzi45UogLHFl6yR5TauDurBQi4Q=="], | ||
|
|
||
| "@vercel/speed-insights": ["@vercel/[email protected]", "", { "peerDependencies": { "@sveltejs/kit": "^1 || ^2", "next": ">= 13", "react": "^18 || ^19 || ^19.0.0-rc", "svelte": ">= 4", "vue": "^3", "vue-router": "^4" }, "optionalPeers": ["@sveltejs/kit", "next", "react", "svelte", "vue", "vue-router"] }, "sha512-y9GVzrUJ2xmgtQlzFP2KhVRoCglwfRQgjyfY607aU0hh0Un6d0OUyrJkjuAlsV18qR4zfoFPs/BiIj9YDS6Wzw=="], | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| export const ALLOW_SEO_INDEXING = process.env.ALLOW_SEO_INDEXING === '1' | ||
| export const VERBOSE = process.env.NEXT_PUBLIC_VERBOSE === '1' | ||
| export const INCLUDE_BILLING = process.env.NEXT_PUBLIC_INCLUDE_BILLING === '1' | ||
| 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 | ||
| export const USE_MOCK_DATA = | ||
| process.env.VERCEL_ENV !== 'production' && | ||
| process.env.NEXT_PUBLIC_MOCK_DATA === '1' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import 'server-cli-only' | ||
|
|
||
| import { l } from '@/lib/clients/logger/logger' | ||
| import { Duration, Ratelimit } from '@upstash/ratelimit' | ||
| import { kv } from '@vercel/kv' | ||
|
|
||
| const SIGN_UP_LIMIT_PER_WINDOW = parseInt( | ||
| process.env.SIGN_UP_LIMIT_PER_WINDOW || '1' | ||
| ) | ||
| const SIGN_UP_WINDOW_HOURS = parseInt(process.env.SIGN_UP_WINDOW_HOURS || '24') | ||
|
|
||
| const SIGN_UP_WINDOW: Duration = `${SIGN_UP_WINDOW_HOURS}h` | ||
|
|
||
| const _ratelimit = new Ratelimit({ | ||
| redis: kv, | ||
| limiter: Ratelimit.slidingWindow(SIGN_UP_LIMIT_PER_WINDOW, SIGN_UP_WINDOW), | ||
| }) | ||
|
|
||
| export async function incrementSignUpRateLimit( | ||
| identifier: string | ||
| ): Promise<boolean> { | ||
| const result = await _ratelimit.limit(identifier) | ||
|
|
||
| if (!result.success) { | ||
| l.error({ | ||
| key: 'sign_up_rate_limit_increment:limit_error', | ||
| context: { | ||
| identifier, | ||
| result, | ||
| }, | ||
| }) | ||
|
|
||
| return false | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return result.remaining === 0 | ||
| } | ||
ben-fornefeld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export async function isSignUpRateLimited( | ||
| identifier: string | ||
| ): Promise<boolean> { | ||
| const result = await _ratelimit.getRemaining(identifier) | ||
|
|
||
| return result.remaining === 0 | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Bug: Rate Limiting Flag Evaluates Incorrectly
The
ENABLE_SIGN_UP_RATE_LIMITINGflag'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 forKV_REST_API_URLandKV_REST_API_TOKENas valid, potentially leading to silent rate limiting failures.