Skip to content

Commit

Permalink
Fixes zammad#5327 - Password validation errors containing %s are not …
Browse files Browse the repository at this point in the history
…interpolated
  • Loading branch information
mantas committed Sep 11, 2024
1 parent d45c789 commit 88ed68a
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ class ProfilePassword extends App.ControllerSubContent
error: (xhr, status, error) =>
return if xhr.status != 422

data = xhr.responseJSON

data = xhr.responseJSON
message = if data.notice
App.i18n.translateContent( data.notice[0], data.notice[1] )
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ class GettingStartedAdmin extends App.ControllerWizardFullScreen

fail: (settings, details) =>
@formEnable(e)
@form.showAlert(details.error_human || details.error || __('User could not be created.'))

message = if _.isArray(details.notice)
App.i18n.translateContent(details.notice[0], details.notice[1])
else
details.error_human || details.error || __('User could not be created.')

@form.showAlert(message)
)

relogin: (data, status, xhr) =>
Expand Down
8 changes: 7 additions & 1 deletion app/assets/javascripts/app/controllers/signup.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ class Signup extends App.ControllerFullPage
))
fail: (settings, details) =>
@formEnable(e)
@form.showAlert(details.error_human || details.error || __('User could not be created.'))

message = if _.isArray(details.notice)
App.i18n.translateContent(details.notice[0], details.notice[1])
else
details.error_human || details.error || __('User could not be created.')

@form.showAlert(message)
)

resend: (e) =>
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def password_reset_verify
render json: { message: 'failed' }, status: :ok
return
rescue PasswordPolicy::Error => e
render json: { message: 'failed', notice: [e] }, status: :ok
render json: { message: 'failed', notice: e.metadata }, status: :ok
return
end

Expand Down Expand Up @@ -602,7 +602,7 @@ def password_change
new_password: params[:password_new]
).execute
rescue PasswordPolicy::Error => e
render json: { message: 'failed', notice: [e.message] }, status: :unprocessable_entity
render json: { message: 'failed', notice: e.metadata }, status: :unprocessable_entity
return
rescue PasswordHash::Error
render json: { message: 'failed', notice: [__('The current password you provided is incorrect.')] }, status: :unprocessable_entity
Expand Down Expand Up @@ -997,7 +997,7 @@ def create_signup
begin
signup.execute
rescue PasswordPolicy::Error => e
render json: { error: e.message }, status: :unprocessable_entity
render json: { message: 'failed', notice: e.metadata }, status: :unprocessable_entity
return
rescue Service::CheckFeatureEnabled::FeatureDisabledError => e
raise Exceptions::UnprocessableEntity, e.message
Expand All @@ -1020,7 +1020,7 @@ def create_admin
)
render json: { message: 'ok' }, status: :created
rescue PasswordPolicy::Error => e
render json: { error: e.message }, status: :unprocessable_entity
render json: { message: 'failed', notice: e.metadata }, status: :unprocessable_entity
rescue Exceptions::MissingAttribute, Service::System::CheckSetup::SystemSetupError => e
raise Exceptions::UnprocessableEntity, e.message
end
Expand Down
22 changes: 11 additions & 11 deletions i18n/zammad.pot
Original file line number Diff line number Diff line change
Expand Up @@ -3337,7 +3337,7 @@ msgstr ""
msgid "Could not parse any sender attribute from the email. Checked fields:"
msgstr ""

#: app/assets/javascripts/app/controllers/signup.coffee:133
#: app/assets/javascripts/app/controllers/signup.coffee:139
msgid "Could not process your request"
msgstr ""

Expand All @@ -3349,7 +3349,7 @@ msgstr ""
msgid "Could not remove the two-factor authentication method for this user."
msgstr ""

#: app/assets/javascripts/app/controllers/_profile/password.coffee:198
#: app/assets/javascripts/app/controllers/_profile/password.coffee:197
msgid "Could not remove two-factor authentication method"
msgstr ""

Expand All @@ -3358,7 +3358,7 @@ msgstr ""
msgid "Could not remove two-factor authentication method."
msgstr ""

#: app/assets/javascripts/app/controllers/_profile/password.coffee:228
#: app/assets/javascripts/app/controllers/_profile/password.coffee:227
#: app/frontend/apps/desktop/pages/personal-setting/views/PersonalSettingTwoFactorAuth.vue:77
msgid "Could not set two-factor authentication method as default"
msgstr ""
Expand Down Expand Up @@ -5460,7 +5460,7 @@ msgstr ""
msgid "Email sent to \"%s\". Please let the user verify their email account."
msgstr ""

