
API Input Validation and Security Basics
A practical API security checklist: input validation, authentication and object-level authorization, rate limiting, CORS, secrets, safe errors, and logging.
Quick answer
API security starts with one principle: no input can be trusted. Validate every request against a defined schema and reject unknown fields; on every request check that the user really has access to that specific object, because this is the most common API vulnerability; rate-limit login and sensitive routes; open CORS only to allowed origins; keep secrets in environment variables or a secrets manager; return errors without internal details; and log security events without recording sensitive data.
Principle one: no input can be trusted
Request bodies, URL parameters, query strings, headers, and even uploaded files can all be manipulated. Define the type, length, format, and range of every field in a validation schema and reject anything outside it.
To prevent injection, always build database queries with parameters and never paste an input string directly into a query. For files, check type and size and store them outside the application's executable path.
To put this into practice, explore the scope and real work for these services: Custom Web App Development and Care & Performance.
Authentication and authorization
Authentication tells you who the user is; authorization tells you what they may do. The most dangerous mistake is an API that checks only that a user is signed in, not that they own the object; then changing an id in the URL exposes other people's data.
On every request, check ownership or permission for that exact record on the server. Never read a role or permission level from user input, keep tokens short-lived, and use HttpOnly, Secure cookies for browser sessions.
The key items of the OWASP API Top 10
The OWASP API Security Top 10 (2023 edition) summarizes the most common API risks. These are the priorities in most projects:
- Broken object level authorization: reaching other users' records by changing an id
- Broken authentication: weak passwords, no attempt limits, and tokens that never expire
- Broken object property level authorization: returning or accepting fields the user should not see or change
- Unrestricted resource consumption: no limits on rate, payload size, or page size
- Broken function level authorization: regular users reaching admin operations
- Security misconfiguration and unsafe consumption of other APIs, including SSRF
Rate and resource limits
Set a request cap per user or IP, especially for sign-in, sign-up, password reset, and public forms. Also limit request body size, items per page, and execution time so one heavy request cannot take the server down.
For operations such as payments, require an idempotency key so a retried request does not move money twice.
CORS, headers, and secrets
A few simple settings that stop a large share of attacks:
- Open CORS only to specific origins and never combine * with cookies or credentials
- For cookie-based state-changing requests, add CSRF protection and Origin checks
- Keep secrets and keys in environment variables or a secrets manager, never in the repository
- Rotate keys periodically and keep each key's permissions minimal
Errors, logging, and monitoring
Error messages should help the user, not the attacker. Record technical details, stack traces, and database messages only in logs with a request id, and return a generic message with a specific error code.
Log failed sign-ins, authorization errors, and sudden traffic spikes, and alert on them; but never write passwords, tokens, or personal data to logs.
Frequently asked questions
Is HTTPS enough to secure an API?
No. HTTPS only encrypts the connection; it is necessary, but it does not stop unauthorized access, injection, or abuse of application logic. Validation, authorization, and rate limiting are still essential.
What is BOLA, broken object level authorization?
It means the API does not check that the requested record belongs to that user. For example, changing an order number in the URL reveals other people's orders. The fix is a server-side ownership or permission check on every request.
Are JWT tokens secure?
It depends on the implementation. Always verify the signature and algorithm, keep expiry short, plan for revoking tokens, and store the token somewhere safe, such as an HttpOnly cookie.
