Course contentsShow
← Security
Lesson 655 of 3,05214. Web Security - XSS and Content InjectionFree lesson

Context-Specific Evasion Techniques

Tailoring payloads to specific injection contexts including HTML attributes, JavaScript strings, and CSS contexts.

Context-Specific Evasion Techniques

What you'll learn: How to craft XSS payloads that work specifically within HTML attributes, JavaScript strings, and CSS contexts by understanding each context's unique escape rules.


Why Context Matters

Not all injection points are created equal. A payload that works perfectly when injected directly into HTML (<script>alert(1)</script>) will fail spectacularly if injected inside a JavaScript string or an HTML attribute. Effective evasion requires understanding where your payload lands and adapting accordingly.

Think of it like language translation: the same idea needs different words depending on who's listening.


HTML Attribute Context

When your input lands inside an attribute like <input value="USER_INPUT">, you need to break out of the attribute first:

  • Close the attribute with a quote: " onload="alert(1)
  • Use event handlers directly: " onerror="alert(1)
  • Or create new attributes: " autofocus onfocus="alert(1)

JavaScript String Context

If your input appears inside JavaScript like var name = "USER_INPUT";, you must escape the string context:

  • Break the string: "; alert(1); //
  • Use string concatenation: "+alert(1)+"
  • Exploit template literals if available: `${alert(1)}`

The key is syntactically valid JavaScript that executes your payload.


CSS Context

Inside <style> tags or inline styles, leverage CSS-specific features:

  • expression() in older IE browsers
  • url() with JavaScript protocol: url("javascript:alert(1)")
  • Import malicious stylesheets with @import

The Core Principle

Always analyze: What characters are allowed? What breaks the current context? What completes valid syntax? Your payload must speak the grammar of its injection point.

Key Takeaway: Successful XSS evasion requires tailoring payloads to their specific injection context—HTML attributes need different escape techniques than JavaScript strings or CSS rules, because each context has unique parsing rules and special characters.