Tool class code to handle XSS vulnerabilities

Source: Internet
Author: User

public class AntiXSS {/** * filters out dangerous HTML code in content, primarily script code, scrolling subtitle code, and script event handling code * * @param content * String to be filtered * @ Return filter results */public static string Replacehtmlcode (string content) {if (null = = content) {return null;} if (0 = = Content.length ()) {return "";} Script event keywords that need to be filtered string[] Eventkeywords = {"onmouseover", "onmouseout", "onmousedown", "onmouseup", "onmousemove", " OnClick "," ondblclick "," onkeypress "," onkeydown "," onkeyup "," ondragstart "," onerrorupdate "," onhelp "," onReadyStateChange "," onrowenter "," onrowexit "," onselectstart "," onload "," onunload "," onbeforeunload "," onblur "," OnError "," onfocus "," onresize "," onscroll "," OnContextMenu "," alert "};content = replace (content," <script "," < Script ", false); content = replace (content," </script "," </script ", false); content = replace (content," <marquee "," <marquee ", false); content = replace (content," </marquee "," </marquee ", false); content = replace (content," ' "," _ ", false);//Replace single quotation marks with underlined content = replace (coNtent, "\" "," _ ", false);//replace double quotation marks with an underscore//filter script event code for (int i = 0; i < eventkeywords.length; i++) {content = replace (cont ENT, Eventkeywords[i], "_" + Eventkeywords[i], false); Add a "_" to invalidate the event code}return content;}            /** * Replaces oldstr in string source with Newstr and finds it in case-sensitive manner * * @param source * needs to be replaced by a string of sources * @param oldstr * The old string that needs to be replaced * @param newstr * Replaced with a new string */private static string replace (string source, String oldstr, String n EWSTR) {return replace (source, Oldstr, Newstr, true);}             /** * Replace Oldstr in string source with Newstr, matchcase for case Sensitive lookup * * @param source * need to be replaced by string * @param oldstr * The old string that needs to be replaced * @param newstr * Replaced with a new string * @param matchcase * If you need to find */private sta in case sensitive way Tic string Replace (string source, String oldstr, String Newstr,boolean matchcase) {if (source = = null) {return null;} First check that the old string exists and does not exist without replacing if (Source.tolowercase (). IndexOf (Oldstr.tolowercase ()) = = 1) {return source;} int findstartpos = 0;intA = 0;while (a >-1) {int b = 0; String str1, str2, STR3, STR4, stra, strb;str1 = SOURCE;STR2 = Str1.tolowercase (); str3 = OLDSTR;STR4 = Str3.tolowercase (); if (matchcase) {stra = STR1;STRB = STR3;} else {stra = STR2;STRB = STR4;} A = Stra.indexof (StrB, Findstartpos), if (a >-1) {b = Oldstr.length (); findstartpos = a + b; StringBuffer bbuf = new StringBuffer (source), Source = Bbuf.replace (A, a + B, newstr) + "";//The new Find start point is at the end of the replaced string FINDSTARTP OS = Findstartpos + newstr.length ()-B;}} return source;} public static void Main (String [] args) {//string str = "./fabu-advsousuo.jsp?username=xxx<script>alert (123);< /script>&password=yyy "; String str= "Http://192.168.63.87:7001/xxx/xxxx/fabu-search.jsp?searchText=<script>alert (' one One '); </script > "; SYSTEM.OUT.PRINTLN (Antixss.replacehtmlcode (str));}
}

Tool class code to handle XSS vulnerabilities

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.