Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(federation): Fix creating local cloudIds with http:// protocol #44453

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions apps/federatedfilesharing/tests/FederatedShareProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ public function testCreate($expirationDate, $expectedDataDate) {
$this->equalTo('myFile'),
$this->anything(),
'shareOwner',
'shareOwner@http://localhost/',
'shareOwner@http://localhost',
'sharedBy',
'sharedBy@http://localhost/'
'sharedBy@http://localhost'
)
->willReturn(true);

Expand Down Expand Up @@ -276,9 +276,9 @@ public function testCreateCouldNotFindServer() {
$this->equalTo('myFile'),
$this->anything(),
'shareOwner',
'shareOwner@http://localhost/',
'shareOwner@http://localhost',
'sharedBy',
'sharedBy@http://localhost/'
'sharedBy@http://localhost'
)->willReturn(false);

$this->rootFolder->method('getById')
Expand Down Expand Up @@ -337,9 +337,9 @@ public function testCreateException() {
$this->equalTo('myFile'),
$this->anything(),
'shareOwner',
'shareOwner@http://localhost/',
'shareOwner@http://localhost',
'sharedBy',
'sharedBy@http://localhost/'
'sharedBy@http://localhost'
)->willThrowException(new \Exception('dummy'));

$this->rootFolder->method('getById')
Expand Down Expand Up @@ -443,9 +443,9 @@ public function testCreateAlreadyShared() {
$this->equalTo('myFile'),
$this->anything(),
'shareOwner',
'shareOwner@http://localhost/',
'shareOwner@http://localhost',
'sharedBy',
'sharedBy@http://localhost/'
'sharedBy@http://localhost'
)->willReturn(true);

$this->rootFolder->expects($this->never())->method($this->anything());
Expand Down Expand Up @@ -514,9 +514,9 @@ public function testUpdate($owner, $sharedBy, $expirationDate) {
$this->equalTo('myFile'),
$this->anything(),
$owner,
$owner . '@http://localhost/',
$owner . '@http://localhost',
$sharedBy,
$sharedBy . '@http://localhost/'
$sharedBy . '@http://localhost'
)->willReturn(true);

if ($owner === $sharedBy) {
Expand Down
25 changes: 12 additions & 13 deletions lib/private/Federation/CloudIdManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,16 @@ protected function getDisplayNameFromContact(string $cloudId): ?string {
public function getCloudId(string $user, ?string $remote): ICloudId {
$isLocal = $remote === null;
if ($isLocal) {
$remote = rtrim($this->removeProtocolFromUrl($this->urlGenerator->getAbsoluteURL('/')), '/');
$fixedRemote = $this->fixRemoteURL($remote);
$host = $fixedRemote;
} else {
// note that for remote id's we don't strip the protocol for the remote we use to construct the CloudId
// this way if a user has an explicit non-https cloud id this will be preserved
// we do still use the version without protocol for looking up the display name
$fixedRemote = $this->fixRemoteURL($remote);
$host = $this->removeProtocolFromUrl($fixedRemote);
$remote = rtrim($this->urlGenerator->getAbsoluteURL('/'), '/');
}

// note that for remote id's we don't strip the protocol for the remote we use to construct the CloudId
// this way if a user has an explicit non-https cloud id this will be preserved
// we do still use the version without protocol for looking up the display name
$remote = $this->removeProtocolFromUrl($remote, true);
$remote = $this->fixRemoteURL($remote);
$host = $this->removeProtocolFromUrl($remote);

$key = $user . '@' . ($isLocal ? 'local' : $host);
$cached = $this->cache[$key] ?? $this->memCache->get($key);
if ($cached) {
Expand All @@ -197,23 +196,23 @@ public function getCloudId(string $user, ?string $remote): ICloudId {
$data = [
'id' => $id,
'user' => $user,
'remote' => $fixedRemote,
'remote' => $remote,
'displayName' => $displayName,
];
$this->cache[$key] = $data;
$this->memCache->set($key, $data, 15 * 60);
return new CloudId($id, $user, $fixedRemote, $displayName);
return new CloudId($id, $user, $remote, $displayName);
}

/**
* @param string $url
* @return string
*/
public function removeProtocolFromUrl(string $url): string {
public function removeProtocolFromUrl(string $url, bool $httpsOnly = false): string {
if (str_starts_with($url, 'https://')) {
return substr($url, 8);
}
if (str_starts_with($url, 'http://')) {
if (!$httpsOnly && str_starts_with($url, 'http://')) {
return substr($url, 7);
}

Expand Down
16 changes: 10 additions & 6 deletions tests/lib/Federation/CloudIdManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,36 +124,40 @@ public function testInvalidCloudId($cloudId) {
$this->cloudIdManager->resolveCloudId($cloudId);
}

public function getCloudIdProvider() {
public function getCloudIdProvider(): array {
return [
['test', 'example.com', 'test@example.com'],
['test', 'http://example.com', 'test@http://example.com', 'test@example.com'],
['test', null, 'test@http://example.com', 'test@example.com', 'http://example.com'],
['test@example.com', 'example.com', 'test@example.com@example.com'],
['test@example.com', 'https://example.com', 'test@example.com@example.com'],
['test@example.com', null, 'test@example.com@example.com'],
['test@example.com', 'https://example.com/index.php/s/shareToken', 'test@example.com@example.com'],
];
}

/**
* @dataProvider getCloudIdProvider
*
* @param string $user
* @param string $remote
* @param null|string $remote
* @param string $id
*/
public function testGetCloudId($user, $remote, $id) {
public function testGetCloudId(string $user, ?string $remote, string $id, ?string $searchCloudId = null, ?string $localHost = 'https://example.com'): void {
if ($remote !== null) {
$this->contactsManager->expects($this->any())
->method('search')
->with($id, ['CLOUD'])
->with($searchCloudId ?? $id, ['CLOUD'])
->willReturn([
[
'CLOUD' => [$id],
'CLOUD' => [$searchCloudId ?? $id],
'FN' => 'Ample Ex',
]
]);
} else {
$this->urlGenerator->expects(self::once())
->method('getAbsoluteUrl')
->willReturn('https://example.com');
->willReturn($localHost);
}

$cloudId = $this->cloudIdManager->getCloudId($user, $remote);
Expand Down
Loading