Comprehensive guide to securing your API infrastructure based on OWASP guidelines, industry standards, and real-world breach analyses.
# Bad: Token in URL
GET /api/users?token=eyJhbGciOiJIUzI1NiIsInR5cCI6...
# Good: Token in Authorization header
GET /api/users
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6...Require MFA for sensitive operations:
The #1 OWASP API vulnerability. Never trust client-provided object IDs:
# Vulnerable: Anyone can access any order
GET /api/orders/12345
# Secure: Validate ownership
def get_order(order_id, user_id):
order = db.query(Order).filter(
Order.id == order_id,
Order.user_id == user_id # <-- Ownership check
).first()
if not order:
raise Forbidden("Access denied")
return order# Bad: Exposing sensitive fields
{
"user": {
"id": 123,
"email": "user@example.com",
"password_hash": "$2b$12$...", // NEVER return this
"ssn": "123-45-6789", // Unnecessary exposure
"internal_notes": "VIP customer" // Internal data
}
}
# Good: Return only necessary fields
{
"user": {
"id": 123,
"email": "user@example.com",
"name": "John Doe"
}
}# Good: Strict validation with Pydantic
from pydantic import BaseModel, EmailStr, constr
class UserCreate(BaseModel):
email: EmailStr
name: constr(min_length=1, max_length=100)
age: int = Field(ge=0, le=150)
class Config:
extra = "forbid" # Reject unknown fieldsHTTP/1.1 200 OK
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 87
X-RateLimit-Reset: 1640995200
Retry-After: 60
# When limit exceeded:
HTTP/1.1 429 Too Many Requests
Retry-After: 60
{
"error": "Rate limit exceeded",
"retry_after": 60
}Log these security events:
❌ Never log:
npm audit or safety check in CI/CD pipelineDownload this guide for offline reference and team training
G8KEPR implements all these security best practices automatically. Get enterprise-grade API security without the complexity.