Code Monkey home page Code Monkey logo

xssrequestfilters's Introduction

XssRequestFilter MIT license

Filter the Cross-site scripting

Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications. XSS enables attackers to inject client-side scripts into web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same-origin policy..read more...

XssRequestFilter is a spring based lib to filter the cross-site scripting in your @Controller /@RestController request (current version have xss filter support for form data and json request) just using a simple @XssFilter annotation . Also, it's easy to customized with own custom rules for filter xss request.

Use @XssFilter annotation on your controller methods where you wish to filter Cross-site scripting. It will remove all xss from request parameter. Also use @ComponentScan("com.xss.filter") in your one of configuration class for active the autoconfiguration for XssRequestFilter.

example:

@Configuration
@ComponentScan("com.xss.filter")
public class Config {
}


@RestController
public class Api {

  /**
   * Test with Content-Type: application/x-www-form-urlencoded
   * Sample Test Curl Request:
   * curl --location --request POST 'http://localhost:8080/test-with-model-attribute' \
   * --header 'Content-Type: application/x-www-form-urlencoded' \
   * --data-urlencode 'name=data <div id=\"demo\">Inject Html</div>'
   *
   *  Response :
   *  {
   *     "name": "data "
   * }
   *
   * you can see in response sample <div id=\"demo\">Inject Html</div> removed by XssFilter
   * */
  @XssFilter
  @RequestMapping(value = "/test-with-model-attribute", method = RequestMethod.POST)
  public Person save(@ModelAttribute Person person) {
    return new Person(person.getName());
  }

  /**
   * Test with Content-Type: application/json
   *
   * Curl request :
   * curl --location --request POST 'http://localhost:8080/test-with-request-body' \
   * --header 'Content-Type: application/json' \
   * --data-raw '{
   *     "name" : "Bhushan <div id=\"demo\">Inject Html</div>"
   * }'
   *
   * Response of API :
   * {
   *     "name": "Bhushan "
   * }
   *
   * Here in response result :  <div id=\"demo\">Inject Html</div> removed by XssFilter
   * */
  @XssFilter
  @PostMapping("/test-with-request-body")
  public Person personNameProcess(@RequestBody Person request,
      HttpServletRequest httpServletRequest) {
    return new Person(request.getName());
  }


  public static class Person {

    private String name;

    public Person() {
    }

    public Person(String name) {
      this.name = name;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }
  }

}


Note : @XssFilter bound with Url Patterns so it can process matched url with different type of request method.

Following regex pattern (most of them are case-insensitive Pattern) are used for filter xss value :

  • (<input(.*?)></input>|<input(.*)/>)

  • <span(.*?)</span>

  • <div(.*)</div>

  • <style>(.*?)</style>

  • <script>(.*?)</script>

  • javascript:

  • </script>

  • <script(.*?)>

  • src[\r\n]*=[\r\n]*\\\'(.*?)\\\'

  • eval\((.*?)\)

  • expression\((.*?)\)

  • vbscript:

  • onload(.*?)=

XSS Filter Evasion Cheat Sheet

This library employs a default blacklist approach to identify known malicious inputs and block them. While this method forms a part of the security strategy, it is not comprehensive on its own. Attackers frequently discover ways to evade blacklist filters, employing diverse encoding techniques or devising new attack vectors not covered by the blacklist. For every identified case, there exist countless scenarios, making it crucial to have a more adaptable defense.

To address this challenge, the library provides an interface called XssAttackService. Developers utilizing this library can create their custom implementations of this interface, enabling them to employ a whitelist approach. This approach offers a more robust defense against XSS attacks. Developers can tailor their implementation according to the specific requirements of their APIs or business data, ensuring a personalized and effective security barrier. The flexibility of the whitelist approach empowers developers to safeguard their applications effectively, mitigating the risks posed by evolving XSS attack techniques.

Create your own custom logic for filter xss request:

