liveBook · Manning

Null byte injection

As explained in Secure APIs

Null byte injection is an attack in which a null character embedded in input is interpreted as a string terminator or otherwise bypasses application validation.

Null byte injection embeds a NUL character in input so validation treats it as an ordinary string or misses its special meaning. In an API, this can reach storage or downstream processing unexpectedly and, in some cases, enable malicious code injection. section 12.3

A registration payload

A fuzzing test submits {"phone_number":"\u0000"} to a student-registration endpoint. JSON represents the NUL as \u0000; the database error exposes it as \x00. PostgreSQL rejects the value because text fields cannot contain NUL bytes, revealing that validation allowed the disguised null through. section 12.3

Reduce this failure mode by:

  • Reject fields containing the \x00 character in data-validation models before they reach storage or business logic. section 12.3
  • Use fuzzing and contract testing to generate unexpected values, inspect stack traces, and add targeted tests for API and business-layer behavior. section 12.3

How Secure APIs covers it

How deep it goes
Explains representations, a database failure, bypass mechanics, and rejection, but defers deeper business-layer security testing.
Examples
Demonstrates a Schemathesis-generated registration request, JSON and hexadecimal NUL notation, and a PostgreSQL stack trace.
What it assumes
Assumes an API with data validation and persistence, while explaining NUL notation and the failure from the request payload onward.
What you can do after
Diagnose null-byte failures with Schemathesis and stack traces, then reject NUL-containing fields during validation.
How it connects
Connects null byte injection with fuzzing, contract testing, data validation, API security testing, and CWE 158.

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.