Preventing XSS with Jsoup

Source: Internet
Author: User

Jsoup is an XSS prevention tool. Jsoup can detect XSS script in HTML and URL also. Now I am giving example with URL. Jsoup can validate the URL with the "Isvalidate ()" method. "Isvalidate ()" Method return type is Boolean. If return type is true, that means URL has an XSS script so we need to clean the URL with the "Clean ()" method. "Clean ()" method would return clean URL as String.

    • Jsoup can handle all cheat sheet scenarios. URL of cheat sheet is: "Https://"

    • Jsoup has the clear API. "Http://"

Antisamy also one of the tool to prevent XSS script, it customized one so we need to configure everything. Its not better than Jsoup

Steps to use Jsoup:

1. Download jsoup jar File

2. Write the code like the following

import;import org.jsoup.jsoup;import;public class jsoupsolution {public static void main ( String[] args)  throws unsupportedencodingexception {/* * sequrity issue  scenario */String input =  "get /selfservice/forgotpwd.jsp?sserror=2& Returnurl= Count=06f080\ "style%3d\" Behavior%3aurl (%23default%23time2) \ "onbegin%3d\" alert (' XSS ') \ "f6bc57307de& Contexttype=external&username=string&contextvalue=%2foam&password=sercure_string&challenge_url _id=6990594052748823869&oam_req=&locale=en_gb&resource_url=http%253a%252f% 2525252fselfservice%2525252flevel3.jsp%2525253freturnurl%252525253dhttp%25252525253a%25252525252f% http/ 1.1 "; string unsafe =  "'; Alert (String.fromCharCode (88,83,83)//'; Alert (String.fromCharCode (88,83,83)) \ "; Alert (String.fromCharCode (88,83,83))//\"; Alert (String.fromCharCode (88,83,83))//--></script>\ " > ' ><script>alert (String.fromCharCode (88,83,83)) </SCRIPT> "; string unsafe1 =  " ";// string input  =//  "\" Behavior%3aurl (%23default%23time2) \ "onbegin%3d\" alert (' XSS ') \ ";// string input  =  "<script>alert (\ "xss\") </script>\ ">";// string  input =  "<form background=\" Javascript:alert (' XSS ') \ ' > '; String input =  "<form bgcolor=\" #000000 \ ""; System.out.println ("input : "  + input); Boolean check = jsoup.isvalid ( Unsafe1, whitelist.relaxed ());if  (!check)  {system.out.println ("its invalid string"); String safe = jsoup.clean (Unsafe1, whitelist.basic ()); System.out.println ("cleaned code: "  + safe); System.out.println ("Its cleaned string by jsoup");boolean onemorecheck =  Jsoup.isvalid (Safe, whitelist.basic ());if  (Onemorecheck)  {system.out.println ("Its safe  code [after clean] ");}  else {system.out.println ("Its harmfull code");}}  else {system.out.println ("its valid string");}}}

Preventing XSS with Jsoup

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: 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.