I know this question was discussed on net various times. and people give some example how to bypass these functions through passing some code. But here one issue, All example of htmlentities/htmlspecialchars is related when we embed as attribute value like

<a href="" title="<?php echo htmlentities([XSS_CODE]) ?>"></a> 


<img onerror="<?php echo htmlentities([XSS_CODE]) ?>" /> 

But if I need to show data as content like below.

<div><?php echo htmlentities([XSS_CODE]) ?></div>

How it could be insecure. As code will not have trigger/events just like in case of attributes have, This should safe in all cases.

I studied https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet . Almost all examples for XSS attack/filter bypass not works for given case. I tried hex encoded value of < & > as given in last para of url, But that again failed and displyed simply as data.

I actually have doubt over https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet#Character_escape_sequences which have some combination which not handled by htmlentities like \x3c, \u003c, %3C. While I myself unable to produce and exploit using them.

I tried example like

$code = "\x3cstrong\x3eHello World\x3\cstrong\x3e";
// OR
$code = "\u003cstrongu003eHello Worldu003c\cstrongu003e";

<div><?php echo htmlentities($code); ?></div>

Note : I tried all attacks on Firfox 40 in Ubuntu machine.

  • Welcome. What about this and that questions?
    – user45139
    Sep 21, 2015 at 14:31
  • @Begueradj None of questions talk about specific scenario, but talk about general rules to prevent attacks. Sep 21, 2015 at 14:39
  • your example shouldn't be insecure. htmlentities is what is recommended to prevent XSS (as you also mentioned, htmlentities is not enough in some locations, but in the general case it's secure). Just out of interest: In your question, you imply that your first example would not be secure. How can the title attribute of a link lead to XSS?
    – tim
    Sep 21, 2015 at 15:40
  • @tim I searched and found link having example for span with title attriute for XSS exploit. Don't know if anchor tag behave differently. blog.astrumfutura.com/2012/03/… Sep 22, 2015 at 5:12

2 Answers 2


htmlentities is the better function to use as it encodes all possible characters.

The only way I can see XSS being achieved in your example is with Internet Explorer if the charset is set to UTF-7.

If your charset is UTF-7 then




when interpreted by the browser. Additionally, htmlentities does not cause any encoding of the characters.

Bear in mind that only old versions of Internet Explorer will auto detect UTF-7 - it would need to be explicitly set in modern versions (either by the website author or by the attacker using some other vector) - see this answer.


When a browser parses an event attribute like "onerror", it first HTML decodes the value of the attribute, and then sends it to the JS engine for execution. That is why it is not enough to only HTML encode user content you are inserting in such attributes.

In contrast, when a browser parses a <script> tag, it does not HTML decode it's content. It just forwards its content to the JS engine for execution.

Similarly, when you insert user content between <div> tags, I do not see a way XSS could be possible if the user data is HTML encoded because the browser does no decoding except of course only for presenting the encoded characters on screen.

And I have no idea how the title attribute of the <a> tag could lead to XSS if it's content is HTML encoded and it is double quoted. Please let me know if you know how :)

  • I searched and found link having example for span with title attriute for XSS exploit. Don't know if anchor tag behave differently. blog.astrumfutura.com/2012/03/… Sep 22, 2015 at 5:12
  • But still title is just attribute, which could be decoded by html or only event attributes can be decoded ? Sep 22, 2015 at 5:14
  • See my updates in question for attack I tried which should not handled by htmlentities but they still now work. Sep 22, 2015 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.