diff --git a/app/models/keypair.rb b/app/models/keypair.rb index 48dbc636b5..07cc5a6042 100644 --- a/app/models/keypair.rb +++ b/app/models/keypair.rb @@ -19,6 +19,7 @@ class Keypair < ApplicationRecord include Expireable + include JsonLdHelper self.inheritance_column = nil @@ -34,6 +35,7 @@ class Keypair < ApplicationRecord validates :uri, presence: true, uniqueness: true, if: -> { account.remote? } validates :uri, absence: true, if: -> { account.local? } + validate :validate_uri_origin, if: -> { account.remote? } validates :local_fragment, presence: true, uniqueness: { scope: :account_id }, format: { with: /\A#[A-Z0-9-]+\Z/i }, if: -> { account.local? } validates :local_fragment, absence: true, if: -> { account.remote? } @@ -130,4 +132,10 @@ class Keypair < ApplicationRecord ) end end + + private + + def validate_uri_origin + errors.add(:uri, :cross_origin) if non_matching_uri_hosts?(uri, account.uri) + end end diff --git a/app/services/activitypub/fetch_remote_key_service.rb b/app/services/activitypub/fetch_remote_key_service.rb index f25cc1d0a1..7ff6056149 100644 --- a/app/services/activitypub/fetch_remote_key_service.rb +++ b/app/services/activitypub/fetch_remote_key_service.rb @@ -45,6 +45,8 @@ class ActivityPub::FetchRemoteKeyService < BaseService end def keypair_from_actor_json(actor_uri, actor_json) + return if non_matching_uri_hosts?(actor_uri, @uri) + actor = find_actor(actor_uri, actor_json) return if actor.nil? diff --git a/app/services/activitypub/process_account_service.rb b/app/services/activitypub/process_account_service.rb index d192e9dde8..b52a44c501 100644 --- a/app/services/activitypub/process_account_service.rb +++ b/app/services/activitypub/process_account_service.rb @@ -397,6 +397,7 @@ class ActivityPub::ProcessAccountService < BaseService end key_id = value + next if non_matching_uri_hosts?(@account.uri, key_id) # Key is fetched without ID validation because of a GoToSocial bug value = fetch_resource_without_id_validation(key_id) @@ -405,7 +406,7 @@ class ActivityPub::ProcessAccountService < BaseService # Special handling for GoToSocial which returns the whole actor for the key ID value = first_of_value(value['publicKey']) if value.is_a?(Hash) && value.key?('publicKey') - next unless value['owner'] == @account.uri + next unless value.present? && value['owner'] == @account.uri key = value['publicKeyPem'] next if key.nil? @@ -431,10 +432,11 @@ class ActivityPub::ProcessAccountService < BaseService end key_id = value + next if non_matching_uri_hosts?(@account.uri, key_id) value = fetch_resource(key_id) - next unless value['type'] == 'Multikey' && value['controller'] == @account.uri + next unless value.present? && value['type'] == 'Multikey' && value['controller'] == @account.uri key_type, key = key_from_multikey(value['publicKeyMultibase']) next if key_type.nil? || key.nil? diff --git a/spec/fabricators/account_fabricator.rb b/spec/fabricators/account_fabricator.rb index 6a5218cee9..8c47d8f11d 100644 --- a/spec/fabricators/account_fabricator.rb +++ b/spec/fabricators/account_fabricator.rb @@ -17,8 +17,9 @@ Fabricator(:account) do indexable true # This is not strictly needed but this avoids generating multiple keys - # and, when `store_private_key` is passed, stores private keys for use in request specs keypairs do |attrs| + uri = attrs[:uri] ? "#{attrs[:uri]}#main-key" : "https://#{attrs[:domain]}/users/#{attrs[:username]}#main-key" if attrs[:domain].present? + if attrs[:legacy_keypair] || attrs[:public_key].present? [] else @@ -27,7 +28,7 @@ Fabricator(:account) do type: :rsa, public_key: SigningKeysHelpers::PUBLIC_RSA_TEST_KEY, private_key: attrs[:domain].blank? ? SigningKeysHelpers::PRIVATE_RSA_TEST_KEY : nil, - uri: attrs[:domain].nil? ? nil : "https://#{attrs[:domain]}/users/#{attrs[:username]}#main-key", + uri: uri, local_fragment: attrs[:domain].nil? ? '#main-key' : nil ), ] diff --git a/spec/requests/signature_verification_spec.rb b/spec/requests/signature_verification_spec.rb index 9556911901..3ed2095823 100644 --- a/spec/requests/signature_verification_spec.rb +++ b/spec/requests/signature_verification_spec.rb @@ -704,7 +704,7 @@ RSpec.describe 'signature verification concern' do end context 'with a known account' do - let!(:actor) { Fabricate(:account, domain: 'remote.domain', uri: 'https://remote.domain/users/bob') } + let!(:actor) { Fabricate(:account, domain: 'remote.domain', uri: 'https://remote.domain/users/bob', keypairs: []) } before do Fabricate(:keypair, account: actor, type: :ed25519, public_key: actor_keypair.public_to_pem, uri: 'https://remote.domain/users/bob#main-key')