Skip to content

Commit

Permalink
Return 200 on HEAD requests to not invalidate tokens
Browse files Browse the repository at this point in the history
Some email clients send preliminary requests to links to check for safety. We don't want
that to invalidate tokens.
  • Loading branch information
mikker committed Jun 19, 2023
1 parent 2f97a19 commit 336593e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/controllers/passwordless/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def create
# @see ControllerHelpers#sign_in
# @see ControllerHelpers#save_passwordless_redirect_location!
def show
# Some email clients will visit links in emails to check if they are
# safe. We don't want to sign in the user in that case.
return head(:ok) if request.head?

# Make it "slow" on purpose to make brute-force attacks more of a hassle
redirect_to_options = Passwordless.redirect_to_response_options.dup
BCrypt::Password.create(params[:token])
Expand Down
12 changes: 12 additions & 0 deletions test/controllers/passwordless/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,17 @@ class << User

Passwordless.restrict_token_reuse = default
end

test("responding to HEAD requests") do
user = User.create(email: "a@a")
passwordless_session = create_session_for(user)

token_path = "/users/sign_in/#{passwordless_session.token}"
head token_path

assert_equal 200, status
assert_equal token_path, path
assert_nil session[Helpers.session_key(user.class)]
end
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require "simplecov"
require "minitest/pride"
require "minitest"

SimpleCov.start do
add_filter("test/dummy")
Expand Down

0 comments on commit 336593e

Please sign in to comment.