yaml_csp_config 1.0.3 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d364ca228a1c6e897f15ceb024b277fe0d5ca6e857b8408e2beec971cd3ea62
4
- data.tar.gz: 9dd744ccf9ee54ac541b364f829fbc284c839826c7a1bf2b862e4a06f7ae4bc1
3
+ metadata.gz: 538a72dc3c9a24f062d458df0725c8450453b7e237738275ca51d8a8a976c6dd
4
+ data.tar.gz: 289d615ab9693ee46f2589fbd2b71dfaad3decdf927b001d8018a0344355ba5e
5
5
  SHA512:
6
- metadata.gz: b860612f0350846c2af70a0fe469f2b42b8dcdafd1899925e4a67834b971d6530481ab56b07c12a59e15d1f308ba7bef0e0f535bfdd08d69e6fc25947d71ef4b
7
- data.tar.gz: 497d63f942a225ce8127bb516a8425330c1fce9cdb9e90fb97cfbc4400f6306cdfc6126738ea05d7972a6ab16e4ae6d8fb95f2c9038727b3aba8b04356cce10f
6
+ metadata.gz: 3a1efd2ecdda38373edb73e2915090bd70db23efab4272b4c35c15ff6fa5af3ddcd2d8f8acc9a57e3cdafc994c778d42b91b4838dbd94a3ca1e27796514b5f59
7
+ data.tar.gz: d9a6efe658f568d4e4deb785238509da49e64e5981b11966cac1a6ce1570f56a74ce05163465bead3e31d8df1cade81b71ae20ce47c6148dfb720e874987b931
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.2.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,73 +1,56 @@
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.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Ierodiaconou
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2022-10-10 00:00:00.000000000 Z
10
+ date: 2025-04-07 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
- name: rails
13
+ name: railties
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
16
  - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: '5.2'
18
+ version: '7.0'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '9.0'
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
23
25
  requirements:
24
26
  - - ">="
25
27
  - !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
- - - ">="
28
+ version: '7.0'
29
+ - - "<"
32
30
  - !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'
31
+ version: '9.0'
41
32
  - !ruby/object:Gem::Dependency
42
- name: standard
33
+ name: activesupport
43
34
  requirement: !ruby/object:Gem::Requirement
44
35
  requirements:
45
36
  - - ">="
46
37
  - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: steep
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
38
+ version: '7.0'
39
+ - - "<"
60
40
  - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
41
+ version: '9.0'
42
+ type: :runtime
63
43
  prerelease: false
64
44
  version_requirements: !ruby/object:Gem::Requirement
65
45
  requirements:
66
46
  - - ">="
67
47
  - !ruby/object:Gem::Version
68
- version: '0'
48
+ version: '7.0'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '9.0'
69
52
  - !ruby/object:Gem::Dependency
70
- name: sqlite3
53
+ name: appraisal
71
54
  requirement: !ruby/object:Gem::Requirement
72
55
  requirements:
73
56
  - - ">="
@@ -107,7 +90,6 @@ homepage: https://github.com/stevegeek/yaml_csp_config
107
90
  licenses:
108
91
  - MIT
109
92
  metadata: {}
110
- post_install_message:
111
93
  rdoc_options: []
112
94
  require_paths:
113
95
  - lib
@@ -115,15 +97,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
97
  requirements:
116
98
  - - ">="
117
99
  - !ruby/object:Gem::Version
118
- version: '2.6'
100
+ version: '3.0'
119
101
  required_rubygems_version: !ruby/object:Gem::Requirement
120
102
  requirements:
121
103
  - - ">="
122
104
  - !ruby/object:Gem::Version
123
105
  version: '0'
124
106
  requirements: []
125
- rubygems_version: 3.3.7
126
- signing_key:
107
+ rubygems_version: 3.6.2
127
108
  specification_version: 4
128
109
  summary: yaml_csp_config provides you with a way to manage your Rails CSP configuration
129
110
  via a YAML file.