Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape semi colons in directive source lists in 3.x releases #420

Merged
merged 1 commit into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions lib/secure_headers/headers/content_security_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,14 @@ def build_source_list_directive(directive)
@config.directive_value(directive)
end
return unless source_list && source_list.any?
normalized_source_list = minify_source_list(directive, source_list)
[symbol_to_hyphen_case(directive), normalized_source_list].join(" ")
normalized_source_list = minify_source_list(directive, source_list).join(" ")

if normalized_source_list.include?(";")
Kernel.warn("#{directive} contains a ; in '#{normalized_source_list}' which will raise an error in future versions. It has been replaced with a blank space.")
end
escaped_source_list = normalized_source_list.gsub(";", " ")

[symbol_to_hyphen_case(directive), escaped_source_list].join(" ").strip
end

# If a directive contains *, all other values are omitted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ module SecureHeaders
end

describe "#value" do
it "uses a safe but non-breaking default value" do
expect(ContentSecurityPolicy.new.value).to eq("default-src https:")
end

it "deprecates and escapes semicolons in directive source lists" do
expect(Kernel).to receive(:warn).with("frame_ancestors contains a ; in 'google.com;script-src *;.;' which will raise an error in future versions. It has been replaced with a blank space.")
expect(ContentSecurityPolicy.new(frame_ancestors: %w(https://google.com;script-src https://*;.;)).value).to eq("frame-ancestors google.com script-src * .")
end

it "discards 'none' values if any other source expressions are present" do
csp = ContentSecurityPolicy.new(default_opts.merge(child_src: %w('self' 'none')))
expect(csp.value).not_to include("'none'")
Expand Down