-
-
Notifications
You must be signed in to change notification settings - Fork 4
(SP: 3)[Security] Harden rate-limit inputs, normalize webhook subjects, and isolate test env #157
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
1d21050
(SP: 1) [Security] Normalize rate-limit subjects (IPv6 hashing) for a…
liudmylasovetovs ad4e3db
(SP: 1) [Security] Harden rate-limit subject derivation (trusted IP, …
liudmylasovetovs a196ce8
(SP: 1) [Security] Harden rate-limits env parsing + trusted IP model;…
liudmylasovetovs 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
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
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,80 @@ | ||
| export type StripeWebhookRateLimitReason = 'missing_sig' | 'invalid_sig'; | ||
|
|
||
| type StripeWebhookRateLimitConfig = { | ||
| max: number; | ||
| windowSeconds: number; | ||
| }; | ||
|
|
||
| // Must match previous inline defaults in the webhook route. | ||
| const DEFAULT_STRIPE_WEBHOOK_RL_MAX = 30; | ||
| const DEFAULT_STRIPE_WEBHOOK_RL_WINDOW_SECONDS = 60; | ||
|
|
||
| function parsePositiveIntStrict(raw: string | undefined): number | null { | ||
| if (raw === undefined) return null; | ||
|
|
||
| const trimmed = raw.trim(); | ||
| if (!trimmed) return null; | ||
|
|
||
| // Strict: digits only (no "+", "-", decimals, exponent, or "10abc"). | ||
| if (!/^\d+$/.test(trimmed)) return null; | ||
|
|
||
| const parsed = Number.parseInt(trimmed, 10); | ||
| if (!Number.isSafeInteger(parsed)) return null; | ||
|
|
||
| return parsed > 0 ? parsed : null; | ||
| } | ||
|
|
||
| function resolveEnvPositiveInt( | ||
| values: Array<string | undefined>, | ||
| fallback: number | ||
| ): number { | ||
| for (const raw of values) { | ||
| const parsed = parsePositiveIntStrict(raw); | ||
| if (parsed !== null) return parsed; | ||
| } | ||
| return fallback; | ||
| } | ||
|
|
||
| export function resolveStripeWebhookRateLimit( | ||
| reason: StripeWebhookRateLimitReason | ||
| ): StripeWebhookRateLimitConfig { | ||
| if (reason === 'missing_sig') { | ||
| return { | ||
| max: resolveEnvPositiveInt( | ||
| [ | ||
| process.env.STRIPE_WEBHOOK_MISSING_SIG_RL_MAX, | ||
| process.env.STRIPE_WEBHOOK_RL_MAX, | ||
| // legacy fallback to preserve current behavior: | ||
| process.env.STRIPE_WEBHOOK_INVALID_SIG_RL_MAX, | ||
| ], | ||
| DEFAULT_STRIPE_WEBHOOK_RL_MAX | ||
| ), | ||
| windowSeconds: resolveEnvPositiveInt( | ||
| [ | ||
| process.env.STRIPE_WEBHOOK_MISSING_SIG_RL_WINDOW_SECONDS, | ||
| process.env.STRIPE_WEBHOOK_RL_WINDOW_SECONDS, | ||
| // legacy fallback to preserve current behavior: | ||
| process.env.STRIPE_WEBHOOK_INVALID_SIG_RL_WINDOW_SECONDS, | ||
| ], | ||
| DEFAULT_STRIPE_WEBHOOK_RL_WINDOW_SECONDS | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| max: resolveEnvPositiveInt( | ||
| [ | ||
| process.env.STRIPE_WEBHOOK_INVALID_SIG_RL_MAX, | ||
| process.env.STRIPE_WEBHOOK_RL_MAX, | ||
| ], | ||
| DEFAULT_STRIPE_WEBHOOK_RL_MAX | ||
| ), | ||
| windowSeconds: resolveEnvPositiveInt( | ||
| [ | ||
| process.env.STRIPE_WEBHOOK_INVALID_SIG_RL_WINDOW_SECONDS, | ||
| process.env.STRIPE_WEBHOOK_RL_WINDOW_SECONDS, | ||
| ], | ||
| DEFAULT_STRIPE_WEBHOOK_RL_WINDOW_SECONDS | ||
| ), | ||
| }; | ||
| } |
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.
🧩 Analysis chain
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 132
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 903
🏁 Script executed:
# Look at the full rate-limit.ts file to understand the context wc -l frontend/lib/security/rate-limit.tsRepository: DevLoversTeam/devlovers.net
Length of output: 110
🏁 Script executed:
# Read the full rate-limit.ts file to understand context and usage cat -n frontend/lib/security/rate-limit.tsRepository: DevLoversTeam/devlovers.net
Length of output: 10136
🏁 Script executed:
# Check .env.example to see what trust flags are documented cat -n frontend/.env.exampleRepository: DevLoversTeam/devlovers.net
Length of output: 2977
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 5727
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 3003
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 91
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 1522
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 428
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 2156
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 1883
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 595
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 1531
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 289
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 1027
Gate
cf-connecting-ipbehind an explicit trust boundary.If any deployment isn't guaranteed to sit behind Cloudflare (or doesn't sanitize this header at the edge), clients can spoof it and evade rate limiting. The current code unconditionally accepts this header for rate-limit subjects, while
x-real-ipandx-forwarded-forare properly guarded byTRUST_FORWARDED_HEADERS. Add an explicitTRUST_CF_CONNECTING_IPflag (defaulting tofalsefor safety) and document it in.env.example.🔒 Suggested guard for trusted CF header
📝 Committable suggestion
🤖 Prompt for AI Agents