A WAF can help reduce XSS risk, but it shouldn’t be your only defense. The best approach is layered defense: sanitize/escape output in the app, use a strict Content Security Policy (CSP), and then use the WAF to catch obvious malicious requests.
What a WAF can do for XSS
A WAF can:
- Detect and block common XSS payloads in query strings, headers, cookies, and request bodies
- Normalize/inspect encoded payloads like
%3Cscript%3E
- Block common attack patterns such as:
<script>...</script>
- event handlers like
onerror=, onload=
javascript: URLs
- HTML tag injection attempts
- suspicious inline JS in inputs
How to configure it effectively
1) Turn on managed XSS rules
If your WAF vendor provides a managed ruleset, enable the XSS protections first. These are usually tuned for common attack patterns and updated over time.
Examples:
- OWASP Core Rule Set
- Cloud WAF managed protections
- Vendor-specific XSS rule groups
2) Inspect all relevant parts of the request
Make sure the WAF checks:
- URL path and query parameters
- POST/PUT body content
- Headers
- Cookies
- JSON/XML payloads
Attackers often hide payloads in non-obvious locations.
3) Normalize inputs before inspection
Your WAF should decode and normalize:
- URL encoding
- HTML entities
- double encoding
- Unicode tricks
- mixed-case obfuscation
Without normalization, payloads like %253Cscript%253E may slip through.
4) Use positive security controls where possible
Instead of only blocking bad patterns, define what is allowed:
- Allowed HTTP methods only
- Allowed content types only
- Parameter allowlists and schema validation
- Character restrictions for fields that don’t need HTML
This reduces false positives and makes evasion harder.
5) Tune for your application
XSS rules can cause false positives, especially if your app legitimately accepts:
- HTML content
- rich text
- SVG
- markdown
- code snippets
For those endpoints:
- allow only the minimum necessary tags/attributes
- sanitize on the server
- use a dedicated HTML sanitizer
- consider endpoint-specific WAF exceptions
6) Log and monitor blocked attempts
Review WAF logs to identify:
- repeated attack sources
- false positives
- parameters frequently targeted
- new bypass techniques
Use alerting for spikes in XSS-related blocks.
Important limitations
A WAF is not enough because:
- Stored XSS may be injected through allowed inputs and later rendered dangerously
- Client-side XSS can occur after the WAF stage
- Obfuscated payloads or novel bypasses may evade signature-based rules
- False positives can block legitimate traffic if rules are too aggressive
Best-practice checklist
Example approach
If you’re protecting a form field that should only contain plain text:
- Block
<, >, ", ', backticks, and event-handler patterns
- Reject HTML tags entirely
- Enforce length and charset limits
- Escape output on display
If you’re protecting a rich text editor:
- Allow only a small set of tags like
<p>, <b>, <i>, <a>
- Strip all scripts, event handlers, and dangerous URL schemes
- Use CSP to reduce impact if something slips through
If you want, I can also help you with:
- WAF rules for a specific vendor (Cloudflare, AWS WAF, ModSecurity, etc.), or
- a sample XSS rule set / regex patterns for a generic WAF.