wasAsked.

How do you secure a REST API?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · Software Engineering, Authentication & Security

Community answers

Posted by wasAsked editorial collection

Securing a REST API requires several layers: transport security, authentication, authorization, input handling, abuse protection and monitoring. 1. Use HTTPS everywhere - TLS 1.2 or 1.3 only; redirect or reject plain HTTP; enable HSTS. - Never send tokens, API keys or passwords over unencrypted connections. 2. Strong authentication - For users: OAuth 2.0 and OpenID Connect with an identity provider (Microsoft Entra ID, Auth0, Keycloak, Okta). APIs validate JWT access tokens: signature, issuer, audience, expiry. - For services: OAuth client credentials, managed identities or mTLS. - API keys only for simple server-to-server scenarios, sent in headers (never in URLs), stored hashed, rotated and scoped. - Never build your own cryptography or password storage; use proven libraries (ASP.NET Core Identity, Spring Security). 3. Authorization on every request - Role, scope or policy-based checks ([Authorize(Policy = "orders.write")], @PreAuthorize). - Object-level authorization: verify the caller owns or may access the specific resource. Broken object level authorization (IDOR) is the top risk in the OWASP API Security Top 10. - Function-level authorization: admin endpoints must check admin rights, not just authentication. - Deny by default; return 404 for resources users must not know exist. 4. Validate and sanitise input - Validate types, lengths, ranges and formats; reject unknown fields to prevent mass assignment (use dedicated request DTOs). - Parameterised queries or ORMs to prevent SQL injection; encode output to prevent XSS where data reaches HTML. - Limit request body sizes and array lengths; validate file uploads. 5. Protect against abuse - Rate limiting and throttling per user, API key or IP (ASP.NET Core rate limiting middleware, API gateways such as Azure API Management or AWS API Gateway). - Pagination limits so clients cannot request millions of records. - Timeouts and resource limits on expensive operations. 6. Secure configuration - Restrictive CORS (only trusted origins; no wildcard with credentials). - Security headers where relevant (X-Content-Type-Options: nosniff, Content-Security-Policy for any HTML). - Disable unnecessary HTTP methods and endpoints; do not expose debug or admin endpoints publicly. - Secrets in a vault (Azure Key Vault, AWS Secrets Manager), not in code or configuration files. 7. Safe error handling - Return generic error messages with a trace ID; never expose stack traces, SQL errors or internal details. - Use consistent Problem Details responses. 8. Logging, monitoring and auditing - Log authentication failures, authorization denials and suspicious patterns, without logging tokens or sensitive personal data. - Alerts for spikes in 401, 403 or 429 responses. - Audit trails for sensitive operations. 9. Protect data - Return only the fields clients need (response DTOs) — avoid excessive data exposure. - Encrypt sensitive data at rest; mask personal data in logs. 10. Keep everything updated and tested - Patch frameworks and dependencies; scan for vulnerable packages. - Security testing: SAST, DAST (OWASP ZAP), dependency scanning, and penetration tests. - Keep an API inventory: remove old, unused or undocumented endpoints and versions. Interview summary: "HTTPS, OAuth 2.0/OIDC with validated JWTs, authorization on every request including object-level checks, strict input validation, rate limiting, secure configuration and secrets management, safe errors, and continuous monitoring and testing — following the OWASP API Security Top 10."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics