Skip to content

Commit f3855a1

Browse files
Merge pull request #53844 from nextcloud/backport/53109/stable30
2 parents 8ff1b8f + 37c6284 commit f3855a1

3 files changed

Lines changed: 49 additions & 8 deletions

File tree

AUTHORS

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

lib/private/Accounts/AccountManager.php

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

730730
try {
731731
// try the public account lookup API of mastodon
732-
$response = $client->get("https://{$instance}/api/v1/accounts/lookup?acct={$username}@{$instance}");
732+
$response = $client->get("https://{$instance}/.well-known/webfinger?resource=acct:{$username}@{$instance}");
733733
// should be a json response with account information
734734
$data = $response->getBody();
735735
if (is_resource($data)) {
@@ -738,9 +738,26 @@ private function sanitizePropertyFediverse(IAccountProperty $property): void {
738738
$decoded = json_decode($data, true);
739739
// ensure the username is the same the user passed
740740
// in this case we can assume this is a valid fediverse server and account
741-
if (!is_array($decoded) || ($decoded['username'] ?? '') !== $username) {
741+
if (!is_array($decoded) || ($decoded['subject'] ?? '') !== "acct:{$username}@{$instance}") {
742742
throw new InvalidArgumentException();
743743
}
744+
// check for activitypub link
745+
if (is_array($decoded['links']) && isset($decoded['links'])) {
746+
$found = false;
747+
foreach ($decoded['links'] as $link) {
748+
// have application/activity+json or application/ld+json
749+
if (isset($link['type']) && (
750+
$link['type'] === 'application/activity+json' ||
751+
$link['type'] === 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
752+
)) {
753+
$found = true;
754+
break;
755+
}
756+
}
757+
if (!$found) {
758+
throw new InvalidArgumentException();
759+
}
760+
}
744761
} catch (InvalidArgumentException) {
745762
throw new InvalidArgumentException(self::PROPERTY_FEDIVERSE);
746763
} 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
}
@@ -833,12 +856,12 @@ public function testSanitizingFediverseServer(string $input, ?string $output, bo
833856
->willReturn($serverResponse);
834857
$client->expects(self::once())
835858
->method('get')
836-
->with('https://example.com/api/v1/accounts/lookup?acct=[email protected]')
859+
->with('https://example.com/.well-known/webfinger?resource=acct:[email protected]')
837860
->willReturn($response);
838861
} else {
839862
$client->expects(self::once())
840863
->method('get')
841-
->with('https://example.com/api/v1/accounts/lookup?acct=[email protected]')
864+
->with('https://example.com/.well-known/webfinger?resource=acct:[email protected]')
842865
->willThrowException(new \Exception('404'));
843866
}
844867

0 commit comments

Comments
 (0)