Course contentsShow
← Security
Lesson 997 of 3,05221. Web Security - API SecurityFree lesson

Content-Type and Accept Header Exploits

Manipulate Content-Type and Accept headers to trigger alternate parsers, bypass validation, or cause processing errors.

Content-Type and Accept Header Exploits

What you'll learn: How attackers manipulate Content-Type and Accept headers to trick APIs into processing data in dangerous ways or bypassing security checks.

What Are These Headers?

The Content-Type header tells the server what format the request body is in (like application/json or application/xml). The Accept header tells the server what format the client wants in the response. APIs use these to decide which parser to invoke—and that's where vulnerabilities arise.

How the Attack Works

Imagine a restaurant kitchen that changes cooking methods based on labels on food containers. If you mislabel raw chicken as "salad," it might skip proper cooking and cause problems.

Similarly, attackers can:

  1. Claim JSON but send XML – Set Content-Type: application/json while actually sending XML containing XXE payloads, hoping the server's XML parser runs without proper validation
  2. Force alternate parsers – Request Accept: application/xml when the API only validates JSON responses, potentially exposing sensitive data the XML serializer includes
  3. Trigger parsing errors – Send malformed content with mismatched Content-Type to cause crashes or bypass input validation entirely
  4. Bypass validation – If validation logic only checks JSON but the server also accepts XML, send the attack payload as XML to evade detection

Real-World Example

An API validates that JSON input has no admin privileges set. An attacker sends:

Content-Type: application/xml

But the validation layer only runs on JSON. The backend accepts XML too—validation bypassed, unauthorized privilege escalation achieved.

Why This Matters

These headers control which code path executes. Different parsers have different vulnerabilities, validation levels, and behaviors. Mismatched expectations create security gaps.

Key Takeaway: Always validate that Content-Type matches actual content, enforce strict parser selection, and apply validation consistently across all supported formats—never trust client-declared headers to determine security controls.