TL;DR
Secure APIs with unknown structures by focusing on input validation, authentication/authorisation, rate limiting, and monitoring. Treat all data as untrusted, regardless of source (device or server). Implement a Web Application Firewall (WAF) and regularly scan for vulnerabilities.
Securing Unknown Structure APIs
- Treat All Input as Untrusted: Regardless of whether the API request comes from an end-user device or your own server, assume all data is potentially malicious. Never trust client-provided data without validation.
- Input Validation: This is critical. Define expected data types, lengths, formats, and ranges for every possible input parameter. Reject anything that doesn’t match.
- Sanitisation: Remove or encode potentially harmful characters from inputs. Be careful with sanitisation – it’s often better to reject invalid input than try to fix it.
- Authentication and Authorisation: Verify the identity of the caller (authentication) and ensure they have permission to access the requested resources (authorisation).
- API Keys: Simple but less secure. Good for basic identification, but easily compromised.
- OAuth 2.0/OpenID Connect: Industry standard for delegated authorisation. Allows users to grant limited access without sharing credentials.
- JSON Web Tokens (JWT): A compact, URL-safe means of representing claims between parties. Use a strong signing algorithm (e.g., RS256).
- Rate Limiting: Prevent abuse and denial-of-service attacks by limiting the number of requests from a single source within a given timeframe.
- Implement at multiple levels: Consider rate limits per IP address, user account, and API key.
- Example (using Nginx):
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s; server { ... location /api/ { limit_req zone=mylimit burst=10 nodelay; ... } }
- Web Application Firewall (WAF): Deploy a WAF to filter malicious traffic before it reaches your API.
- Cloud-based WAFs: AWS WAF, Cloudflare, Azure Web Application Firewall. Easy to set up and maintain.
- Open Source WAFs: ModSecurity (requires more configuration).
- Configure rules: Protect against common attacks like SQL injection, cross-site scripting (XSS), and remote code execution (RCE).
- Logging and Monitoring: Record all API requests and responses for auditing and security analysis.
- Log relevant data: IP address, timestamp, user ID, requested resource, status code, request body.
- Monitor for anomalies: Unusual traffic patterns, error rates, or suspicious activity.
- Alerting: Set up alerts to notify you of potential security incidents.
- Regular Security Scanning: Identify vulnerabilities in your API code and infrastructure.
- Dynamic Application Security Testing (DAST): Scan the running API for vulnerabilities. Tools like OWASP ZAP, Burp Suite.
- Static Application Security Testing (SAST): Analyse the source code for potential security flaws.
- Penetration testing: Hire a professional to simulate real-world attacks against your API.
- Data Encryption: Protect sensitive data in transit and at rest.
- HTTPS/TLS: Use HTTPS for all API communication. Ensure you have a valid SSL/TLS certificate.
- Encryption at Rest: Encrypt sensitive data stored in databases or other storage systems.

