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

[stable29] fix(federation): Fix missing protocol on CloudID remote #46173

Merged
merged 1 commit into from
Jun 27, 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
5 changes: 5 additions & 0 deletions apps/federatedfilesharing/tests/AddressHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public function dataTestSplitUserRemote() {
foreach ($protocols as $protocol) {
$baseUrl = $user . '@' . $protocol . $remote;

if ($protocol === '') {
// https:// protocol is expected in the final result
$protocol = 'https://';
}

$testCases[] = [$baseUrl, $user, $protocol . $remote];
$testCases[] = [$baseUrl . '/', $user, $protocol . $remote];
$testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote];
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Collaboration/Collaborators/RemotePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function search($search, $limit, $offset, ISearchResult $searchResult): b
public function splitUserRemote(string $address): array {
try {
$cloudId = $this->cloudIdManager->resolveCloudId($address);
return [$cloudId->getUser(), $cloudId->getRemote()];
return [$cloudId->getUser(), $this->cloudIdManager->removeProtocolFromUrl($cloudId->getRemote(), true)];
} catch (\InvalidArgumentException $e) {
throw new \InvalidArgumentException('Invalid Federated Cloud ID', 0, $e);
}
Expand Down
16 changes: 13 additions & 3 deletions lib/private/Federation/CloudIdManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function resolveCloudId(string $cloudId): ICloudId {
}

// Find the first character that is not allowed in user names
$id = $this->fixRemoteURL($cloudId);
$id = $this->stripShareLinkFragments($cloudId);
$posSlash = strpos($id, '/');
$posColon = strpos($id, ':');

Expand All @@ -129,6 +129,7 @@ public function resolveCloudId(string $cloudId): ICloudId {
$this->userManager->validateUserId($user);

if (!empty($user) && !empty($remote)) {
$remote = $this->ensureDefaultProtocol($remote);
return new CloudId($id, $user, $remote, $this->getDisplayNameFromContact($id));
}
}
Expand Down Expand Up @@ -174,8 +175,9 @@ public function getCloudId(string $user, ?string $remote): ICloudId {
// 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->fixRemoteURL($remote);
$remote = $this->stripShareLinkFragments($remote);
$host = $this->removeProtocolFromUrl($remote);
$remote = $this->ensureDefaultProtocol($remote);

$key = $user . '@' . ($isLocal ? 'local' : $host);
$cached = $this->cache[$key] ?? $this->memCache->get($key);
Expand Down Expand Up @@ -220,6 +222,14 @@ public function removeProtocolFromUrl(string $url, bool $httpsOnly = false): str
return $url;
}

protected function ensureDefaultProtocol(string $remote): string {
if (!str_contains($remote, '://')) {
$remote = 'https://' . $remote;
}

return $remote;
}

/**
* Strips away a potential file names and trailing slashes:
* - http://localhost
Expand All @@ -232,7 +242,7 @@ public function removeProtocolFromUrl(string $url, bool $httpsOnly = false): str
* @param string $remote
* @return string
*/
protected function fixRemoteURL(string $remote): string {
protected function stripShareLinkFragments(string $remote): string {
$remote = str_replace('\\', '/', $remote);
if ($fileNamePosition = strpos($remote, '/index.php')) {
$remote = substr($remote, 0, $fileNamePosition);
Expand Down
4 changes: 3 additions & 1 deletion lib/public/Federation/ICloudIdManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ public function isValidCloudId(string $cloudId): bool;
* remove scheme/protocol from an url
*
* @param string $url
* @param bool $httpsOnly
*
* @return string
* @since 28.0.0
* @since 30.0.0 - Optional parameter $httpsOnly was added
*/
public function removeProtocolFromUrl(string $url): string;
public function removeProtocolFromUrl(string $url, bool $httpsOnly = false): string;
}
5 changes: 5 additions & 0 deletions tests/lib/Collaboration/Collaborators/RemotePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ public function dataTestSplitUserRemote() {
foreach ($protocols as $protocol) {
$baseUrl = $user . '@' . $protocol . $remote;

if ($protocol === 'https://') {
// https:// protocol is not expected in the final result
$protocol = '';
}

$testCases[] = [$baseUrl, $user, $protocol . $remote];
$testCases[] = [$baseUrl . '/', $user, $protocol . $remote];
$testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote];
Expand Down
24 changes: 8 additions & 16 deletions tests/lib/Federation/CloudIdManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function setUp(): void {
);
}

public function cloudIdProvider() {
public function cloudIdProvider(): array {
return [
['test@example.com', 'test', 'example.com', 'test@example.com'],
['test@example.com/cloud', 'test', 'example.com/cloud', 'test@example.com/cloud'],
Expand All @@ -75,12 +75,8 @@ public function cloudIdProvider() {

/**
* @dataProvider cloudIdProvider
*
* @param string $cloudId
* @param string $user
* @param string $remote
*/
public function testResolveCloudId($cloudId, $user, $remote, $cleanId) {
public function testResolveCloudId(string $cloudId, string $user, string $noProtocolRemote, string $cleanId): void {
$displayName = 'Ample Ex';

$this->contactsManager->expects($this->any())
Expand All @@ -96,12 +92,12 @@ public function testResolveCloudId($cloudId, $user, $remote, $cleanId) {
$cloudId = $this->cloudIdManager->resolveCloudId($cloudId);

$this->assertEquals($user, $cloudId->getUser());
$this->assertEquals($remote, $cloudId->getRemote());
$this->assertEquals('https://' . $noProtocolRemote, $cloudId->getRemote());
$this->assertEquals($cleanId, $cloudId->getId());
$this->assertEquals($displayName . '@' . $remote, $cloudId->getDisplayId());
$this->assertEquals($displayName . '@' . $noProtocolRemote, $cloudId->getDisplayId());
}

public function invalidCloudIdProvider() {
public function invalidCloudIdProvider(): array {
return [
['example.com'],
['test:foo@example.com'],
Expand All @@ -115,7 +111,7 @@ public function invalidCloudIdProvider() {
* @param string $cloudId
*
*/
public function testInvalidCloudId($cloudId) {
public function testInvalidCloudId(string $cloudId): void {
$this->expectException(\InvalidArgumentException::class);

$this->contactsManager->expects($this->never())
Expand All @@ -126,10 +122,10 @@ public function testInvalidCloudId($cloudId) {

public function getCloudIdProvider(): array {
return [
['test', 'example.com', 'test@example.com'],
['test', 'example.com', 'test@example.com', null, 'https://example.com', 'https://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', 'http://example.com'],
['test@example.com', 'example.com', 'test@example.com@example.com'],
['test@example.com', 'example.com', 'test@example.com@example.com', null, 'https://example.com', 'https://example.com'],
['test@example.com', 'https://example.com', 'test@example.com@example.com'],
['test@example.com', null, 'test@example.com@example.com', null, 'https://example.com', 'https://example.com'],
['test@example.com', 'https://example.com/index.php/s/shareToken', 'test@example.com@example.com', null, 'https://example.com', 'https://example.com'],
Expand All @@ -138,10 +134,6 @@ public function getCloudIdProvider(): array {

/**
* @dataProvider getCloudIdProvider
*
* @param string $user
* @param null|string $remote
* @param string $id
*/
public function testGetCloudId(string $user, ?string $remote, string $id, ?string $searchCloudId = null, ?string $localHost = 'https://example.com', ?string $expectedRemoteId = null): void {
if ($remote !== null) {
Expand Down
Loading