Skip to content

Commit 936a160

Browse files
authored
Merge pull request #421 from twitter/escape-semi-colons-5.x
escape semicolons by replacing them with spaces for 5.x line
2 parents 0b33264 + f950507 commit 936a160

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ language: ruby
22

33
rvm:
44
- ruby-head
5-
- 2.4.2
6-
- 2.3.5
7-
- 2.2.8
5+
- 2.6
6+
- 2.5
7+
- 2.4
88
- jruby-head
99

1010
env:

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ group :test do
99
gem "pry-nav"
1010
gem "rack"
1111
gem "rspec"
12-
gem "rubocop"
12+
gem "rubocop", "< 0.68"
1313
gem "rubocop-github"
1414
gem "term-ansicolor"
1515
gem "tins"

lib/secure_headers/headers/content_security_policy.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,14 @@ def build_source_list_directive(directive)
138138
end
139139

140140
if source_list != OPT_OUT && source_list && source_list.any?
141-
normalized_source_list = minify_source_list(directive, source_list)
142-
[symbol_to_hyphen_case(directive), normalized_source_list].join(" ")
141+
minified_source_list = minify_source_list(directive, source_list).join(" ")
142+
143+
if minified_source_list.include?(";")
144+
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.")
145+
end
146+
147+
escaped_source_list = minified_source_list.gsub(";", " ")
148+
[symbol_to_hyphen_case(directive), escaped_source_list].join(" ").strip
143149
end
144150
end
145151

spec/lib/secure_headers/headers/content_security_policy_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ module SecureHeaders
2828
expect(ContentSecurityPolicy.new.value).to eq("default-src https:; form-action 'self'; img-src https: data: 'self'; object-src 'none'; script-src https:; style-src 'self' 'unsafe-inline' https:")
2929
end
3030

31+
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.")
33+
expect(ContentSecurityPolicy.new(frame_ancestors: %w(https://google.com;script-src https://*;.;)).value).to eq("frame-ancestors google.com script-src * .")
34+
end
35+
3136
it "discards 'none' values if any other source expressions are present" do
3237
csp = ContentSecurityPolicy.new(default_opts.merge(child_src: %w('self' 'none')))
3338
expect(csp.value).not_to include("'none'")

0 commit comments

Comments
 (0)