Filtering JavaScript out of HTML

后端 未结 6 1639
走了就别回头了
走了就别回头了 2020-12-16 05:29

I have a rich text editor that passes HTML to the server. That HTML is then displayed to other users. I want to make sure there is no JavaScript in that HTML. Is there any w

6条回答
  •  醉酒成梦
    2020-12-16 05:52

    If you want the html to be changed so users can see the HTML code itself. Do a string replace of all '<', '>', '&' and ';'. For example '<' becomes '<'.

    If you want the html to work, the easiest way is to remove all HTML and Javascript and then replace the HTML only. Unfortunately there is almost not sure way of removing all javascript and allowing only HTML.

    For example you may want to allow images. However you may not know that you can do

    
    

    and it can run that script. It becomes very unsafe very fast$. This is why most websites like Wikipedia and this website use special markdown language. This makes it much easier to allow formatting but not malicious javascript.

提交回复
热议问题