Git Product home page Git Product logo

plug_content_security_policy's Introduction

plug_content_security_policy

Hex

This is a Plug module for inserting a Content Security Policy header into the response. It supports generating nonces for inline <script> and <style> tags as specified in CSP Level 2.

Installation

Add plug_content_security_policy to the list of dependencies in your mix.exs:

def deps do
  [
    {:plug_content_security_policy, "~> 0.2.1"}
  ]
end

Usage

Add the PlugContentSecurityPolicy module to your pipeline:

defmodule YourApp.Endpoint do
  # Use application config
  plug PlugContentSecurityPolicy

  # Pass configuration explicitly
  plug PlugContentSecurityPolicy,
    nonces_for: [:style_src],
    directives: %{script_src: ~w(https: 'self')}
end

If nonces are requested for any directives, they will be available in the assigns map of the conn as <directive>_nonce โ€” e.g., conn.assigns[:style_src_nonce] โ€” and the nonce will be inserted into the CSP header.

Report-Only Mode

In order to use the report-only header, set report_only: true in your config and provide a report_uri:

config :plug_content_security_policy,
  report_only: true,
  directives: %{
    report_uri: "/csp-violation-report-endpoint/"
  }

Configuration

You can configure the CSP directives using Mix. The default configuration is shown below:

config :plug_content_security_policy,
  nonces_for: [],
  report_only: false,
  directives: %{
    default_src: ~w('none'),
    connect_src: ~w('self'),
    child_src: ~w('self'),
    img_src: ~w('self'),
    script_src: ~w('self'),
    style_src: ~w('self')
  }

Values should be passed to each directive as a list of strings. Please see the CSP spec for a full list of directives and valid attributes.

To request that a nonce be generated for a directive, pass its key to nonces_for:

config :plug_content_security_policy,
  nonces_for: [:script_src]

Development

Setup

bin/setup

Running the tests

bin/test

License

ISC

plug_content_security_policy's People

Contributors

halostatue avatar xtian avatar

Watchers

 avatar

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.