liveBook · Manning

Output sanitization

As explained in Secure APIs

Output sanitization is the cleaning or encoding of data produced by an external or untrusted source before it is processed, stored, or rendered.

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.
figure

Trace how an unsanitized third-party payload reaches an application database and enables SQL injection. section 5.7

How Secure APIs covers it

How deep it goes
Explains runtime schema validation, rejection of invalid responses, and failure handling, but does not detail sanitization algorithms.
Examples
Uses a third-party API payload containing SQL injection that reaches a consuming application's database.
What you can do after
Apply it via OpenAPI response schemas, runtime validation libraries, rejection rules, and dependency-failure handling.
How it connects
Connects output sanitization with SQL injection, XSS, OpenAPI, circuit breakers, graceful degradation, and data validation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secure APIs. The section links point to the parts of the book it draws on.