Skip to content

Commit 4dcb282

Browse files
authored
Merge pull request #53109 from TechnicalSuwako/master
fix(settings): change Mastodon only URI to webfinger
2 parents 77939fa + e489713 commit 4dcb282

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@
620620
- szaimen <[email protected]>
621621
- tbartenstein <[email protected]>
622622
- tbelau666 <[email protected]>
623+
- TechnicalSuwako <[email protected]>
623624
- tgrant <[email protected]>
624625
- timm2k <[email protected]>
625626
- tux-rampage <[email protected]>

lib/private/Accounts/AccountManager.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ private function sanitizePropertyFediverse(IAccountProperty $property): void {
734734

735735
try {
736736
// try the public account lookup API of mastodon
737-
$response = $client->get("https://{$instance}/api/v1/accounts/lookup?acct={$username}@{$instance}");
737+
$response = $client->get("https://{$instance}/.well-known/webfinger?resource=acct:{$username}@{$instance}");
738738
// should be a json response with account information
739739
$data = $response->getBody();
740740
if (is_resource($data)) {
@@ -743,9 +743,26 @@ private function sanitizePropertyFediverse(IAccountProperty $property): void {
743743
$decoded = json_decode($data, true);
744744
// ensure the username is the same the user passed
745745
// in this case we can assume this is a valid fediverse server and account
746-
if (!is_array($decoded) || ($decoded['username'] ?? '') !== $username) {
746+
if (!is_array($decoded) || ($decoded['subject'] ?? '') !== "acct:{$username}@{$instance}") {
747747
throw new InvalidArgumentException();
748748
}
749+
// check for activitypub link
750+
if (is_array($decoded['links']) && isset($decoded['links'])) {
751+
$found = false;
752+
foreach ($decoded['links'] as $link) {
753+
// have application/activity+json or application/ld+json
754+
if (isset($link['type']) && (
755+
$link['type'] === 'application/activity+json' ||
756+
$link['type'] === 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
757+
)) {
758+
$found = true;
759+
break;
760+
}
761+
}
762+
if (!$found) {
763+
throw new InvalidArgumentException();
764+
}
765+
}
749766
} catch (InvalidArgumentException) {
750767
throw new InvalidArgumentException(self::PROPERTY_FEDIVERSE);
751768
} catch (\Exception $error) {

tests/lib/Accounts/AccountManagerTest.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -786,20 +786,41 @@ public static function dataSanitizeFediverseServer(): array {
786786
787787
788788
true,
789-
json_encode(['username' => 'foo']),
789+
json_encode([
790+
'subject' => 'acct:[email protected]',
791+
'links' => [
792+
[
793+
'rel' => 'self',
794+
'type' => 'application/activity+json',
795+
'href' => 'https://example.com/users/foo',
796+
],
797+
],
798+
]),
790799
],
791800
'valid response - no at' => [
792801
793802
794803
true,
795-
json_encode(['username' => 'foo']),
804+
json_encode([
805+
'subject' => 'acct:[email protected]',
806+
'links' => [
807+
[
808+
'rel' => 'self',
809+
'type' => 'application/activity+json',
810+
'href' => 'https://example.com/users/foo',
811+
],
812+
],
813+
]),
796814
],
797815
// failures
798816
'invalid response' => [
799817
800818
null,
801819
true,
802-
json_encode(['not found']),
820+
json_encode([
821+
'subject' => 'acct:[email protected]',
822+
'links' => [],
823+
]),
803824
],
804825
'no response' => [
805826
@@ -811,7 +832,9 @@ public static function dataSanitizeFediverseServer(): array {
811832
812833
null,
813834
true,
814-
json_encode(['username' => '[email protected]']),
835+
json_encode([
836+
'links' => [],
837+
]),
815838
],
816839
];
817840
}
@@ -831,12 +854,12 @@ public function testSanitizingFediverseServer(string $input, ?string $output, bo
831854
->willReturn($serverResponse);
832855
$client->expects(self::once())
833856
->method('get')
834-
->with('https://example.com/api/v1/accounts/lookup?acct=[email protected]')
857+
->with('https://example.com/.well-known/webfinger?resource=acct:[email protected]')
835858
->willReturn($response);
836859
} else {
837860
$client->expects(self::once())
838861
->method('get')
839-
->with('https://example.com/api/v1/accounts/lookup?acct=[email protected]')
862+
->with('https://example.com/.well-known/webfinger?resource=acct:[email protected]')
840863
->willThrowException(new \Exception('404'));
841864
}
842865

0 commit comments

Comments
 (0)