GithubHelp home page GithubHelp logo

guoweb / contentsecuritypolicyfilter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ronaldploeger/contentsecuritypolicyfilter

0.0 0.0 0.0 260 KB

A configurable Java Servlet filter adding the "Content-Security-Policy" header to a ServletResponse

License: Apache License 2.0

Java 100.00%

contentsecuritypolicyfilter's Introduction

Content Security Policy Filter (Java)

Adds the 'Content-Security-Policy' or 'Content-Security-Policy-Report-Only' Header to the response.

Also see:

Normally you will only need a limited number or none of the init parameters. If no init parameter is defined the Header will look like this:

Content-Security-Policy = default-src 'none'

Here is an example full configuration of the ContentSecurityPolicyFilter.

    <filter>
       <filter-name>ContentSecurityPolicyFilter</filter-name>
       <filter-class>de.saville.csp.ContentSecurityPolicyFilter</filter-class>
       
       <init-param>
           <!-- If not specified the default is false -->
           <param-name>report-only</param-name>
           <param-value>false</param-value>
        </init-param>
        <!-- Optionally add a reporter-uri -->            
       <init-param>
           <param-name>report-uri</param-name>
           <param-value>/ContentSecurityPolicyReporter</param-value>
        </init-param>
       <init-param>
           <param-name>sandbox</param-name>
           <param-value>true</param-value>
           <!-- true enables the sandbox behaviour - the default is false - one can also specify exceptions, e.g.
           <param-value>allow-forms allow-same-origin</param-value>
           -->
        </init-param>
       <!-- Remember that special keywords have to be put in single quotes, e.g. 'none', 'self' -->
       <init-param>
           <!-- If not specified the default is 'none' -->
           <param-name>default-src</param-name>
           <param-value>'none'</param-value>
        </init-param>
       <init-param>
           <param-name>img-src</param-name>
            <param-value>http://*.example.com</param-value>
        </init-param>
       <init-param>
           <param-name>script-src</param-name>
           <param-value>'self' js.example.com</param-value>
        </init-param>
       <init-param>
           <param-name>style-src</param-name>
           <param-value>'self'</param-value>
        </init-param>  
       <init-param>
           <param-name>connect-src</param-name>
           <param-value>'self'</param-value>
        </init-param> 
       <init-param>
           <param-name>font-src</param-name>
           <param-value>'self'</param-value>
        </init-param>   
       <init-param>
           <param-name>object-src</param-name>
           <param-value>'self'</param-value>
        </init-param>  
       <init-param>
           <param-name>media-src</param-name>
           <param-value>'self'</param-value>
        </init-param> 
       <init-param>
           <param-name>frame-src</param-name>
           <param-value>'self'</param-value>
        </init-param> 
    </filter>
    
    <filter-mapping> 
       <filter-name>ContentSecurityPolicyFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Optionally configure a Servlet to log the CSP violations:

     <servlet>
         <servlet-name>ContentSecurityPolicyReporter</servlet-name>
         <servlet-class>de.saville.csp.ContentSecurityPolicyReporter</servlet-class>
     </servlet>

     <servlet-mapping>
         <servlet-name>ContentSecurityPolicyReporter</servlet-name>
         <url-pattern>/ContentSecurityPolicyReporter</url-pattern>
     </servlet-mapping>          

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.