Output sanitization cleans or encodes data from external or untrusted sources before an application processes, stores, or renders it. It prevents malformed or malicious API responses from becoming application failures, corrupted records, SQL injection, or XSS. section 5.7
Apply output sanitization as part of defensive API consumption:
- Validate responses against models derived from documented OpenAPI response schemas. section 5.7
- Reject invalid responses instead of persisting or rendering them, and contact the API provider. section 5.7
- Handle unavailable or malfunctioning dependencies with circuit breakers and graceful degradation. section 5.7
Malicious data in a third-party response
A threat actor inserts a SQL injection statement into a third-party API. An consuming API accepts the response without validation or sanitization, and the statement executes against its database. Response validation and safe query handling interrupt this path. section 5.7
Figure 5.9 Lack of validation and sanitization on the data coming from third-party APIs exposes our applications to unsafe consumption of APIs exploits. In this example, a threat actor inserts a SQL injection statement into a third-party API. When our API consumes that data, the SQL injection runs against our database with devastating consequences.
Trace how an unsanitized third-party payload reaches an application database and enables SQL injection. section 5.7
