Don't use compacted data when parsing accounts

This commit is contained in:
Michael 2021-05-16 16:41:07 +00:00
parent a4a2f4616c
commit 6f2fcd2859
3 changed files with 55 additions and 42 deletions

View file

@ -165,11 +165,10 @@ class APContact
return $fetched_contact; return $fetched_contact;
} }
$compacted = JsonLD::compact($data); if (empty($data['id'])) {
if (empty($compacted['@id'])) {
return $fetched_contact; return $fetched_contact;
} }
// Detect multiple fast repeating request to the same address // Detect multiple fast repeating request to the same address
// See https://github.com/friendica/friendica/issues/9303 // See https://github.com/friendica/friendica/issues/9303
$cachekey = 'apcontact:getByURL:' . $url; $cachekey = 'apcontact:getByURL:' . $url;
@ -180,40 +179,39 @@ class APContact
DI::cache()->set($cachekey, System::callstack(20), Duration::FIVE_MINUTES); DI::cache()->set($cachekey, System::callstack(20), Duration::FIVE_MINUTES);
} }
$apcontact['url'] = $compacted['@id']; $apcontact['url'] = $data['id'];
$apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid', '@value'); $apcontact['uuid'] = JsonLD::fetchElement($data, 'diaspora:guid');
$apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type')); $apcontact['type'] = JsonLD::fetchElement($data, 'type');
$apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id'); $apcontact['following'] = JsonLD::fetchElement($data, 'following');
$apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id'); $apcontact['followers'] = JsonLD::fetchElement($data, 'followers');
$apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id'); $apcontact['inbox'] = JsonLD::fetchElement($data, 'inbox');
self::unarchiveInbox($apcontact['inbox'], false); self::unarchiveInbox($apcontact['inbox'], false);
$apcontact['outbox'] = JsonLD::fetchElement($data, 'outbox');
$apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
$apcontact['sharedinbox'] = ''; $apcontact['sharedinbox'] = '';
if (!empty($compacted['as:endpoints'])) { if (!empty($data['endpoints'])) {
$apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id'); $apcontact['sharedinbox'] = JsonLD::fetchElement($data['endpoints'], 'sharedInbox');
self::unarchiveInbox($apcontact['sharedinbox'], true); self::unarchiveInbox($apcontact['sharedinbox'], true);
} }
$apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername', '@value') ?? ''; $apcontact['nick'] = JsonLD::fetchElement($data, 'preferredUsername') ?? '';
$apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name', '@value'); $apcontact['name'] = JsonLD::fetchElement($data, 'name');
if (empty($apcontact['name'])) { if (empty($apcontact['name'])) {
$apcontact['name'] = $apcontact['nick']; $apcontact['name'] = $apcontact['nick'];
} }
$apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary', '@value')); $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($data, 'summary'));
$apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id'); $apcontact['photo'] = JsonLD::fetchElement($data, 'icon');
if (is_array($apcontact['photo']) || !empty($compacted['as:icon']['as:url']['@id'])) { if (is_array($apcontact['photo']) || !empty($data['icon']['url'])) {
$apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id'); $apcontact['photo'] = JsonLD::fetchElement($data['icon'], 'url');
} }
if (empty($apcontact['alias'])) { if (empty($apcontact['alias'])) {
$apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id'); $apcontact['alias'] = JsonLD::fetchElement($data, 'url');
if (is_array($apcontact['alias'])) { if (is_array($apcontact['alias'])) {
$apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id'); $apcontact['alias'] = JsonLD::fetchElement($data['url'], 'href');
} }
} }
@ -243,43 +241,43 @@ class APContact
} }
$apcontact['pubkey'] = null; $apcontact['pubkey'] = null;
if (!empty($compacted['w3id:publicKey'])) { if (!empty($data['publicKey'])) {
$apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted['w3id:publicKey'], 'w3id:publicKeyPem', '@value')); $apcontact['pubkey'] = trim(JsonLD::fetchElement($data['publicKey'], 'publicKeyPem'));
if (strstr($apcontact['pubkey'], 'RSA ')) { if (strstr($apcontact['pubkey'], 'RSA ')) {
$apcontact['pubkey'] = Crypto::rsaToPem($apcontact['pubkey']); $apcontact['pubkey'] = Crypto::rsaToPem($apcontact['pubkey']);
} }
} }
$apcontact['manually-approve'] = (int)JsonLD::fetchElement($compacted, 'as:manuallyApprovesFollowers'); $apcontact['manually-approve'] = (int)JsonLD::fetchElement($data, 'manuallyApprovesFollowers');
if (!empty($compacted['as:generator'])) { if (!empty($data['generator'])) {
$apcontact['baseurl'] = JsonLD::fetchElement($compacted['as:generator'], 'as:url', '@id'); $apcontact['baseurl'] = JsonLD::fetchElement($data['generator'], 'url');
$apcontact['generator'] = JsonLD::fetchElement($compacted['as:generator'], 'as:name', '@value'); $apcontact['generator'] = JsonLD::fetchElement($data['generator'], 'name');
} }
if (!empty($apcontact['following'])) { if (!empty($apcontact['following'])) {
$data = ActivityPub::fetchContent($apcontact['following']); $content = ActivityPub::fetchContent($apcontact['following']);
if (!empty($data)) { if (!empty($content)) {
if (!empty($data['totalItems'])) { if (!empty($content['totalItems'])) {
$apcontact['following_count'] = $data['totalItems']; $apcontact['following_count'] = $content['totalItems'];
} }
} }
} }
if (!empty($apcontact['followers'])) { if (!empty($apcontact['followers'])) {
$data = ActivityPub::fetchContent($apcontact['followers']); $content = ActivityPub::fetchContent($apcontact['followers']);
if (!empty($data)) { if (!empty($content)) {
if (!empty($data['totalItems'])) { if (!empty($content['totalItems'])) {
$apcontact['followers_count'] = $data['totalItems']; $apcontact['followers_count'] = $content['totalItems'];
} }
} }
} }
if (!empty($apcontact['outbox'])) { if (!empty($apcontact['outbox'])) {
$data = ActivityPub::fetchContent($apcontact['outbox']); $content = ActivityPub::fetchContent($apcontact['outbox']);
if (!empty($data)) { if (!empty($content)) {
if (!empty($data['totalItems'])) { if (!empty($content['totalItems'])) {
$apcontact['statuses_count'] = $data['totalItems']; $apcontact['statuses_count'] = $content['totalItems'];
} }
} }
} }

