Skip to content

Feat/federated sharing - #8184

Open
luka-nextcloud wants to merge 13 commits into
mainfrom
feat/federated-sharing
Open

luka-nextcloud wants to merge 13 commits into
mainfrom
feat/federated-sharing

Conversation

@luka-nextcloud

@luka-nextcloud luka-nextcloud commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor
  • Resolves: #
  • Target version: main

Summary

TODO

  • ...

Checklist

  • Code is properly formatted
  • Sign-off message is added to all commits
  • Tests (unit, integration, api and/or acceptance) are included
  • Documentation (manuals or wiki) has been updated or is not required

@github-actions

Copy link
Copy Markdown
Contributor

🐢 Performance warning.
It looks like the query count of the integration tests increased with this PR.
Database query count is now 96828 was 96706 (+0.12%)
Please check your code again. If you added a new test this can be expected and the base value in tests/integration/base-query-count.txt can be increased.

@github-actions

Copy link
Copy Markdown
Contributor

🐢 Performance warning.
It looks like the query count of the integration tests increased with this PR.
Database query count is now 206554 was 198420 (+4.09%)
Please check your code again. If you added a new test this can be expected and the base value in tests/integration/base-query-count.txt can be increased.

@luka-nextcloud

Copy link
Copy Markdown
Contributor Author

@CarlSchwan @come-nc I would need advice from experts of files_sharing app:
What is the correct way to mount the endpoint and ensure the remote share file is visible right after accepted the remote share (after called $this->externalManager->acceptShare($externalShare))?

@luka-nextcloud
luka-nextcloud force-pushed the feat/federated-sharing branch from 1eca4c9 to 38e34df Compare October 6, 2026 06:58
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

🐢 Performance warning.
It looks like the query count of the integration tests increased with this PR.
Database query count is now 205527 was 198420 (+3.58%)
Please check your code again. If you added a new test this can be expected and the base value in tests/integration/base-query-count.txt can be increased.

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

🐢 Performance warning.
It looks like the query count of the integration tests increased with this PR.
Database query count is now 205990 was 198420 (+3.81%)
Please check your code again. If you added a new test this can be expected and the base value in tests/integration/base-query-count.txt can be increased.

@luka-nextcloud
luka-nextcloud marked this pull request as ready for review October 20, 2026 09:02
@github-actions

Copy link
Copy Markdown
Contributor

🐢 Performance warning.
It looks like the query count of the integration tests increased with this PR.
Database query count is now 202736 was 198420 (+2.17%)
Please check your code again. If you added a new test this can be expected and the base value in tests/integration/base-query-count.txt can be increased.

@benjaminfrueh benjaminfrueh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@luka-nextcloud thanks for working on this! I left some comments, mostly about the attachment part.

One more general thing: the PR doesn't add tests for the new federation code yet. It would be great to have some.

The PR description is still empty. Could you add a short summary of what's included and how to test it?

