yaml_csp_config 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d364ca228a1c6e897f15ceb024b277fe0d5ca6e857b8408e2beec971cd3ea62
4
- data.tar.gz: 9dd744ccf9ee54ac541b364f829fbc284c839826c7a1bf2b862e4a06f7ae4bc1
3
+ metadata.gz: e7621b0c8c7678d033718dc0cbc40b6a1e1511f1f16a5a0bd143d1f3917224fa
4
+ data.tar.gz: 9b5b960c7b7b0764c7019b1fbe37ebcffbe7365f3f716d2996c0f2dad59228d0
5
5
  SHA512:
6
- metadata.gz: b860612f0350846c2af70a0fe469f2b42b8dcdafd1899925e4a67834b971d6530481ab56b07c12a59e15d1f308ba7bef0e0f535bfdd08d69e6fc25947d71ef4b
7
- data.tar.gz: 497d63f942a225ce8127bb516a8425330c1fce9cdb9e90fb97cfbc4400f6306cdfc6126738ea05d7972a6ab16e4ae6d8fb95f2c9038727b3aba8b04356cce10f
6
+ metadata.gz: a9fd4e9f2f152d0921ed9f9cc29ec2ec10de464a611a36fc01a78701136904dee686b6790d7e6c09cf763735efcfaed965defae93f8c38a616746b6f14f2c142
7
+ data.tar.gz: 4e6f9cfc2d777baaf0baf72e52b0800f7a2bdeaf73a1c65f68e618bb27d56925ab40e435de253343f289e1af829bb37a06d85a7a5f7a61ba62e07d50fd2f761a
data/README.md CHANGED
@@ -2,92 +2,31 @@
2
2
 
3
3
  ### What?
4
4
 
5
- This Rails plugin gem is designed to allow you to be able to specify your content security policy
6
- for Rails 5.2+ in a YAML file, instead of using the Rails DSL.
5
+ A gem for Rails 6+ that allows you to specify your content security policy (CSP) in a YAML file, instead of using the Rails DSL.
7
6
 
8
- This makes the configuration of your content security policy more akin to configuring other things
9
- through YAML files.
10
-
11
- The gem also contains a extra few features. These allow you to add content security policy configuration
12
- via environment variables, either by configuring a specific addition for a specific directive or by
13
- configuring the name of a group of configurations to be applied from the configuration file in the
14
- application. This is useful for deployed environments where the content security policy may be slightly
15
- different per deployment.
16
7
 
17
8
  ### Why?
18
9
 
19
- * Configure your CSP in YAML
20
- * Provide additional CSP configuration which is applied according to environment variables
21
-
22
- ## Example
23
-
24
- Below is an artificial example of a security policy before and after converting DSL to YAML,
25
- making use of YAML aliases to allow sharing of policy configurations:
26
-
27
- ### Before (Without this gem):
28
-
29
- `config/initializers/content_security_policy.rb`
30
-
31
- ```ruby
32
- GOOGLE_STATIC = ["https://*.googleapis.com", "https://*.gstatic.com"].freeze
33
-
34
- CSP_SCRIPT_HOSTS = %w[
35
- https://cdnjs.cloudflare.com
36
- https://www.google-analytics.com
37
- https://maps.googleapis.com
38
- ].freeze
39
-
40
- CSP_FONT_HOSTS = (["https://fonts.gstatic.com"] + GOOGLE_STATIC).freeze
10
+ The YAML configuration is potentially more structured, and easier to read and maintain
11
+ than using the Ruby DSL with conditional logic on env vars and so on.
41
12
 
42
- CSP_IMAGE_HOSTS = (["https://s3.amazonaws.com"] + GOOGLE_STATIC).freeze
43
-
44
- CSP_WEBPACKER_HOST = "http://localhost:3035"
13
+ Also config of the CSP becomes similar to configuring other things in Rails, such as the database, via YAML files.
45
14
 
46
- CSP_DEV_CONNECT_SRC = %w[
47
- http://localhost:3035
48
- ws://localhost:3000
49
- ws://localhost:3035
50
- ws://127.0.0.1:35729
51
- ].freeze
52
-
53
- CSP_REVIEW_CONNECT_SRC = %w[
54
- wss://*.herokuapp.com
55
- ].freeze
15
+ ### Features
56
16
 
57
- Rails.application.config.content_security_policy do |policy|
58
- policy.report_uri("/csp-violation-report-endpoint")
59
-
60
- policy.default_src(:self)
61
-
62
- policy.object_src(:none)
63
-
64
- policy.font_src(:self, *CSP_FONT_HOSTS)
65
-
66
- policy.style_src(:self, :data, :unsafe_inline)
67
-
68
- if Rails.env.development?
69
- policy.img_src(:self, :data, CSP_WEBPACKER_HOST, *CSP_IMAGE_HOSTS)
70
-
71
- policy.script_src(:self, :unsafe_eval, CSP_WEBPACKER_HOST, *CSP_SCRIPT_HOSTS)
72
-
73
- policy.connect_src(:self, *CSP_DEV_CONNECT_SRC)
74
- else
75
- policy.img_src(:self, :data, *CSP_IMAGE_HOSTS)
17
+ * Configure your CSP in YAML
18
+ * Use anchors/aliases to avoid duplicated blocks of URLs between different policy directives
19
+ * Create Rails env specific configurations (eg directives only for `development`)
20
+ * Extend the content security policy configuration via environment variables. Useful for deployed environments where the CSP is different per deployment.
21
+ 1) configure a specific addition for a specific directive or
22
+ 2) specify the name of a group of configurations to be applied.
23
+ * The YAML file can contain ERB
76
24
 
77
- policy.script_src(:self, *CSP_SCRIPT_HOSTS)
78
-
79
- if ENV["IN_REVIEW_APP"].present?
80
- policy.connect_src(:self, *CSP_REVIEW_CONNECT_SRC)
81
- else
82
- policy.connect_src(:self)
83
- end
84
- end
85
- end
25
+ ## Example
86
26
 
87
- # ...
88
- ```
27
+ Below is an example of a security policy in YAML and Rails DSL.
89
28
 
