diff --git a/app/controllers/users/passwords_controller.rb b/app/controllers/users/passwords_controller.rb index 2a7ec672a4..b2bed324d6 100644 --- a/app/controllers/users/passwords_controller.rb +++ b/app/controllers/users/passwords_controller.rb @@ -53,6 +53,9 @@ def send_password_reset_mail end def send_password_reset_sms + # Never text a reset link to a number the requester typed: only the account's stored number qualifies. + return unless submitted_phone_matches_account? + # for case where user enters ONLY a phone number, generate a new reset token to use; # otherwise, use the same reset token as sent by devise mailer @reset_token ||= @resource.generate_password_reset_token @@ -62,11 +65,16 @@ def send_password_reset_sms sms_params = { From: @resource.casa_org.twilio_phone_number, Body: password_reset_msg(@resource.display_name, @short_io_service.short_url), - To: @phone_number + To: @resource.phone_number } twilio_service.send_sms(sms_params) end + def submitted_phone_matches_account? + stored = @resource.phone_number.to_s.gsub(/\D/, "") + stored.present? && stored == @phone_number.to_s.gsub(/\D/, "") + end + def valid_params?(email, phone_number) return empty_fields_error if params_not_present(email, phone_number) diff --git a/spec/requests/users/passwords_spec.rb b/spec/requests/users/passwords_spec.rb index 647f755af2..99324254e3 100644 --- a/spec/requests/users/passwords_spec.rb +++ b/spec/requests/users/passwords_spec.rb @@ -87,6 +87,27 @@ end end + context "when the email belongs to one account and the phone number is someone else's" do + let(:params) { {user: {email: user.email, phone_number: "+13333333333"}} } + + it "does not text anyone a reset link, but still emails the account" do + expect_any_instance_of(User).to receive(:send_reset_password_instructions).once + request + expect(twillio_service_double).not_to have_received(:send_sms) + end + end + + context "when the submitted number is formatted differently from the stored one" do + let(:params) { {user: {email: user.email, phone_number: "+1 (222) 222-2222"}} } + + it "texts the stored number, never the submitted string" do + request + expect(twillio_service_double).to have_received(:send_sms).once.with( + {From: org.twilio_phone_number, Body: a_string_matching("reset_url"), To: user.phone_number} + ) + end + end + context "with invalid parameters" do let(:params) { {user: {email: "", phone_number: ""}} }