]> git.mxchange.org Git - friendica.git/commitdiff
Warnings/Errors reduced - improved relay processing
authorMichael <heluecht@pirati.ca>
Sat, 27 Aug 2022 08:08:58 +0000 (08:08 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 27 Aug 2022 08:08:58 +0000 (08:08 +0000)
src/Model/GServer.php
src/Module/Api/Mastodon/PushSubscription.php
src/Protocol/ActivityPub/Receiver.php
src/Protocol/OStatus.php
src/Util/HTTPSignature.php
src/Worker/UpdateContacts.php

index d0993141a809536cc1bc96686a9eb650dd0cf1a1..08cefba56c244793718aef27540c947965597ff4 100644 (file)
@@ -1330,7 +1330,7 @@ class GServer
                        return false;
                }
 
-               $xrd = XML::parseString($curlResult->getBody());
+               $xrd = XML::parseString($curlResult->getBody(), true);
                if (!is_object($xrd)) {
                        return false;
                }
index dc07ab55aa134e8efaa9c04600f9dc7887fb6a4e..945ca639203ee547a3e1c652729c5d74132ca80d 100644 (file)
@@ -50,13 +50,13 @@ class PushSubscription extends BaseApi
                        'endpoint'                      => $request['subscription']['endpoint'] ?? '',
                        'pubkey'                        => $request['subscription']['keys']['p256dh'] ?? '',
                        'secret'                        => $request['subscription']['keys']['auth'] ?? '',
-                       Notification::TYPE_FOLLOW       => $request['data']['alerts'][Notification::TYPE_FOLLOW] ?? false,
-                       Notification::TYPE_LIKE         => $request['data']['alerts'][Notification::TYPE_LIKE] ?? false,
-                       Notification::TYPE_RESHARE      => $request['data']['alerts'][Notification::TYPE_RESHARE] ?? false,
-                       Notification::TYPE_MENTION      => $request['data']['alerts'][Notification::TYPE_MENTION] ?? false,
-                       Notification::TYPE_POLL         => $request['data']['alerts'][Notification::TYPE_POLL] ?? false,
-                       Notification::TYPE_INTRODUCTION => $request['data']['alerts'][Notification::TYPE_INTRODUCTION] ?? false,
-                       Notification::TYPE_POST         => $request['data']['alerts'][Notification::TYPE_POST] ?? false,
+                       Notification::TYPE_FOLLOW       => filter_var($request['data']['alerts'][Notification::TYPE_FOLLOW] ?? false, FILTER_VALIDATE_BOOLEAN),
+                       Notification::TYPE_LIKE         => filter_var($request['data']['alerts'][Notification::TYPE_LIKE] ?? false, FILTER_VALIDATE_BOOLEAN),
+                       Notification::TYPE_RESHARE      => filter_var($request['data']['alerts'][Notification::TYPE_RESHARE] ?? false, FILTER_VALIDATE_BOOLEAN),
+                       Notification::TYPE_MENTION      => filter_var($request['data']['alerts'][Notification::TYPE_MENTION] ?? false, FILTER_VALIDATE_BOOLEAN),
+                       Notification::TYPE_POLL         => filter_var($request['data']['alerts'][Notification::TYPE_POLL] ?? false, FILTER_VALIDATE_BOOLEAN),
+                       Notification::TYPE_INTRODUCTION => filter_var($request['data']['alerts'][Notification::TYPE_INTRODUCTION] ?? false, FILTER_VALIDATE_BOOLEAN),
+                       Notification::TYPE_POST         => filter_var($request['data']['alerts'][Notification::TYPE_POST] ?? false, FILTER_VALIDATE_BOOLEAN),
                ];
 
                $ret = Subscription::replace($subscription);
index 8c68bcba60df9f88ec50be211e47050c2fe02ce5..10ff58b4bd231504b32c943bf624cf2439170f86 100644 (file)
@@ -113,9 +113,16 @@ class Receiver
                        APContact::unmarkForArchival($apcontact);
                }
 
