]> git.openstreetmap.org Git - rails.git/blob - config/initializers/secure_headers.rb
Merge pull request #4550 from tomhughes/drop-user-tokens
[rails.git] / config / initializers / secure_headers.rb
1 csp_policy = {
2   :preserve_schemes => true,
3   :default_src => %w['self'],
4   :child_src => %w['self'],
5   :connect_src => %w['self'],
6   :font_src => %w['none'],
7   :form_action => %w['self'],
8   :frame_ancestors => %w['self'],
9   :frame_src => %w['self'],
10   :img_src => %w['self' data: www.gravatar.com *.wp.com tile.openstreetmap.org *.tile.openstreetmap.org *.tile.thunderforest.com tile.tracestrack.com *.openstreetmap.fr],
11   :manifest_src => %w['self'],
12   :media_src => %w['none'],
13   :object_src => %w['self'],
14   :plugin_types => %w[],
15   :script_src => %w['self'],
16   :style_src => %w['self'],
17   :worker_src => %w['none'],
18   :report_uri => []
19 }
20
21 csp_policy[:connect_src] << Settings.matomo["location"] if defined?(Settings.matomo)
22 csp_policy[:img_src] << Settings.matomo["location"] if defined?(Settings.matomo)
23 csp_policy[:script_src] << Settings.matomo["location"] if defined?(Settings.matomo)
24
25 csp_policy[:img_src] << Settings.avatar_storage_url if Settings.key?(:avatar_storage_url)
26 csp_policy[:img_src] << Settings.trace_image_storage_url if Settings.key?(:trace_image_storage_url)
27
28 csp_policy[:report_uri] << Settings.csp_report_url if Settings.key?(:csp_report_url)
29
30 cookie_policy = {
31   :httponly => { :only => %w[_osm_session _osm_totp_token] }
32 }
33
34 SecureHeaders::Configuration.default do |config|
35   config.hsts = SecureHeaders::OPT_OUT
36   config.referrer_policy = "strict-origin-when-cross-origin"
37
38   if Settings.csp_enforce
39     config.csp = csp_policy
40     config.csp_report_only = SecureHeaders::OPT_OUT
41   elsif Settings.key?(:csp_report_url)
42     config.csp = SecureHeaders::OPT_OUT
43     config.csp_report_only = csp_policy
44   else
45     config.csp = SecureHeaders::OPT_OUT
46     config.csp_report_only = SecureHeaders::OPT_OUT
47   end
48
49   config.cookies = cookie_policy
50 end