]> cat aescling's git repositories - mastodon.git/commitdiff
Support more variations of ActivityPub keyId in signature (#4630)
authorEugen Rochko <eugen@zeonfederated.com>
Mon, 21 Aug 2017 20:57:34 +0000 (22:57 +0200)
committerGitHub <noreply@github.com>
Mon, 21 Aug 2017 20:57:34 +0000 (22:57 +0200)
- Tries to avoid performing HTTP request if the keyId is an actor URI
- Likewise if the URI is a fragment URI on top of actor URI
- Resolves public key, returns owner if the owner links back to the key

app/controllers/concerns/signature_verification.rb
app/helpers/jsonld_helper.rb
app/lib/activitypub/activity.rb
app/lib/activitypub/activity/accept.rb
app/lib/activitypub/activity/reject.rb
app/lib/activitypub/activity/undo.rb
app/lib/activitypub/tag_manager.rb
app/services/activitypub/fetch_remote_key_service.rb [new file with mode: 0644]

index aeb8da879e7e908ab1181a961ed975bcb3a651e8..4211283ed737a42e668abb24c40b3fdc94f80211 100644 (file)
@@ -98,7 +98,9 @@ module SignatureVerification
     if key_id.start_with?('acct:')
       ResolveRemoteAccountService.new.call(key_id.gsub(/\Aacct:/, ''))
     elsif !ActivityPub::TagManager.instance.local_uri?(key_id)
-      ActivityPub::FetchRemoteAccountService.new.call(key_id)
+      account   = ActivityPub::TagManager.instance.uri_to_resource(key_id, Account)
+      account ||= ActivityPub::FetchRemoteKeyService.new.call(key_id)
+      account
     end
   end
 end
index c750a703814a2522dbd18d5cd32d40f420bc2ff2..d8b3ddf18c59076c186611dfe731a512b7f1fc99 100644 (file)
@@ -9,6 +9,10 @@ module JsonLdHelper
     value.is_a?(Array) ? value.first : value
   end
 
+  def value_or_id(value)
+    value.is_a?(String) ? value : value['id']
+  end
+
   def supported_context?(json)
     equals_or_includes?(json['@context'], ActivityPub::TagManager::CONTEXT)
   end
@@ -20,7 +24,7 @@ module JsonLdHelper
   end
 
   def body_to_json(body)
-    body.nil? ? nil : Oj.load(body, mode: :strict)
+    body.is_a?(String) ? Oj.load(body, mode: :strict) : body
   rescue Oj::ParseError
     nil
   end
index f8de8060c4ef8b72ccf4df6f3d4cecbb418f9512..14e3ca784b67cce106c93d9018c967a49b86058a 100644 (file)
@@ -58,7 +58,7 @@ class ActivityPub::Activity
   end
 
   def object_uri
-    @object_uri ||= @object.is_a?(String) ? @object : @object['id']
+    @object_uri ||= value_or_id(@object)
   end
 
   def redis
index 44c432ae7436fb11a5e266ef64fd5b669ca83828..bd90c901944f08ed3ff4ce46b9578d20cc98d13a 100644 (file)
@@ -20,6 +20,6 @@ class ActivityPub::Activity::Accept < ActivityPub::Activity
   end
 
   def target_uri
-    @target_uri ||= @object['actor']
+    @target_uri ||= value_or_id(@object['actor'])
   end
 end
index 6a234994ef253bba0110a4e589e7887da8417365..d815feeb6c0d19e6fa7113cadf29296d75ec2cb9 100644 (file)
@@ -20,6 +20,6 @@ class ActivityPub::Activity::Reject < ActivityPub::Activity
   end
 
   def target_uri
-    @target_uri ||= @object['actor']
+    @target_uri ||= value_or_id(@object['actor'])
   end
 end
index 078e97ed491ee38410229c3ce472675966bf1cc6..097b1dba4811feb9051b07c126b77b2be97830de 100644 (file)
@@ -64,6 +64,6 @@ class ActivityPub::Activity::Undo < ActivityPub::Activity
   end
 
   def target_uri
-    @target_uri ||= @object['object'].is_a?(String) ? @object['object'] : @object['object']['id']
+    @target_uri ||= value_or_id(@object['object'])
   end
 end
index 855881612e43c39127c341b25721a5eb93148652..3c16006cb161fcadd914623b45f4554a1a47525f 100644 (file)
@@ -93,7 +93,7 @@ class ActivityPub::TagManager
     elsif ::TagManager.instance.local_id?(uri)
       klass.find_by(id: ::TagManager.instance.unique_tag_to_local_id(uri, klass.to_s))
     else
-      klass.find_by(uri: uri)
+      klass.find_by(uri: uri.split('#').first)
     end
   end
 end
diff --git a/app/services/activitypub/fetch_remote_key_service.rb b/app/services/activitypub/fetch_remote_key_service.rb
new file mode 100644 (file)
index 0000000..ebd6407
--- /dev/null
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+class ActivityPub::FetchRemoteKeyService < BaseService
+  include JsonLdHelper
+
+  # Returns account that owns the key
+  def call(uri, prefetched_json = nil)
+    @json = body_to_json(prefetched_json) || fetch_resource(uri)
+
+    return unless supported_context?(@json) && expected_type?
+    return find_account(uri, @json) if person?
+
+    @owner = fetch_resource(owner_uri)
+
+    return unless supported_context?(@owner) && confirmed_owner?
+
+    find_account(owner_uri, @owner)
+  end
+
+  private
+
+  def find_account(uri, prefetched_json)
+    account   = ActivityPub::TagManager.instance.uri_to_resource(uri, Account)
+    account ||= ActivityPub::FetchRemoteAccountService.new.call(uri, prefetched_json)
+    account
+  end
+
+  def expected_type?
+    person? || public_key?
+  end
+
+  def person?
+    @json['type'] == 'Person'
+  end
+
+  def public_key?
+    @json['publicKeyPem'].present? && @json['owner'].present?
+  end
+
+  def owner_uri
+    @owner_uri ||= value_or_id(@json['owner'])
+  end
+
+  def confirmed_owner?
+    @owner['type'] == 'Person' && value_or_id(@owner['publicKey']) == @json['id']
+  end
+end