7.1 KiB
API Security & Rate Limiting
Overview
AtlasOS implements comprehensive API security measures including rate limiting, security headers, CORS protection, and request validation to protect the API from abuse and attacks.
Rate Limiting
Token Bucket Algorithm
The rate limiter uses a token bucket algorithm:
- Default Rate: 100 requests per minute per client
- Window: 60 seconds
- Token Refill: Tokens are refilled based on elapsed time
- Per-Client: Rate limiting is applied per IP address or user ID
Rate Limit Headers
All responses include rate limit headers:
X-RateLimit-Limit: 100
X-RateLimit-Window: 60
Rate Limit Exceeded
When rate limit is exceeded, the API returns:
{
"code": "SERVICE_UNAVAILABLE",
"message": "rate limit exceeded",
"details": "too many requests, please try again later"
}
HTTP Status: 429 Too Many Requests
Client Identification
Rate limiting uses different keys based on authentication:
- Authenticated Users:
user:{user_id}- More granular per-user limiting - Unauthenticated:
ip:{ip_address}- IP-based limiting
Public Endpoints
Public endpoints (login, health checks) are excluded from rate limiting to ensure availability.
Security Headers
All responses include security headers:
X-Content-Type-Options
- Value:
nosniff - Purpose: Prevents MIME type sniffing
X-Frame-Options
- Value:
DENY - Purpose: Prevents clickjacking attacks
X-XSS-Protection
- Value:
1; mode=block - Purpose: Enables XSS filtering in browsers
Referrer-Policy
- Value:
strict-origin-when-cross-origin - Purpose: Controls referrer information
Permissions-Policy
- Value:
geolocation=(), microphone=(), camera=() - Purpose: Disables unnecessary browser features
Strict-Transport-Security (HSTS)
- Value:
max-age=31536000; includeSubDomains - Purpose: Forces HTTPS connections (only on HTTPS)
- Note: Only added when request is over TLS
Content-Security-Policy (CSP)
- Value:
default-src 'self'; script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; img-src 'self' data:; font-src 'self' https://cdn.jsdelivr.net; connect-src 'self'; - Purpose: Restricts resource loading to prevent XSS
CORS (Cross-Origin Resource Sharing)
Allowed Origins
By default, the following origins are allowed:
http://localhost:8080http://localhost:3000http://127.0.0.1:8080- Same-origin requests (no Origin header)
CORS Headers
When a request comes from an allowed origin:
Access-Control-Allow-Origin: http://localhost:8080
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 3600
Preflight Requests
OPTIONS requests are handled automatically:
- Status:
204 No Content - Headers: All CORS headers included
- Purpose: Browser preflight checks
Request Size Limits
Maximum Request Body Size
- Limit: 10 MB (10,485,760 bytes)
- Enforcement: Automatic via
http.MaxBytesReader - Error: Returns
413 Request Entity Too Largeif exceeded
Content-Type Validation
POST, PUT, and PATCH requests must include a valid Content-Type header:
Allowed Types:
application/jsonapplication/x-www-form-urlencodedmultipart/form-data
Error Response:
{
"code": "BAD_REQUEST",
"message": "Content-Type must be application/json"
}
Middleware Chain Order
Security middleware is applied in the following order (outer to inner):
- CORS - Handles preflight requests
- Security Headers - Adds security headers
- Request Size Limit - Enforces 10MB limit
- Content-Type Validation - Validates request content type
- Rate Limiting - Enforces rate limits
- Error Recovery - Catches panics
- Request ID - Generates request IDs
- Logging - Logs requests
- Audit - Records audit logs
- Authentication - Validates JWT tokens
- Routes - Handles requests
Public Endpoints
The following endpoints are excluded from certain security checks:
/api/v1/auth/login- Rate limiting, Content-Type validation/api/v1/auth/logout- Rate limiting, Content-Type validation/healthz- Rate limiting, Content-Type validation/metrics- Rate limiting, Content-Type validation/api/docs- Rate limiting, Content-Type validation/api/openapi.yaml- Rate limiting, Content-Type validation
Best Practices
For API Consumers
- Respect Rate Limits: Implement exponential backoff when rate limited
- Use Authentication: Authenticated users get better rate limits
- Include Content-Type: Always include
Content-Type: application/json - Handle Errors: Check for
429status and retry after delay - Request Size: Keep request bodies under 10MB
For Administrators
- Monitor Rate Limits: Check logs for rate limit violations
- Adjust Limits: Modify rate limit values in code if needed
- CORS Configuration: Update allowed origins for production
- HTTPS: Always use HTTPS in production for HSTS
- Security Headers: Review CSP policy for your use case
Configuration
Rate Limiting
Rate limits are currently hardcoded but can be configured:
// In rate_limit.go
rateLimiter := NewRateLimiter(100, time.Minute) // 100 req/min
CORS Origins
Update allowed origins in security_middleware.go:
allowedOrigins := []string{
"https://yourdomain.com",
"https://app.yourdomain.com",
}
Request Size Limit
Modify in app.go:
a.requestSizeMiddleware(10*1024*1024) // 10MB
Error Responses
Rate Limit Exceeded
{
"code": "SERVICE_UNAVAILABLE",
"message": "rate limit exceeded",
"details": "too many requests, please try again later"
}
Status: 429 Too Many Requests
Request Too Large
{
"code": "BAD_REQUEST",
"message": "request body too large"
}
Status: 413 Request Entity Too Large
Invalid Content-Type
{
"code": "BAD_REQUEST",
"message": "Content-Type must be application/json"
}
Status: 400 Bad Request
Monitoring
Rate Limit Metrics
Monitor rate limit violations:
- Check audit logs for rate limit events
- Monitor
429status codes in access logs - Track rate limit headers in responses
Security Events
Monitor for security-related events:
- Invalid Content-Type headers
- Request size violations
- CORS violations (check server logs)
- Authentication failures
Future Enhancements
- Configurable Rate Limits: Environment variable configuration
- Per-Endpoint Limits: Different limits for different endpoints
- IP Whitelisting: Bypass rate limits for trusted IPs
- Rate Limit Metrics: Prometheus metrics for rate limiting
- Distributed Rate Limiting: Redis-based for multi-instance deployments
- Advanced CORS: Configurable CORS via environment variables
- Request Timeout: Configurable request timeout limits