90
- ### After (With this gem):
29
+ ### In YAML (with this gem):
91
30
 
92
31
  `config/content_security_policy.yml`
93
32
 
@@ -149,6 +88,68 @@ review_apps:
149
88
  - wss://*.herokuapp.com
150
89
  ```
151
90
 
91
+ ### Equivalent in Ruby DSL:
92
+
93
+ `config/initializers/content_security_policy.rb`
94
+
95
+ ```ruby
96
+ GOOGLE_STATIC = ["https://*.googleapis.com", "https://*.gstatic.com"].freeze
97
+
98
+ CSP_SCRIPT_HOSTS = %w[
99
+ https://cdnjs.cloudflare.com
100
+ https://www.google-analytics.com
101
+ https://maps.googleapis.com
102
+ ].freeze
103
+
104
+ CSP_FONT_HOSTS = (["https://fonts.gstatic.com"] + GOOGLE_STATIC).freeze
105
+
106
+ CSP_IMAGE_HOSTS = (["https://s3.amazonaws.com"] + GOOGLE_STATIC).freeze
107
+
108
+ CSP_WEBPACKER_HOST = "http://localhost:3035"
109
+
110
+ CSP_DEV_CONNECT_SRC = %w[
111
+ http://localhost:3035
112
+ ws://localhost:3000
113
+ ws://localhost:3035
114
+ ws://127.0.0.1:35729
115
+ ].freeze
116
+
117
+ CSP_REVIEW_CONNECT_SRC = %w[
118
+ wss://*.herokuapp.com
119
+ ].freeze
120
+
121
+ Rails.application.config.content_security_policy do |policy|
122
+ policy.report_uri("/csp-violation-report-endpoint")
123
+
124
+ policy.default_src(:self)
125
+
126
+ policy.object_src(:none)
127
+
128
+ policy.font_src(:self, *CSP_FONT_HOSTS)
129
+
130
+ policy.style_src(:self, :data, :unsafe_inline)
131
+
132
+ if Rails.env.development?
133
+ policy.img_src(:self, :data, CSP_WEBPACKER_HOST, *CSP_IMAGE_HOSTS)
134
+
135
+ policy.script_src(:self, :unsafe_eval, CSP_WEBPACKER_HOST, *CSP_SCRIPT_HOSTS)
136
+
137
+ policy.connect_src(:self, *CSP_DEV_CONNECT_SRC)
138
+ else
139
+ policy.img_src(:self, :data, *CSP_IMAGE_HOSTS)
140
+
141
+ policy.script_src(:self, *CSP_SCRIPT_HOSTS)
142
+
143
+ if ENV["IN_REVIEW_APP"].present?
144
+ policy.connect_src(:self, *CSP_REVIEW_CONNECT_SRC)
145
+ else
146
+ policy.connect_src(:self)
147
+ end
148
+ end
149
+ end
150
+
151
+ # ...
152
+ ```
152
153
 
153
154
  ## Installation
154
155
  Add to your Gemfile:
@@ -168,17 +169,9 @@ Then run the **generator to add the initializer**
168
169
 
169
170
  ## Usage
170
171
 
171
- ### `ActionDispatch::ContentSecurityPolicy.load_from_file`
172
-
173
- `YamlCspConfig` extends `ActionDispatch::ContentSecurityPolicy` with a method to
174
- load configuration from a YAML file. By default the initializer will add the `load_from_file`
175
- instance method and call it on initialisation.
176
-
177
- If you wish instead to call it explicitly make sure to comment it out from the initializer.
178
-
179
172
  ### YAML file format
180
173
 
181
- **Note: The YAML file can also be an ERB template.**
174
+ Note: The YAML file can also be an ERB template.
182
175
 
183
176
  The file must contain at at least the 'base' configuration group, containing the base or common CSP
184
177
  configuration.
@@ -252,6 +245,14 @@ For example:
252
245
 
253
246
  will add `host.cdn` to the `script_src` directive.
254
247
 
248
+ ### Note this extends `ActionDispatch::ContentSecurityPolicy.load_from_file`
249
+
250
+ `YamlCspConfig` extends `ActionDispatch::ContentSecurityPolicy` with a method to
251
+ load configuration from a YAML file. By default the initializer will add the `load_from_file`
252
+ instance method and call it on initialisation.
253
+
254
+ If you wish instead to call it explicitly make sure to comment it out from the initializer.
255
+
255
256
  ## Run type check (RBS & steep)
256
257
 
257
258
  First copy the signatures for Rails from `https://github.com/pocke/rbs_rails/tree/master/assets/sig`
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YamlCspConfig
4
- VERSION = "1.0.3"
4
+ VERSION = "1.1.0"
5
5
  end
