Skip to content

Commit 87b525e

Browse files
committed
add more tests around unknown hash behavior
1 parent 99eed49 commit 87b525e

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

spec/lib/secure_headers/view_helpers_spec.rb

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,35 @@ module SecureHeaders
6868
let(:app) { lambda { |env| [200, env, "app"] } }
6969
let(:middleware) { Middleware.new(app) }
7070
let(:request) { Rack::Request.new("HTTP_USER_AGENT" => USER_AGENTS[:chrome]) }
71+
let(:filename) { "app/views/asdfs/index.html.erb" }
7172

72-
before(:each) do
73+
before(:all) do
7374
Configuration.default do |config|
7475
config.csp[:script_src] = %w('self')
7576
config.csp[:style_src] = %w('self')
7677
end
7778
end
7879

79-
it "raises an error when attempting to hash unknown content" do
80+
after(:each) do
81+
Configuration.instance_variable_set(:@script_hashes, nil)
82+
Configuration.instance_variable_set(:@style_hashes, nil)
83+
end
84+
85+
it "raises an error when using hashed content without precomputed hashes" do
86+
expect {
87+
Message.new(request).result
88+
}.to raise_error(ViewHelpers::UnexpectedHashedScriptException)
89+
end
90+
91+
it "raises an error when using hashed content with precomputed hashes, but none for the given file" do
92+
Configuration.instance_variable_set(:@script_hashes, filename.reverse => ["'sha256-123'"])
93+
expect {
94+
Message.new(request).result
95+
}.to raise_error(ViewHelpers::UnexpectedHashedScriptException)
96+
end
97+
98+
it "raises an error when using previously unknown hashed content with precomputed hashes for a given file" do
99+
Configuration.instance_variable_set(:@script_hashes, filename => ["'sha256-123'"])
80100
expect {
81101
Message.new(request).result
82102
}.to raise_error(ViewHelpers::UnexpectedHashedScriptException)
@@ -87,9 +107,9 @@ module SecureHeaders
87107
allow(SecureRandom).to receive(:base64).and_return("abc123")
88108

89109
expected_hash = "sha256-3/URElR9+3lvLIouavYD/vhoICSNKilh15CzI/nKqg8="
90-
Configuration.instance_variable_set(:@script_hashes, "app/views/asdfs/index.html.erb" => ["'#{expected_hash}'"])
110+
Configuration.instance_variable_set(:@script_hashes, filename => ["'#{expected_hash}'"])
91111
expected_style_hash = "sha256-7oYK96jHg36D6BM042er4OfBnyUDTG3pH1L8Zso3aGc="
92-
Configuration.instance_variable_set(:@style_hashes, "app/views/asdfs/index.html.erb" => ["'#{expected_style_hash}'"])
112+
Configuration.instance_variable_set(:@style_hashes, filename => ["'#{expected_style_hash}'"])
93113

94114
# render erb that calls out to helpers.
95115
Message.new(request).result
@@ -99,9 +119,6 @@ module SecureHeaders
99119
expect(env[CSP::HEADER_NAME]).to match(/script-src[^;]*'nonce-abc123'/)
100120
expect(env[CSP::HEADER_NAME]).to match(/style-src[^;]*'nonce-abc123'/)
101121
expect(env[CSP::HEADER_NAME]).to match(/style-src[^;]*'#{Regexp.escape(expected_style_hash)}'/)
102-
ensure
103-
Configuration.instance_variable_set(:@script_hashes, nil)
104-
Configuration.instance_variable_set(:@style_hashes, nil)
105122
end
106123
end
107124
end

0 commit comments

Comments
 (0)