View file

@ -60,7 +60,7 @@ use Friendica\Util\JsonLD;
class ActivityPub class ActivityPub
{ {
const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public'; const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1', const CONTEXT = ['https://www.w3.org/ns/activitystreams',
['vcard' => 'http://www.w3.org/2006/vcard/ns#', ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/', 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
'diaspora' => 'https://diasporafoundation.org/ns/', 'diaspora' => 'https://diasporafoundation.org/ns/',

View file

@ -24,6 +24,7 @@ namespace Friendica\Util;
use Friendica\Core\Cache\Duration; use Friendica\Core\Cache\Duration;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Exception; use Exception;
use Friendica\Core\System;
use Friendica\DI; use Friendica\DI;
/** /**
@ -67,6 +68,20 @@ class JsonLD
return $data; return $data;
} }
public static function removeSecurityLink(array $json)
{
if (!is_array($json['@context'])) {
return $json;
}
if (($key = array_search('https://w3id.org/security/v1', $json['@context'])) !== false) {
unset($json['@context'][$key]);
$json['@context'] = array_values(array_filter($json['@context']));
}
return $json;
}
public static function fixContext(array $json) public static function fixContext(array $json)
{ {
// Preparation for adding possibly missing content to the context // Preparation for adding possibly missing content to the context
@ -111,7 +126,7 @@ class JsonLD
*/ */
public static function normalize($json) public static function normalize($json)
{ {
$json = self::fixContext($json); $json = self::removeSecurityLink($json);
jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader'); jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
@ -177,7 +192,7 @@ class JsonLD
} }
catch (Exception $e) { catch (Exception $e) {
$compacted = false; $compacted = false;
Logger::error('compacting error'); Logger::error('compacting error', ['callstack' => System::callstack(20)]);
// Sooner or later we should log some details as well - but currently this leads to memory issues // Sooner or later we should log some details as well - but currently this leads to memory issues
// Logger::log('compacting error:' . substr(print_r($e, true), 0, 10000), Logger::DEBUG); // Logger::log('compacting error:' . substr(print_r($e, true), 0, 10000), Logger::DEBUG);
} }