#: app/assets/javascripts/app/controllers/signup.coffee:118
#: app/assets/javascripts/app/controllers/signup.coffee:124
#: app/frontend/apps/desktop/pages/authentication/views/Signup.vue:91
msgid "Email sent to \"%s\". Please verify your email account."
msgstr ""
Expand Down Expand Up @@ -6555,7 +6555,7 @@ msgstr ""
msgid "Generate Token"
msgstr ""

#: app/assets/javascripts/app/controllers/_profile/password.coffee:241
#: app/assets/javascripts/app/controllers/_profile/password.coffee:240
#: app/assets/javascripts/app/views/profile/password.jst.eco:113
msgid "Generate recovery codes"
msgstr ""
Expand Down Expand Up @@ -11544,7 +11544,7 @@ msgstr ""
msgid "Remove selected values"
msgstr ""

#: app/assets/javascripts/app/controllers/_profile/password.coffee:177
#: app/assets/javascripts/app/controllers/_profile/password.coffee:176
msgid "Remove two-factor authentication"
msgstr ""

Expand Down Expand Up @@ -14299,7 +14299,7 @@ msgstr ""
msgid "The parameter 'mentionable_type' is invalid."
msgstr ""

#: app/assets/javascripts/app/controllers/_profile/password.coffee:136
#: app/assets/javascripts/app/controllers/_profile/password.coffee:135
#: app/assets/javascripts/app/controllers/password_reset_verify.coffee:143
msgid "The password could not be set. Please contact your administrator."
msgstr ""
Expand Down Expand Up @@ -16177,13 +16177,13 @@ msgstr ""
msgid "Two-factor authentication method could not be initiated."
msgstr ""

#: app/assets/javascripts/app/controllers/_profile/password.coffee:191
#: app/assets/javascripts/app/controllers/_profile/password.coffee:190
#: app/frontend/apps/desktop/components/TwoFactor/TwoFactorConfiguration/TwoFactorConfigurationSecurityKeys.vue:156
#: app/frontend/apps/desktop/pages/personal-setting/views/PersonalSettingTwoFactorAuth.vue:126
msgid "Two-factor authentication method was removed."
msgstr ""

#: app/assets/javascripts/app/controllers/_profile/password.coffee:221
#: app/assets/javascripts/app/controllers/_profile/password.coffee:220
#: app/frontend/apps/desktop/pages/personal-setting/views/PersonalSettingTwoFactorAuth.vue:98
msgid "Two-factor authentication method was set as default."
msgstr ""
Expand Down Expand Up @@ -16653,8 +16653,8 @@ msgstr ""
msgid "User authorization failed."
msgstr ""

#: app/assets/javascripts/app/controllers/getting_started/admin.coffee:104
#: app/assets/javascripts/app/controllers/signup.coffee:98
#: app/assets/javascripts/app/controllers/getting_started/admin.coffee:108
#: app/assets/javascripts/app/controllers/signup.coffee:102
#: app/frontend/apps/mobile/entities/user/composables/useUserCreate.ts:55
msgid "User could not be created."
msgstr ""
Expand Down
12 changes: 10 additions & 2 deletions lib/password_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
class PasswordPolicy
include ::Mixin::HasBackends

class PasswordPolicy::Error < StandardError; end
class PasswordPolicy::Error < StandardError
attr_reader :metadata

def initialize(metadata)
@metadata = metadata

super(metadata.try(:first))
end
end

attr_reader :password

Expand All @@ -16,7 +24,7 @@ def initialize(password)
def valid!
return if valid?

raise PasswordPolicy::Error, error.first
raise PasswordPolicy::Error, error
end

def valid?
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/user/password_reset_verify_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

it 'returns failure with notice', if: notice do
send_request
expect(json_response).to include({ 'message' => 'failed', 'notice' => [include(notice)] })
expect(json_response).to include({ 'message' => 'failed', 'notice' => include(start_with(notice)) })
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/requests/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
params = { email: '[email protected]', signup: true }
post '/api/v1/users', params: params, headers: headers, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response['error']).to be_truthy
expect(json_response['message']).to eq('failed')

# already existing user with enabled feature, pretend signup is successful
params = { email: '[email protected]', password: 'asd1ASDasd!', signup: true }
Expand Down Expand Up @@ -212,7 +212,7 @@
params = { email: '[email protected]', password: 'asdasdasdasd', signup: true }
post '/api/v1/users', params: params, headers: headers, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response['error']).to include('Invalid password')
expect(json_response['notice']).to include(include('Invalid password'))
end

it 'verified with no current user', authenticated_as: :admin do
Expand Down

0 comments on commit 88ed68a

Please sign in to comment.