If the predefined blacklist patterns don't meet your security needs, you have the flexibility to establish your own custom whitelist approach or even combine whitelisted and blacklisted filters for XSS patterns. To facilitate this customization, the XssRequestFilter offers robust support. It allows you to implement tailored logic, empowering you to filter XSS requests according to your specific requirements. This flexibility ensures that you can create a security framework that aligns precisely with your application's unique demands, providing a higher level of protection against XSS attacks.

By default this framework use DefaultRansackXssImpl service for filter the xss request, this service implemented XssAttackService interface.

So for your custom logic for filter xss request you just need following steps:

  1. Create a class which will XssAttackService the RansackXss interface.
  2. Implement the String ransack(String value); method. (value can be ServletRequest headers/parameters/json payload form where client can inject the xss, you need to perform the filter on this value, you can take a reference of DefaultRansackXssServiceImpl class)
  3. Create a bean of this class

done... Now instead of DefaultRansackXssServiceImpl, XssAttackService will use your class implementation rules for filter xss

Supported Request content type : (version >= 1.1.0)

  • application/x-www-form-urlencoded
  • application/json

Xss Version Support with Spring-boot

Springboot vesrion xss.filter version support
SpringBoot 2.*.* 1.1.0
SpringBoot 3.*.* 2.0.0

Download it from here

  • Apache Maven

      <dependency>
       <groupId>com.github.techguy-bhushan</groupId>
       <artifactId>xss.filter</artifactId>
       <version>2.0.0</version>
      </dependency> 
  • Gradle/Grails

compile 'com.github.techguy-bhushan:xss.filter:2.0.0'

Here are some useful classes used in XssRequestFilter

XssFiltersConfiguration :

This Component will search all the url's which action are annotated with @XssFilter (collect the list of urls, which will be pick by CustomXssFilter )

CustomXssFilter:

This filter will only work for request which action have annotated @XssFilter (with help of XssFiltersConfiguration)

CaptureRequestWrapper :

This class is responsible for filter the XSS in request you can add or remove the XSS handling logic in #stripXSS method in CaptureRequestWrapper, CustomXssFilter use this class for remove xss in request.

FilterConfig :

This component will register CustomXssFilter if there will any @XssFilter annotation used in url mapping, if there will no @XssFilter used in application then CustomXssFilter will disable.

ServletInputStreamXssFilterProvider

This component has list of ServletInputStreamXssFilterProvider which are responsible for process ServletInputStream based on supported content types, you can customize this component based on your own requirement.

Please create a new issue if you found any issue, also you can create a pull request from improvement.

Thank you!

xssrequestfilters's People

Contributors

techguy-bhushan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

xssrequestfilters's Issues

Found an exploit in this library

Hey, I have just found an exploit on your library, where should i send you a detailed report? Can you open a security advisory here on github?

Not able to start application


APPLICATION FAILED TO START


Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

com.xss.filters.config.XssFiltersConfiguration.xssMatches(XssFiltersConfiguration.java:44)

The following method did not exist:

org.springframework.web.servlet.mvc.method.RequestMappingInfo.getPathPatternsCondition()Lorg/springframework/web/servlet/mvc/condition/PathPatternsRequestCondition;

The method's class, org.springframework.web.servlet.mvc.method.RequestMappingInfo, is available from the following locations:

jar:file:/C:/Users/u.rao/.m2/repository/org/springframework/spring-webmvc/5.2.7.RELEASE/spring-webmvc-5.2.7.RELEASE.jar!/org/springframework/web/servlet/mvc/method/RequestMappingInfo.class

The class hierarchy was loaded from the following locations:

org.springframework.web.servlet.mvc.method.RequestMappingInfo: file:/C:/Users/u.rao/.m2/repository/org/springframework/spring-webmvc/5.2.7.RELEASE/spring-webmvc-5.2.7.RELEASE.jar

Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.servlet.mvc.method.RequestMappingInfo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.