XSS Filter Bypass Methodology
Overview
This skill provides a systematic approach for bypassing HTML/JavaScript sanitization filters in authorized security testing contexts (CTF challenges, penetration testing, security research). The methodology emphasizes understanding filter mechanisms before attempting bypasses, avoiding trial-and-error approaches in favor of systematic analysis.
Phase 1: Filter Analysis
Before attempting any bypasses, thoroughly analyze the filter implementation:
Identify the Sanitization Library
- Determine which library performs sanitization (BeautifulSoup, DOMPurify, html-sanitizer, etc.)
- Identify the parser being used (html.parser, lxml, html5lib for BeautifulSoup)
- Research known quirks and bypass techniques for that specific library/parser combination
Map Filter Behavior
Create a systematic map of what the filter blocks vs. preserves:
Blocked Elements: Test which HTML tags are removed
- Script-related:
<script>, <noscript>
- Frame-related:
<iframe>, <frame>, <object>, <embed>
- Other dangerous:
<base>, <link>, <meta>
Blocked Attributes: Test which attributes are stripped
- Event handlers:
onclick, onload, onerror, onmouseover, etc.
- URL attributes with javascript:
href="javascript:...", src="javascript:..."
Preserved Elements: Identify what passes through unchanged
- Standard HTML elements:
<div>, <span>, <p>, <img>, <a>, <style>
- SVG elements:
<svg>, <animate>, <set>
- Math elements:
<math>
Examine Filter Implementation
- Read the filter source code if available
- Look for regex-based filtering (often bypassable)
- Check if filtering is case-sensitive
- Determine if the filter runs once or recursively
Phase 2: Systematic Bypass Categories
Organize bypass attempts by category rather than random trial-and-error:
Category 1: Parser Differential Exploits
Different parsers interpret malformed HTML differently. The server-side sanitizer may parse HTML differently than the browser:
- Nested tag confusion:
<noscript><style></noscript><img src=x>
- Comment injection:
<!--<script>-->alert(1)<!--</script>-->
- Encoding mismatches: UTF-7, charset switching
Category 2: Alternative JavaScript Execution Vectors
If <script> is blocked, identify other execution paths:
- SVG with script:
<svg><script>alert(1)</script></svg>
- SVG event handlers:
<svg>
- SVG animate:
<svg><animate>
- Math elements:
<math><maction actiontype="statusline#http://evil">
Category 3: Event Handler Variations
If standard event handlers are blocked:
- Less common events:
onfocus, onblur, onanimationend, ontransitionend
- Attribute injection: Breaking out of attribute context
- Data attributes with event delegation
Category 4: URL-Based Execution
- javascript: protocol in href:
<a href="javascript:alert(1)">
- data: URLs:
<a href="data:text/html,<script>alert(1)</script>">
- Encoded payloads: URL encoding, HTML entities, mixed encoding
Category 5: CSS-Based Attacks
- CSS expressions (legacy IE):
<div style="background:expression(alert(1))">
- CSS injection for data exfiltration
- @import with data URLs
Phase 3: Testing Methodology
Build a Testing Harness First
Before testing individual payloads, create infrastructure for efficient testing:
# Example: Test multiple payloads at once
payloads = [
'<script>alert(1)</script>',
'<img src=x
'<svg
# ... more payloads
]
for payload in payloads:
filtered = apply_filter(payload)
print(f"Input: {payload}")
print(f"Output: {filtered}")
print(f"Preserved: {payload == filtered}")
print("---")
Two-Stage Verification
- Stage 1 - Filter preservation: Does the payload survive the filter?
- Stage 2 - Browser execution: Does the filtered payload execute in the browser?
Run Stage 1 tests first to eliminate non-viable candidates before slower browser testing.
Document All Attempts
Maintain a log of:
- What was tried
- Why it failed (filtered out vs. didn't execute)
- Insights gained for next attempt
Phase 4: Verification and Validation
Multiple Verification Steps
- Run the verification test multiple times
- Check all success criteria, not just the primary indicator
- Examine the filtered output for anomalies (duplicate tags, malformed HTML)
Cross-Browser Considerations
- A bypass working in Chrome may not work in Firefox or Safari
- Identify which browser the test environment uses
- Document browser-specific behavior
Handle Verification Discrepancies
If initial tests pass but final verification fails:
- Re-read the task requirements
- Check for additional validation steps
- Examine timing issues or race conditions
- Verify the test environment matches expectations
Common Pitfalls to Avoid
Premature Success Declaration
- Do not celebrate after a single test pass
- Run additional verification rounds
- Check the overall task status, not just test output
Workarounds vs. Understanding
- Avoid hacky workarounds that mask underlying issues
- If the test expects files in unexpected locations, understand why before copying files
- Workarounds may introduce inconsistencies
Inefficient Trial-and-Error
- Do not try random XSS vectors without a systematic framework
- Research before attempting; look up known bypass techniques first
- Understand why previous attempts failed before trying similar approaches
Ignoring Malformed Output
- Pay attention to duplicate or malformed tags in filtered output
- Malformed output may indicate an unstable bypass
- Question whether the solution is reliable
Missing Root Cause Analysis
When a bypass works, understand WHY:
- How does the sanitizer parse the payload?
- What browser behavior enables execution?
- Is this a stable, reliable technique or a fragile edge case?
Reference Resources
For authorized security testing contexts, these resources provide bypass techniques:
- OWASP XSS Filter Evasion Cheat Sheet
- PortSwigger Web Security Academy
- HTML5 Security Cheatsheet
- BeautifulSoup documentation on parser differences
- Browser-specific parsing quirks documentation
1---2name: break-filter-js-from-html3description: This skill provides guidance for XSS filter bypass tasks where the goal is to craft HTML payloads that execute JavaScript despite sanitization filters. Use this skill when tasks involve bypassing HTML sanitizers (like BeautifulSoup), exploiting parser differentials between server-side sanitizers and browsers, or security testing/CTF challenges involving XSS filter evasion.4---5
6# XSS Filter Bypass Methodology
7
8## Overview
9
10This skill provides a systematic approach for bypassing HTML/JavaScript sanitization filters in authorized security testing contexts (CTF challenges, penetration testing, security research). The methodology emphasizes understanding filter mechanisms before attempting bypasses, avoiding trial-and-error approaches in favor of systematic analysis.
11
12## Phase 1: Filter Analysis
13
14Before attempting any bypasses, thoroughly analyze the filter implementation:
15
16### Identify the Sanitization Library
17- Determine which library performs sanitization (BeautifulSoup, DOMPurify, html-sanitizer, etc.)
18- Identify the parser being used (html.parser, lxml, html5lib for BeautifulSoup)
19- Research known quirks and bypass techniques for that specific library/parser combination
20
21### Map Filter Behavior
22Create a systematic map of what the filter blocks vs. preserves:
23
241. **Blocked Elements**: Test which HTML tags are removed
25 - Script-related: `<script>`, `<noscript>`
26 - Frame-related: `<iframe>`, `<frame>`, `<object>`, `<embed>`
27 - Other dangerous: `<base>`, `<link>`, `<meta>`
28
292. **Blocked Attributes**: Test which attributes are stripped
30 - Event handlers: `onclick`, `onload`, `onerror`, `onmouseover`, etc.
31 - URL attributes with javascript: `href="javascript:..."`, `src="javascript:..."`
32
333. **Preserved Elements**: Identify what passes through unchanged
34 - Standard HTML elements: `<div>`, `<span>`, `<p>`, `<img>`, `<a>`, `<style>`
35 - SVG elements: `<svg>`, `<animate>`, `<set>`
36 - Math elements: `<math>`
37
38### Examine Filter Implementation
39- Read the filter source code if available
40- Look for regex-based filtering (often bypassable)
41- Check if filtering is case-sensitive
42- Determine if the filter runs once or recursively
43
44## Phase 2: Systematic Bypass Categories
45
46Organize bypass attempts by category rather than random trial-and-error:
47
48### Category 1: Parser Differential Exploits
49Different parsers interpret malformed HTML differently. The server-side sanitizer may parse HTML differently than the browser:
50
51- **Nested tag confusion**: `<noscript><style></noscript><img src=x onerror=alert(1)></style>`
52- **Comment injection**: `<!--<script>-->alert(1)<!--</script>-->`
53- **Encoding mismatches**: UTF-7, charset switching
54
55### Category 2: Alternative JavaScript Execution Vectors
56If `<script>` is blocked, identify other execution paths:
57
58- **SVG with script**: `<svg><script>alert(1)</script></svg>`
59- **SVG event handlers**: `<svg onload=alert(1)>`
60- **SVG animate**: `<svg><animate onbegin=alert(1)>`
61- **Math elements**: `<math><maction actiontype="statusline#http://evil">`
62
63### Category 3: Event Handler Variations
64If standard event handlers are blocked:
65
66- **Less common events**: `onfocus`, `onblur`, `onanimationend`, `ontransitionend`
67- **Attribute injection**: Breaking out of attribute context
68- **Data attributes with event delegation**
69
70### Category 4: URL-Based Execution
71- **javascript: protocol in href**: `<a href="javascript:alert(1)">`
72- **data: URLs**: `<a href="data:text/html,<script>alert(1)</script>">`
73- **Encoded payloads**: URL encoding, HTML entities, mixed encoding
74
75### Category 5: CSS-Based Attacks
76- **CSS expressions** (legacy IE): `<div style="background:expression(alert(1))">`
77- **CSS injection for data exfiltration**
78- **@import with data URLs**
79
80## Phase 3: Testing Methodology
81
82### Build a Testing Harness First
83Before testing individual payloads, create infrastructure for efficient testing:
84
85```python
86# Example: Test multiple payloads at once
87payloads = [
88 '<script>alert(1)</script>',
89 '<img src=x onerror=alert(1)>',
90 '<svg onload=alert(1)>',
91 # ... more payloads
92]
93
94for payload in payloads:
95 filtered = apply_filter(payload)
96 print(f"Input: {payload}")
97 print(f"Output: {filtered}")
98 print(f"Preserved: {payload == filtered}")
99 print("---")
100```
101
102### Two-Stage Verification
1031. **Stage 1 - Filter preservation**: Does the payload survive the filter?
1042. **Stage 2 - Browser execution**: Does the filtered payload execute in the browser?
105
106Run Stage 1 tests first to eliminate non-viable candidates before slower browser testing.
107
108### Document All Attempts
109Maintain a log of:
110- What was tried
111- Why it failed (filtered out vs. didn't execute)
112- Insights gained for next attempt
113
114## Phase 4: Verification and Validation
115
116### Multiple Verification Steps
117- Run the verification test multiple times
118- Check all success criteria, not just the primary indicator
119- Examine the filtered output for anomalies (duplicate tags, malformed HTML)
120
121### Cross-Browser Considerations
122- A bypass working in Chrome may not work in Firefox or Safari
123- Identify which browser the test environment uses
124- Document browser-specific behavior
125
126### Handle Verification Discrepancies
127If initial tests pass but final verification fails:
128- Re-read the task requirements
129- Check for additional validation steps
130- Examine timing issues or race conditions
131- Verify the test environment matches expectations
132
133## Common Pitfalls to Avoid
134
135### Premature Success Declaration
136- Do not celebrate after a single test pass
137- Run additional verification rounds
138- Check the overall task status, not just test output
139
140### Workarounds vs. Understanding
141- Avoid hacky workarounds that mask underlying issues
142- If the test expects files in unexpected locations, understand why before copying files
143- Workarounds may introduce inconsistencies
144
145### Inefficient Trial-and-Error
146- Do not try random XSS vectors without a systematic framework
147- Research before attempting; look up known bypass techniques first
148- Understand why previous attempts failed before trying similar approaches
149
150### Ignoring Malformed Output
151- Pay attention to duplicate or malformed tags in filtered output
152- Malformed output may indicate an unstable bypass
153- Question whether the solution is reliable
154
155### Missing Root Cause Analysis
156When a bypass works, understand WHY:
157- How does the sanitizer parse the payload?
158- What browser behavior enables execution?
159- Is this a stable, reliable technique or a fragile edge case?
160
161## Reference Resources
162
163For authorized security testing contexts, these resources provide bypass techniques:
164
165- OWASP XSS Filter Evasion Cheat Sheet
166- PortSwigger Web Security Academy
167- HTML5 Security Cheatsheet
168- BeautifulSoup documentation on parser differences
169- Browser-specific parsing quirks documentation