简体   繁体   中英

When is it appropriate to escape html entities?

In my server-side application every output is passed through htmlentities function. In that way I can assure that my application is xss safe.

But why the input can't diplay the htmlentities correctly? For example, this line of code :

  <input class="form-control" placeholder="name"    id="name" />
name.value = '&lt;script&gt;'

NOTE : &lt;script&gt; = htmlentities("<script>"); &lt;script&gt; = htmlentities("<script>");

this code display the word &lt;script&gt; inside the bar. but i expected to see <script> . right ?

Wrong. htmlentities() does the job of converting characters to HTML entities.

Take the following example and see for yourself.

<input type="text" value="<?php echo htmlentities("<script>"); ?>" />
<input class="form-control" placeholder="name"    id="name" />
<input class="form-control" placeholder="name"    id="address" />
<script type="text/javascript">
    document.getElementById("name").value = "<?php echo htmlentities("<script>"); ?>".replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
    document.getElementById("address").value = "<?php echo htmlentities("<script>"); ?>";
</script>

The difference lies in that you are using javascript to update the value of html element and javascript just puts the escaped value on the box. The point is you have to unescape the escaped entities in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM