publishing_platform_app_config 0.1.0 → 0.2.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: 11e94f7bba1b501b8bff7c62ad70627dfa8b963ddba0a57ce9c308713556c065
4
- data.tar.gz: 0f4081a9a9aafdf33f303bc6b514805aaaf113b1717769d100437f6ba3d220fc
3
+ metadata.gz: ea7eb6a338b040aba9272faf3130d42aed758692158ef9a010bf038e5b887ef4
4
+ data.tar.gz: 599d4301e0865e826df615ec3f715a326f95bd1bfc5588f6fe2d33cd363b606b
5
5
  SHA512:
6
- metadata.gz: a87d39eeceeb4cc6dc18553d09d602d7e53665ceb5bdbc91c2ec25682316f174635a195e986ff92f4ea5d60d4aebadb665a4b9f747ec85e2e0fcc62fd2d355d1
7
- data.tar.gz: 41a93c4372595ec6a7d6404dc2cd0503567a1ba592024c78e7ce4baef0a0af72ae8f747184aa9f00ea1f3f585ab4a171185f4c3004a02e3d4fc7d3f2b65c8c43
6
+ metadata.gz: db2c14514990234c81df6dd6728d3281402401125c51f2328218b98cb50c06c049dac67a968f25f6f0ee52cebae21d2158d7f9d26fb99d3da6a0452077d422e7
7
+ data.tar.gz: d1fe89fb6a497fff2a5abdb181edb40633dc1467c11e85a610e3a4957b311281118068f948fb17355a1c8a3ee891c15e4ad3a45729302dae810ac01b63f90bcd
@@ -0,0 +1,109 @@
1
+ module PublishingPlatformContentSecurityPolicy
2
+ # Generate a Content Security Policy (CSP) directive.
3
+ #
4
+ # If you are making a change here you should consider 2 basic rules of thumb:
5
+ #
6
+ # 1. Are you creating a XSS risk? Adding unsafe-* declarations, allowing data: URLs or being overly permissive (e.g. https) risks these
7
+ # 2. Is this change needed globally, if it's just one or two apps the change should be applied in them directly.
8
+
9
+ PUBLISHING_PLATFORM_DOMAINS = [
10
+ "*.publishing-platform.co.uk",
11
+ "*.dev.publishing-platform.co.uk",
12
+ ].uniq.freeze
13
+
14
+ GOOGLE_ANALYTICS_DOMAINS = %w[www.google-analytics.com
15
+ ssl.google-analytics.com
16
+ stats.g.doubleclick.net
17
+ www.googletagmanager.com
18
+ www.region1.google-analytics.com
19
+ region1.google-analytics.com].freeze
20
+
21
+ GOOGLE_STATIC_DOMAINS = %w[www.gstatic.com].freeze
22
+
23
+ def self.build_policy(policy)
24
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/default-src
25
+ policy.default_src :self
26
+
27
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/base-uri
28
+ policy.base_uri :none
29
+
30
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/img-src
31
+ # Note: we purposely don't include `data:` here because it produces a security risk.
32
+ policy.img_src :self,
33
+ *PUBLISHING_PLATFORM_DOMAINS,
34
+ *GOOGLE_ANALYTICS_DOMAINS, # Tracking pixels
35
+ # Allow YouTube thumbnails
36
+ "https://img.youtube.com",
37
+ "https://i.ytimg.com"
38
+
39
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src
40
+ # Note: we purposely don't include `data:`, `unsafe-inline` or `unsafe-eval` because
41
+ # they are security risks, if you need them for a legacy app please only apply them at
42
+ # an app level.
43
+ policy.script_src :self,
44
+ *GOOGLE_ANALYTICS_DOMAINS,
45
+ *GOOGLE_STATIC_DOMAINS,
46
+ # Allow YouTube Embeds
47
+ "*.ytimg.com",
48
+ "www.youtube.com",
49
+ "www.youtube-nocookie.com"
50
+
51
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src
52
+ # Note: we purposely don't include `data:`, `unsafe-inline` or `unsafe-eval` because
53
+ # they are security risks, if you need them for a legacy app please only apply them at
54
+ # an app level.
55
+ policy.style_src :self, *GOOGLE_STATIC_DOMAINS
56
+
57
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/font-src
58
+ # Note: we purposely don't include data here because it produces a security risk.
59
+ policy.font_src :self
60
+
61
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/connect-src
62
+ policy.connect_src :self,
63
+ *PUBLISHING_PLATFORM_DOMAINS,
64
+ *GOOGLE_ANALYTICS_DOMAINS
65
+
66
+ # Disallow all <object>, <embed>, and <applet> elements
67
+ #
68
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/object-src
69
+ policy.object_src :none
70
+
71
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src
72
+ policy.frame_src :self, *PUBLISHING_PLATFORM_DOMAINS, "www.youtube.com", "www.youtube-nocookie.com" # Allow youtube embeds
73
+
74
+ # Disallow non-publishing-platform.co.uk domains from embeding a page using <frame>, <iframe>, <object>, or <embed> to prevent clickjacking
75
+ #
76
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors
77
+ policy.frame_ancestors :self, *PUBLISHING_PLATFORM_DOMAINS
78
+
79
+ policy.report_uri ENV["PUBLISHING_PLATFORM_CSP_REPORT_URI"] if ENV.include?("PUBLISHING_PLATFORM_CSP_REPORT_URI")
80
+ end
81
+
82
+ def self.configure
83
+ Rails.application.config.content_security_policy_report_only = ENV.include?("PUBLISHING_PLATFORM_CSP_REPORT_ONLY")
84
+
85
+ # Sets a nonce per request that can be set on script-src and style-src
86
+ # directives depending on the value of Rails.application.config.content_security_policy_nonce_directives
87
+ #
88
+ # Note: if an application needs to set unsafe-inline they will need to
89
+ # unset this generator (by setting this config option to nil in their application)
90
+ Rails.application.config.content_security_policy_nonce_generator = ->(_request) { SecureRandom.base64(16) }
91
+
92
+ # This only applies the nonce generator to the script-src directive. We need this to
93
+ # use unsafe-inline for style-src as a nonce will override it.
94
+ #
95
+ # When we want to apply it to style-src we can remove this line as the Rails default
96
+ # is for both script-src and style-src
97
+ Rails.application.config.content_security_policy_nonce_directives = %w[script-src]
98
+
99
+ policy = Rails.application.config.content_security_policy(&method(:build_policy))
100
+
101
+ # # allow apps to customise the CSP by passing a block e.g:
102
+ # PublishingPlatformContentSecurityPolicy.configure do |policy|
103
+ # policy.image_src(*policy.image_src, "https://i.ytimg.com")
104
+ # end
105
+ yield(policy) if block_given?
106
+
107
+ policy
108
+ end
109
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PublishingPlatformAppConfig
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -2,5 +2,6 @@ require "publishing_platform_app_config/version"
2
2
  require "publishing_platform_app_config/publishing_platform_error"
3
3
 
4
4
  if defined?(Rails)
5
+ require "publishing_platform_app_config/publishing_platform_content_security_policy"
5
6
  require "publishing_platform_app_config/railtie"
6
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: publishing_platform_app_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Publishing Platform
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-11 00:00:00.000000000 Z
11
+ date: 2024-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sentry-rails
@@ -69,6 +69,7 @@ files:
69
69
  - bin/console
70
70
  - bin/setup
71
71
  - lib/publishing_platform_app_config.rb
72
+ - lib/publishing_platform_app_config/publishing_platform_content_security_policy.rb
72
73
  - lib/publishing_platform_app_config/publishing_platform_error.rb
73
74
  - lib/publishing_platform_app_config/publishing_platform_error/configuration.rb
74
75
  - lib/publishing_platform_app_config/railtie.rb