2017-08-08 20:52:15 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-30 10:10:46 +01:00
|
|
|
class ActivityPub::InboxesController < ActivityPub::BaseController
|
2017-08-08 20:52:15 +01:00
|
|
|
include SignatureVerification
|
2019-03-20 16:20:16 +00:00
|
|
|
include JsonLdHelper
|
2019-07-08 11:03:45 +01:00
|
|
|
include AccountOwnedConcern
|
2017-08-08 20:52:15 +01:00
|
|
|
|
2021-01-08 11:26:29 +00:00
|
|
|
before_action :skip_unknown_actor_activity
|
2022-09-21 21:45:57 +01:00
|
|
|
before_action :require_actor_signature!
|
2019-10-24 21:45:35 +01:00
|
|
|
skip_before_action :authenticate_user!
|
2019-07-11 19:11:09 +01:00
|
|
|
|
2017-08-08 20:52:15 +01:00
|
|
|
def create
|
2019-07-11 19:11:09 +01:00
|
|
|
upgrade_account
|
Add follower synchronization mechanism (#14510)
* Add support for followers synchronization on the receiving end
Check the `collectionSynchronization` attribute on `Create` and `Announce`
activities and synchronize followers from provided collection if possible.
* Add tests for followers synchronization on the receiving end
* Add support for follower synchronization on the sender's end
* Add tests for the sending end
* Switch from AS attributes to HTTP header
Replace the custom `collectionSynchronization` ActivityStreams attribute by
an HTTP header (`X-AS-Collection-Synchronization`) with the same syntax as
the `Signature` header and the following fields:
- `collectionId` to specify which collection to synchronize
- `digest` for the SHA256 hex-digest of the list of followers known on the
receiving instance (where “receiving instance” is determined by accounts
sharing the same host name for their ActivityPub actor `id`)
- `url` of a collection that should be fetched by the instance actor
Internally, move away from the webfinger-based `domain` attribute and use
account `uri` prefix to group accounts.
* Add environment variable to disable followers synchronization
Since the whole mechanism relies on some new preconditions that, in some
extremely rare cases, might not be met, add an environment variable
(DISABLE_FOLLOWERS_SYNCHRONIZATION) to disable the mechanism altogether and
avoid followers being incorrectly removed.
The current conditions are:
1. all managed accounts' actor `id` and inbox URL have the same URI scheme and
netloc.
2. all accounts whose actor `id` or inbox URL share the same URI scheme and
netloc as a managed account must be managed by the same Mastodon instance
as well.
As far as Mastodon is concerned, breaking those preconditions require extensive
configuration changes in the reverse proxy and might also cause other issues.
Therefore, this environment variable provides a way out for people with highly
unusual configurations, and can be safely ignored for the overwhelming majority
of Mastodon administrators.
* Only set follower synchronization header on non-public statuses
This is to avoid unnecessary computations and allow Follow-related
activities to be handled by the usual codepath instead of going through
the synchronization mechanism (otherwise, any Follow/Undo/Accept activity
would trigger the synchronization mechanism even if processing the activity
itself would be enough to re-introduce synchronization)
* Change how ActivityPub::SynchronizeFollowersService handles follow requests
If the remote lists a local follower which we only know has sent a follow
request, consider the follow request as accepted instead of sending an Undo.
* Integrate review feeback
- rename X-AS-Collection-Synchronization to Collection-Synchronization
- various minor refactoring and code style changes
* Only select required fields when computing followers_hash
* Use actor URI rather than webfinger domain in synchronization endpoint
* Change hash computation to be a XOR of individual hashes
Makes it much easier to be memory-efficient, and avoid sorting discrepancy issues.
* Marginally improve followers_hash computation speed
* Further improve hash computation performances by using pluck_each
2020-10-21 17:04:09 +01:00
|
|
|
process_collection_synchronization
|
2019-07-11 19:11:09 +01:00
|
|
|
process_payload
|
|
|
|
head 202
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-01-08 11:26:29 +00:00
|
|
|
def skip_unknown_actor_activity
|
|
|
|
head 202 if unknown_affected_account?
|
2019-07-11 19:11:09 +01:00
|
|
|
end
|
|
|
|
|
2021-01-08 11:26:29 +00:00
|
|
|
def unknown_affected_account?
|
2019-03-20 16:20:16 +00:00
|
|
|
json = Oj.load(body, mode: :strict)
|
2021-01-08 11:26:29 +00:00
|
|
|
json.is_a?(Hash) && %w(Delete Update).include?(json['type']) && json['actor'].present? && json['actor'] == value_or_id(json['object']) && !Account.where(uri: json['actor']).exists?
|
2019-03-20 16:20:16 +00:00
|
|
|
rescue Oj::ParseError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2019-07-08 11:03:45 +01:00
|
|
|
def account_required?
|
|
|
|
params[:account_username].present?
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
|
2020-11-07 23:28:39 +00:00
|
|
|
def skip_temporary_suspension_response?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-08-08 20:52:15 +01:00
|
|
|
def body
|
2019-03-31 16:27:24 +01:00
|
|
|
return @body if defined?(@body)
|
2019-07-11 19:11:09 +01:00
|
|
|
|
|
|
|
@body = request.body.read
|
|
|
|
@body.force_encoding('UTF-8') if @body.present?
|
|
|
|
|
2019-03-31 16:27:24 +01:00
|
|
|
request.body.rewind if request.body.respond_to?(:rewind)
|
2019-07-11 19:11:09 +01:00
|
|
|
|
2019-03-31 16:27:24 +01:00
|
|
|
@body
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
|
2017-08-21 00:14:40 +01:00
|
|
|
def upgrade_account
|
2022-09-21 21:45:57 +01:00
|
|
|
if signed_request_account&.ostatus?
|
2017-09-03 00:11:23 +01:00
|
|
|
signed_request_account.update(last_webfingered_at: nil)
|
2018-01-22 13:25:09 +00:00
|
|
|
ResolveAccountWorker.perform_async(signed_request_account.acct)
|
2017-09-03 00:11:23 +01:00
|
|
|
end
|
|
|
|
|
2022-09-21 21:45:57 +01:00
|
|
|
DeliveryFailureTracker.reset!(signed_request_actor.inbox_url)
|
2017-08-21 00:14:40 +01:00
|
|
|
end
|
|
|
|
|
Add follower synchronization mechanism (#14510)
* Add support for followers synchronization on the receiving end
Check the `collectionSynchronization` attribute on `Create` and `Announce`
activities and synchronize followers from provided collection if possible.
* Add tests for followers synchronization on the receiving end
* Add support for follower synchronization on the sender's end
* Add tests for the sending end
* Switch from AS attributes to HTTP header
Replace the custom `collectionSynchronization` ActivityStreams attribute by
an HTTP header (`X-AS-Collection-Synchronization`) with the same syntax as
the `Signature` header and the following fields:
- `collectionId` to specify which collection to synchronize
- `digest` for the SHA256 hex-digest of the list of followers known on the
receiving instance (where “receiving instance” is determined by accounts
sharing the same host name for their ActivityPub actor `id`)
- `url` of a collection that should be fetched by the instance actor
Internally, move away from the webfinger-based `domain` attribute and use
account `uri` prefix to group accounts.
* Add environment variable to disable followers synchronization
Since the whole mechanism relies on some new preconditions that, in some
extremely rare cases, might not be met, add an environment variable
(DISABLE_FOLLOWERS_SYNCHRONIZATION) to disable the mechanism altogether and
avoid followers being incorrectly removed.
The current conditions are:
1. all managed accounts' actor `id` and inbox URL have the same URI scheme and
netloc.
2. all accounts whose actor `id` or inbox URL share the same URI scheme and
netloc as a managed account must be managed by the same Mastodon instance
as well.
As far as Mastodon is concerned, breaking those preconditions require extensive
configuration changes in the reverse proxy and might also cause other issues.
Therefore, this environment variable provides a way out for people with highly
unusual configurations, and can be safely ignored for the overwhelming majority
of Mastodon administrators.
* Only set follower synchronization header on non-public statuses
This is to avoid unnecessary computations and allow Follow-related
activities to be handled by the usual codepath instead of going through
the synchronization mechanism (otherwise, any Follow/Undo/Accept activity
would trigger the synchronization mechanism even if processing the activity
itself would be enough to re-introduce synchronization)
* Change how ActivityPub::SynchronizeFollowersService handles follow requests
If the remote lists a local follower which we only know has sent a follow
request, consider the follow request as accepted instead of sending an Undo.
* Integrate review feeback
- rename X-AS-Collection-Synchronization to Collection-Synchronization
- various minor refactoring and code style changes
* Only select required fields when computing followers_hash
* Use actor URI rather than webfinger domain in synchronization endpoint
* Change hash computation to be a XOR of individual hashes
Makes it much easier to be memory-efficient, and avoid sorting discrepancy issues.
* Marginally improve followers_hash computation speed
* Further improve hash computation performances by using pluck_each
2020-10-21 17:04:09 +01:00
|
|
|
def process_collection_synchronization
|
|
|
|
raw_params = request.headers['Collection-Synchronization']
|
2022-09-21 21:45:57 +01:00
|
|
|
return if raw_params.blank? || ENV['DISABLE_FOLLOWERS_SYNCHRONIZATION'] == 'true' || signed_request_account.nil?
|
Add follower synchronization mechanism (#14510)
* Add support for followers synchronization on the receiving end
Check the `collectionSynchronization` attribute on `Create` and `Announce`
activities and synchronize followers from provided collection if possible.
* Add tests for followers synchronization on the receiving end
* Add support for follower synchronization on the sender's end
* Add tests for the sending end
* Switch from AS attributes to HTTP header
Replace the custom `collectionSynchronization` ActivityStreams attribute by
an HTTP header (`X-AS-Collection-Synchronization`) with the same syntax as
the `Signature` header and the following fields:
- `collectionId` to specify which collection to synchronize
- `digest` for the SHA256 hex-digest of the list of followers known on the
receiving instance (where “receiving instance” is determined by accounts
sharing the same host name for their ActivityPub actor `id`)
- `url` of a collection that should be fetched by the instance actor
Internally, move away from the webfinger-based `domain` attribute and use
account `uri` prefix to group accounts.
* Add environment variable to disable followers synchronization
Since the whole mechanism relies on some new preconditions that, in some
extremely rare cases, might not be met, add an environment variable
(DISABLE_FOLLOWERS_SYNCHRONIZATION) to disable the mechanism altogether and
avoid followers being incorrectly removed.
The current conditions are:
1. all managed accounts' actor `id` and inbox URL have the same URI scheme and
netloc.
2. all accounts whose actor `id` or inbox URL share the same URI scheme and
netloc as a managed account must be managed by the same Mastodon instance
as well.
As far as Mastodon is concerned, breaking those preconditions require extensive
configuration changes in the reverse proxy and might also cause other issues.
Therefore, this environment variable provides a way out for people with highly
unusual configurations, and can be safely ignored for the overwhelming majority
of Mastodon administrators.
* Only set follower synchronization header on non-public statuses
This is to avoid unnecessary computations and allow Follow-related
activities to be handled by the usual codepath instead of going through
the synchronization mechanism (otherwise, any Follow/Undo/Accept activity
would trigger the synchronization mechanism even if processing the activity
itself would be enough to re-introduce synchronization)
* Change how ActivityPub::SynchronizeFollowersService handles follow requests
If the remote lists a local follower which we only know has sent a follow
request, consider the follow request as accepted instead of sending an Undo.
* Integrate review feeback
- rename X-AS-Collection-Synchronization to Collection-Synchronization
- various minor refactoring and code style changes
* Only select required fields when computing followers_hash
* Use actor URI rather than webfinger domain in synchronization endpoint
* Change hash computation to be a XOR of individual hashes
Makes it much easier to be memory-efficient, and avoid sorting discrepancy issues.
* Marginally improve followers_hash computation speed
* Further improve hash computation performances by using pluck_each
2020-10-21 17:04:09 +01:00
|
|
|
|
|
|
|
# Re-using the syntax for signature parameters
|
|
|
|
tree = SignatureParamsParser.new.parse(raw_params)
|
|
|
|
params = SignatureParamsTransformer.new.apply(tree)
|
|
|
|
|
|
|
|
ActivityPub::PrepareFollowersSynchronizationService.new.call(signed_request_account, params)
|
|
|
|
rescue Parslet::ParseFailed
|
|
|
|
Rails.logger.warn 'Error parsing Collection-Synchronization header'
|
|
|
|
end
|
|
|
|
|
2017-08-08 20:52:15 +01:00
|
|
|
def process_payload
|
2022-09-21 21:45:57 +01:00
|
|
|
ActivityPub::ProcessingWorker.perform_async(signed_request_actor.id, body, @account&.id, signed_request_actor.class.name)
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
end
|