Link sanitation bypass in xss_clean()
The xss_clean() function in CodeIgniter failed to detect HTML5 entities like 
 and : because its regular expression only matched entities not followed by a semicolon. An attacker could inject a javascript: link using these entities, bypassing the filter and triggering XSS when a user clicks the link.
Originally disclosed on HackerOne
- Triaged in 3 days
- Fixed in 30 days
- Disclosed after 41 days
Hi there,
While researching a website that uses your framework xss_clean() function to sanitize user's input in comments, I was able to bypass it and could trigger XSS payloads using javascript links in allowed tags such as anchors. This could be achieved by using the new HTML5 standard entities such as 
 	 :
PoC
Run the following piece of code under CI framework
echo $this->security->xss_clean('<a href="javascript
:eval(String.fromCharCode(97, 108, 101, 114, 116, 40, 100, 111, 99, 117, 109, 101, 110, 116, 46, 100, 111, 109, 97, 105, 110, 41));">XSS Link</a>');
For a demo
- Go to http://www.aorank.com/tutorial/codeigniter_xss_clean/index.php/form/ (a demo of a tutorial illustrating xss_clean();)
- In employee name's field, enter
<a href="javascript
:eval(String.fromCharCode(97, 108, 101, 114, 116, 40, 100, 111, 99, 117, 109, 101, 110, 116, 46, 100, 111, 109, 97, 105, 110, 41));">XSS Link</a>
- Submit the form
- The payload is injected into the page and XSS vector is triggered as soon as user clicks the link.
Why?
Actually the website was using an old version of xss_clean() that did not take the new standard entities into consideration. However, when I downloaded the latest version of CI to see if this is fixed, I noticed that you check if they exist and handle them accordingly but there is a flaw in the regular expression used to detect standard entities that causes the XSS bypass.
In line 677 in ./system/core/Security.php
if (preg_match_all('/&[a-z]{2,}(?![a-z;])/i', $str, $matches))
We can see that this regular expression matches alpha characters preceded by ampersand (&) and not followed by a semi-colon (;) --I have no idea why you do this BTW, since entities are usually followed by a semicolon :D--
So when user injects 
 and/or 	 they are not detected by the regular expression and hence not converted to their corresponding characters. That's why the XSS filter fails to detect the presence javascript: in the attribute.
Remediation
Regular expression should be fixed to count for entities followed by a semi-colon :))
Looking forward to hearing from you soon ^_^
Sincerely, Abood Nour
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The regular expression used to detect HTML entities only matched entities not followed by a semicolon, so entities like 
 and : were not converted and the filter failed to spot javascript: in links.
Impact
An attacker could execute JavaScript in a victim's browser when they click a crafted link; the report only demonstrates the XSS trigger.
What the attacker needed
Victim clicks a link; application uses xss_clean() on user input
The fix
The regular expression should be fixed to account for entities followed by a semicolon.
How it was found
- Go to the demo form page.
- Enter a crafted anchor tag with HTML5 entities in the employee name field.
- Submit the form.
- The payload is injected and XSS triggers when the link is clicked.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A filter missed some special character codes browsers understand, so an attacker could sneak in a link that runs JavaScript when clicked.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.