@@ -3,6 +3,7 @@ class Configuration
33 DEFAULT_CONFIG = :default
44 NOOP_CONFIGURATION = "secure_headers_noop_config"
55 class NotYetConfiguredError < StandardError ; end
6+ class IllegalPolicyModificationError < StandardError ; end
67 class << self
78 # Public: Set the global default configuration.
89 #
@@ -23,12 +24,12 @@ def default(&block)
2324 # if no value is supplied.
2425 #
2526 # Returns: the newly created config
26- def override ( name , base = DEFAULT_CONFIG )
27+ def override ( name , base = DEFAULT_CONFIG , & block )
2728 unless get ( base )
2829 raise NotYetConfiguredError , "#{ base } policy not yet supplied"
2930 end
3031 override = @configurations [ base ] . dup
31- yield ( override )
32+ override . instance_eval & block if block_given?
3233 add_configuration ( name , override )
3334 end
3435
@@ -99,7 +100,7 @@ def deep_copy_if_hash(value)
99100 end
100101
101102 attr_writer :hsts , :x_frame_options , :x_content_type_options ,
102- :x_xss_protection , :csp , : x_download_options, :x_permitted_cross_domain_policies ,
103+ :x_xss_protection , :x_download_options , :x_permitted_cross_domain_policies ,
103104 :hpkp , :dynamic_csp , :secure_cookies
104105
105106 attr_reader :cached_headers , :csp , :dynamic_csp , :secure_cookies
@@ -166,6 +167,14 @@ def validate_config!
166167
167168 protected
168169
170+ def csp = ( new_csp )
171+ if self . dynamic_csp
172+ raise IllegalPolicyModificationError , "You are attempting to modify CSP settings directly. Use dynamic_csp= isntead."
173+ end
174+
175+ @csp = new_csp
176+ end
177+
169178 def cached_headers = ( headers )
170179 @cached_headers = headers
171180 end
0 commit comments