Skip to content

Commit

Permalink
Fix handling of no session
Browse files Browse the repository at this point in the history
  • Loading branch information
szechyjs committed Nov 4, 2020
1 parent 7d78711 commit 616cf6c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
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.blank?

# 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

0 comments on commit 616cf6c

Please sign in to comment.