Content Security Policy

CSP stands for Content Security Policy.

Is a W3C specification offering the possibility to instruct the client browser from which location and/or which type of resources are allowed to be loaded. To define a loading behavior, the CSP specification use “directive” where a directive defines a loading behavior for a target resource type.

This article is based on version 1.1 of the W3C specification.

Directives can be specified using HTTP response header (a server may send more than one CSP HTTP header field with a given resource representation and a server may send different CSP header field values with different representations of the same resource or with different resources) or HTML Meta tag, the HTTP headers below are defined by the specs:

The supported directives are:

An introduction to CSP is available on HTML5Rocks. The browser support is shown on http://caniuse.com/#feat=contentsecuritypolicy

Risk

The risk with CSP can have 2 main sources:

  1. Policies misconfiguration,
  2. Too permissive policies.

Countermeasure

This article will focus on providing an sample implementation of a JEE Web Filter in order to apply a set of CSP policies on all HTTP response returned by server.

The policies will instruct the browser to have the loading behavior below using all HTTP headers defined in W3C Specs: