Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[32748] Remove whitespace from login and mail

Oliver Günther93577fb31df9
diff --git a/Gemfile b/Gemfile
index 1db90f7413c..eae3883563c 100644
--- a/Gemfile
+++ b/Gemfile
@@ -60,6 +60,9 @@ gem 'typed_dag', '~> 2.0.2'
gem 'addressable', '~> 2.7.0'
+# Remove whitespace from model input
+gem "auto_strip_attributes", "~> 2.5"
+
# Provide timezone info for TZInfo used by AR
gem 'tzinfo-data', '~> 1.2019.3'
diff --git a/Gemfile.lock b/Gemfile.lock
index a5ec7f381b5..697cecb7289 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -296,6 +296,8 @@ GEM
rbtree3 (~> 0.5)
ast (2.4.0)
attr_required (1.0.1)
+ auto_strip_attributes (2.5.0)
+ activerecord (>= 4.0)
autoprefixer-rails (9.7.4)
execjs
awesome_nested_set (3.2.0)
@@ -967,6 +969,7 @@ DEPENDENCIES
acts_as_tree (~> 2.9.0)
addressable (~> 2.7.0)
airbrake (~> 9.4.3)
+ auto_strip_attributes (~> 2.5)
autoprefixer-rails (~> 9.7.4)
awesome_nested_set (~> 3.2.0)
aws-sdk-core (~> 3.91.0)
@@ -1125,4 +1128,4 @@ RUBY VERSION
ruby 2.6.5p114
BUNDLED WITH
- 2.0.2
+ 2.1.4
diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb
index 215d998eecc..d2e08cdf11c 100644
--- a/app/controllers/account_controller.rb
+++ b/app/controllers/account_controller.rb
@@ -373,7 +373,7 @@ class AccountController < ApplicationController
if OpenProject::Configuration.disable_password_login?
render_404
else
- password_authentication(params[:username], params[:password])
+ password_authentication(params[:username]&.strip, params[:password])
end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 6b10206db75..9eb47d5e5bc 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -131,6 +131,9 @@ class User < Principal
validates_confirmation_of :password, allow_nil: true
validates_inclusion_of :mail_notification, in: MAIL_NOTIFICATION_OPTIONS.map(&:first), allow_blank: true
+ auto_strip_attributes :login, nullify: false
+ auto_strip_attributes :mail, nullify: false
+
validate :login_is_not_special_value
validate :password_meets_requirements
diff --git a/db/migrate/20200325101528_strip_whitespace_from_user_attributes.rb b/db/migrate/20200325101528_strip_whitespace_from_user_attributes.rb
new file mode 100644
index 00000000000..f7d47fece44
--- /dev/null
+++ b/db/migrate/20200325101528_strip_whitespace_from_user_attributes.rb
@@ -0,0 +1,12 @@
+class StripWhitespaceFromUserAttributes < ActiveRecord::Migration[6.0]
+ def up
+ User.update_all('login = TRIM(login), mail = TRIM(mail)')
+ end
+
+ def down
+ warn <<~WARNING
+ We cannot restore leading and trailing white space in user login and mail attributes.
+ Please ensure you do not have logins with only leading or trailing white space differences.
+ WARNING
+ end
+end
diff --git a/spec/features/auth/login_spec.rb b/spec/features/auth/login_spec.rb
index c2d149efa65..18d1c87417a 100644
--- a/spec/features/auth/login_spec.rb
+++ b/spec/features/auth/login_spec.rb
@@ -67,6 +67,17 @@ describe 'Login', type: :feature do
password_confirmation: user_password)
end
+ context 'with leading and trailing space in login' do
+ it 'can still login' do
+ # first login
+ login_with(" #{user.login} ", user.password)
+
+ # on the my page
+ expect(page)
+ .to have_current_path my_page_path
+ end
+ end
+
context 'with force password change' do
let(:force_password_change) { true }
let(:first_login) { true }