mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
feat(rate-limit): add environment variable to disable rate limiting for local testing
This commit is contained in:
@@ -59,6 +59,9 @@ SMTP_FROM="[email protected]"
|
|||||||
# ============================================================================
|
# ============================================================================
|
||||||
# development | production | test
|
# development | production | test
|
||||||
NODE_ENV="development"
|
NODE_ENV="development"
|
||||||
|
# Disable all app-level rate limiting for local testing. Leave unset to keep rate limiting enabled.
|
||||||
|
# Accepted truthy values: "true", "1", "yes", "on"
|
||||||
|
# DISABLE_RATE_LIMIT="true"
|
||||||
# Admin emails for accessing the /admin panel (comma separated list)
|
# Admin emails for accessing the /admin panel (comma separated list)
|
||||||
# e.g., "[email protected],[email protected]"
|
# e.g., "[email protected],[email protected]"
|
||||||
ADMIN_EMAILS=""
|
ADMIN_EMAILS=""
|
||||||
|
|||||||
@@ -18,6 +18,13 @@ interface RateLimitResult {
|
|||||||
resetAt: Date;
|
resetAt: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const TRUTHY_ENV_VALUES = new Set(['1', 'true', 'yes', 'on']);
|
||||||
|
|
||||||
|
function isRateLimitDisabled(): boolean {
|
||||||
|
const rawValue = process.env.DISABLE_RATE_LIMIT?.trim().toLowerCase();
|
||||||
|
return rawValue !== undefined && TRUTHY_ENV_VALUES.has(rawValue);
|
||||||
|
}
|
||||||
|
|
||||||
// Industry-standard rate limit defaults per action
|
// Industry-standard rate limit defaults per action
|
||||||
export const RATE_LIMIT_CONFIGS: Record<string, RateLimitConfig> = {
|
export const RATE_LIMIT_CONFIGS: Record<string, RateLimitConfig> = {
|
||||||
// Auth — strict to prevent brute force / credential stuffing
|
// Auth — strict to prevent brute force / credential stuffing
|
||||||
@@ -73,6 +80,15 @@ export async function checkRateLimit(
|
|||||||
config?: RateLimitConfig
|
config?: RateLimitConfig
|
||||||
): Promise<RateLimitResult> {
|
): Promise<RateLimitResult> {
|
||||||
const { windowMs, maxRequests } = config || RATE_LIMIT_CONFIGS[action] || RATE_LIMIT_CONFIGS.api;
|
const { windowMs, maxRequests } = config || RATE_LIMIT_CONFIGS[action] || RATE_LIMIT_CONFIGS.api;
|
||||||
|
|
||||||
|
if (isRateLimitDisabled()) {
|
||||||
|
return {
|
||||||
|
allowed: true,
|
||||||
|
remaining: maxRequests,
|
||||||
|
resetAt: new Date(Date.now() + windowMs),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const windowSeconds = Math.floor(windowMs / 1000);
|
const windowSeconds = Math.floor(windowMs / 1000);
|
||||||
|
|
||||||
// Validate inputs before passing to query — defence in depth.
|
// Validate inputs before passing to query — defence in depth.
|
||||||
|
|||||||
Reference in New Issue
Block a user