How to sanitize HTML code to prevent XSS attacks in Java or JSP?

后端 未结 3 800
花落未央
花落未央 2020-12-01 08:17

I\'m writing a servlet-based application in which I need to provide a messaging system. I\'m in a rush, so I choose CKEditor to provide editing capabilities, and I currently

相关标签:
3条回答
  • 2020-12-01 09:08

    You should use AntiSamy. (That's what I did)

    0 讨论(0)
  • 2020-12-01 09:09

    I'd recommend using Jsoup for this. Here's an extract of relevance from its site.

    Sanitize untrusted HTML

    Problem

    You want to allow untrusted users to supply HTML for output on your website (e.g. as comment submission). You need to clean this HTML to avoid cross-site scripting (XSS) attacks.

    Solution

    Use the jsoup HTML Cleaner with a configuration specified by a Whitelist.

    String unsafe = 
          "<p><a href='http://example.com/' onclick='stealCookies()'>Link</a></p>";
    String safe = Jsoup.clean(unsafe, Whitelist.basic());
          // now: <p><a href="http://example.com/" rel="nofollow">Link</a></p>
    

    Jsoup offers more advantages than that as well. See also Pros and Cons of HTML parsers in Java.

    0 讨论(0)
  • 2020-12-01 09:09

    If none of the ready-made options seem like enough, there is an excellent series of articles on XSS and attack prevention at Google Code. It should provide plenty of information to work with, if you end up going down that path.

    0 讨论(0)
提交回复
热议问题