Repository navigation
fix: block local remote shares on typos and contacts with a local Cloud ID #64318
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
Draft
salmart-dev
wants to merge
3
commits into
master
Choose a base branch
from
fix/block-local-remote-shares
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| use OCP\Federation\ICloudIdManager; | ||
| use OCP\IAppConfig; | ||
| use OCP\IConfig; | ||
| use OCP\IUser; | ||
| use OCP\IUserManager; | ||
| use OCP\IUserSession; | ||
| use OCP\Share\IShare; | ||
|
|
@@ -73,16 +74,21 @@ public function search(string $search, int $limit, int $offset, ISearchResult $s | |
| } | ||
|
|
||
| $localUser = $this->userManager->get($remoteUser); | ||
| if ($localUser !== null && $remoteUser !== $this->userId && $cloudId === $localUser->getCloudId()) { | ||
| $result['wide'][] = [ | ||
| 'label' => $contact['FN'], | ||
| 'uuid' => $contact['UID'], | ||
| 'value' => [ | ||
| 'shareType' => IShare::TYPE_USER, | ||
| 'shareWith' => $remoteUser | ||
| ], | ||
| 'shareWithDisplayNameUnique' => $contact['EMAIL'] !== null && $contact['EMAIL'] !== '' ? $contact['EMAIL'] : $contact['UID'], | ||
| ]; | ||
| if ($localUser !== null && $this->isLocalUserCloudId($cloudId, $localUser)) { | ||
| if ($localUser->getUID() !== $this->userId) { | ||
| // add share as local share if not self | ||
| $result['wide'][] = [ | ||
| 'label' => $contact['FN'], | ||
| 'uuid' => $contact['UID'], | ||
| 'value' => [ | ||
| 'shareType' => IShare::TYPE_USER, | ||
| 'shareWith' => $localUser->getUID() | ||
| ], | ||
| 'shareWithDisplayNameUnique' => $contact['EMAIL'] !== null && $contact['EMAIL'] !== '' ? $contact['EMAIL'] : $contact['UID'], | ||
| ]; | ||
| } | ||
| // do not offer local user as remote share | ||
| continue; | ||
| } | ||
|
|
||
| $emailMatch = false; | ||
|
|
@@ -139,7 +145,7 @@ public function search(string $search, int $limit, int $offset, ISearchResult $s | |
| try { | ||
| [$remoteUser, $serverUrl] = $this->splitUserRemote($search); | ||
| $localUser = $this->userManager->get($remoteUser); | ||
| if ($localUser === null || $search !== $localUser->getCloudId()) { | ||
| if ($localUser === null || !$this->isLocalUserCloudId($search, $localUser)) { | ||
| $result['exact'][] = [ | ||
| 'label' => $remoteUser . " ($serverUrl)", | ||
| 'uuid' => $remoteUser, | ||
|
|
@@ -161,6 +167,39 @@ public function search(string $search, int $limit, int $offset, ISearchResult $s | |
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the cloud id points to the given local user, ignoring: | ||
| * - letter casing of the user and host | ||
| * - protocol | ||
| * - trailing slash | ||
| */ | ||
| private function isLocalUserCloudId(string $cloudId, IUser $localUser): bool { | ||
| try { | ||
| $remoteCloudId = $this->cloudIdManager->resolveCloudId($cloudId); | ||
| $localCloudId = $this->cloudIdManager->resolveCloudId($localUser->getCloudId()); | ||
| } catch (\InvalidArgumentException) { | ||
| return false; | ||
| } | ||
|
|
||
| return mb_strtolower($remoteCloudId->getUser()) === mb_strtolower($localCloudId->getUser()) | ||
| && $this->normalizeRemote($remoteCloudId->getRemote()) === $this->normalizeRemote($localCloudId->getRemote()); | ||
| } | ||
|
|
||
| /** | ||
| * Normalize a remote for comparison: the protocol and trailing slashes are | ||
| * dropped, and the host is lowercased. The path is kept as-is, as it is | ||
| * case-sensitive (RFC 3986, section 6.2.2.1). | ||
| */ | ||
| private function normalizeRemote(string $remote): string { | ||
| $remote = rtrim(preg_replace('#^https?://#i', '', $remote), '/'); | ||
| $pathPosition = strpos($remote, '/'); | ||
| if ($pathPosition === false) { | ||
| return strtolower($remote); | ||
| } | ||
|
|
||
| return strtolower(substr($remote, 0, $pathPosition)) . substr($remote, $pathPosition); | ||
| } | ||
|
Comment on lines
+184
to
+201
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic should be moved into a new method in ICloudId, so it's reusable. |
||
|
|
||
| /** | ||
| * split user and remote from federated cloud id | ||
| * | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A fix in this regard should respect the way that the user backend would actually do comparison for the user ID. According to the OCM specs, the identifier is case-sensitive, so lower-casing for comparison is only okay when the backend would do the same to identify the user. So lower-casing may be okay for checking if an inputted cloud ID may match a local user, but is not okay to compare if two remote users are the same user, as the decision about casing can only be done by the remote.