Skip to content

Commit 0d054b7

Browse files
committed
Merge pull request #253 from stefansundin/fix-false-boolean
Fix false booleans being included in the CSP directive
2 parents b11b7f8 + 3558578 commit 0d054b7

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/secure_headers/headers/content_security_policy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def build_value
5353
directives.map do |directive_name|
5454
case DIRECTIVE_VALUE_TYPES[directive_name]
5555
when :boolean
56-
symbol_to_hyphen_case(directive_name)
56+
symbol_to_hyphen_case(directive_name) if @config[directive_name]
5757
when :string
5858
[symbol_to_hyphen_case(directive_name), @config[directive_name]].join(" ")
5959
else

spec/lib/secure_headers/headers/content_security_policy_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ module SecureHeaders
7171
expect(csp.value).to eq("default-src example.org")
7272
end
7373

74+
it "does add a boolean directive if the value is true" do
75+
csp = ContentSecurityPolicy.new(default_src: ["https://example.org"], block_all_mixed_content: true, upgrade_insecure_requests: true)
76+
expect(csp.value).to eq("default-src example.org; block-all-mixed-content; upgrade-insecure-requests")
77+
end
78+
79+
it "does not add a boolean directive if the value is false" do
80+
csp = ContentSecurityPolicy.new(default_src: ["https://example.org"], block_all_mixed_content: true, upgrade_insecure_requests: false)
81+
expect(csp.value).to eq("default-src example.org; block-all-mixed-content")
82+
end
83+
7484
it "deduplicates any source expressions" do
7585
csp = ContentSecurityPolicy.new(default_src: %w(example.org example.org example.org))
7686
expect(csp.value).to eq("default-src example.org")

0 commit comments

Comments
 (0)