Skip to content

Feat/federated sharing - #8184

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

luka-nextcloud wants to merge 12 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.

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 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 on lines +59 to +70
if (count($files) === 0) {
// Retry later, maybe the share is not yet mounted
$this->jobList->add(ScanFederatedAttachment::class, [
'cardId' => $cardId,
'shareId' => $shareId,
'userId' => $userId,
]);

// Scan the files to make sure the share is mounted and the files are available
exec('php ' . \OC::$SERVERROOT . '/occ files:scan --path="/' . $userId . '/files' . $externalShare->getMountpoint() . '"');
return;
}

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.

What's the reason for occ files:scan here? Is it because the share isn't visible yet? I would like to understand the case where getById() comes back empty.

Either way I think we should avoid exec() here as the mount point comes from the remote share name and goes into the shell command unescaped, so a file name could lead to shell injection.

We could run the storage scanner directly and only re-queue the job if the file still isn't found afterwards:

Suggested change
if (count($files) === 0) {
// Retry later, maybe the share is not yet mounted
$this->jobList->add(ScanFederatedAttachment::class, [
'cardId' => $cardId,
'shareId' => $shareId,
'userId' => $userId,
]);
// Scan the files to make sure the share is mounted and the files are available
exec('php ' . \OC::$SERVERROOT . '/occ files:scan --path="/' . $userId . '/files' . $externalShare->getMountpoint() . '"');
return;
}
if (count($files) === 0) {
$node->getStorage()->getScanner()->scan($node->getInternalPath());
$files = $userFolder->getById($node->getId());
}
if (count($files) === 0) {
// Retry later, maybe the share is not yet mounted
$this->jobList->add(ScanFederatedAttachment::class, [
'cardId' => $cardId,
'shareId' => $shareId,
'userId' => $userId,
]);
return;
}

In general it's better not to call occ via exec() from app code, since the php binary might not be available or could be on a different path, exec() may be disabled and failures go unnoticed.

Comment on lines +1390 to +1394
$qb->select('id', 'accepted', 'user')
->from('share_external')
->where($qb->expr()->eq('refresh_token', $qb->createNamedParameter($shareToken)))
->andWhere($qb->expr()->eq('remote', $qb->createNamedParameter($remote . '/')))
->setMaxResults(1);

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 only looks up the share by token and remote, not by user. So a remote server could send the token of a share that belongs to another user here and it would get accepted and mounted in that users files even if they are not part of the board.

Could we also check the user? In getRemoteFile() it should be the current user. In acceptRemoteAttachment() we could at least check that the user the share belongs to is part of the board.

$share->setSharedBy($attachmentShare->getShareOwner());
$share->setShareOwner($attachmentShare->getShareOwner());

$createdShare = $this->shareManager->createShare($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.

These shares are only removed when the attachment is deleted. If a remote participant leaves or is removed from the board they keep access to the files. Could we also remove them in that case?

// 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?

Comment on lines +77 to +78
$remote = substr($externalShare->getRemote(), 0, strrpos($externalShare->getRemote(), '/'));
$federatedId = $externalShare->getOwner() . '@' . $remote;

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 builds the cloud ID as user@https://nextcloud.local (with the protocol), but the board ACL has user@nextcloud.local. So the permission check can fail and it throws Card not found and attachments might not show up. Could we use the cloud ID manager instead?

Suggested change
$remote = substr($externalShare->getRemote(), 0, strrpos($externalShare->getRemote(), '/'));
$federatedId = $externalShare->getOwner() . '@' . $remote;
$federatedId = $this->cloudIdManager->getCloudId($externalShare->getOwner(), $externalShare->getRemote())->getId();

#[PublicPage]
public function getAll(int $cardId, ?int $boardId = null): DataResponse {
$this->ensureLocalBoard($boardId);
$board = $this->boardService->find($boardId, false);

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.

boardId is optional but find() is now always called with it, so requests without boardId fail with a 500. This is potentially a breaking API change and could affect existing clients. Could we keep the if ($boardId) check from ensureLocalBoard() here and in create()/delete()? Same in CardOcsController::update(), where boardId became required.

Since this check is repeated in a lot of controllers now, maybe a small helper like BoardService::findExternalBoard(?int $boardId): ?Board (returning the board only if it's federated) would make this simpler and avoid missing the null check again.

Comment on lines +61 to +65
$this->jobList->add(ScanFederatedAttachment::class, [
'cardId' => $cardId,
'shareId' => $shareId,
'userId' => $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 re-queues the job without a limit, so if the file never shows up (e.g. the remote is gone), it would retry on every cron run forever. If the retry is needed, could we add an attempt counter to the arguments and stop after a few tries?

'shareToken' => $externalShare ? $externalShare->getRefreshToken() : null,
]);
} else {
$shareToken = $this->ensureFederatedAttachmentShare($share, $node, $requestUserId);

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.

With this the GET for the attachment list creates and accepts shares on the first load and mounts them again on every load (mountExternalShare() in DeckShareProvider::ensureAcceptRemoteShare()). With the 5-second polling this repeats while the card is open.

What do you think about skipping the mount when it's already set up and stopping the polling after a while if an attachment doesn't become available?

private IL10N $l,
private ITimeFactory $timeFactory,
private IMimeTypeLoader $mimeTypeLoader,
private 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.

Since DeckShareProvider is injected in a lot of places this makes Deck stop working entirely when files_sharing is disabled, even for local boards. Could we move the federated share logic into a separate service thats only loaded when needed?

return $externalShare;
}

public function mountExternalShare(ExternalShare $externalShare, IUser $user): ExternalShareMount {

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.

mountExternalShare() builds the mount manually using files_sharing internals so it could break when files_sharing changes. Instead files_sharing could mount the accepted share itself by setting up the users filesystem again with OCP\Files\ISetupManager injected:

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

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