@@ -5,6 +5,7 @@ module YamlCspConfig
5
5
  class YamlLoader
6
6
  DIRECTIVES = %i[
7
7
  base_uri
8
+ block_all_mixed_content
8
9
  child_src
9
10
  connect_src
10
11
  default_src
@@ -15,10 +16,23 @@ module YamlCspConfig
15
16
  img_src
16
17
  manifest_src
17
18
  media_src
19
+ navigate_to
18
20
  object_src
21
+ plugin_types
19
22
  prefetch_src
23
+ referrer
24
+ report_to
25
+ report_uri
26
+ require_trusted_types_for
27
+ sandbox
20
28
  script_src
29
+ script_src_attr
30
+ script_src_elem
21
31
  style_src
32
+ style_src_attr
33
+ style_src_elem
34
+ trusted_types
35
+ upgrade_insecure_requests
22
36
  worker_src
23
37
  ].freeze
24
38
 
@@ -98,7 +112,8 @@ module YamlCspConfig
98
112
  DIRECTIVES.each do |rule|
99
113
  d = rule.to_s
100
114
  k = env_var_key_prefix + d.upcase
101
- add_to_csp(policies, d, ENV[k].split(" ")) if ENV[k].present?
115
+ override_env_var_value = ENV[k]
116
+ add_to_csp(policies, d, override_env_var_value.split(" ")) if override_env_var_value
102
117
  end
103
118
  policies
104
119
  end
@@ -7,10 +7,11 @@ require "yaml_csp_config/yaml_loader"
7
7
  # Exposes a configuration class for initializer
8
8
  module YamlCspConfig
9
9
  class << self
10
- attr_reader :configuration
10
+ def configuration
11
+ @configuration ||= Configuration.new
12
+ end
11
13
 
12
14
  def configure
13
- @configuration ||= Configuration.new
14
15
  yield(configuration) if block_given?
15
16
  configuration
16
17
  end
data/sig/types.rbs CHANGED
@@ -49,6 +49,7 @@ module YamlCspConfig
49
49
  def env_var_group_override: (cspGroup config, cspPolicyRules policies) -> cspPolicyRules
50
50
  def env_var_direct_override: (cspPolicyRules policies) -> cspPolicyRules
51
51
  def add_to_csp: (cspPolicyRules policies, String rule, (Symbol | String | Array[String | Symbol]) value) -> void
52
+ def parse_policies_config: (untyped) -> untyped
52
53
  def config_key_base: -> String
53
54
  end
54
55
  end
metadata CHANGED
@@ -1,85 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_csp_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Ierodiaconou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-10 00:00:00.000000000 Z
11
+ date: 2024-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: railties
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.2'
19
+ version: '7.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: '5.2'
27
- - !ruby/object:Gem::Dependency
28
- name: rbs_rails
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
29
+ version: '7.0'
30
+ - - "<"
32
31
  - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
32
+ version: '8.0'
41
33
  - !ruby/object:Gem::Dependency
42
- name: standard
34
+ name: activesupport
43
35
  requirement: !ruby/object:Gem::Requirement
44
36
  requirements:
45
37
  - - ">="
46
38
  - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
39
+ version: '7.0'
40
+ - - "<"
53
41
  - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: steep
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
42
+ version: '8.0'
43
+ type: :runtime
63
44
  prerelease: false
64
45
  version_requirements: !ruby/object:Gem::Requirement
65
46
  requirements:
66
47
  - - ">="
67
48
  - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: sqlite3
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
49
+ version: '7.0'
50
+ - - "<"
81
51
  - !ruby/object:Gem::Version
82
- version: '0'
52
+ version: '8.0'
83
53
  description: yaml_csp_config provides you with a way to manage your Rails 5.2+ CSP
84
54
  configuration via a YAML file. The CSP configuration can also be extended by environment
85
55
  variables.
@@ -115,14 +85,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
85
  requirements:
116
86
  - - ">="
117
87
  - !ruby/object:Gem::Version
118
- version: '2.6'
88
+ version: '3.0'
119
89
  required_rubygems_version: !ruby/object:Gem::Requirement
120
90
  requirements:
121
91
  - - ">="
122
92
  - !ruby/object:Gem::Version
123
93
  version: '0'
124
94
  requirements: []
125
- rubygems_version: 3.3.7
95
+ rubygems_version: 3.5.3
126
96
  signing_key:
127
97
  specification_version: 4
128
98
  summary: yaml_csp_config provides you with a way to manage your Rails CSP configuration