Add uniqueness constraint on Account uri (#39882)

This commit is contained in:
Claire authored and GitHub committed 2026-07-28 08:09:21 +00:00
1 parent 20c105c194
commit e0efc9683f
14 files changed
+236 -47

No files matched your search

+3 -12
View File
@@ -253,20 +253,11 @@ module Mastodon::CLI
desc 'fix-duplicates', 'Find duplicate remote accounts and merge them'
option :dry_run, type: :boolean
long_desc <<-LONG_DESC
Merge known remote accounts sharing an ActivityPub actor identifier.
Such duplicates can occur when a remote server admin misconfigures their
domain configuration.
This command is deprecated as of Mastodon v4.7.0.
LONG_DESC
def fix_duplicates
Account.remote.duplicate_uris.pluck(:uri).each do |uri|
say("Duplicates found for #{uri}")
begin
ActivityPub::FetchRemoteAccountService.new.call(uri) unless dry_run?
rescue => e
say("Error processing #{uri}: #{e}", :red)
end
end
# TODO: remove this after 4.7.0
say('This command is deprecated as Mastodon v4.7.0 migrations enforce ActivityPub actor identifier uniqueness', :yellow)
end
desc 'backup USERNAME', 'Request a backup for a user'
+35 -9
View File
@@ -164,11 +164,23 @@ namespace :tests do
exit(1)
end
unless Account.find_by(uri: 'https://social.example.com/alice').domain == 'example.com'
puts 'Duplicate accounts with same URI incorrectly deduplicated'
exit(1)
end
if Account.exists?(domain: 'example.com', username: %w(bogus1 bogus2 bogus3))
puts 'Bogus remote accounts not correctly purged'
exit(1)
end
puts 'No errors found. Database state is consistent with a successful migration process.'
end
desc 'Populate the database with test data for 3.3.0'
task populate_v3_3_0: :environment do # rubocop:disable Naming/VariableNumber
user_public_key = ActiveRecord::Base.connection.quote(OpenSSL::PKey::RSA.new(2048).public_key.to_pem)
ActiveRecord::Base.connection.execute(<<~SQL.squish)
INSERT INTO "webauthn_credentials"
(user_id, nickname, external_id, public_key, created_at, updated_at)
@@ -206,6 +218,19 @@ namespace :tests do
(token, application_id, scopes, resource_owner_id, created_at)
VALUES
('secret', 2, 'write:accounts read:me', 4, now());
/* Duplicate remote accounts
Before Mastodon v4.7.0, we had no uniqueness constraint on account `uri`, which lead to issues.
Furthermore, older tests caused remote accounts without protocol identifiers.
*/
INSERT INTO "accounts"
(id, username, domain, uri, private_key, public_key, created_at, updated_at, last_webfingered_at)
VALUES
(12, 'alice', 'social.example.com', 'https://social.example.com/alice', NULL, #{user_public_key}, '2021-01-01'::date, now(), '2021-01-09'::date),
(13, 'alice', 'example.com', 'https://social.example.com/alice', NULL, #{user_public_key}, '2021-01-10'::date, now(), '2021-01-11'::date),
(14, 'bogus1', 'example.com', '', NULL, '', now(), now(), now()),
(15, 'bogus2', 'example.com', '', NULL, '', now(), now(), now()),
(16, 'bogus3', 'example.com', '', NULL, '', now(), now(), now());
SQL
end
@@ -336,19 +361,19 @@ namespace :tests do
(2, 'user', NULL, #{user_private_key}, #{user_public_key}, now(), now());
INSERT INTO "accounts"
(id, username, domain, private_key, public_key, created_at, updated_at, remote_url, salmon_url)
(id, username, domain, uri, private_key, public_key, created_at, updated_at, remote_url, salmon_url)
VALUES
(3, 'remote', 'remote.com', NULL, #{remote_public_key}, now(), now(),
(3, 'remote', 'remote.com', 'https://remote.com/remote', NULL, #{remote_public_key}, now(), now(),
'https://remote.com/@remote', 'https://remote.com/salmon/1'),
(4, 'Remote', 'remote.com', NULL, #{remote_public_key}, now(), now(),
(4, 'Remote', 'remote.com', 'https://remote.com/Remote', NULL, #{remote_public_key}, now(), now(),
'https://remote.com/@Remote', 'https://remote.com/salmon/1'),
(5, 'REMOTE', 'Remote.com', NULL, #{remote_public_key2}, now() - interval '1 year', now() - interval '1 year',
(5, 'REMOTE', 'Remote.com', 'https://remote.com/stale/remote', NULL, #{remote_public_key2}, now() - interval '1 year', now() - interval '1 year',
'https://remote.com/stale/@REMOTE', 'https://remote.com/stale/salmon/1');
INSERT INTO "accounts"
(id, username, domain, private_key, public_key, created_at, updated_at, protocol, inbox_url, outbox_url, followers_url)
(id, username, domain, uri, private_key, public_key, created_at, updated_at, protocol, inbox_url, outbox_url, followers_url)
VALUES
(6, 'bob', 'ActivityPub.com', NULL, #{remote_public_key_ap}, now(), now(),
(6, 'bob', 'ActivityPub.com', 'https://activitypub.com/users/bob', NULL, #{remote_public_key_ap}, now(), now(),
1, 'https://activitypub.com/users/bob/inbox', 'https://activitypub.com/users/bob/outbox', 'https://activitypub.com/users/bob/followers');
INSERT INTO "accounts"
@@ -358,10 +383,11 @@ namespace :tests do
(8, 'pt_user', NULL, #{user_private_key}, #{user_public_key}, now(), now());
INSERT INTO "accounts"
(id, username, domain, private_key, public_key, created_at, updated_at, protocol, inbox_url, outbox_url, followers_url, suspended)
(id, username, domain, uri, private_key, public_key, created_at, updated_at, protocol, inbox_url, outbox_url, followers_url, suspended)
VALUES
(9, 'evil', 'activitypub.com', NULL, #{remote_public_key_ap}, now(), now(),
1, 'https://activitypub.com/users/evil/inbox', 'https://activitypub.com/users/evil/outbox',
(9, 'evil', 'activitypub.com', 'https://activitypub.com/users/evil', NULL,
#{remote_public_key_ap}, now(), now(), 1,
'https://activitypub.com/users/evil/inbox', 'https://activitypub.com/users/evil/outbox',
'https://activitypub.com/users/evil/followers', true);
-- users