Fix cross-domain redirects breaking origin-based security model

This commit is contained in:
Claire committed 2026-10-29 14:56:52 +01:00
1 parent d2ea2974a1
commit 4aedf08e51
14 files changed
+40 -34

No files matched your search

+12 -9
View File
@@ -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)
@@ -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)
@@ -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
@@ -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
@@ -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)
@@ -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)
@@ -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
@@ -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
@@ -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)
@@ -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
+7 -4
View File
@@ -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)
@@ -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?
@@ -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
+5 -5
View File
@@ -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