Skip to content

Commit 0d6a164

Browse files
committed
Merge pull request #213 from twitter/handle-validation
:preserve_schemes flag would throw an error if you actually tried to use it ...
2 parents 85eb110 + 7d96d7a commit 0d6a164

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ SecureHeaders::Configuration.default do |config|
3737
config.x_permitted_cross_domain_policies = "none"
3838
config.csp = {
3939
# "meta" values. these will shaped the header, but the values are not included in the header.
40-
report_only: true, # default: false
41-
preserve_schemes: true # default: false. Schemes are removed from host sources to save bytes and discourage mixed content.
40+
report_only: true, # default: false
41+
preserve_schemes: true, # default: false. Schemes are removed from host sources to save bytes and discourage mixed content.
4242

4343
# directive values: these values will directly translate into source directives
4444
default_src: %w(https: 'self'),

lib/secure_headers/headers/content_security_policy.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,12 @@ class ContentSecurityPolicy
145145
STAR,
146146
DATA_PROTOCOL,
147147
BLOB_PROTOCOL
148-
]
148+
].freeze
149+
150+
META_CONFIGS = [
151+
:report_only,
152+
:preserve_schemes
153+
].freeze
149154

150155
class << self
151156
# Public: generate a header name, value array that is user-agent-aware.
@@ -165,7 +170,7 @@ def validate_config!(config)
165170
return if config.nil? || config == OPT_OUT
166171
raise ContentSecurityPolicyConfigError.new(":default_src is required") unless config[:default_src]
167172
config.each do |key, value|
168-
if key == :report_only
173+
if META_CONFIGS.include?(key)
169174
raise ContentSecurityPolicyConfigError.new("#{key} must be a boolean value") unless boolean?(value) || value.nil?
170175
else
171176
validate_directive!(key, value)

spec/lib/secure_headers/headers/content_security_policy_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,35 @@ module SecureHeaders
2323
end
2424

2525
describe "#validate_config!" do
26+
it "accepts all keys" do
27+
# (pulled from README)
28+
config = {
29+
# "meta" values. these will shaped the header, but the values are not included in the header.
30+
report_only: true, # default: false
31+
preserve_schemes: true, # default: false. Schemes are removed from host sources to save bytes and discourage mixed content.
32+
33+
# directive values: these values will directly translate into source directives
34+
default_src: %w(https: 'self'),
35+
frame_src: %w('self' *.twimg.com itunes.apple.com),
36+
connect_src: %w(wws:),
37+
font_src: %w('self' data:),
38+
img_src: %w(mycdn.com data:),
39+
media_src: %w(utoob.com),
40+
object_src: %w('self'),
41+
script_src: %w('self'),
42+
style_src: %w('unsafe-inline'),
43+
base_uri: %w('self'),
44+
child_src: %w('self'),
45+
form_action: %w('self' github.com),
46+
frame_ancestors: %w('none'),
47+
plugin_types: %w(application/x-shockwave-flash),
48+
block_all_mixed_content: true, # see [http://www.w3.org/TR/mixed-content/](http://www.w3.org/TR/mixed-content/)
49+
report_uri: %w(https://example.com/uri-directive)
50+
}
51+
52+
CSP.validate_config!(config)
53+
end
54+
2655
it "requires a :default_src value" do
2756
expect do
2857
CSP.validate_config!(script_src: %('self'))
@@ -35,6 +64,12 @@ module SecureHeaders
3564
end.to raise_error(ContentSecurityPolicyConfigError)
3665
end
3766

67+
it "requires :preserve_schemes to be a truthy value" do
68+
expect do
69+
CSP.validate_config!(default_opts.merge(preserve_schemes: "steve"))
70+
end.to raise_error(ContentSecurityPolicyConfigError)
71+
end
72+
3873
it "requires :block_all_mixed_content to be a boolean value" do
3974
expect do
4075
CSP.validate_config!(default_opts.merge(block_all_mixed_content: "steve"))

0 commit comments

Comments
 (0)