Skip to content

Commit

Permalink
Merge pull request from GHSA-w978-rmpf-qmwg
Browse files Browse the repository at this point in the history
Filter and warn on newlines in configurations
  • Loading branch information
oreoshake authored Jan 21, 2020
2 parents 1298905 + 3a2b548 commit 3016957
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/secure_headers/headers/content_security_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ def build_source_list_directive(directive)
if source_list != OPT_OUT && source_list && source_list.any?
minified_source_list = minify_source_list(directive, source_list).join(" ")

if minified_source_list.include?(";")
Kernel.warn("#{directive} contains a ; in '#{minified_source_list}' which will raise an error in future versions. It has been replaced with a blank space.")
if minified_source_list =~ /(\n|;)/
Kernel.warn("#{directive} contains a #{$1} in #{minified_source_list.inspect} which will raise an error in future versions. It has been replaced with a blank space.")
end

escaped_source_list = minified_source_list.gsub(";", " ")
escaped_source_list = minified_source_list.gsub(/[\n;]/, " ")
[symbol_to_hyphen_case(directive), escaped_source_list].join(" ").strip
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ module SecureHeaders
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(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 "deprecates and escapes semicolons in directive source lists" do
expect(Kernel).to receive(:warn).with(%(frame_ancestors contains a \n in "\\nfoo.com\\nhacked" which will raise an error in future versions. It has been replaced with a blank space.))
expect(ContentSecurityPolicy.new(frame_ancestors: ["\nfoo.com\nhacked"]).value).to eq("frame-ancestors foo.com hacked")
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

0 comments on commit 3016957

Please sign in to comment.