better_content_security_policy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +23 -0
- data/.vscode/settings.json +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +185 -0
- data/LICENSE.txt +21 -0
- data/README.md +126 -0
- data/Rakefile +12 -0
- data/better_content_security_policy.gemspec +41 -0
- data/lib/better_content_security_policy/content_security_policy.rb +118 -0
- data/lib/better_content_security_policy/has_content_security_policy.rb +30 -0
- data/lib/better_content_security_policy/railtie.rb +10 -0
- data/lib/better_content_security_policy/version.rb +5 -0
- data/lib/better_content_security_policy.rb +5 -0
- data/sig/better_content_security_policy.rbs +4 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 50e701c2051110a406b0ba73d7773bfd94c74889939e5302be70da1e44f29ed3
|
4
|
+
data.tar.gz: dd67545b3d05169cbbc3fb1dfacd3cf501adf481f12f3706a54c9cdb70106db9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a6dea4753c4dcc13dd6fa39a19a1b2108cce36641d4379a9471a90ebc92380966f914077322b9c1726974b8e5e73f7e0dde70b726f50ff11d57d49595c1ae55a
|
7
|
+
data.tar.gz: f434678f26584c02569911e2250b739625cf02fd391654cd0f2e2ee1b73d6fd0f6a81df944f4e9f95bbc318821cab61b5b0bdb7e246e8b9b82c6525155fb9edc
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.5
|
5
|
+
NewCops: enable
|
6
|
+
|
7
|
+
Style/StringLiterals:
|
8
|
+
Enabled: true
|
9
|
+
EnforcedStyle: double_quotes
|
10
|
+
|
11
|
+
Style/StringLiteralsInInterpolation:
|
12
|
+
Enabled: true
|
13
|
+
EnforcedStyle: double_quotes
|
14
|
+
|
15
|
+
Layout/LineLength:
|
16
|
+
Max: 120
|
17
|
+
|
18
|
+
RSpec/ExampleLength:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Metrics/BlockLength:
|
22
|
+
Exclude:
|
23
|
+
- "spec/**/*"
|
@@ -0,0 +1,13 @@
|
|
1
|
+
{
|
2
|
+
"files.trimTrailingWhitespace": true,
|
3
|
+
"editor.formatOnSave": true,
|
4
|
+
"ruby.rubocop.executePath": "/usr/local/bin/rubocop-daemon-wrapper/",
|
5
|
+
"ruby.lint": {
|
6
|
+
"rubocop": false
|
7
|
+
},
|
8
|
+
"[ruby]": {
|
9
|
+
"editor.defaultFormatter": "misogi.ruby-rubocop"
|
10
|
+
},
|
11
|
+
"ruby.rubocop.onSave": true,
|
12
|
+
"ruby.rubocop.autocorrectArg": "--auto-correct-all"
|
13
|
+
}
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at git@ndbroadbent.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in better_content_security_policy.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
gem "rspec", "~> 3.0"
|
10
|
+
gem "rubocop", "~> 1.21"
|
11
|
+
gem "rubocop-daemon"
|
12
|
+
gem "rubocop-rspec"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
better_content_security_policy (0.1.0)
|
5
|
+
rails (>= 5.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actioncable (6.1.7)
|
11
|
+
actionpack (= 6.1.7)
|
12
|
+
activesupport (= 6.1.7)
|
13
|
+
nio4r (~> 2.0)
|
14
|
+
websocket-driver (>= 0.6.1)
|
15
|
+
actionmailbox (6.1.7)
|
16
|
+
actionpack (= 6.1.7)
|
17
|
+
activejob (= 6.1.7)
|
18
|
+
activerecord (= 6.1.7)
|
19
|
+
activestorage (= 6.1.7)
|
20
|
+
activesupport (= 6.1.7)
|
21
|
+
mail (>= 2.7.1)
|
22
|
+
actionmailer (6.1.7)
|
23
|
+
actionpack (= 6.1.7)
|
24
|
+
actionview (= 6.1.7)
|
25
|
+
activejob (= 6.1.7)
|
26
|
+
activesupport (= 6.1.7)
|
27
|
+
mail (~> 2.5, >= 2.5.4)
|
28
|
+
rails-dom-testing (~> 2.0)
|
29
|
+
actionpack (6.1.7)
|
30
|
+
actionview (= 6.1.7)
|
31
|
+
activesupport (= 6.1.7)
|
32
|
+
rack (~> 2.0, >= 2.0.9)
|
33
|
+
rack-test (>= 0.6.3)
|
34
|
+
rails-dom-testing (~> 2.0)
|
35
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
36
|
+
actiontext (6.1.7)
|
37
|
+
actionpack (= 6.1.7)
|
38
|
+
activerecord (= 6.1.7)
|
39
|
+
activestorage (= 6.1.7)
|
40
|
+
activesupport (= 6.1.7)
|
41
|
+
nokogiri (>= 1.8.5)
|
42
|
+
actionview (6.1.7)
|
43
|
+
activesupport (= 6.1.7)
|
44
|
+
builder (~> 3.1)
|
45
|
+
erubi (~> 1.4)
|
46
|
+
rails-dom-testing (~> 2.0)
|
47
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
48
|
+
activejob (6.1.7)
|
49
|
+
activesupport (= 6.1.7)
|
50
|
+
globalid (>= 0.3.6)
|
51
|
+
activemodel (6.1.7)
|
52
|
+
activesupport (= 6.1.7)
|
53
|
+
activerecord (6.1.7)
|
54
|
+
activemodel (= 6.1.7)
|
55
|
+
activesupport (= 6.1.7)
|
56
|
+
activestorage (6.1.7)
|
57
|
+
actionpack (= 6.1.7)
|
58
|
+
activejob (= 6.1.7)
|
59
|
+
activerecord (= 6.1.7)
|
60
|
+
activesupport (= 6.1.7)
|
61
|
+
marcel (~> 1.0)
|
62
|
+
mini_mime (>= 1.1.0)
|
63
|
+
activesupport (6.1.7)
|
64
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
65
|
+
i18n (>= 1.6, < 2)
|
66
|
+
minitest (>= 5.1)
|
67
|
+
tzinfo (~> 2.0)
|
68
|
+
zeitwerk (~> 2.3)
|
69
|
+
ast (2.4.2)
|
70
|
+
builder (3.2.4)
|
71
|
+
concurrent-ruby (1.1.10)
|
72
|
+
crass (1.0.6)
|
73
|
+
diff-lcs (1.5.0)
|
74
|
+
erubi (1.11.0)
|
75
|
+
globalid (1.0.0)
|
76
|
+
activesupport (>= 5.0)
|
77
|
+
i18n (1.12.0)
|
78
|
+
concurrent-ruby (~> 1.0)
|
79
|
+
loofah (2.19.0)
|
80
|
+
crass (~> 1.0.2)
|
81
|
+
nokogiri (>= 1.5.9)
|
82
|
+
mail (2.7.1)
|
83
|
+
mini_mime (>= 0.1.1)
|
84
|
+
marcel (1.0.2)
|
85
|
+
method_source (1.0.0)
|
86
|
+
mini_mime (1.1.2)
|
87
|
+
minitest (5.15.0)
|
88
|
+
nio4r (2.5.8)
|
89
|
+
nokogiri (1.12.5-x86_64-darwin)
|
90
|
+
racc (~> 1.4)
|
91
|
+
parallel (1.22.1)
|
92
|
+
parser (3.1.2.1)
|
93
|
+
ast (~> 2.4.1)
|
94
|
+
racc (1.6.0)
|
95
|
+
rack (2.2.4)
|
96
|
+
rack-test (2.0.2)
|
97
|
+
rack (>= 1.3)
|
98
|
+
rails (6.1.7)
|
99
|
+
actioncable (= 6.1.7)
|
100
|
+
actionmailbox (= 6.1.7)
|
101
|
+
actionmailer (= 6.1.7)
|
102
|
+
actionpack (= 6.1.7)
|
103
|
+
actiontext (= 6.1.7)
|
104
|
+
actionview (= 6.1.7)
|
105
|
+
activejob (= 6.1.7)
|
106
|
+
activemodel (= 6.1.7)
|
107
|
+
activerecord (= 6.1.7)
|
108
|
+
activestorage (= 6.1.7)
|
109
|
+
activesupport (= 6.1.7)
|
110
|
+
bundler (>= 1.15.0)
|
111
|
+
railties (= 6.1.7)
|
112
|
+
sprockets-rails (>= 2.0.0)
|
113
|
+
rails-dom-testing (2.0.3)
|
114
|
+
activesupport (>= 4.2.0)
|
115
|
+
nokogiri (>= 1.6)
|
116
|
+
rails-html-sanitizer (1.4.3)
|
117
|
+
loofah (~> 2.3)
|
118
|
+
railties (6.1.7)
|
119
|
+
actionpack (= 6.1.7)
|
120
|
+
activesupport (= 6.1.7)
|
121
|
+
method_source
|
122
|
+
rake (>= 12.2)
|
123
|
+
thor (~> 1.0)
|
124
|
+
rainbow (3.1.1)
|
125
|
+
rake (13.0.6)
|
126
|
+
regexp_parser (2.6.0)
|
127
|
+
rexml (3.2.5)
|
128
|
+
rspec (3.11.0)
|
129
|
+
rspec-core (~> 3.11.0)
|
130
|
+
rspec-expectations (~> 3.11.0)
|
131
|
+
rspec-mocks (~> 3.11.0)
|
132
|
+
rspec-core (3.11.0)
|
133
|
+
rspec-support (~> 3.11.0)
|
134
|
+
rspec-expectations (3.11.1)
|
135
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
136
|
+
rspec-support (~> 3.11.0)
|
137
|
+
rspec-mocks (3.11.1)
|
138
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
139
|
+
rspec-support (~> 3.11.0)
|
140
|
+
rspec-support (3.11.1)
|
141
|
+
rubocop (1.28.2)
|
142
|
+
parallel (~> 1.10)
|
143
|
+
parser (>= 3.1.0.0)
|
144
|
+
rainbow (>= 2.2.2, < 4.0)
|
145
|
+
regexp_parser (>= 1.8, < 3.0)
|
146
|
+
rexml
|
147
|
+
rubocop-ast (>= 1.17.0, < 2.0)
|
148
|
+
ruby-progressbar (~> 1.7)
|
149
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
150
|
+
rubocop-ast (1.17.0)
|
151
|
+
parser (>= 3.1.1.0)
|
152
|
+
rubocop-daemon (0.3.2)
|
153
|
+
rubocop
|
154
|
+
rubocop-rspec (2.10.0)
|
155
|
+
rubocop (~> 1.19)
|
156
|
+
ruby-progressbar (1.11.0)
|
157
|
+
sprockets (4.1.1)
|
158
|
+
concurrent-ruby (~> 1.0)
|
159
|
+
rack (> 1, < 3)
|
160
|
+
sprockets-rails (3.4.2)
|
161
|
+
actionpack (>= 5.2)
|
162
|
+
activesupport (>= 5.2)
|
163
|
+
sprockets (>= 3.0.0)
|
164
|
+
thor (1.2.1)
|
165
|
+
tzinfo (2.0.5)
|
166
|
+
concurrent-ruby (~> 1.0)
|
167
|
+
unicode-display_width (2.3.0)
|
168
|
+
websocket-driver (0.7.5)
|
169
|
+
websocket-extensions (>= 0.1.0)
|
170
|
+
websocket-extensions (0.1.5)
|
171
|
+
zeitwerk (2.6.1)
|
172
|
+
|
173
|
+
PLATFORMS
|
174
|
+
x86_64-darwin-21
|
175
|
+
|
176
|
+
DEPENDENCIES
|
177
|
+
better_content_security_policy!
|
178
|
+
rake (~> 13.0)
|
179
|
+
rspec (~> 3.0)
|
180
|
+
rubocop (~> 1.21)
|
181
|
+
rubocop-daemon
|
182
|
+
rubocop-rspec
|
183
|
+
|
184
|
+
BUNDLED WITH
|
185
|
+
2.3.15
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Nathan Broadbent
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# Better Content Security Policy
|
2
|
+
|
3
|
+
This gem makes it easy to configure a dynamic `Content-Security-Policy` header for your Rails application.
|
4
|
+
You can easily customize the rules in your controllers, and you can also update the rules in your views.
|
5
|
+
|
6
|
+
Read the MDN Web Docs to learn more about Content Security Policies: https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
|
7
|
+
|
8
|
+
## Features
|
9
|
+
|
10
|
+
- Configure unique `Content-Security-Policy` rules for different controllers and actions.
|
11
|
+
- Configure `Content-Security-Policy` rules alongside `script` tags in your views, so that rendering a view partial will automatically add all of the required CSP rules for those resources.
|
12
|
+
- Still uses some features from Rails, such as `Rails.application.config.content_security_policy_nonce_generator` to generate nonce values.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Install the gem and add to the application's Gemfile by executing:
|
17
|
+
|
18
|
+
$ bundle add better_content_security_policy
|
19
|
+
|
20
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
21
|
+
|
22
|
+
$ gem install better_content_security_policy
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Include the `BetterContentSecurityPolicy::HasContentSecurityPolicy` concern in your `ApplicationController`:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class ApplicationController < ActionController::Base
|
30
|
+
include BetterContentSecurityPolicy::HasContentSecurityPolicy
|
31
|
+
```
|
32
|
+
|
33
|
+
Define a `#configure_content_security_policy` method in `ApplicationController` to configure the default `Content-Security-Policy` rules:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
def configure_content_security_policy
|
37
|
+
content_security_policy.default_src :none
|
38
|
+
content_security_policy.font_src :self
|
39
|
+
content_security_policy.script_src :self
|
40
|
+
content_security_policy.style_src :self
|
41
|
+
content_security_policy.img_src :self
|
42
|
+
content_security_policy.connect_src :self
|
43
|
+
content_security_policy.prefetch_src :self
|
44
|
+
|
45
|
+
content_security_policy.report_uri = "http://example.com/csp_reports"
|
46
|
+
content_security_policy.report_only = true
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
You can define more `#configure_content_security_policy` methods in any other controllers. Call `super` if you want to inherit your default configuration from ApplicationController. Otherwise, you can omit the call to `super` if you want to start from scratch with a new policy.
|
51
|
+
|
52
|
+
You are now able to access content_security_policy in your controllers and views. After you have finished rendering the response, an `after_action` callback will generate and add the `Content-Security-Policy` header.
|
53
|
+
|
54
|
+
## Examples
|
55
|
+
|
56
|
+
#### Plausible Analytics
|
57
|
+
|
58
|
+
Here's an example `HAML` partial that includes the JavaScript snippet for [Plausible Analytics](https://plausible.io/).
|
59
|
+
|
60
|
+
```haml
|
61
|
+
# app/views/layouts/_plausible_analytics.html.haml
|
62
|
+
|
63
|
+
- if PLAUSIBLE_ANALYTICS_HOST
|
64
|
+
- content_security_policy.connect_src PLAUSIBLE_ANALYTICS_HOST
|
65
|
+
- content_security_policy.script_src PLAUSIBLE_ANALYTICS_HOST
|
66
|
+
= javascript_include_tag "#{PLAUSIBLE_ANALYTICS_HOST}/js/script.js", defer: true, data: { domain: local_assigns[:domain].presence || request.host }
|
67
|
+
= javascript_tag nonce: true do
|
68
|
+
window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }
|
69
|
+
```
|
70
|
+
|
71
|
+
Whenever this view partial is rendered, the connect-src and script-src directives will be automatically added to your `Content-Security-Policy` header.
|
72
|
+
|
73
|
+
#### Gravatar Images
|
74
|
+
|
75
|
+
You can also override any helper methods that add resources from external sites, and update them so that they will automatically add the required `Content-Security-Policy` rules. Here's the overridden helper method that I use to generate Gravatar image URLs:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
def gravatar_image_url(email, options = {})
|
79
|
+
content_security_policy.img_src 'https://secure.gravatar.com'
|
80
|
+
content_security_policy.img_src 'https://*.wp.com'
|
81
|
+
super
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
> Note: It's fine to call this method multiple times. Any duplicate entries are automatically removed.
|
86
|
+
|
87
|
+
## Nonces
|
88
|
+
|
89
|
+
This gem does not need to provide any extra functionality for working with `nonce` values. You can still set up the Rails nonce generator in `config/initializers/content_security_policy.rb`:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
|
93
|
+
Rails.application.config.content_security_policy_nonce_generator = ->(_request) { SecureRandom.base64(16) }
|
94
|
+
```
|
95
|
+
|
96
|
+
The Rails `content_security_policy?` method will return false since we are not using the CSP feature from Rails, so the `csp_meta_tag` helper will not work. You will need to create the meta tag manually:
|
97
|
+
|
98
|
+
```
|
99
|
+
<%= tag("meta", name: "csp-nonce", content: content_security_policy_nonce) %>
|
100
|
+
```
|
101
|
+
|
102
|
+
You must also manually set up the `nonce-*` value in your `#configure_content_security_policy` method:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
def configure_content_security_policy
|
106
|
+
content_security_policy.script_src :self, "nonce-#{content_security_policy_nonce}"
|
107
|
+
# ...
|
108
|
+
```
|
109
|
+
|
110
|
+
## Development
|
111
|
+
|
112
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
113
|
+
|
114
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
115
|
+
|
116
|
+
## Contributing
|
117
|
+
|
118
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/better_content_security_policy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/better_content_security_policy/blob/main/CODE_OF_CONDUCT.md).
|
119
|
+
|
120
|
+
## License
|
121
|
+
|
122
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
123
|
+
|
124
|
+
## Code of Conduct
|
125
|
+
|
126
|
+
Everyone interacting in the BetterContentSecurityPolicy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/better_content_security_policy/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/better_content_security_policy/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "better_content_security_policy"
|
7
|
+
spec.version = BetterContentSecurityPolicy::VERSION
|
8
|
+
spec.authors = ["Nathan Broadbent"]
|
9
|
+
spec.email = ["nathan@docspring.com"]
|
10
|
+
|
11
|
+
spec.summary = "Configure a dynamic Content-Security-Policy header that you can customize in your controllers."
|
12
|
+
spec.description = "This gem makes it easy to configure a dynamic Content-Security-Policy header " \
|
13
|
+
"for your Rails application. You can easily customize the rules in your controllers, " \
|
14
|
+
"and you can also update the rules in your views."
|
15
|
+
spec.homepage = "https://github.com/DocSpring/better_content_security_policy"
|
16
|
+
spec.license = "MIT"
|
17
|
+
spec.required_ruby_version = ">= 2.5.0"
|
18
|
+
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
20
|
+
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
22
|
+
spec.metadata["source_code_uri"] = "https://github.com/DocSpring/better_content_security_policy"
|
23
|
+
spec.metadata["changelog_uri"] = "https://github.com/DocSpring/better_content_security_policy/blob/master/CHANGELOG.md"
|
24
|
+
|
25
|
+
# Specify which files should be added to the gem when it is released.
|
26
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
27
|
+
spec.files = Dir.chdir(__dir__) do
|
28
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
29
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
30
|
+
end
|
31
|
+
end
|
32
|
+
spec.bindir = "exe"
|
33
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
34
|
+
spec.require_paths = ["lib"]
|
35
|
+
|
36
|
+
# For more information and examples about making a new gem, check out our
|
37
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
38
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
39
|
+
|
40
|
+
spec.add_dependency "rails", ">= 5.0.0"
|
41
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BetterContentSecurityPolicy
|
4
|
+
# DSL for building a Content Security Policy.
|
5
|
+
# An instance of this class will be available in your controllers and views.
|
6
|
+
# You can call a method multiple times to add additional rules to the policy.
|
7
|
+
class ContentSecurityPolicy
|
8
|
+
DIRECTIVES = %w[
|
9
|
+
base-uri
|
10
|
+
child-src
|
11
|
+
connect-src
|
12
|
+
default-src
|
13
|
+
font-src
|
14
|
+
form-action
|
15
|
+
frame-src
|
16
|
+
img-src
|
17
|
+
manifest-src
|
18
|
+
media-src
|
19
|
+
navigate-to
|
20
|
+
object-src
|
21
|
+
prefetch-src
|
22
|
+
script-src
|
23
|
+
style-src
|
24
|
+
worker-src
|
25
|
+
].freeze
|
26
|
+
|
27
|
+
SCHEME_SOURCES = %w[
|
28
|
+
blob
|
29
|
+
data
|
30
|
+
filesystem
|
31
|
+
http
|
32
|
+
https
|
33
|
+
mediastream
|
34
|
+
].freeze
|
35
|
+
|
36
|
+
QUOTED_SOURCES = %w[
|
37
|
+
none
|
38
|
+
self
|
39
|
+
unsafe-eval
|
40
|
+
unsafe-hashes
|
41
|
+
unsafe-inline
|
42
|
+
wasm-unsafe-eval
|
43
|
+
].freeze
|
44
|
+
|
45
|
+
attr_accessor :directives, :report_uri, :report_only
|
46
|
+
|
47
|
+
def initialize
|
48
|
+
@directives = {}
|
49
|
+
end
|
50
|
+
|
51
|
+
def report_only?
|
52
|
+
@report_only
|
53
|
+
end
|
54
|
+
|
55
|
+
def valid_directive?(directive)
|
56
|
+
DIRECTIVES.include?(kebab_case(directive))
|
57
|
+
end
|
58
|
+
|
59
|
+
# Handles directive methods, such as #script_src and #style_src.
|
60
|
+
# Can be called multiple times to add additional sources.
|
61
|
+
def method_missing(directive_sym, *args)
|
62
|
+
directive = directive_sym.to_s.downcase
|
63
|
+
@directives[directive] ||= []
|
64
|
+
@directives[directive] += args.flatten.compact.map(&:to_s)
|
65
|
+
@directives[directive]
|
66
|
+
end
|
67
|
+
|
68
|
+
def respond_to_missing?(directive)
|
69
|
+
valid_directive?(directive)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Converts sources from our Ruby DSL (camelcase) into proper Content-Security-Policy sources.
|
73
|
+
# (kebab-case, trailing colon, wrapped in single quotes, etc.) A few examples:
|
74
|
+
# data => data:
|
75
|
+
# http => http:
|
76
|
+
# self => 'self'
|
77
|
+
# unsafe_eval => 'unsafe-eval'
|
78
|
+
# https://example.com => https://example.com
|
79
|
+
def csp_source(dsl_source)
|
80
|
+
return "#{dsl_source}:" if SCHEME_SOURCES.include?(dsl_source)
|
81
|
+
|
82
|
+
kebab_source = kebab_case(dsl_source)
|
83
|
+
return "'#{kebab_source}'" if QUOTED_SOURCES.include?(kebab_source)
|
84
|
+
return "'#{dsl_source}'" if dsl_source.start_with?("nonce-")
|
85
|
+
|
86
|
+
dsl_source
|
87
|
+
end
|
88
|
+
|
89
|
+
def to_s
|
90
|
+
directive_strings = @directives.uniq.sort.map do |directive, dsl_sources|
|
91
|
+
[
|
92
|
+
kebab_case(directive),
|
93
|
+
dsl_sources.map { |source| csp_source(source) }.join(" ")
|
94
|
+
].join(" ")
|
95
|
+
end
|
96
|
+
directive_strings << "report-uri #{report_uri}" if report_uri.present?
|
97
|
+
directive_strings << ""
|
98
|
+
directive_strings.join("; ").strip
|
99
|
+
end
|
100
|
+
|
101
|
+
def header_name
|
102
|
+
report_only? ? "Content-Security-Policy-Report-Only" : "Content-Security-Policy"
|
103
|
+
end
|
104
|
+
|
105
|
+
def to_h
|
106
|
+
header_value = to_s
|
107
|
+
return {} if header_value.blank?
|
108
|
+
|
109
|
+
{ header_name => header_value }
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
def kebab_case(str)
|
115
|
+
str.to_s.downcase.tr("_", "-")
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/concern"
|
4
|
+
|
5
|
+
module BetterContentSecurityPolicy
|
6
|
+
# Include this module in your ApplicationController to configure a dynamic Content Security Policy.
|
7
|
+
# The header will be set in an after_action after the response has been rendered.
|
8
|
+
# This means that you can also modify the policy in your views.
|
9
|
+
module HasContentSecurityPolicy
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
included do
|
13
|
+
helper_method :content_security_policy
|
14
|
+
before_action :configure_content_security_policy
|
15
|
+
after_action :set_content_security_policy_header
|
16
|
+
end
|
17
|
+
|
18
|
+
def content_security_policy
|
19
|
+
@content_security_policy ||= BetterContentSecurityPolicy::ContentSecurityPolicy.new
|
20
|
+
end
|
21
|
+
|
22
|
+
# Override this method in your controller to configure the content security policy.
|
23
|
+
# Call `super` if you want to inherit the parent controller's policy.
|
24
|
+
def configure_content_security_policy; end
|
25
|
+
|
26
|
+
def set_content_security_policy_header
|
27
|
+
response.headers.merge!(content_security_policy.to_h)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: better_content_security_policy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nathan Broadbent
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-10-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.0.0
|
27
|
+
description: This gem makes it easy to configure a dynamic Content-Security-Policy
|
28
|
+
header for your Rails application. You can easily customize the rules in your controllers,
|
29
|
+
and you can also update the rules in your views.
|
30
|
+
email:
|
31
|
+
- nathan@docspring.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- ".rspec"
|
37
|
+
- ".rubocop.yml"
|
38
|
+
- ".vscode/settings.json"
|
39
|
+
- CHANGELOG.md
|
40
|
+
- CODE_OF_CONDUCT.md
|
41
|
+
- Gemfile
|
42
|
+
- Gemfile.lock
|
43
|
+
- LICENSE.txt
|
44
|
+
- README.md
|
45
|
+
- Rakefile
|
46
|
+
- better_content_security_policy.gemspec
|
47
|
+
- lib/better_content_security_policy.rb
|
48
|
+
- lib/better_content_security_policy/content_security_policy.rb
|
49
|
+
- lib/better_content_security_policy/has_content_security_policy.rb
|
50
|
+
- lib/better_content_security_policy/railtie.rb
|
51
|
+
- lib/better_content_security_policy/version.rb
|
52
|
+
- sig/better_content_security_policy.rbs
|
53
|
+
homepage: https://github.com/DocSpring/better_content_security_policy
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata:
|
57
|
+
homepage_uri: https://github.com/DocSpring/better_content_security_policy
|
58
|
+
source_code_uri: https://github.com/DocSpring/better_content_security_policy
|
59
|
+
changelog_uri: https://github.com/DocSpring/better_content_security_policy/blob/master/CHANGELOG.md
|
60
|
+
rubygems_mfa_required: 'true'
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.5.0
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubygems_version: 3.3.22
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Configure a dynamic Content-Security-Policy header that you can customize
|
80
|
+
in your controllers.
|
81
|
+
test_files: []
|