Skip to content

Commit

Permalink
Merge pull request #465 from ggalmazor/enhancement/add_missing_v3_dir…
Browse files Browse the repository at this point in the history
…ectives

Add missing CSP version 3 directives
  • Loading branch information
oreoshake authored Feb 9, 2021
2 parents 62d5fb8 + 9bfb355 commit 7693be0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ SecureHeaders::Configuration.default do |config|
sandbox: true, # true and [] will set a maximally restrictive setting
plugin_types: %w(application/x-shockwave-flash),
script_src: %w('self'),
script_src_elem: %w('self'),
script_src_attr: %w('self'),
style_src: %w('unsafe-inline'),
style_src_elem: %w('unsafe-inline'),
style_src_attr: %w('unsafe-inline'),
worker_src: %w('self'),
upgrade_insecure_requests: true, # see https://www.w3.org/TR/upgrade-insecure-requests/
report_uri: %w(https://report-uri.io/example-csp)
Expand Down
4 changes: 4 additions & 0 deletions lib/secure_headers/headers/content_security_policy_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ def initialize(hash)
@sandbox = nil
@script_nonce = nil
@script_src = nil
@script_src_elem = nil
@script_src_attr = nil
@style_nonce = nil
@style_src = nil
@style_src_elem = nil
@style_src_attr = nil
@worker_src = nil
@upgrade_insecure_requests = nil
@disable_nonce_backwards_compatibility = nil
Expand Down
14 changes: 13 additions & 1 deletion lib/secure_headers/headers/policy_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def self.included(base)
REQUIRE_SRI_FOR = :require_sri_for
UPGRADE_INSECURE_REQUESTS = :upgrade_insecure_requests
WORKER_SRC = :worker_src
SCRIPT_SRC_ELEM = :script_src_elem
SCRIPT_SRC_ATTR = :script_src_attr
STYLE_SRC_ELEM = :style_src_elem
STYLE_SRC_ATTR = :style_src_attr

DIRECTIVES_3_0 = [
DIRECTIVES_2_0,
Expand All @@ -87,7 +91,11 @@ def self.included(base)
PREFETCH_SRC,
REQUIRE_SRI_FOR,
WORKER_SRC,
UPGRADE_INSECURE_REQUESTS
UPGRADE_INSECURE_REQUESTS,
SCRIPT_SRC_ELEM,
SCRIPT_SRC_ATTR,
STYLE_SRC_ELEM,
STYLE_SRC_ATTR
].flatten.freeze

ALL_DIRECTIVES = (DIRECTIVES_1_0 + DIRECTIVES_2_0 + DIRECTIVES_3_0).uniq.sort
Expand Down Expand Up @@ -117,7 +125,11 @@ def self.included(base)
PREFETCH_SRC => :source_list,
SANDBOX => :sandbox_list,
SCRIPT_SRC => :source_list,
SCRIPT_SRC_ELEM => :source_list,
SCRIPT_SRC_ATTR => :source_list,
STYLE_SRC => :source_list,
STYLE_SRC_ELEM => :source_list,
STYLE_SRC_ATTR => :source_list,
WORKER_SRC => :source_list,
UPGRADE_INSECURE_REQUESTS => :boolean,
}.freeze
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/secure_headers/headers/content_security_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,26 @@ module SecureHeaders
csp = ContentSecurityPolicy.new({default_src: %w('self'), script_src: [ContentSecurityPolicy::STRICT_DYNAMIC], script_nonce: 123456, disable_nonce_backwards_compatibility: true })
expect(csp.value).to eq("default-src 'self'; script-src 'strict-dynamic' 'nonce-123456'")
end

it "supports script-src-elem directive" do
csp = ContentSecurityPolicy.new({script_src: %w('self'), script_src_elem: %w('self')})
expect(csp.value).to eq("script-src 'self'; script-src-elem 'self'")
end

it "supports script-src-attr directive" do
csp = ContentSecurityPolicy.new({script_src: %w('self'), script_src_attr: %w('self')})
expect(csp.value).to eq("script-src 'self'; script-src-attr 'self'")
end

it "supports style-src-elem directive" do
csp = ContentSecurityPolicy.new({style_src: %w('self'), style_src_elem: %w('self')})
expect(csp.value).to eq("style-src 'self'; style-src-elem 'self'")
end

it "supports style-src-attr directive" do
csp = ContentSecurityPolicy.new({style_src: %w('self'), style_src_attr: %w('self')})
expect(csp.value).to eq("style-src 'self'; style-src-attr 'self'")
end
end
end
end
4 changes: 4 additions & 0 deletions spec/lib/secure_headers/headers/policy_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ module SecureHeaders
style_src: %w('unsafe-inline'),
upgrade_insecure_requests: true, # see https://www.w3.org/TR/upgrade-insecure-requests/
worker_src: %w(worker.com),
script_src_elem: %w(example.com),
script_src_attr: %w(example.com),
style_src_elem: %w(example.com),
style_src_attr: %w(example.com),

report_uri: %w(https://example.com/uri-directive),
}
Expand Down

0 comments on commit 7693be0

Please sign in to comment.