Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of no csrf session token #4731

Merged
merged 1 commit into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/sidekiq/web/csrf_protection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ def valid_token?(env, giventoken)
end

sess = session(env)

# Checks that Rack::Session::Cookie did not return empty session
# object in case the digest verification failed
return false if sess.empty?

localtoken = sess[:csrf]

# Checks that Rack::Session::Cookie actualy contains the csrf toekn
return false if localtoken.nil?

# Rotate the session token after every use
sess[:csrf] = SecureRandom.base64(TOKEN_LENGTH)

Expand Down
15 changes: 15 additions & 0 deletions test/test_csrf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,19 @@ def test_empty_session_post
assert_equal 403, result[0]
assert_equal ["Forbidden"], result[2]
end

def test_empty_csrf_session_post
goodtoken = call(env) do |envy|
envy[:csrf_token]
end
assert goodtoken

# Make a POST without csrf session data and good token
result = call(env(:post, { "authenticity_token" => goodtoken }, { 'session_id' => 'foo' })) do
raise "shouldnt be called"
end
refute_nil result
assert_equal 403, result[0]
assert_equal ["Forbidden"], result[2]
end
end