]> cat aescling's git repositories - mastodon.git/commitdiff
Dereference object URIs in Create and Update messages (#14359)
authorThibG <thib@sitedethib.com>
Wed, 22 Jul 2020 09:43:17 +0000 (11:43 +0200)
committerGitHub <noreply@github.com>
Wed, 22 Jul 2020 09:43:17 +0000 (11:43 +0200)
* Dereference object URIs in Create and Update messages

Fixes #14353

Signed-off-by: Thibaut Girka <thib@sitedethib.com>
* Refactor, and perform origin check *before* attempting to fetch object

Co-authored-by: Fire Demon <firedemon@creature.cafe>
app/lib/activitypub/activity.rb
app/lib/activitypub/activity/create.rb
app/lib/activitypub/activity/update.rb

index 0ce279d287321d5c7aa4e922c3db89cfc1403f64..ab946470b9f5c7e3ff0a74246803af8151068532 100644 (file)
@@ -157,6 +157,34 @@ class ActivityPub::Activity
     fetch_remote_original_status
   end
 
+  def dereference_object!
+    return unless @object.is_a?(String)
+    return if invalid_origin?(@object)
+
+    object = fetch_resource(@object, true, signed_fetch_account)
+    return unless object.present? && object.is_a?(Hash) && supported_context?(object)
+
+    @object = object
+  end
+
+  def signed_fetch_account
+    first_mentioned_local_account || first_local_follower
+  end
+
+  def first_mentioned_local_account
+    audience = (as_array(@json['to']) + as_array(@json['cc'])).uniq
+    local_usernames = audience.select { |uri| ActivityPub::TagManager.instance.local_uri?(uri) }
+                              .map { |uri| ActivityPub::TagManager.instance.uri_to_local_id(uri, :username) }
+
+    return if local_usernames.empty?
+
+    Account.local.where(username: local_usernames).first
+  end
+
+  def first_local_follower
+    @account.followers.local.first
+  end
+
   def follow_request_from_object
     @follow_request ||= FollowRequest.find_by(target_account: @account, uri: object_uri) unless object_uri.nil?
   end
index e81452e3cae83e398af501225db538be170163b4..08dd98e942d9a2227153605d8fb27311dc1acf93 100644 (file)
@@ -2,6 +2,8 @@
 
 class ActivityPub::Activity::Create < ActivityPub::Activity
   def perform
+    dereference_object!
+
     case @object['type']
     when 'EncryptedMessage'
       create_encrypted_message
index 70035325b65b4dfc02307db5ceceb0df33281ef7..018e2df549299b56644893e856e1097c675328d4 100644 (file)
@@ -4,6 +4,8 @@ class ActivityPub::Activity::Update < ActivityPub::Activity
   SUPPORTED_TYPES = %w(Application Group Organization Person Service).freeze
 
   def perform
+    dereference_object!
+
     if equals_or_includes_any?(@object['type'], SUPPORTED_TYPES)
       update_account
     elsif equals_or_includes_any?(@object['type'], %w(Question))