This document outlines the security measures implemented in this Next.js application to protect API endpoints and ensure secure operation.
The application implements multiple layers of security:
- Domain Validation - CORS protection based on allowed domains
- Rate Limiting - Per-IP request limits to prevent abuse
- API Key Authentication - Optional API key protection for sensitive endpoints
- Request Logging - Monitoring and logging of all API requests
- Security Headers - Standard security headers on all responses
# Required: Your app's domain for CORS validation
NEXT_PUBLIC_APP_URL=https://yourdomain.com
# Optional: API key for scraping endpoints
API_SECRET_KEY=your-secret-api-key-here| Endpoint | Rate Limit | API Key Required | CORS Protected |
|---|---|---|---|
/api/repositories |
30/min | No | Yes |
/api/stats |
60/min | No | Yes |
/api/paper |
10/min | Yes* | No** |
/api/paper/detail |
10/min | Yes* | No** |
/api/trending |
10/min | Yes* | No** |
/api/ossinsight |
10/min | Yes* | No** |
*API key required only if API_SECRET_KEY is set in environment
**CORS disabled for server-to-server scraping calls
The application implements in-memory rate limiting with the following limits:
- General endpoints: 100 requests per minute per IP
- Search endpoints: 30 requests per minute per IP
- Scraping endpoints: 10 requests per minute per IP
- Stats endpoint: 60 requests per minute per IP
Rate limit headers are included in responses:
X-RateLimit-Limit: Maximum requests allowedX-RateLimit-Remaining: Requests remaining in current windowX-RateLimit-Reset: Unix timestamp when limit resets
CORS protection is enforced based on the NEXT_PUBLIC_APP_URL environment variable:
- Requests must originate from the configured domain
- Subdomains are automatically allowed (e.g.,
api.yourdomain.com) - Development mode allows
localhostand127.0.0.1 - Direct API calls (no origin/referer) are allowed in development only
Scraping endpoints can be protected with an API key:
- Set
API_SECRET_KEYin your environment variables - Include the key in requests:
X-API-Key: your-secret-api-key-here - If no API key is configured, endpoints remain public
All API responses include security headers:
X-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1; mode=blockReferrer-Policy: strict-origin-when-cross-originAccess-Control-Allow-Origin: [configured-domain]
All API requests are logged with:
- HTTP method and endpoint
- Client IP address
- Origin/referer information
- Timestamp
Failed security checks are logged as warnings:
- Invalid origin attempts
- Rate limit violations
- Invalid API key attempts
- Set NEXT_PUBLIC_APP_URL: Always configure your production domain
- Use API Keys: Set
API_SECRET_KEYfor scraping endpoint protection - Monitor Logs: Watch for security violations and unusual patterns
- Consider Redis: For high-traffic applications, replace in-memory rate limiting with Redis
- Enable HTTPS: Ensure all traffic uses HTTPS in production
- Database Security: Use Supabase RLS policies for additional data protection
- Allows requests from
localhostand127.0.0.1 - More permissive CORS policy
- Detailed error messages
- Direct API access allowed
- Strict domain validation
- Limited error information
- No direct API access without proper origin
- Enhanced security logging
403 Forbidden - Invalid Origin
- Check
NEXT_PUBLIC_APP_URLis correctly set - Ensure requests come from the configured domain
- Verify subdomain configuration if needed
429 Too Many Requests
- Rate limit exceeded, wait for reset time
- Check
X-RateLimit-Resetheader for reset timestamp - Consider implementing client-side rate limiting
401 Unauthorized - Invalid API Key
- Verify
X-API-Keyheader is included - Check API key matches
API_SECRET_KEYenvironment variable - Ensure API key is required for the endpoint
This security implementation should be regularly reviewed and updated:
- Monitor for new security vulnerabilities
- Update rate limits based on usage patterns
- Review and rotate API keys regularly
- Audit security logs for suspicious activity
- Keep dependencies updated for security patches