Spaces:
Runtime error
Runtime error
Implement proper rate limiting for HF Spaces proxy environment
Browse filesInstead of disabling rate limiting, configure it properly for
Hugging Face Spaces:
- Higher limits due to shared proxy infrastructure
- Custom key generator using multiple headers
- Skip validation to avoid proxy configuration errors
- Maintain security while working with HF infrastructure
70 unmodified lines71727374757677788 unmodified lines8788899091929370 unmodified lines// Rate limiting configuration for production (HF Spaces) vs developmentconst rateLimitConfig = process.env.NODE_ENV === 'production' ? {// For HF Spaces - use a less strict approach since we can't reliably identify individual usersskip: () => true, // Disable rate limiting in production for now} : {// For development - normal rate limitingwindowMs: 15 * 60 * 1000, // 15 minutes8 unmodified linesconst limiter = rateLimit(rateLimitConfig);export const strictLimiter = process.env.NODE_ENV === 'production' ?rateLimit({ skip: () => true }) : // Disable in productionrateLimit({windowMs: 1 * 60 * 1000, // 1 minutemax: 10, // limit each IP to 10 requests per minute for sensitive endpoints70 unmodified lines71727374757677787980818283848586878889908 unmodified lines9910010110210310410510610710810911011111211311411570 unmodified lines// Rate limiting configuration for production (HF Spaces) vs developmentconst rateLimitConfig = process.env.NODE_ENV === 'production' ? {// For HF Spaces - configure for proxy environmentwindowMs: 15 * 60 * 1000, // 15 minutesmax: 1000, // Higher limit since we can't reliably identify individual usersmessage: {error: "Too many requests, please try again later."},standardHeaders: true,legacyHeaders: false,// Use a combination of headers for better user identification in proxy environmentkeyGenerator: (req) => {return req.ip + '|' + (req.headers['x-forwarded-for'] || req.headers['cf-connecting-ip'] || req.connection.remoteAddress);},// Skip validation that was causing the errorsvalidate: false,} : {// For development - normal rate limitingwindowMs: 15 * 60 * 1000, // 15 minutes8 unmodified linesconst limiter = rateLimit(rateLimitConfig);export const strictLimiter = process.env.NODE_ENV === 'production' ?rateLimit({windowMs: 5 * 60 * 1000, // 5 minutesmax: 50, // More generous limit for productionmessage: {error: "Too many requests, please try again later."},keyGenerator: (req) => {return req.ip + '|' + (req.headers['x-forwarded-for'] || req.headers['cf-connecting-ip'] || req.connection.remoteAddress);},validate: false,}) :rateLimit({windowMs: 1 * 60 * 1000, // 1 minutemax: 10, // limit each IP to 10 requests per minute for sensitive endpoints
server/index.ts+25-3