From d9bedbb4733bdfb27c81ad3d117f99415589e788 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 8 Mar 2024 13:48:21 +0000 Subject: [PATCH] Centralized logging for a wrong JSON content-type --- src/Model/APContact.php | 3 +-- src/Model/Item.php | 2 +- src/Protocol/ActivityPub/Processor.php | 3 +-- src/Util/HTTPSignature.php | 14 ++++++++++---- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Model/APContact.php b/src/Model/APContact.php index d71e182e37..392f56ea43 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -208,8 +208,7 @@ class APContact if (!$failed && ($curlResult->getReturnCode() == 410)) { $data = ['@context' => ActivityPub::CONTEXT, 'id' => $url, 'type' => 'Tombstone']; - } elseif (!$failed && !HTTPSignature::isValidContentType($curlResult->getContentType())) { - Logger::debug('Unexpected content type', ['content-type' => $curlResult->getContentType(), 'url' => $url]); + } elseif (!$failed && !HTTPSignature::isValidContentType($curlResult->getContentType(), $url)) { $failed = true; } } catch (\Exception $exception) { diff --git a/src/Model/Item.php b/src/Model/Item.php index 8ecfc9dbb2..535e8f7989 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -4097,7 +4097,7 @@ class Item } $curlResult = DI::httpClient()->head($uri, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON_AS]); - if (HTTPSignature::isValidContentType($curlResult->getContentType())) { + if (HTTPSignature::isValidContentType($curlResult->getContentType(), $uri)) { $fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri, [], '', $completion, $uid); } diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index a22be1c5bc..c8a5ae4d5d 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -1618,8 +1618,7 @@ class Processor return ''; } - if (!HTTPSignature::isValidContentType($curlResult->getContentType())) { - Logger::notice('Unexpected content type', ['content-type' => $curlResult->getContentType(), 'url' => $url]); + if (!HTTPSignature::isValidContentType($curlResult->getContentType(), $url)) { return ''; } diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index bf5d632dcc..095b73ea7c 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -443,8 +443,7 @@ class HTTPSignature return []; } - if (!self::isValidContentType($curlResult->getContentType())) { - Logger::notice('Unexpected content type', ['content-type' => $curlResult->getContentType(), 'url' => $request]); + if (!self::isValidContentType($curlResult->getContentType(), $request)) { return []; } @@ -457,9 +456,16 @@ class HTTPSignature * @param string $contentType * @return boolean */ - public static function isValidContentType(string $contentType): bool + public static function isValidContentType(string $contentType, string $url = ''): bool { - return in_array(current(explode(';', $contentType)), ['application/activity+json', 'application/ld+json']); + if (in_array(current(explode(';', $contentType)), ['application/activity+json', 'application/ld+json'])) { + return true; + } + + if (current(explode(';', $contentType)) == 'application/json') { + Logger::notice('Unexpected content type, possibly from a remote system that is not standard compliant.', ['content-type' => $contentType, 'url' => $url]); + } + return false; } /**