Skip to main content
LowAPIOWASP A03:2021CWE-436

How to fix: JSON body served as HTML or with no Content-Type

The response body is JSON-shaped but is not labelled as JSON by its Content-Type header. It is either declared as text/html, or served with no Content-Type at all.

Why it matters

A JSON document labelled text/html is parsed as markup. Any string value in it that an attacker controls, a username, a search term, an error message echoing input, becomes live HTML in the browser, which is stored or reflected XSS on your own origin. With no Content-Type at all the browser sniffs, and a body whose first bytes look like markup is treated as a document.

The detector checks structure rather than parsing the whole body: it requires the response to open with a JSON object or array and to have no HTML document markers near the start, so an HTML page that happens to embed JSON does not match. X-Content-Type-Options: nosniff blocks the sniffing half of this but does not help when the server explicitly says text/html, so the Content-Type itself has to be correct.

How to fix it

  1. 1Return Content-Type: application/json (or application/problem+json for errors) on every JSON response, including error paths.
  2. 2Check the framework's error handler specifically: it is the usual place a JSON API falls back to the default HTML content type.
  3. 3Send X-Content-Type-Options: nosniff as a second layer so a missing type never gets sniffed into a document.

Code examples

Express: JSON content type on errors too

JavaScript
app.use((err, req, res, _next) => {
  res.status(err.status || 500)
     .type('application/problem+json')
     .json({ title: 'Request failed', status: err.status || 500 });
});

Next.js Route Handler

TypeScript
return new Response(JSON.stringify(payload), {
  status: 200,
  headers: {
    'Content-Type': 'application/json; charset=utf-8',
    'X-Content-Type-Options': 'nosniff',
  },
});

References

Detected automatically by VulnRadar. The check id is api-json-response-content-type-mismatch, stable across every scan so you can filter or diff on it.

See every api check

Questions

How severe is JSON body served as HTML or with no Content-Type?
VulnRadar rates this low severity in the api category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id api-json-response-content-type-mismatch, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does JSON body served as HTML or with no Content-Type map to?
It maps to CWE-436. In the OWASP Top 10 it falls under A03:2021.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for json body served as html or with no content-type

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

JSON body served as HTML or with no Content-Type | VulnRadar