Prevent use locale with empty string (#18543)

Somehow user's locale could be an empty string, And empty string itself
are treated as true value.
pull/18556/head
Jeong Arm 2022-05-28 21:32:08 +09:00 committed by GitHub
parent 0ac6e5d434
commit fed7380e9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 20 deletions

View File

@ -18,7 +18,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.unconfirmed_email.presence || @resource.email, mail to: @resource.unconfirmed_email.presence || @resource.email,
subject: I18n.t(@resource.pending_reconfirmation? ? 'devise.mailer.reconfirmation_instructions.subject' : 'devise.mailer.confirmation_instructions.subject', instance: @instance), subject: I18n.t(@resource.pending_reconfirmation? ? 'devise.mailer.reconfirmation_instructions.subject' : 'devise.mailer.confirmation_instructions.subject', instance: @instance),
template_name: @resource.pending_reconfirmation? ? 'reconfirmation_instructions' : 'confirmation_instructions' template_name: @resource.pending_reconfirmation? ? 'reconfirmation_instructions' : 'confirmation_instructions'
@ -32,7 +32,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.reset_password_instructions.subject') mail to: @resource.email, subject: I18n.t('devise.mailer.reset_password_instructions.subject')
end end
end end
@ -43,7 +43,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.password_change.subject') mail to: @resource.email, subject: I18n.t('devise.mailer.password_change.subject')
end end
end end
@ -54,7 +54,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.email_changed.subject') mail to: @resource.email, subject: I18n.t('devise.mailer.email_changed.subject')
end end
end end
@ -65,7 +65,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.two_factor_enabled.subject') mail to: @resource.email, subject: I18n.t('devise.mailer.two_factor_enabled.subject')
end end
end end
@ -76,7 +76,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.two_factor_disabled.subject') mail to: @resource.email, subject: I18n.t('devise.mailer.two_factor_disabled.subject')
end end
end end
@ -87,7 +87,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.two_factor_recovery_codes_changed.subject') mail to: @resource.email, subject: I18n.t('devise.mailer.two_factor_recovery_codes_changed.subject')
end end
end end
@ -98,7 +98,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_enabled.subject') mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_enabled.subject')
end end
end end
@ -109,7 +109,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_disabled.subject') mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_disabled.subject')
end end
end end
@ -121,7 +121,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_credential.added.subject') mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_credential.added.subject')
end end
end end
@ -133,7 +133,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_credential.deleted.subject') mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_credential.deleted.subject')
end end
end end
@ -144,7 +144,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('user_mailer.welcome.subject') mail to: @resource.email, subject: I18n.t('user_mailer.welcome.subject')
end end
end end
@ -156,7 +156,7 @@ class UserMailer < Devise::Mailer
return unless @resource.active_for_authentication? return unless @resource.active_for_authentication?
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('user_mailer.backup_ready.subject') mail to: @resource.email, subject: I18n.t('user_mailer.backup_ready.subject')
end end
end end
@ -167,7 +167,7 @@ class UserMailer < Devise::Mailer
@instance = Rails.configuration.x.local_domain @instance = Rails.configuration.x.local_domain
@statuses = @warning.statuses.includes(:account, :preloadable_poll, :media_attachments, active_mentions: [:account]) @statuses = @warning.statuses.includes(:account, :preloadable_poll, :media_attachments, active_mentions: [:account])
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t("user_mailer.warning.subject.#{@warning.action}", acct: "@#{user.account.local_username_and_domain}") mail to: @resource.email, subject: I18n.t("user_mailer.warning.subject.#{@warning.action}", acct: "@#{user.account.local_username_and_domain}")
end end
end end
@ -177,7 +177,7 @@ class UserMailer < Devise::Mailer
@instance = Rails.configuration.x.local_domain @instance = Rails.configuration.x.local_domain
@appeal = appeal @appeal = appeal
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('user_mailer.appeal_approved.subject', date: l(@appeal.created_at)) mail to: @resource.email, subject: I18n.t('user_mailer.appeal_approved.subject', date: l(@appeal.created_at))
end end
end end
@ -187,7 +187,7 @@ class UserMailer < Devise::Mailer
@instance = Rails.configuration.x.local_domain @instance = Rails.configuration.x.local_domain
@appeal = appeal @appeal = appeal
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('user_mailer.appeal_rejected.subject', date: l(@appeal.created_at)) mail to: @resource.email, subject: I18n.t('user_mailer.appeal_rejected.subject', date: l(@appeal.created_at))
end end
end end
@ -200,8 +200,14 @@ class UserMailer < Devise::Mailer
@detection = Browser.new(user_agent) @detection = Browser.new(user_agent)
@timestamp = timestamp.to_time.utc @timestamp = timestamp.to_time.utc
I18n.with_locale(@resource.locale || I18n.default_locale) do I18n.with_locale(locale) do
mail to: @resource.email, subject: I18n.t('user_mailer.suspicious_sign_in.subject') mail to: @resource.email, subject: I18n.t('user_mailer.suspicious_sign_in.subject')
end end
end end
private
def locale
@resource.locale.presence || I18n.default_locale
end
end end

View File

@ -47,7 +47,7 @@ class MoveWorker
def copy_account_notes! def copy_account_notes!
AccountNote.where(target_account: @source_account).find_each do |note| AccountNote.where(target_account: @source_account).find_each do |note|
text = I18n.with_locale(note.account.user&.locale || I18n.default_locale) do text = I18n.with_locale(note.account.user&.locale.presence || I18n.default_locale) do
I18n.t('move_handler.copy_account_note_text', acct: @source_account.acct) I18n.t('move_handler.copy_account_note_text', acct: @source_account.acct)
end end
@ -90,7 +90,7 @@ class MoveWorker
def add_account_note_if_needed!(account, id) def add_account_note_if_needed!(account, id)
unless AccountNote.where(account: account, target_account: @target_account).exists? unless AccountNote.where(account: account, target_account: @target_account).exists?
text = I18n.with_locale(account.user&.locale || I18n.default_locale) do text = I18n.with_locale(account.user&.locale.presence || I18n.default_locale) do
I18n.t(id, acct: @source_account.acct) I18n.t(id, acct: @source_account.acct)
end end
AccountNote.create!(account: account, target_account: @target_account, comment: text) AccountNote.create!(account: account, target_account: @target_account, comment: text)

View File

@ -51,7 +51,7 @@ class Web::PushNotificationWorker
private private
def push_notification_json def push_notification_json
json = I18n.with_locale(@subscription.locale || I18n.default_locale) do json = I18n.with_locale(@subscription.locale.presence || I18n.default_locale) do
ActiveModelSerializers::SerializableResource.new( ActiveModelSerializers::SerializableResource.new(
@notification, @notification,
serializer: Web::NotificationSerializer, serializer: Web::NotificationSerializer,