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
- 1Return Content-Type: application/json (or application/problem+json for errors) on every JSON response, including error paths.
- 2Check the framework's error handler specifically: it is the usual place a JSON API falls back to the default HTML content type.
- 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
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
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.
More api checks
- LowTRACE method referenced in API response body
- InfoREST endpoint allows DELETE
- LowGraphQL introspection field suggestions
- MediumGraphQL endpoint has no rate-limit headers
- LowOpenAPI server URL leaks internal host
- LowCORS preflight result not cached
- HighBearer token in URL query string
- MediumJSONP callback parameter accepted
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.