From 9924ca88106fff34eb0ed105f394c20f9197a69c Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 27 Mar 2018 00:44:10 +0200 Subject: [PATCH] Fix UniqueUsernameValidator comparison Comparison was downcasing only one side, therefore if previously existing account had a non-lowercase spelling, it would be ignored when checking for duplicates. New rake task `mastodon:maintenance:find_duplicate_usernames` will help find constraint violations that might have occured from the presence of this bug. Bump version to 2.3.3 --- app/models/concerns/account_finder_concern.rb | 2 +- app/validators/unique_username_validator.rb | 2 +- lib/mastodon/version.rb | 2 +- lib/tasks/mastodon.rake | 18 ++++++++++++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/models/concerns/account_finder_concern.rb b/app/models/concerns/account_finder_concern.rb index 2e8a7fb37..6b7237e89 100644 --- a/app/models/concerns/account_finder_concern.rb +++ b/app/models/concerns/account_finder_concern.rb @@ -30,7 +30,7 @@ module AccountFinderConcern end def account - scoped_accounts.take + scoped_accounts.order(id: :asc).take end private diff --git a/app/validators/unique_username_validator.rb b/app/validators/unique_username_validator.rb index c76407b16..fb67105dd 100644 --- a/app/validators/unique_username_validator.rb +++ b/app/validators/unique_username_validator.rb @@ -6,7 +6,7 @@ class UniqueUsernameValidator < ActiveModel::Validator normalized_username = account.username.downcase.delete('.') - scope = Account.where(domain: nil, username: normalized_username) + scope = Account.where(domain: nil).where('lower(username) = ?', normalized_username) scope = scope.where.not(id: account.id) if account.persisted? account.errors.add(:username, :taken) if scope.exists? diff --git a/lib/mastodon/version.rb b/lib/mastodon/version.rb index 121c5c693..a6927eec3 100644 --- a/lib/mastodon/version.rb +++ b/lib/mastodon/version.rb @@ -13,7 +13,7 @@ module Mastodon end def patch - 2 + 3 end def pre diff --git a/lib/tasks/mastodon.rake b/lib/tasks/mastodon.rake index cf32b1495..18bdc347f 100644 --- a/lib/tasks/mastodon.rake +++ b/lib/tasks/mastodon.rake @@ -740,6 +740,24 @@ namespace :mastodon do LinkCrawlWorker.push_bulk status_ids end + desc 'Find case-insensitive username duplicates of local users' + task find_duplicate_usernames: :environment do + include RoutingHelper + + disable_log_stdout! + + duplicate_masters = Account.find_by_sql('SELECT * FROM accounts WHERE id IN (SELECT min(id) FROM accounts WHERE domain IS NULL GROUP BY lower(username) HAVING count(*) > 1)') + pastel = Pastel.new + + duplicate_masters.each do |account| + puts pastel.yellow("First of their name: ") + pastel.bold(account.username) + " (#{admin_account_url(account.id)})" + + Account.where('lower(username) = ?', account.username.downcase).where.not(id: account.id).each do |duplicate| + puts " " + pastel.red("Duplicate: ") + admin_account_url(duplicate.id) + end + end + end + desc 'Remove all home feed regeneration markers' task remove_regeneration_markers: :environment do keys = Redis.current.keys('account:*:regeneration')