From 4aedf08e512f56cfcb89cecb9385161d06a1241e Mon Sep 17 00:00:00 2001 From: Claire Date: Wed, 28 Oct 2026 11:32:59 +0100 Subject: [PATCH] Fix cross-domain redirects breaking origin-based security model --- app/helpers/json_ld_helper.rb | 21 +++++++++++-------- .../fetch_featured_tags_collection_service.rb | 2 +- .../activitypub/fetch_remote_actor_service.rb | 2 +- ...etch_remote_featured_collection_service.rb | 2 +- .../activitypub/fetch_remote_key_service.rb | 4 ++-- .../activitypub/fetch_remote_poll_service.rb | 2 +- .../fetch_remote_status_service.rb | 6 +++--- .../process_featured_item_service.rb | 4 ++-- .../verify_featured_item_service.rb | 2 +- .../activitypub/verify_quote_service.rb | 2 +- app/services/fetch_resource_service.rb | 11 ++++++---- .../activitypub/fetch_all_replies_worker.rb | 4 ++-- .../repair_remote_collections_scheduler.rb | 2 +- spec/helpers/json_ld_helper_spec.rb | 10 ++++----- 14 files changed, 40 insertions(+), 34 deletions(-) diff --git a/app/helpers/json_ld_helper.rb b/app/helpers/json_ld_helper.rb index ad765e7e1a..680a9868df 100644 --- a/app/helpers/json_ld_helper.rb +++ b/app/helpers/json_ld_helper.rb @@ -220,19 +220,19 @@ module JsonLdHelper # Fetch the resource given by uri. # @param uri [String] - # @param id_is_known [Boolean] + # @param allow_redirects [Boolean] # @param on_behalf_of [nil, Account] # @param raise_on_error [Symbol<:all, :temporary, :none>] See {#fetch_resource_without_id_validation} for possible values - def fetch_resource(uri, id_is_known, on_behalf_of = nil, raise_on_error: :none, request_options: {}) - unless id_is_known - json = fetch_resource_without_id_validation(uri, on_behalf_of, raise_on_error: raise_on_error) + def fetch_resource(uri, allow_redirects: false, on_behalf_of: nil, raise_on_error: :none, request_options: {}) + if allow_redirects + json = fetch_resource_without_id_validation(uri, on_behalf_of: on_behalf_of, raise_on_error: raise_on_error) return if !json.is_a?(Hash) || unsupported_uri_scheme?(json['id']) uri = json['id'] end - json = fetch_resource_without_id_validation(uri, on_behalf_of, raise_on_error: raise_on_error, request_options: request_options) + json = fetch_resource_without_id_validation(uri, allow_redirects: false, on_behalf_of: on_behalf_of, raise_on_error: raise_on_error, request_options: request_options) json.present? && json['id'] == uri ? json : nil end @@ -241,28 +241,31 @@ module JsonLdHelper # If an error is raised, it contains the response and can be captured for handling like # # begin - # fetch_resource_without_id_validation(uri, nil, true) + # fetch_resource_without_id_validation(uri, raise_on_error: all) # rescue Mastodon::UnexpectedResponseError => e # e.response # end # # @param uri [String] # @param on_behalf_of [nil, Account] + # @param allow_redirects [Boolean] # @param raise_on_error [Symbol<:all, :temporary, :none>] # - +:all+ - raise if response code is not in the 2xx range # - +:temporary+ - raise if the response code is not an "unsalvageable error" like a 404 # (see {#response_error_unsalvageable} ) # - +:none+ - do not raise, return +nil+ - def fetch_resource_without_id_validation(uri, on_behalf_of = nil, raise_on_error: :none, request_options: {}) + def fetch_resource_without_id_validation(uri, allow_redirects: true, on_behalf_of: nil, raise_on_error: :none, request_options: {}) on_behalf_of ||= Account.representative + request_options = request_options.merge(follow: false) unless allow_redirects + build_request(uri, on_behalf_of, options: request_options).perform do |response| raise Mastodon::UnexpectedResponseError, response if !response_successful?(response) && ( raise_on_error == :all || (!response_error_unsalvageable?(response) && raise_on_error == :temporary) ) - body_to_json(response.body_with_limit) if response.code == 200 && valid_activitypub_content_type?(response) + body_to_json(response.body_with_limit) if response.code == 200 && valid_activitypub_content_type?(response) && (allow_redirects || response.uri.to_s == uri) end end @@ -329,7 +332,7 @@ module JsonLdHelper return collection_or_uri if collection_or_uri.is_a?(Hash) return if !reference_uri.nil? && non_matching_uri_hosts?(reference_uri, collection_or_uri) - fetch_resource_without_id_validation(collection_or_uri, on_behalf_of, raise_on_error: :temporary) + fetch_resource_without_id_validation(collection_or_uri, on_behalf_of: on_behalf_of, raise_on_error: :temporary) end def valid_activitypub_content_type?(response) diff --git a/app/services/activitypub/fetch_featured_tags_collection_service.rb b/app/services/activitypub/fetch_featured_tags_collection_service.rb index 26a26576dd..2a7fdd00b7 100644 --- a/app/services/activitypub/fetch_featured_tags_collection_service.rb +++ b/app/services/activitypub/fetch_featured_tags_collection_service.rb @@ -7,7 +7,7 @@ class ActivityPub::FetchFeaturedTagsCollectionService < BaseService return if url.blank? || account.suspended? || account.local? @account = account - @json = fetch_resource(url, true, local_follower) + @json = fetch_resource(url, on_behalf_of: local_follower) return unless supported_context?(@json) diff --git a/app/services/activitypub/fetch_remote_actor_service.rb b/app/services/activitypub/fetch_remote_actor_service.rb index f5f00ef120..c268361e3a 100644 --- a/app/services/activitypub/fetch_remote_actor_service.rb +++ b/app/services/activitypub/fetch_remote_actor_service.rb @@ -15,7 +15,7 @@ class ActivityPub::FetchRemoteActorService < BaseService @json = begin if prefetched_body.nil? - fetch_resource(uri, true) + fetch_resource(uri) else body_to_json(prefetched_body, compare_id: uri) end diff --git a/app/services/activitypub/fetch_remote_featured_collection_service.rb b/app/services/activitypub/fetch_remote_featured_collection_service.rb index 968dc6811e..9ac43674ba 100644 --- a/app/services/activitypub/fetch_remote_featured_collection_service.rb +++ b/app/services/activitypub/fetch_remote_featured_collection_service.rb @@ -5,7 +5,7 @@ class ActivityPub::FetchRemoteFeaturedCollectionService < BaseService def call(uri, request_id: nil, prefetched_body: nil, on_behalf_of: nil) json = if prefetched_body.nil? - fetch_resource(uri, true, on_behalf_of) + fetch_resource(uri, on_behalf_of: on_behalf_of) else body_to_json(prefetched_body, compare_id: uri) end diff --git a/app/services/activitypub/fetch_remote_key_service.rb b/app/services/activitypub/fetch_remote_key_service.rb index 378401c574..a14567cfa7 100644 --- a/app/services/activitypub/fetch_remote_key_service.rb +++ b/app/services/activitypub/fetch_remote_key_service.rb @@ -13,14 +13,14 @@ class ActivityPub::FetchRemoteKeyService < BaseService @suppress_errors = suppress_errors @uri = uri - @json = fetch_resource(uri, false) + @json = fetch_resource(uri, allow_redirects: true) raise Error, "Unable to fetch key JSON at #{uri}" if @json.nil? raise Error, "Unsupported JSON-LD context for document #{uri}" unless supported_context?(@json) || (supported_security_context?(@json) && @json['owner'].present? && !actor_type?) raise Error, "Unexpected object type for key #{uri}" unless expected_type? return keypair_from_actor_json(@json['id'], @json) if actor_type? - @owner = fetch_resource(owner_uri, true) + @owner = fetch_resource(owner_uri) raise Error, "Unable to fetch actor JSON #{owner_uri}" if @owner.nil? raise Error, "Unsupported JSON-LD context for document #{owner_uri}" unless supported_context?(@owner) diff --git a/app/services/activitypub/fetch_remote_poll_service.rb b/app/services/activitypub/fetch_remote_poll_service.rb index 41b9b2f0c9..9e34ff8600 100644 --- a/app/services/activitypub/fetch_remote_poll_service.rb +++ b/app/services/activitypub/fetch_remote_poll_service.rb @@ -4,7 +4,7 @@ class ActivityPub::FetchRemotePollService < BaseService include JsonLdHelper def call(poll, on_behalf_of = nil) - json = fetch_resource(poll.status.uri, true, on_behalf_of) + json = fetch_resource(poll.status.uri, on_behalf_of: on_behalf_of) return unless supported_context?(json) diff --git a/app/services/activitypub/fetch_remote_status_service.rb b/app/services/activitypub/fetch_remote_status_service.rb index 4ef5a6571e..8cc33aa9b9 100644 --- a/app/services/activitypub/fetch_remote_status_service.rb +++ b/app/services/activitypub/fetch_remote_status_service.rb @@ -14,7 +14,7 @@ class ActivityPub::FetchRemoteStatusService < BaseService @depth = depth || 0 @request_id = request_id || "#{Time.now.utc.to_i}-status-#{uri}" @json = if prefetched_body.nil? - fetch_status(uri, true, on_behalf_of) + fetch_status(uri, on_behalf_of) else body_to_json(prefetched_body, compare_id: uri) end @@ -83,9 +83,9 @@ class ActivityPub::FetchRemoteStatusService < BaseService equals_or_includes_any?(@json['type'], ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES) end - def fetch_status(uri, id_is_known, on_behalf_of = nil) + def fetch_status(uri, on_behalf_of = nil) begin - fetch_resource(uri, id_is_known, on_behalf_of, raise_on_error: :all) + fetch_resource(uri, on_behalf_of: on_behalf_of, raise_on_error: :all) rescue Mastodon::UnexpectedResponseError => e return unless e.response.code == 404 diff --git a/app/services/activitypub/process_featured_item_service.rb b/app/services/activitypub/process_featured_item_service.rb index 92e50e2031..08972378cd 100644 --- a/app/services/activitypub/process_featured_item_service.rb +++ b/app/services/activitypub/process_featured_item_service.rb @@ -10,7 +10,7 @@ class ActivityPub::ProcessFeaturedItemService def call(collection, uri_or_object, position: nil, request_id: nil) @collection = collection @request_id = request_id - @item_json = uri_or_object.is_a?(String) ? fetch_resource(uri_or_object, true) : uri_or_object + @item_json = uri_or_object.is_a?(String) ? fetch_resource(uri_or_object) : uri_or_object @actor_uri = value_or_id(@item_json['featuredObject']) @approval_uri = value_or_id(@item_json['featureAuthorization']) return if non_matching_uri_hosts?(@collection.uri, @item_json['id']) @@ -69,7 +69,7 @@ class ActivityPub::ProcessFeaturedItemService return false if local_actor_uri? return false if Account.exists?(uri: @actor_uri) - object_json = fetch_resource(@actor_uri, true, raise_on_error: :temporary) + object_json = fetch_resource(@actor_uri, raise_on_error: :temporary) object_json.nil? || (as_array(object_json['type']) & ActivityPub::FetchRemoteActorService::SUPPORTED_TYPES).empty? end diff --git a/app/services/activitypub/verify_featured_item_service.rb b/app/services/activitypub/verify_featured_item_service.rb index a626874049..7baedc35c4 100644 --- a/app/services/activitypub/verify_featured_item_service.rb +++ b/app/services/activitypub/verify_featured_item_service.rb @@ -5,7 +5,7 @@ class ActivityPub::VerifyFeaturedItemService def call(collection_item, approval_uri, request_id: nil) @collection_item = collection_item - @authorization = fetch_resource(approval_uri, true, raise_on_error: :temporary) + @authorization = fetch_resource(approval_uri, raise_on_error: :temporary) if @authorization.nil? @collection_item.update!(state: :rejected) diff --git a/app/services/activitypub/verify_quote_service.rb b/app/services/activitypub/verify_quote_service.rb index 095e416c70..27d116f282 100644 --- a/app/services/activitypub/verify_quote_service.rb +++ b/app/services/activitypub/verify_quote_service.rb @@ -52,7 +52,7 @@ class ActivityPub::VerifyQuoteService < BaseService def fetch_approval_object(uri, prefetched_body: nil) if prefetched_body.nil? - fetch_resource(uri, true, @quote.account.followers.local.first, raise_on_error: :temporary) + fetch_resource(uri, on_behalf_of: @quote.account.followers.local.first, raise_on_error: :temporary) else body_to_json(prefetched_body, compare_id: uri) end diff --git a/app/services/fetch_resource_service.rb b/app/services/fetch_resource_service.rb index eb3d000a1d..61a5e83bfe 100644 --- a/app/services/fetch_resource_service.rb +++ b/app/services/fetch_resource_service.rb @@ -22,11 +22,14 @@ class FetchResourceService < BaseService def process(url, terminal: false) @url = url - perform_request { |response| process_response(response, terminal:) } + perform_request(allow_redirects: !terminal) { |response| process_response(response, terminal:) } end - def perform_request(&block) - Request.new(:get, @url).tap do |request| + def perform_request(allow_redirects: true, &block) + options = {} + options[:follow] = false unless allow_redirects + + Request.new(:get, @url, **options).tap do |request| request.add_headers('Accept' => ACCEPT_HEADER) # In a real setting we want to sign all outgoing requests, @@ -50,7 +53,7 @@ class FetchResourceService < BaseService return unless supported_context?(json) && (equals_or_includes_any?(json['type'], ActivityPub::FetchRemoteActorService::SUPPORTED_TYPES) || expected_type?(json)) - if json['id'] != @url + if json['id'] != @url || json['id'] != response.uri.to_s return if terminal return process(json['id'], terminal: true) diff --git a/app/workers/activitypub/fetch_all_replies_worker.rb b/app/workers/activitypub/fetch_all_replies_worker.rb index 73f2caabf8..02017ff976 100644 --- a/app/workers/activitypub/fetch_all_replies_worker.rb +++ b/app/workers/activitypub/fetch_all_replies_worker.rb @@ -69,7 +69,7 @@ class ActivityPub::FetchAllRepliesWorker # @param parent_status [String, Hash] # status URI, or the prefetched body of the Note object def get_replies_uri(parent_status) - resource = parent_status.is_a?(Hash) ? parent_status : fetch_resource(parent_status, true) + resource = parent_status.is_a?(Hash) ? parent_status : fetch_resource(parent_status) resource&.fetch('replies', nil) rescue => e Rails.logger.info { "FetchAllRepliesWorker - #{@root_status.uri}: Caught exception while resolving replies URI #{parent_status}: #{e} - #{e.message}" } @@ -83,7 +83,7 @@ class ActivityPub::FetchAllRepliesWorker # # @param root_status_uri [String] def get_root_replies(root_status_uri, options = {}) - root_status_body = fetch_resource(root_status_uri, true) + root_status_body = fetch_resource(root_status_uri) return if root_status_body.nil? diff --git a/app/workers/scheduler/repair_remote_collections_scheduler.rb b/app/workers/scheduler/repair_remote_collections_scheduler.rb index 52ed03e6b7..5c6f469c0e 100644 --- a/app/workers/scheduler/repair_remote_collections_scheduler.rb +++ b/app/workers/scheduler/repair_remote_collections_scheduler.rb @@ -18,7 +18,7 @@ class Scheduler::RepairRemoteCollectionsScheduler successful = true affected_collections.find_each do |collection| - json = fetch_resource(collection.uri, true) + json = fetch_resource(collection.uri) if json.nil? || !json.is_a?(Hash) || !json.key?('attributedTo') successful = false next diff --git a/spec/helpers/json_ld_helper_spec.rb b/spec/helpers/json_ld_helper_spec.rb index e26e9cd637..fbabe5530b 100644 --- a/spec/helpers/json_ld_helper_spec.rb +++ b/spec/helpers/json_ld_helper_spec.rb @@ -82,26 +82,26 @@ RSpec.describe JsonLdHelper do end describe '#fetch_resource' do - context 'when the second argument is false' do + context 'when allow_redirects is true' do it 'returns resource even if the retrieved ID and the given URI does not match' do stub_request(:get, 'https://bob.test/').to_return(body: '{"id": "https://alice.test/"}', headers: { 'Content-Type': 'application/activity+json' }) stub_request(:get, 'https://alice.test/').to_return(body: '{"id": "https://alice.test/"}', headers: { 'Content-Type': 'application/activity+json' }) - expect(fetch_resource('https://bob.test/', false)).to eq({ 'id' => 'https://alice.test/' }) + expect(fetch_resource('https://bob.test/', allow_redirects: true)).to eq({ 'id' => 'https://alice.test/' }) end it 'returns nil if the object identified by the given URI and the object identified by the retrieved ID does not match' do stub_request(:get, 'https://mallory.test/').to_return(body: '{"id": "https://marvin.test/"}', headers: { 'Content-Type': 'application/activity+json' }) stub_request(:get, 'https://marvin.test/').to_return(body: '{"id": "https://alice.test/"}', headers: { 'Content-Type': 'application/activity+json' }) - expect(fetch_resource('https://mallory.test/', false)).to be_nil + expect(fetch_resource('https://mallory.test/', allow_redirects: true)).to be_nil end end - context 'when the second argument is true' do + context 'when allow_redirects is false' do it 'returns nil if the retrieved ID and the given URI does not match' do stub_request(:get, 'https://mallory.test/').to_return(body: '{"id": "https://alice.test/"}', headers: { 'Content-Type': 'application/activity+json' }) - expect(fetch_resource('https://mallory.test/', true)).to be_nil + expect(fetch_resource('https://mallory.test/', allow_redirects: false)).to be_nil end end end