From 79802c5556b7bc2caec2924ce0e8fb6c459bdb4c Mon Sep 17 00:00:00 2001 From: Sudeep Tarlekar Date: Mon, 28 Sep 2026 10:03:24 +0000 Subject: [PATCH] Fix server error on registration for wrong DOB (#40607) --- .../auth/registrations_controller.rb | 24 ++++++++++++++ .../auth/registrations_controller_spec.rb | 31 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/app/controllers/auth/registrations_controller.rb b/app/controllers/auth/registrations_controller.rb index b315b273d5..f14da9912f 100644 --- a/app/controllers/auth/registrations_controller.rb +++ b/app/controllers/auth/registrations_controller.rb @@ -29,6 +29,8 @@ class Auth::RegistrationsController < Devise::RegistrationsController def create super + rescue ActiveRecord::MultiparameterAssignmentErrors => e + handle_multiparameter_assignment_error(e.errors) end def update @@ -109,6 +111,28 @@ class Auth::RegistrationsController < Devise::RegistrationsController end end + def handle_multiparameter_assignment_error(errors) + build_resource(sign_up_params_without_invalid_multiparameter_attributes(errors)) + + errors.each do |attribute_error| + resource.errors.add(attribute_error.attribute, :invalid) + end + + clean_up_passwords resource + set_minimum_password_length + respond_with resource + end + + def sign_up_params_without_invalid_multiparameter_attributes(errors) + invalid_attributes = errors.map { |attribute_error| attribute_error.attribute.to_s } + + sign_up_params.to_h.reject do |key, _value| + invalid_attributes.any? do |attribute| + key.to_s == attribute || key.to_s.match?(/\A#{Regexp.escape(attribute)}\(\d+i\)\z/) + end + end + end + def determine_layout %w(edit update).include?(action_name) ? 'admin' : 'auth' end diff --git a/spec/controllers/auth/registrations_controller_spec.rb b/spec/controllers/auth/registrations_controller_spec.rb index d4caed4ab2..869c6a0016 100644 --- a/spec/controllers/auth/registrations_controller_spec.rb +++ b/spec/controllers/auth/registrations_controller_spec.rb @@ -283,6 +283,37 @@ RSpec.describe Auth::RegistrationsController do end end + context 'with an invalid date of birth' do + subject do + Setting.registrations_mode = 'open' + Setting.min_age = 16 + post :create, params: { + user: { + :account_attributes => { username: 'test' }, + :email => 'test@example.com', + :password => '12345678', + :password_confirmation => '12345678', + :agreement => 'true', + 'date_of_birth(1i)' => '2019', + 'date_of_birth(2i)' => '32', + 'date_of_birth(3i)' => '01', + }, + } + end + + it 'responds with an error message about the date of birth' do + expect { subject }.to_not raise_error + + expect(response).to have_http_status(:success) + expect(date_of_birth_error_text).to eq(I18n.t('errors.messages.invalid')) + expect(User.find_by(email: 'test@example.com')).to be_nil + end + + def date_of_birth_error_text + response.parsed_body.css('.user_date_of_birth .error').text + end + end + it_behaves_like 'registration mode based responses', :create end