+               $sig_contact = HTTPSignature::getKeyIdContact($header);
+               if (APContact::isRelay($sig_contact)) {
+                       Logger::info('Message from a relay', ['url' => $sig_contact['url']]);
+                       self::processRelayPost($ldactivity, $sig_contact['url']);
+                       return;
+               }
+
                $http_signer = HTTPSignature::getSigner($body, $header);
                if ($http_signer === false) {
-                       Logger::warning('Invalid HTTP signature, message will be discarded.');
+                       Logger::warning('Invalid HTTP signature, message will be discarded.', ['uid' => $uid, 'actor' => $actor, 'header' => $header, 'body' => $body]);
                        return;
                } elseif (empty($http_signer)) {
                        Logger::info('Signer is a tombstone. The message will be discarded, the signer account is deleted.');
@@ -170,18 +177,43 @@ class Receiver
        {
                $type = JsonLD::fetchElement($activity, '@type');
                if (!$type) {
-                       Logger::info('Empty type', ['activity' => $activity, 'actor' => $actor]);
+                       Logger::notice('Empty type', ['activity' => $activity, 'actor' => $actor]);
                        return;
                }
 
-               if ($type != 'as:Announce') {
-                       Logger::info('Not an announcement', ['activity' => $activity, 'actor' => $actor]);
-                       return;
-               }
+               $object_type = JsonLD::fetchElement($activity, 'as:object', '@type') ?? '';
 
                $object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
                if (empty($object_id)) {
-                       Logger::info('No object id found', ['activity' => $activity, 'actor' => $actor]);
+                       Logger::notice('No object id found', ['type' => $type, 'object_type' => $object_type, 'actor' => $actor, 'activity' => $activity]);
+                       return;
+               }
+
+               $handle = ($type == 'as:Announce');
+
+               if (!$handle && in_array($type, ['as:Create', 'as:Update'])) {
+                       $handle = in_array($object_type, self::CONTENT_TYPES);
+               }
+
+               if (!$handle) {
+                       $trust_source = false;
+                       $object_data = self::prepareObjectData($activity, 0, false, $trust_source);
+
+                       if (!$trust_source) {
+                               Logger::notice('Activity trust could not be achieved.',  ['type' => $type, 'object_type' => $object_type, 'object_id' => $object_id, 'actor' => $actor, 'activity' => $activity]);
+                               return;
+                       }
+
+                       if (empty($object_data)) {
+                               Logger::notice('No object data found', ['type' => $type, 'object_type' => $object_type, 'object_id' => $object_id, 'actor' => $actor, 'activity' => $activity]);
+                               return;
+                       }
+       
+                       if (self::routeActivities($object_data, $type, true)) {
+                               Logger::debug('Handled activity', ['type' => $type, 'object_type' => $object_type, 'object_id' => $object_id, 'actor' => $actor]);
+                       } else {
+                               Logger::info('Unhandled activity', ['type' => $type, 'object_type' => $object_type, 'object_id' => $object_id, 'actor' => $actor, 'activity' => $activity]);
+                       }
                        return;
                }
 
@@ -196,7 +228,7 @@ class Receiver
                        return;
                }
 
-               Logger::info('Got relayed message id', ['id' => $object_id, 'actor' => $actor]);
+               Logger::debug('Got relayed message id', ['id' => $object_id, 'actor' => $actor]);
 
                $item_id = Item::searchByLink($object_id);
                if ($item_id) {
index 6531d75af1d8c765af170f3e02d5f682c8564317..3758b0fe644c1f139a6902e988d9a4f5a7724abf 100644 (file)
@@ -1169,10 +1169,6 @@ class OStatus
                                ];
                                XML::addElement($doc, $root, 'link', '', $attributes);
                                break;
-
-                       default:
-                               Logger::warning('Unsupported type', ['type' => $siteinfo['type'], 'url' => $siteinfo['url'] ?? '']);
-                               break;
                }
 
                if (!DI::config()->get('system', 'ostatus_not_attach_preview') && ($siteinfo['type'] != 'photo') && isset($siteinfo['image'])) {
index f4f0a4e85cc5b48fe9345f73926a18ce5d300871..225a2bcf20f16081b97d7f7f392311aeb75ebad9 100644 (file)
@@ -485,6 +485,31 @@ class HTTPSignature
                return $curlResult;
        }
 
+       /**
+        * Fetch the apcontact entry of the keyId in the given header
+        *
+        * @param array $http_headers
+        *
+        * @return array APContact entry
+        */
+       public static function getKeyIdContact(array $http_headers): array
+       {
+               if (empty($http_headers['HTTP_SIGNATURE'])) {
+                       Logger::debug('No HTTP_SIGNATURE header', ['header' => $http_headers]);
+                       return [];
+               }
+
+               $sig_block = self::parseSigHeader($http_headers['HTTP_SIGNATURE']);
+
+               if (empty($sig_block['keyId'])) {
+                       Logger::debug('No keyId', ['sig_block' => $sig_block]);
+                       return [];
+               }
+
+               $url = (strpos($sig_block['keyId'], '#') ? substr($sig_block['keyId'], 0, strpos($sig_block['keyId'], '#')) : $sig_block['keyId']);
+               return APContact::getByURL($url);
+       }
+
        /**
         * Gets a signer from a given HTTP request
         *
index e6469fe19e0a1ae8dca8fbd254334abb7c6ce556..31426e79f22245c9ef65210f5d4a49b1d3d29e6b 100644 (file)
@@ -55,7 +55,7 @@ class UpdateContacts
                        $condition = array_merge(['local-data' => true], $condition);
                }
 
-               $condition = array_merge(["`next-update` < ?", DateTimeFormat::utcNow()], $condition);
+               $condition = DBA::mergeConditions(["`next-update` < ?", DateTimeFormat::utcNow()], $condition);
                $contacts = DBA::select('contact', ['id'], $condition, ['order' => ['next-update'], 'limit' => $limit]);
                $count = 0;
                while ($contact = DBA::fetch($contacts)) {