Comment thread lib/Cron/ScanFederatedAttachment.php Resolved
Comment thread lib/Sharing/DeckShareProvider.php Resolved
Comment thread lib/Service/FilesAppService.php Resolved
// check for users
foreach ($acls as $acl) {
if ($acl->getType() === Acl::PERMISSION_TYPE_USER && $acl->getParticipant() === $userId) {
if (in_array($acl->getType(), [Acl::PERMISSION_TYPE_USER, Acl::PERMISSION_TYPE_REMOTE]) && $acl->getParticipant() === $userId) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now also matches remote ACLs by participant ID. Since local user IDs can contain @, a local user whose ID is the same as a remote participants cloud ID (e.g. user1@other.server) would get that remote participants permissions. Could we keep remote ACLs out of userCan() and handle the federated case separately?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still compares the remote user cloud ID with the local user ID, so a local user whose ID is the same as a remote participant's cloud ID still gets their permissions. I tested and such a local user can read the board and create cards.

Since only DeckShareProvider::create() needs this for the remote sharedBy, maybe keep userCan() for local users only and check remote participants there explicitly, e.g.:

if ($this->userManager->userExists($sharedBy)) {
	// existing checkPermission() for local users
} elseif (!$this->permissionService->remoteParticipantCan($boardId, $sharedBy, Acl::PERMISSION_EDIT)) {
	$valid = false;
}

with remoteParticipantCan() only checking PERMISSION_TYPE_REMOTE ACLs and probably also checking that federation is enabled like externalUserCan() does.

Comment thread lib/Cron/ScanFederatedAttachment.php Resolved
Comment thread lib/Cron/ScanFederatedAttachment.php Resolved
Comment thread lib/Service/FilesAppService.php Resolved
Comment thread lib/Sharing/DeckShareProvider.php Resolved
Comment thread lib/Sharing/DeckShareProvider.php Resolved
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>

@benjaminfrueh benjaminfrueh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@luka-nextcloud thanks for the updates, a lot of things are fixed and improved now 🚀

I did a second round of review and added some follow-ups, mostly around the remote user check, the cleanup when someone leaves, the files_sharing dependency and the comment mentions.

More generally: the share lookups are now spread as raw queries across RemoteShareHelper, FilesAppService and DeckShareProvider. Moving them into one place, e.g. a FederatedShareMapper like the existing mappers, would make the logic easier to follow and to test.

Tests for the new federation code and a short PR description with what's included and how to test it would still be great.


public function acceptRemoteAttachment(int $cardId, string $token, $receiverUserId): void {
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT, $receiverUserId);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the user check, in RemoteShareHelper::getRemoteFile() it works now. In AttachmentService::acceptRemoteAttachment() the user comes from the remote request, and the second PermissionService::checkPermission() doesn't check it, since federated requests only check the access token. So a remote server could still send another users share.

The share always goes to the user the board was shared by, so that user has to be the owner or have share permission. Could we check that instead?

Suggested change
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT, $receiverUserId);
$boardId = $this->cardMapper->findBoardId($cardId);
if (!$this->permissionService->canUserShareBoard($boardId, $receiverUserId)) {
throw new NoPermissionException('Permission denied for remote attachment');
}

With a small helper in the PermissionService:

public function canUserShareBoard(int $boardId, string $userId): bool {
	if ($this->userIsBoardOwner($boardId, $userId)) {
		return true;
	}
	return $this->userCan($this->aclMapper->findAll($boardId), Acl::PERMISSION_SHARE, $userId);
}

Comment on lines +313 to +316
$deleteQb = $this->dbConnection->getQueryBuilder();
$deleteQb->delete('share')
->where($deleteQb->expr()->in('id', $deleteQb->createNamedParameter($shareIds, IQueryBuilder::PARAM_INT_ARRAY)));
$deleteQb->executeStatement();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the cleanup!
This does almost the same as DeckShareProvider::deleteRemoteShares() but deletes the shares with a direct query, so the remote server isn't notified and the share stays on their side.

Could we move both into one method in RemoteShareHelper that deletes through the share manager? $federatedCloudId could be optional, so when a participant leaves, only their shares are deleted and when an attachment is deleted, the shares of all participants are deleted.

$share = $this->shareManager->getShareByToken((string)$row['token']);
$this->shareManager->deleteShare($share);

Comment on lines +278 to +279
->andWhere($qb->expr()->eq('st.board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('c.deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This skips cards in the trash, so if a participant leaves or is removed while a card is deleted, their shares for its attachments aren't removed and they can still read those files (also after the card is restored). Could we drop this condition?

Suggested change
->andWhere($qb->expr()->eq('st.board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('c.deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
->andWhere($qb->expr()->eq('st.board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT)));

// check for users
foreach ($acls as $acl) {
if ($acl->getType() === Acl::PERMISSION_TYPE_USER && $acl->getParticipant() === $userId) {
if (in_array($acl->getType(), [Acl::PERMISSION_TYPE_USER, Acl::PERMISSION_TYPE_REMOTE]) && $acl->getParticipant() === $userId) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still compares the remote user cloud ID with the local user ID, so a local user whose ID is the same as a remote participant's cloud ID still gets their permissions. I tested and such a local user can read the board and create cards.

Since only DeckShareProvider::create() needs this for the remote sharedBy, maybe keep userCan() for local users only and check remote participants there explicitly, e.g.:

if ($this->userManager->userExists($sharedBy)) {
	// existing checkPermission() for local users
} elseif (!$this->permissionService->remoteParticipantCan($boardId, $sharedBy, Acl::PERMISSION_EDIT)) {
	$valid = false;
}

with remoteParticipantCan() only checking PERMISSION_TYPE_REMOTE ACLs and probably also checking that federation is enabled like externalUserCan() does.

}

$attachmentNode = $userFolder->get($attachmentFolder);
if (!$attachmentNode instanceof Folder) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small edge case: if the users attachment folder is a share they received, the attachment would be mounted inside someone elses shared folder. FilesAppService::createForRemote() handles this with isShared(), maybe we could just skip the move in that case:

Suggested change
if (!$attachmentNode instanceof Folder) {
if (!$attachmentNode instanceof Folder || $attachmentNode->isShared()) {

Comment on lines +198 to +209
public function mountExternalShare(ExternalShare $externalShare, IUser $user): ExternalShareMount {
$this->setupManager->tearDown();
$this->setupManager->setupForUser($user);

$mountPoint = '/' . $user->getUID() . '/files/' . ltrim($externalShare->getMountpoint(), '/');
$existingMount = $this->getMountedExternalShare($mountPoint);
if ($existingMount !== null) {
return $existingMount;
}

throw new GenericShareException('Failed to mount external share.');
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for switching to the setup manager!
This now runs tearDown() + setupForUser() for every remote attachment on every load of the attachment list, even when the share is already mounted. Could we check for the existing mount first and only set up the filesystem again if it isn't mounted yet?

Something like:

public function mountExternalShare(ExternalShare $externalShare, IUser $user): ExternalShareMount {
	$mountPoint = '/' . $user->getUID() . '/files/' . ltrim($externalShare->getMountpoint(), '/');
	$existingMount = $this->getMountedExternalShare($mountPoint);
	if ($existingMount !== null) {
		return $existingMount;
	}

	$this->setupManager->tearDown();
	$this->setupManager->setupForUser($user);

	return $this->getMountedExternalShare($mountPoint) ?? throw new GenericShareException('Failed to mount external share.');
}

public function __construct(
private readonly IDBConnection $dbConnection,
private readonly IManager $shareManager,
private readonly ExternalShareManager $externalShareManager,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for moving this into a separate helper!
Deck still fails with a 500 when files_sharing is disabled though, because RemoteShareHelper is injected in a lot of places and needs ExternalShareManager in its constructor. Could we fetch it lazily instead, like CirclesService does for the circles app, so it is only loaded when a federated share is handled?

private function getExternalShareManager(): ExternalShareManager {
	return $this->container->get(ExternalShareManager::class);
}

with ContainerInterface $container injected instead of ExternalShareManager

private readonly IJobList $jobList,
private readonly IRootFolder $rootFolder,
private readonly IPreview $preview,
private readonly \OC\Files\Mount\Manager $mountManager,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we inject the public IMountManager instead of the private \OC\Files\Mount\Manager? Only find() is used, which is part of the interface.

return $localizedUsers;
}

public function localizeRemoteComments(Board $localBoard, array $comments): array {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About comment mentions on federated boards: this currently feels a bit overengineered and error prone.

ExternalBoardService::localizeRemoteComments() rewrites the message text and the mentions list separately, and CommentItem.vue strips federated_user/ again, so they easily get out of sync, e.g. with more than one mention in a comment or with quoted mentions like @"bob".

I think the mention handling could be simplified by leaving the message text as it is:

  1. CommentService::formatComment() adds the cloud ID to each mention (bob -> bob@nextcloud.local).
  2. ExternalBoardService::localizeRemoteComments() only checks for each mention whether that cloud ID is a local user and adds the user to show without touching the message.
  3. CommentItem.vue finds the mention in the text by its original ID and renders that user, so the federated_user/ replacing isn't needed anymore.

That would also reduce the logic and code quite a bit.

/**
* @param int[] $deckShareIds
*/
private function deleteExternalSharesSharedByFederatedUser(array $deckShareIds, string $federatedCloudId): void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the comment on deleteFederatedAttachmentShares(): this removes the attachments the participant uploaded with direct queries, and reimplements what DeckShareProvider::delete() already does. Could we reuse that by deleting the attachments through the share manager instead? That would simplify the logic here quite a bit.

while ($row = $cursor->fetchAssociative()) {
	try {
		$this->shareManager->deleteShare($this->shareManager->getShareById('deck:' . $row['id']));
	} catch (\Exception) {
		continue;
	}
}

The catch keeps one broken attachment from stopping the whole cleanup.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants