Back to Sails

Content security policy

docs/concepts/Security/ContentSecurityPolicy.md

12.12.20003.6 KB
Original Source

Content security policy

Content Security Policy (CSP) is a W3C specification for instructing the client browser as to which location and/or which type of resources are allowed to be loaded. This spec uses "directives" to define loading behaviors for target resource types. Directives can be specified using HTTP response headers or HTML <meta> tags.

Enabling CSP

Using lusca

lusca is open-source under the Apache license

First:

sh
# In your sails app
npm install lusca --save --save-exact

Then add csp in config/http.js:

js

  // ...

  csp: require('lusca').csp({
    policy: {
      'default-src': '*'
    }
  }),

  // ...

  order: [
    // ...
    'csp',
    // ...
  ]

Supported directives

To give you an idea how this works, here's a snapshot of supported CSP directives, as of 2017:

Directive
default-srcLoading policy for all resources type in case a resource type dedicated directive is not defined (fallback)
script-srcDefines which scripts the protected resource can execute
object-srcDefines from where the protected resource can load plugins
style-srcDefines which styles (CSS) the user applies to the protected resource
img-srcDefines from where the protected resource can load images
media-srcDefines from where the protected resource can load video and audio
frame-srcDefines from where the protected resource can embed frames
font-srcDefines from where the protected resource can load fonts
connect-srcDefines which URIs the protected resource can load using script interfaces
form-actionDefines which URIs can be used as the action of HTML form elements
sandboxSpecifies an HTML sandbox policy that the user agent applies to the protected resource
script-nonceDefines script execution by requiring the presence of the specified nonce on script elements
plugin-typesDefines the set of plugins that can be invoked by the protected resource by limiting the types of resources that can be embedded
reflected-xssInstructs a user agent to activate or deactivate any heuristics used to filter or block reflected cross-site scripting attacks, equivalent to the effects of the non-standard X-XSS-Protection header
report-uriSpecifies a URI to which the user agent sends reports about policy violation

For more information, see the W3C CSP Spec.

Browser compatibility

Different CSP response headers are supported by different browsers. For example, Content-Security-Policy is the W3C standard, but various versions of Chrome, Firefox, and IE use X-Content-Security-Policy or X-WebKit-CSP. For the latest information on browser support, see OWasp.

Additional Resources

<docmeta name="displayName" value="Content security policy"> <docmeta name="tags" value="csp,content security policy">