Skip to content

Commit 3016957

Browse files
authored
Merge pull request from GHSA-w978-rmpf-qmwg
Filter and warn on newlines in configurations
2 parents 1298905 + 3a2b548 commit 3016957

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/secure_headers/headers/content_security_policy.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ def build_source_list_directive(directive)
106106
if source_list != OPT_OUT && source_list && source_list.any?
107107
minified_source_list = minify_source_list(directive, source_list).join(" ")
108108

109-
if minified_source_list.include?(";")
110-
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.")
109+
if minified_source_list =~ /(\n|;)/
110+
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.")
111111
end
112112

113-
escaped_source_list = minified_source_list.gsub(";", " ")
113+
escaped_source_list = minified_source_list.gsub(/[\n;]/, " ")
114114
[symbol_to_hyphen_case(directive), escaped_source_list].join(" ").strip
115115
end
116116
end

spec/lib/secure_headers/headers/content_security_policy_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@ module SecureHeaders
2929
end
3030

3131
it "deprecates and escapes semicolons in directive source lists" do
32-
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.")
32+
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.))
3333
expect(ContentSecurityPolicy.new(frame_ancestors: %w(https://google.com;script-src https://*;.;)).value).to eq("frame-ancestors google.com script-src * .")
3434
end
3535

36+
it "deprecates and escapes semicolons in directive source lists" do
37+
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.))
38+
expect(ContentSecurityPolicy.new(frame_ancestors: ["\nfoo.com\nhacked"]).value).to eq("frame-ancestors foo.com hacked")
39+
end
40+
3641
it "discards 'none' values if any other source expressions are present" do
3742
csp = ContentSecurityPolicy.new(default_opts.merge(child_src: %w('self' 'none')))
3843
expect(csp.value).not_to include("'none'")

0 commit comments

Comments
 (0)