Skip to content

Commit 09af86d

Browse files
committed
fix(federation): Fix creating local cloudIds with http:// protocol
Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent 46906b7 commit 09af86d

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

apps/federatedfilesharing/tests/FederatedShareProviderTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ public function testCreate($expirationDate, $expectedDataDate) {
195195
$this->equalTo('myFile'),
196196
$this->anything(),
197197
'shareOwner',
198-
'shareOwner@http://localhost/',
198+
'shareOwner@http://localhost',
199199
'sharedBy',
200-
'sharedBy@http://localhost/'
200+
'sharedBy@http://localhost'
201201
)
202202
->willReturn(true);
203203

@@ -276,9 +276,9 @@ public function testCreateCouldNotFindServer() {
276276
$this->equalTo('myFile'),
277277
$this->anything(),
278278
'shareOwner',
279-
'shareOwner@http://localhost/',
279+
'shareOwner@http://localhost',
280280
'sharedBy',
281-
'sharedBy@http://localhost/'
281+
'sharedBy@http://localhost'
282282
)->willReturn(false);
283283

284284
$this->rootFolder->method('getById')
@@ -337,9 +337,9 @@ public function testCreateException() {
337337
$this->equalTo('myFile'),
338338
$this->anything(),
339339
'shareOwner',
340-
'shareOwner@http://localhost/',
340+
'shareOwner@http://localhost',
341341
'sharedBy',
342-
'sharedBy@http://localhost/'
342+
'sharedBy@http://localhost'
343343
)->willThrowException(new \Exception('dummy'));
344344

345345
$this->rootFolder->method('getById')
@@ -443,9 +443,9 @@ public function testCreateAlreadyShared() {
443443
$this->equalTo('myFile'),
444444
$this->anything(),
445445
'shareOwner',
446-
'shareOwner@http://localhost/',
446+
'shareOwner@http://localhost',
447447
'sharedBy',
448-
'sharedBy@http://localhost/'
448+
'sharedBy@http://localhost'
449449
)->willReturn(true);
450450

451451
$this->rootFolder->expects($this->never())->method($this->anything());
@@ -514,9 +514,9 @@ public function testUpdate($owner, $sharedBy, $expirationDate) {
514514
$this->equalTo('myFile'),
515515
$this->anything(),
516516
$owner,
517-
$owner . '@http://localhost/',
517+
$owner . '@http://localhost',
518518
$sharedBy,
519-
$sharedBy . '@http://localhost/'
519+
$sharedBy . '@http://localhost'
520520
)->willReturn(true);
521521

522522
if ($owner === $sharedBy) {

lib/private/Federation/CloudIdManager.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,52 +168,51 @@ protected function getDisplayNameFromContact(string $cloudId): ?string {
168168
public function getCloudId(string $user, ?string $remote): ICloudId {
169169
$isLocal = $remote === null;
170170
if ($isLocal) {
171-
$remote = rtrim($this->removeProtocolFromUrl($this->urlGenerator->getAbsoluteURL('/')), '/');
172-
$fixedRemote = $this->fixRemoteURL($remote);
173-
$host = $fixedRemote;
174-
} else {
175-
// note that for remote id's we don't strip the protocol for the remote we use to construct the CloudId
176-
// this way if a user has an explicit non-https cloud id this will be preserved
177-
// we do still use the version without protocol for looking up the display name
178-
$fixedRemote = $this->fixRemoteURL($remote);
179-
$host = $this->removeProtocolFromUrl($fixedRemote);
171+
$remote = rtrim($this->urlGenerator->getAbsoluteURL('/'), '/');
172+
$remote = $this->removeProtocolFromUrl($remote, true);
180173
}
181174

175+
// note that for remote id's we don't strip the protocol for the remote we use to construct the CloudId
176+
// this way if a user has an explicit non-https cloud id this will be preserved
177+
// we do still use the version without protocol for looking up the display name
178+
$remote = $this->fixRemoteURL($remote);
179+
$host = $this->removeProtocolFromUrl($remote);
180+
182181
$key = $user . '@' . ($isLocal ? 'local' : $host);
183182
$cached = $this->cache[$key] ?? $this->memCache->get($key);
184183
if ($cached) {
185184
$this->cache[$key] = $cached; // put items from memcache into local cache
186185
return new CloudId($cached['id'], $cached['user'], $cached['remote'], $cached['displayName']);
187186
}
188187

188+
$id = $user . '@' . $remote;
189189
if ($isLocal) {
190190
$localUser = $this->userManager->get($user);
191191
$displayName = $localUser ? $localUser->getDisplayName() : '';
192192
} else {
193-
$displayName = $this->getDisplayNameFromContact($user . '@' . $host);
193+
$displayName = $this->getDisplayNameFromContact($id);
194194
}
195-
$id = $user . '@' . $remote;
196195

197196
$data = [
198197
'id' => $id,
199198
'user' => $user,
200-
'remote' => $fixedRemote,
199+
'remote' => $remote,
201200
'displayName' => $displayName,
202201
];
203202
$this->cache[$key] = $data;
204203
$this->memCache->set($key, $data, 15 * 60);
205-
return new CloudId($id, $user, $fixedRemote, $displayName);
204+
return new CloudId($id, $user, $remote, $displayName);
206205
}
207206

208207
/**
209208
* @param string $url
210209
* @return string
211210
*/
212-
public function removeProtocolFromUrl(string $url): string {
211+
public function removeProtocolFromUrl(string $url, bool $httpsOnly = false): string {
213212
if (str_starts_with($url, 'https://')) {
214213
return substr($url, 8);
215214
}
216-
if (str_starts_with($url, 'http://')) {
215+
if (!$httpsOnly && str_starts_with($url, 'http://')) {
217216
return substr($url, 7);
218217
}
219218

tests/lib/Federation/CloudIdManagerTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ public function testInvalidCloudId($cloudId) {
124124
$this->cloudIdManager->resolveCloudId($cloudId);
125125
}
126126

127-
public function getCloudIdProvider() {
127+
public function getCloudIdProvider(): array {
128128
return [
129129
['test', 'example.com', 'test@example.com'],
130+
['test', 'http://example.com', 'test@http://example.com'],
131+
['test', null, 'test@http://example.com', 'http://example.com'],
130132
['test@example.com', 'example.com', 'test@example.com@example.com'],
131133
['test@example.com', null, 'test@example.com@example.com'],
132134
];
@@ -136,10 +138,10 @@ public function getCloudIdProvider() {
136138
* @dataProvider getCloudIdProvider
137139
*
138140
* @param string $user
139-
* @param string $remote
141+
* @param null|string $remote
140142
* @param string $id
141143
*/
142-
public function testGetCloudId($user, $remote, $id) {
144+
public function testGetCloudId(string $user, ?string $remote, string $id, ?string $localHost = 'https://example.com'): void {
143145
if ($remote !== null) {
144146
$this->contactsManager->expects($this->any())
145147
->method('search')
@@ -153,7 +155,7 @@ public function testGetCloudId($user, $remote, $id) {
153155
} else {
154156
$this->urlGenerator->expects(self::once())
155157
->method('getAbsoluteUrl')
156-
->willReturn('https://example.com');
158+
->willReturn($localHost);
157159
}
158160

159161
$cloudId = $this->cloudIdManager->getCloudId($user, $remote);

0 commit comments

Comments
 (0)