Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add validation to sandbox and plugin_types directives which accept bo…
…oleans and loosely defined strints
  • Loading branch information
oreoshake committed Jan 23, 2020
commit 0b26d92ba634678df1746b04379628f2ad1a0eac
6 changes: 3 additions & 3 deletions lib/secure_headers/headers/policy_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,10 @@ def validate_sandbox_expression!(directive, sandbox_token_expression)
return if boolean?(sandbox_token_expression) && sandbox_token_expression == true
ensure_array_of_strings!(directive, sandbox_token_expression)
valid = sandbox_token_expression.compact.all? do |v|
v.is_a?(String) && v.start_with?("allow-")
v.is_a?(String) && v.start_with?("allow-") && v !~ ESCAPE_SEQUENCE
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be cleaner to move this check to ensure_array_of_strings.

But the belt-and-suspenders approach of also raising an error on output is also probably a good idea.

end
if !valid
raise ContentSecurityPolicyConfigError.new("#{directive} must be True or an array of zero or more sandbox token strings (ex. allow-forms)")
raise ContentSecurityPolicyConfigError.new("#{directive} must be True or an array of zero or more sandbox token strings (ex. allow-forms). Was #{sandbox_token_expression.inspect}")
end
end

Expand All @@ -343,7 +343,7 @@ def validate_media_type_expression!(directive, media_type_expression)
ensure_array_of_strings!(directive, media_type_expression)
valid = media_type_expression.compact.all? do |v|
# All media types are of the form: <type from RFC 2045> "/" <subtype from RFC 2045>.
v =~ /\A.+\/.+\z/
v =~ /\A[^\n;]+\/[^\n;]+\z/
end
if !valid
raise ContentSecurityPolicyConfigError.new("#{directive} must be an array of valid media types (ex. application/pdf)")
Expand Down
14 changes: 13 additions & 1 deletion spec/lib/secure_headers/headers/policy_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ module SecureHeaders
it "rejects anything not of the form allow-* as a sandbox value" do
expect do
ContentSecurityPolicy.validate_config!(ContentSecurityPolicyConfig.new(default_opts.merge(sandbox: ["steve"])))
end.to raise_error(ContentSecurityPolicyConfigError, "sandbox must be True or an array of zero or more sandbox token strings (ex. allow-forms)")
end.to raise_error(ContentSecurityPolicyConfigError, %(sandbox must be True or an array of zero or more sandbox token strings (ex. allow-forms). Was ["steve"]))
end

it "accepts anything of the form allow-* as a sandbox value " do
Expand All @@ -147,6 +147,12 @@ module SecureHeaders
end.to_not raise_error
end

it "rejects escapes in sandbox value " do
expect do
ContentSecurityPolicy.validate_config!(ContentSecurityPolicyConfig.new(default_opts.merge(sandbox: ["allow-; script-src foo"])))
end.to raise_error(ContentSecurityPolicyConfigError, %(sandbox must be True or an array of zero or more sandbox token strings (ex. allow-forms). Was ["allow-; script-src foo"]))
end

it "accepts true as a sandbox policy" do
expect do
ContentSecurityPolicy.validate_config!(ContentSecurityPolicyConfig.new(default_opts.merge(sandbox: true)))
Expand All @@ -159,6 +165,12 @@ module SecureHeaders
end.to raise_error(ContentSecurityPolicyConfigError, "plugin_types must be an array of valid media types (ex. application/pdf)")
end

it "rejects escape values as plugin-type value" do
expect do
ContentSecurityPolicy.validate_config!(ContentSecurityPolicyConfig.new(default_opts.merge(plugin_types: [";/script-src*"])))
end.to raise_error(ContentSecurityPolicyConfigError, "plugin_types must be an array of valid media types (ex. application/pdf)")
end

it "accepts anything of the form type/subtype as a plugin-type value " do
expect do
ContentSecurityPolicy.validate_config!(ContentSecurityPolicyConfig.new(default_opts.merge(plugin_types: ["application/pdf"])))
Expand Down