From bfe91618e2d633607f8e8cfeeda79f135992d86d Mon Sep 17 00:00:00 2001 From: compwron Date: Mon, 16 Nov 2026 00:21:35 -0800 Subject: [PATCH] Send password reset SMS only to the account's stored phone number The reset SMS was sent to the phone number typed into the form, so an email plus the requester's own number delivered a working reset link for someone else's account. Text only the stored number, and only when the submitted number matches it; the email is unaffected. Co-Authored-By: Claude Sonnet 5.5 --- app/controllers/users/passwords_controller.rb | 10 ++++++++- spec/requests/users/passwords_spec.rb | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) 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: ""}} }