Require a valid session token to resend a confirmation
authorTom Hughes <tom@compton.nu>
Mon, 20 Jul 2015 20:32:34 +0000 (21:32 +0100)
committerTom Hughes <tom@compton.nu>
Mon, 20 Jul 2015 20:32:34 +0000 (21:32 +0100)
Make user#confirm_resend require a valid token in the session
that matches the requested user, and ensure trying to login as
an unconfirmed user sets such a token.

Fixes #1010

app/controllers/user_controller.rb
test/controllers/user_controller_test.rb

index 8a5fa3f..0c25149 100644 (file)
@@ -318,16 +318,19 @@ class UserController < ApplicationController
     else
       user = User.find_by_display_name(params[:display_name])
 
-      redirect_to root_path if !user || user.active?
+      redirect_to root_path if user.nil? || user.active?
     end
   end
 
   def confirm_resend
-    if user = User.find_by_display_name(params[:display_name])
-      Notifier.signup_confirm(user, user.tokens.create).deliver_now
-      flash[:notice] = t "user.confirm_resend.success", :email => "your email" # user.email
-    else
+    user = User.find_by_display_name(params[:display_name])
+    token = UserToken.find_by_token(session[:token])
+
+    if user.nil? || token.nil? || token.user != user
       flash[:error] = t "user.confirm_resend.failure", :name => params[:display_name]
+    else
+      Notifier.signup_confirm(user, user.tokens.create).deliver_now
+      flash[:notice] = t "user.confirm_resend.success", :email => user.email
     end
 
     redirect_to :action => "login"
@@ -631,6 +634,8 @@ class UserController < ApplicationController
   ##
   #
   def unconfirmed_login(user)
+    session[:token] = user.tokens.create.token
+
     redirect_to :action => "confirm", :display_name => user.display_name
 
     session.delete(:remember_me)
index 03238ea..37b8a7f 100644 (file)
@@ -474,6 +474,8 @@ class UserControllerTest < ActionController::TestCase
   end
 
   def test_confirm_resend_success
+    session[:token] = users(:inactive_user).tokens.create.token
+
     assert_difference "ActionMailer::Base.deliveries.size", 1 do
       get :confirm_resend, :display_name => users(:inactive_user).display_name
     end
@@ -489,7 +491,17 @@ class UserControllerTest < ActionController::TestCase
     ActionMailer::Base.deliveries.clear
   end
 
-  def test_confirm_resend_failure
+  def test_confirm_resend_no_token
+    assert_no_difference "ActionMailer::Base.deliveries.size" do
+      get :confirm_resend, :display_name => users(:inactive_user).display_name
+    end
+
+    assert_response :redirect
+    assert_redirected_to login_path
+    assert_match "User Inactive User not found.", flash[:error]
+  end
+
+  def test_confirm_resend_unknown_user
     assert_no_difference "ActionMailer::Base.deliveries.size" do
       get :confirm_resend, :display_name => "No Such User"
     end