AC-016 Contract caught CS-RESP-008CS-RESP-006

Malformed JSON that parsers read differently

Broken bodies and broken connections

How to run this card's commands yourself

1. The problem

In one sentence

The body is almost JSON, and different parsers disagree about it: one accepts it, one rejects it, one reads it differently.

A story

A server sends {"id":1} garbage. A lenient parser in the mobile app reads {"id":1} and ignores the rest. The web client’s strict parser throws. A third service, a backend that reads the same API, accepts it but reads a duplicated key differently. The same body means three different things, depending on who reads it.

Why it hurts

Bugs that depend on the parser are the hardest to find, because every team tests with its own parser and sees a different result.

  • For developers: “it works on my machine” for JSON bodies. The fix is to make the server send strict JSON, not to find a more forgiving parser.
  • For managers: a defect that shows in some clients and not in others looks random, and takes much longer to find than a plain failure.
  • For DevOps: a strict check in the pipeline is the neutral referee. Run it against the real response, not a re-serialised copy.

Example

Six answers that are not strict JSON (or that a parser may read differently). Each is a fixture:

/hostile/bom                   a byte-order mark before the {
/hostile/invalid-utf8          the bytes ff fe inside a string
/hostile/raw-newline-in-string a raw line break inside a string
/hostile/nan-literal           {"id":NaN}
/hostile/trailing-garbage      {"id":1} garbage
/hostile/duplicate-keys        {"id":1,"id":"two"}

How to detect it without clrsig

  • curl -s URL | jq . reports a parse error for most of these. Try a second parser (a browser console, another language) and compare: if they disagree, the body is not safe.
  • curl -s URL | xxd | head shows the raw bytes, including a hidden byte-order mark.

How to fix it

There are two usual causes:

  1. The server is wrong (more likely). It builds JSON by hand, or leaks text (a log line, a banner) after the value. Use a real JSON serialiser for the whole body.
  2. A layer after the server changes the body (a proxy that adds text, a file with a byte-order mark). Find that layer.

The spec is rarely the cause here. Never loosen the check.

AC-010 The body is not valid JSON, or is empty, AC-002 The Content-Type is not the one the spec promised, AC-017 The body is cut off, or the connection drops.

Words used on this page are explained once in the glossary.

2. How clrsig helps

Check

CS-RESP-008 Body is not valid JSON (strict), and CS-RESP-006 Body field of the wrong type (for the duplicate key). State: caught. Severity today: failure (exit code 1). clrsig reads the body as strict JSON (RFC 8259).

Command

--paths limits the run to these six GET operations, so nothing is written:

clrsig conform specs/conform-fail.yaml \
  -b http://localhost:8080/conform-fail \
  --env clrsig/environments/conform-fail.yaml \
  --paths /hostile/bom,/hostile/invalid-utf8,/hostile/raw-newline-in-string,/hostile/nan-literal,/hostile/trailing-garbage,/hostile/duplicate-keys \
  --explain

Exit code: 0 clean, 1 a finding, 2 the spec or the setup is broken. Here it is 1.

What you see

Output from clrsig v0.21.0, recorded on 2026-11-30. The lines for the 70 operations that --paths skipped are left out by grep -v '^ SKIP'; no line is edited.

conform-fail API  1.0.0
Base URL: http://localhost:8080/conform-fail
Limited by --paths /hostile/bom,/hostile/invalid-utf8,/hostile/raw-newline-in-string,/hostile/nan-literal,/hostile/trailing-garbage,/hostile/duplicate-keys

  FAIL  GET     /hostile/bom  200  15ms
         ✗ GET /hostile/bom 200: the response body is not valid JSON (invalid character 'ï' looking for beginning of value)
         → [CS-RESP-008] The response body is not valid JSON. Why: The spec declares a JSON body for response 200, but what arrived cannot be parsed, so no field was compared. Where: response body (spec: GET /hostile/bom → response 200 → body schema). Fix: Make the server send valid JSON (check for a truncated, HTML or plain-text error page), or correct the declared media type in the spec.
  FAIL  GET     /hostile/duplicate-keys  200  2ms
         ✗ Error at "/id": value must be an integer
Value:
  "two"
         → [CS-RESP-006] "id" is text, the spec says integer. Why: The server sent text ("two") where the spec declares an integer. Where: response body, field id (spec: GET /hostile/duplicate-keys → response 200 → body schema). Fix: Send an integer from the server (for example without quotes for a number), or change the type in the spec if the server is right.
  FAIL  GET     /hostile/invalid-utf8  200  2ms
         ✗ GET /hostile/invalid-utf8 200: the response body is not valid JSON (byte 0xff at offset 20 is not UTF-8)
         → [CS-RESP-008] The response body is not valid JSON. Why: The spec declares a JSON body for response 200, but what arrived cannot be parsed, so no field was compared. Where: response body (spec: GET /hostile/invalid-utf8 → response 200 → body schema). Fix: Make the server send valid JSON (check for a truncated, HTML or plain-text error page), or correct the declared media type in the spec.
  FAIL  GET     /hostile/nan-literal  200  1ms
         ✗ GET /hostile/nan-literal 200: the response body is not valid JSON (invalid character 'N' looking for beginning of value)
         → [CS-RESP-008] The response body is not valid JSON. Why: The spec declares a JSON body for response 200, but what arrived cannot be parsed, so no field was compared. Where: response body (spec: GET /hostile/nan-literal → response 200 → body schema). Fix: Make the server send valid JSON (check for a truncated, HTML or plain-text error page), or correct the declared media type in the spec.
  FAIL  GET     /hostile/raw-newline-in-string  200  1ms
         ✗ GET /hostile/raw-newline-in-string 200: the response body is not valid JSON (invalid character '\n' in string literal)
         → [CS-RESP-008] The response body is not valid JSON. Why: The spec declares a JSON body for response 200, but what arrived cannot be parsed, so no field was compared. Where: response body (spec: GET /hostile/raw-newline-in-string → response 200 → body schema). Fix: Make the server send valid JSON (check for a truncated, HTML or plain-text error page), or correct the declared media type in the spec.
  FAIL  GET     /hostile/trailing-garbage  200  1ms
         ✗ GET /hostile/trailing-garbage 200: the response body is not valid JSON (text after the JSON value)
         → [CS-RESP-008] The response body is not valid JSON. Why: The spec declares a JSON body for response 200, but what arrived cannot be parsed, so no field was compared. Where: response body (spec: GET /hostile/trailing-garbage → response 200 → body schema). Fix: Make the server send valid JSON (check for a truncated, HTML or plain-text error page), or correct the declared media type in the spec.

FAILED  76 operations  ·  0 passed  ·  6 failed  ·  70 skipped  31ms

How to read it: five bodies fail as invalid JSON (CS-RESP-008), with the parser’s own reason. The duplicate key is read the way most parsers do, with the last value winning, so id is the text "two" and fails the type check (CS-RESP-006).

Where to look

  • Desktop app: Check Conformance lists each failure with its explanation.
  • Reports: with --format json, each finding has a checkId.
  • GitHub Action: it runs the same command.

How clrsig helps you fix it

It gives the parser’s reason, which usually points at the exact byte or character. It treats strictness as the rule, so a body that one lenient parser reads is still reported. The bom case is rejected although the JSON standard lets a parser ignore a byte-order mark: the sandbox notes call that strict and defensible.

Limits

  • For duplicate keys the standard leaves the result undefined; clrsig uses the last value.
  • With an OpenAPI 3.0 spec, as here, only the first problem in a response body is reported. With 3.1, each problem has its own line.

More ways

Nothing yet.

3. Video