Skip to content

Commit 66c2b26

Browse files
committed
add json export
1 parent 5c202ae commit 66c2b26

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/secure_headers/headers/content_security_policy.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'base64'
33
require 'securerandom'
44
require 'user_agent_parser'
5+
require 'json'
56

67
module SecureHeaders
78
class ContentSecurityPolicyBuildError < StandardError; end
@@ -166,6 +167,11 @@ def value
166167
end
167168
end
168169

170+
def to_json
171+
build_value
172+
@config.to_json
173+
end
174+
169175
private
170176

171177
def add_script_hashes

spec/lib/secure_headers/headers/content_security_policy_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ def request_for user_agent, request_uri=nil, options={:ssl => false}
5656
end
5757
end
5858

59+
it "exports a policy to JSON" do
60+
policy = ContentSecurityPolicy.new(default_opts)
61+
expected = %({"default_src":["https:"],"script_src":["'unsafe-inline'","'unsafe-eval'","https:","data:"],"style_src":["'unsafe-inline'","https:","about:"],"img_src":["https:","data:"]})
62+
expect(policy.to_json).to eq(expected)
63+
end
64+
5965
context "when using hash sources" do
6066
it "adds hashes and unsafe-inline to the script-src" do
6167
policy = ContentSecurityPolicy.new(default_opts.merge(:script_hashes => ['sha256-abc123']))

0 commit comments

Comments
 (0)