Is using jquery parseHTML to remove script tags enough to prevent XSS attacks?

隐身守侯 提交于 2021-02-10 17:33:29

问题


We are using a WYSWIG Editor(Froala Editor) and storing raw HTML that is created by the user. Thus, escaping the string is not an option. I am intending to store the HTML string in a variable or a data-attribute enclosed within quotes. Then, read that HTML string and remove script tags using jquery's parseHTML as well as keep only certain attributes before loading the HTML into the editor. Is this approach enough to prevent all XSS attacks?


回答1:


It is not. A few counter-examples:

  • <a href="javascript:alert(1)">
  • <div onclick="alert(1)">
  • <img src="javascript:alert(1)"> (doesn't actually work anymore in modern browsers)
  • <div style="background-image: url(javascript:alert(1))"> (doesn't work anymore)

Part of the difficulty is that it also depends on which browser the user is using. The bottomline is, you need a proper sanitizer, which can also be on the client-side. (It can also be on the server, but consider the "preview" feature of the editor if there is any - if previews are not sent to the server, a server-side sanitizer is not of much use. :) )

Google Caja is (was?) a html sanitizer project that also had a pure javascript component. There are other solutions as well.

Note that the editor javascript must support running its contents through a custom sanitizer before inserting it into the DOM if you want to do this in javascript.



来源:https://stackoverflow.com/questions/53427381/is-using-jquery-parsehtml-to-remove-script-tags-enough-to-prevent-xss-attacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!