• Stars
    star
    157
  • Rank 238,399 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ember-cli-content-security-policy

This addon makes it easy to use Content Security Policy (CSP) in your project. The policy can be delivered either via a Content-Security-Policy HTTP response header or as a meta tag in the index.html file.

If configured to deliver the CSP using a HTTP response header, the header is set automatically if served with Ember CLI's express server in development or via FastBoot in production. If FastBoot is not used to serve the app in production, the web server must be configured to set the CSP header. The configured CSP could be exported with a provided Ember CLI command.

If configured to deliver the CSP using the meta tag no additional configuration of the web server serving the application in production is needed.

In any case, using this addon helps keeping CSP in the forefront of your thoughts while developing an Ember application.

Compatibility

  • Ember.js v2.18 or above
  • Ember CLI v3.4 or above
  • Node.js v12 or above

Installation

ember install ember-cli-content-security-policy

Configuration

This addon is configured via config/content-security-policy.js file.

type directiveName =
  // Fetch Directives
  | 'child-src'
  | 'connect-src'
  | 'default-src'
  | 'font-src'
  | 'frame-src'
  | 'image-src'
  | 'manifest-src'
  | 'media-src'
  | 'object-src'
  | 'prefetch-src'
  | 'script-src'
  | 'script-src-elem'
  | 'script-src-attr'
  | 'style-src'
  | 'style-src-elem'
  | 'style-src-attr'
  | 'worker-src'
  // Document Directives
  | 'base-uri'
  | 'plugin-types'
  | 'sandbox'
  // Navigation Directives
  | 'form-action'
  | 'frame-ancestors'
  | 'navigate-to'
  // Reporting Directives
  | 'report-uri'
  | 'report-to'
  // Directives Defined in Other Documents
  | 'block-all-mixed-content'
  | 'upgrade-insecure-requests'
  | 'require-sri-for';

interface EmberCLIContentSecurityPolicyConfig {
  // CSP is delivered via HTTP Header if delivery includes `"header"` and via
  // meta element if it includes `"meta"`.
  delivery?: string;

  // Controls if addon is enabled at all.
  enabled?: boolean;

  // Controls if addon causes tests to fail if they violate configured CSP
  // policy.
  failTests: true;

  // A hash of options representing a Content Security Policy. The key must be
  // a CSP directive name as defined by spec. The value must be an array of
  // strings that form a CSP directive value, most likely a source list, e.g.
  // {
  //   'default-src': ["'none'"],
  //   'style-src': ["'self'", 'examples.com']
  // }
  // Please refer to CSP specification for details on valid CSP directives:
  // https://w3c.github.io/webappsec-csp/#framework-directives
  policy?: {[key: directiveName]: string[]};

  // Controls if CSP is used in report only mode. For delivery mode `"header"`
  // this causes `Content-Security-Policy-Report-Only` HTTP header to be used.
  // Can not be used together with delivery mode `"meta"` as this is not
  // supported by CSP spec.
  reportOnly?: boolean;
}

If you omit some or all of the keys, the default configuration will be used, which is:

// config/content-security-policy.js

module.exports = function (environment) {
  return {
    delivery: ['header'],
    enabled: true,
    failTests: true,
    policy: {
      'default-src': ["'none'"],
      'script-src': ["'self'"],
      'font-src': ["'self'"],
      'connect-src': ["'self'"],
      'img-src': ["'self'"],
      'style-src': ["'self'"],
      'media-src': ["'self'"],
    },
    reportOnly: true,
  };
};

