This commit is contained in:
Matt Jankowski 2025-09-03 20:05:43 +00:00 committed by GitHub
commit 80e4367695
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 11 deletions

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
module User::Registration
extend ActiveSupport::Concern
REGISTRATION_ATTEMPT_WAIT_TIME = 3.seconds.freeze
included do
attribute :registration_form_time, :datetime
validate :validate_registration_wait, on: :create, if: :registration_form_time?
end
private
def validate_registration_wait
errors.add(:base, I18n.t('auth.too_fast')) if registration_form_time > REGISTRATION_ATTEMPT_WAIT_TIME.ago
end
end

View File

@ -64,6 +64,7 @@ class User < ApplicationRecord
include User::LdapAuthenticable
include User::Omniauthable
include User::PamAuthenticable
include User::Registration
devise :two_factor_authenticatable,
otp_secret_length: 32
@ -99,9 +100,8 @@ class User < ApplicationRecord
validates :agreement, acceptance: { allow_nil: false, accept: [true, 'true', '1'] }, on: :create
# Honeypot/anti-spam fields
attr_accessor :registration_form_time, :website, :confirm_password
attr_accessor :website, :confirm_password
validates_with RegistrationFormTimeValidator, on: :create
validates :website, absence: true, on: :create
validates :confirm_password, absence: true, on: :create
validates :date_of_birth, presence: true, date_of_birth: true, on: :create, if: -> { Setting.min_age.present? && !bypass_registration_checks? }

View File

@ -1,9 +0,0 @@
# frozen_string_literal: true
class RegistrationFormTimeValidator < ActiveModel::Validator
REGISTRATION_FORM_MIN_TIME = 3.seconds.freeze
def validate(user)
user.errors.add(:base, I18n.t('auth.too_fast')) if user.registration_form_time.present? && user.registration_form_time > REGISTRATION_FORM_MIN_TIME.ago
end
end

View File

@ -39,6 +39,15 @@ RSpec.describe User do
end
it { is_expected.to allow_value('admin@localhost').for(:email) }
context 'when registration form time is present' do
subject { Fabricate.build :user }
before { stub_const 'User::REGISTRATION_ATTEMPT_WAIT_TIME', 3.seconds }
it { is_expected.to allow_value(10.seconds.ago).for(:registration_form_time) }
it { is_expected.to_not allow_value(1.second.ago).for(:registration_form_time).against(:base) }
end
end
describe 'Normalizations' do