Keywords such as self, none, unsafe-inline, nonces and digests must be wrapped in single quotes (') as shown above. Please find more details about valid source expression in Β§ 2.3.1. Source Lists of CSP specification.

Changes to the configuration require a restart of a running Ember development server instance.

Example

If your site uses Google Fonts, Mixpanel, a custom API at custom-api.local and you want to deliver the CSP using a meta element:

// config/content-security-policy.js

module.exports = function (environment) {
  return {
    delivery: ['meta'],
    policy: {
      // Deny everything by default
      'default-src': ["'none'"],
      // Allow scripts at https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js
      'script-src': ["'self'", 'https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js'],
      // Allow fonts to be loaded from http://fonts.gstatic.com
      'font-src': ["'self'", 'http://fonts.gstatic.com'],
      // Allow data (xhr/websocket) from api-js.mixpanel.com and custom-api.local
      'connect-src': ["'self'", 'https://api-js.mixpanel.com', 'https://custom-api.local'],
      // Allow images from the origin itself (i.e. current domain)
      'img-src': ["'self'"],
      // Allow CSS loaded from https://fonts.googleapis.com
      'style-src': ["'self'", 'https://fonts.googleapis.com'],
      // Omit `media-src` from policy
      // Browser will fallback to default-src for media resources (which is 'none', see above)
      'media-src': null,
    },
    reportOnly: false,
  };
};

FastBoot Integration

This addon sets the CSP HTTP response header in FastBoot if it's enabled for the used environment and delivery contains "header". It does not override existing CSP headers.

If using reportOnly mode you must provide a valid reportUri directive pointing to an endpoint that accepts violation reports. As reportUri directive is deprecated you should additionally provide a reportTo directive, even so it's only supported by Google Chrome so far.

If you don't want the addon to inject the CSP header in FastBoot on production (e.g. cause CSP header should be set by a reverse proxy in front of FastBoot App Server), you should either remove "header" from delivery option or disable the addon entirely.

// config/content-security-policy.js

module.exports = function (environment) {
  return {
    enabled: environment !== 'production',
    delivery: ['header'],
  };
};

External Configuration

In order to configure your production web server, you can use the csp-headers Ember CLI command to obtain the configured Content Security Policy:

$ ember csp-headers --environment production --report-uri /csp-report

# Content Security Policy Header Configuration
#
# for Apache: Header set Content-Security-Policy-Report-Only "..."
# for Nginx : add_header Content-Security-Policy-Report-Only "...";

default-src 'none'; script-src 'self'; connect-src 'self'; img-src 'self'; style-src 'self'; report-uri /csp-report;

Development Support

Ember CLI's live reload feature requires a Web Socket connection. If live reload is used with ember serve or ember test --server the URL used for that Web Socket connection is injected into connect-src and script-src directives automatically.

Test Support

The addon helps you to ensure that your app or addon is compliant with a specific Content Security Policy by providing test support. It causes tests to fail if the code triggers a violation of the configured CSP.

It's recommended to test your project for CSP compliance. But you could disable it nevertheless by setting enabled option to false for test environment:

// config/content-security-policy.js

module.exports = function (environment) {
  return {
    enabled: environment !== 'test',
  };
};

Compatibility with other addons

Some addons are not compatible with a strict Content Security Policy. If you face any CSP violations caused by a third-party addon please report at their side. Often it's only a small change to required to make it compliant with a strict CSP. You may want to suggest adding this addon to test for compliance with a strict CSP.

For some addons compliance with a strict CSP requires a custom configuration. This documentation lists required configuration for some famous once.

Ember Auto Import

Ember Auto Import uses the eval function by default in development builds. This violates the default CSP policy. It's recommended to set Ember Auto Import's forbidEval option to true if using Content Security Policy. You should not add 'unsafe-eval' to script-src directive as this disables main security provided by CSP.

Embroider

Webpack, which is used by Embroider, uses the eval function by default in development builds to generate a source map. This violates the default CSP policy. It's recommended to configure Webpack to use 'source-map' strategy to generate source maps. To do so, add the following Embroider configuration:

return require('@embroider/compat').compatBuild(app, Webpack, {
  packagerOptions: {
    // other configuration
    webpackConfig: {
      devtool: 'source-map',
    },
  },
});

For addons using maybeEmbroider utility provided by @embroider/test-setup the configuration looks like this:

const { maybeEmbroider } = require('@embroider/test-setup');
return maybeEmbroider(app, {
  // other configuration
  packagerOptions: {
    webpackConfig: {
      devtool: 'source-map',
    },
  },
});

ember-cli-code-coverage

Ember-cli-code-coverage uses Istanbul, which injects new Function('return this') by default into the app. This requires 'unsafe-eval' to be allowed by the script directive. Currently there isn't any other option than either adding 'unsafe-eval' to script directive if code coverage is enabled or disable CSP at all. Details could be found in this issue.

Deprecations

Please find detailed information about deprecations in deprecation guide.

More Repositories

1

pivot.js

Build Pivot Tables from CSV/JSON Data
JavaScript
782
star
2

git-repo-info

JavaScript
111
star
3

codemod-cli

JavaScript
89
star
4

singleton_process

Ensure that a given process is only running once. Helpful for ensure that scheduled tasks do not overlap if they run longer than the scheduled interval.
Ruby
68
star
5

ember-cli-pretender

JavaScript
66
star
6

ember-cli-cjs-transform

JavaScript
48
star
7

ember-cli-divshot

JavaScript
45
star
8

sparkles-component

JavaScript
43
star
9

ember-router-helpers

JavaScript
38
star
10

ember-waypoints

Ember CLI Addon for using jQuery-Waypoints
JavaScript
35
star
11

ember-cli-esnext

JavaScript
35
star
12

release-it-yarn-workspaces

JavaScript
32
star
13

create-rwjblue-release-it-setup

JavaScript
30
star
14

ember-native-dom-event-dispatcher

JavaScript
25
star
15

broccoli-timepiece

JavaScript
21
star
16

ember-cli-ic-ajax

JavaScript
21
star
17

ember-holy-futuristic-template-namespacing-batman

JavaScript
21
star
18

X12

X12 Parser - a library to manipulate X12 structures using native Ruby syntax. This is only a github clone of the X12 rubyforge project located at: http://rubyforge.org/projects/x12parser/
Ruby
20
star
19

release-it-lerna-changelog

JavaScript
18
star
20

ember-test-friendly-error-handler

Build testable error handlers that don't throw in production...
JavaScript
15
star
21

ember-git-version

JavaScript
14
star
22

ember-new-computed

JavaScript
14
star
23

validate-peer-dependencies

Validate the peerDependencies of a given package.json have been satisfied.
JavaScript
14
star
24

get-function-location

JavaScript
12
star
25

ember-data-nested-urls

Ruby
12
star
26

ember-live-api

Ember API Docs via Ember App Kit
JavaScript
12
star
27

ember-template-invocation-location

JavaScript
11
star
28

ember-functional-component

Use "pure functions" as components in an Ember app.
JavaScript
11
star
29

container-resolver-namespaces

JavaScript
11
star
30

ember-cli-selenium

JavaScript
11
star
31

no-jquery-app

JavaScript
9
star
32

broccoli-file-creator

JavaScript
8
star
33

ember-template-module-name-remover

JavaScript
7
star
34

--ghost-modules-sample

Test repo to play with new module layout changes...
JavaScript
7
star
35

broccoli-file-remover

JavaScript
7
star
36

__glimmer-demo-using-route-recognizer

TypeScript
7
star
37

dotvim

Lua
6
star
38

pretender-query-param-handler

JavaScript
6
star
39

broccoli-jshint

JavaScript
6
star
40

ember-cli-delay-app-boot

JavaScript
6
star
41

work_days

Calculate the number of work days in a given period.
Ruby
6
star
42

broccoli-string-replace

JavaScript
5
star
43

dotfiles

Shell
4
star
44

rfc232-demo-app

JavaScript
4
star
45

experimental-glimmer-app

TypeScript
4
star
46

ember-title

JavaScript
4
star
47

broccoli-fixturify

JavaScript
3
star
48

broccoli-slow-trees

JavaScript
3
star
49

broccoli-unwatched-tree

JavaScript
3
star
50

broccoli-watched-tree

JavaScript
3
star
51

broccoli-ember-inline-template-compiler

JavaScript
3
star
52

--ember-qunit-5-sample-app

JavaScript
3
star
53

broccoli-export-tree

JavaScript
3
star
54

broccoli-file-mover

JavaScript
3
star
55

cal-rs

`cal` (but better!) in rust
Rust
3
star
56

test-glimmer-playground

JavaScript
2
star
57

lsp-playground

Playground for LSP shenanigans
Rust
2
star
58

ember-cli-symlinking-tests

JavaScript
2
star
59

terminal_server_utilities

Utility functions to make dealing with Microsoft Terminal Servers easier to manage.
Ruby
2
star
60

___demo-babel-config

JavaScript
2
star
61

test-github-device-flow-auth

Rust
2
star
62

broccoli-derequire

JavaScript
2
star
63

broccoli-use-strict-remover

JavaScript
2
star
64

prawn_examples

Example scripts for using the prawn gem.
Ruby
1
star
65

simple-dll

JavaScript
1
star
66

-tracking-rendered-modifier-testbed

JavaScript
1
star
67

octane-app-20190219

JavaScript
1
star
68

--travis-modules-sample

JavaScript
1
star
69

mail_examples

Example scripts using the Mail gem.
Ruby
1
star
70

asset-rev-issue

CSS
1
star
71

pingly

Ping a specified host, logs the output, and calls a block on packet loss.
Ruby
1
star
72

repro-ember-cli-htmlbars-template-modules

JavaScript
1
star
73

_____ember-cli-test

JavaScript
1
star
74

intro-to-ember-suncoast.js-2014-02-19

JavaScript
1
star
75

-demo-disabling-rerendering-in-ssr-mode

JavaScript
1
star
76

___test-embroider-in-addon-development

JavaScript
1
star
77

-demo-of-fastboot-without-fastboot

JavaScript
1
star
78

ember-cli-build-visualizer

JavaScript
1
star
79

ember-holy-futuristic-template-namespacing-batman-codemod

JavaScript
1
star
80

debugging-broccoli

JavaScript
1
star
81

ey-cloud-recipes

A starter repo for custom chef recipes on EY's cloud platform
Ruby
1
star
82

adafter

JavaScript
1
star
83

--new-app-blueprint

JavaScript
1
star
84

testing-ega

JavaScript
1
star
85

htmlbars-test

JavaScript
1
star
86

openai_playground

Rust
1
star
87

log-lint-times

JavaScript
1
star
88

homebrew-tap

Homebrew formulas for my personal projects
Ruby
1
star
89

vimfiles

vimfiles
Vim Script
1
star
90

__demo-including-ts-library

TypeScript
1
star