Skip to content
Open
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
6 changes: 1 addition & 5 deletions lib/Controller/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use OCA\Tables\Middleware\Attribute\RequirePermission;
use OCA\Tables\Service\ShareService;
use OCA\Tables\Service\ValueObject\ShareCreate;
use OCA\Tables\Service\ValueObject\ShareToken;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
Expand Down Expand Up @@ -90,15 +89,12 @@ public function create(
bool $permissionDelete = false,
bool $permissionManage = false,
int $displayMode = Application::NAV_ENTRY_MODE_ALL,
?string $password = null,
?string $shareToken = null,
): DataResponse {
$shareTokenObject = $shareToken !== null ? new ShareToken($shareToken) : null;
$dto = new ShareCreate(
$nodeId, $nodeType, $receiver, $receiverType,
$permissionRead, $permissionCreate, $permissionUpdate,
$permissionDelete, $permissionManage, $displayMode,
$password, $shareTokenObject,
null, null,
);

return $this->handleError(function () use ($dto) {
Expand Down
20 changes: 20 additions & 0 deletions lib/Middleware/ShareControlMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use OCP\AppFramework\PublicShareController;
use OCP\IRequest;
use OCP\ISession;
use OCP\Share\IManager as ShareManager;
use ReflectionMethod;

class ShareControlMiddleware extends Middleware {
Expand All @@ -31,6 +32,7 @@ public function __construct(
private readonly IRequest $request,
private readonly ShareService $shareService,
private readonly ISession $session,
private readonly ShareManager $shareManager,
) {
}

Expand Down Expand Up @@ -72,11 +74,29 @@ private function assertIsAccessible(string $tokenInput): void {
/**
* @throws NotFoundError
* @throws InvalidArgumentException
* @throws PermissionError
*/
public function assertShareTokenIsValidAndExisting(string $tokenInput): void {
$shareToken = new ShareToken($tokenInput);
$this->share = $this->shareService->findByToken($shareToken);
$this->shareService->assertPublicShareAccessible($this->share);
$this->assertPublicLinkSharingAllowedForShareSender();
}

/**
* if public link token is already issued, prevent them from working after admin disables
*
* @throws PermissionError
*/
private function assertPublicLinkSharingAllowedForShareSender(): void {
if (!$this->shareManager->shareApiAllowLinks()) {
throw new PermissionError('Public link sharing is disabled by your administrator.');
}

$sender = $this->share->getSender();
if ($sender !== null && $this->shareManager->sharingDisabledForUser($sender)) {
throw new PermissionError('Sharing is restricted by your administrator for your account.');
}
}

public function afterException($controller, $methodName, \Exception $exception): DataResponse {
Expand Down
24 changes: 16 additions & 8 deletions lib/Service/ShareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,7 @@ public function assertPublicShareAccessible(Share $share): void {
* @throws PermissionError
*/
public function createLinkShare(Table|View $node, ?string $password = null): Share {
// check admin sharing policy for this user (allowed/excluded sharing groups)
if ($this->shareManager->sharingDisabledForUser($this->userId)) {
throw new PermissionError('Sharing is restricted by your administrator for your account.');
}
// check global admin setting for public link sharing
if (!$this->shareManager->shareApiAllowLinks()) {
throw new PermissionError('Public link sharing is disabled by your administrator.');
}
$this->assertPublicLinkSharingAllowed();

for ($i = 0; $i < 3; $i++) {
// there is the theoretical chance, that an existing share token would be re-used,
Expand Down Expand Up @@ -230,6 +223,18 @@ protected function generateShareToken(): ShareToken {
return new ShareToken($shareToken);
}

/**
* @throws PermissionError
*/
private function assertPublicLinkSharingAllowed(): void {
if ($this->shareManager->sharingDisabledForUser($this->userId)) {
throw new PermissionError('Sharing is restricted by your administrator for your account.');
}
if (!$this->shareManager->shareApiAllowLinks()) {
throw new PermissionError('Public link sharing is disabled by your administrator.');
}
}

/**
* @param string|null $userId
* @return array<int, View> Indexed by view id
Expand Down Expand Up @@ -359,6 +364,9 @@ private function buildBaseShare(
if ($receiverType === ShareReceiverType::GROUP && !$this->shareManager->allowGroupSharing()) {
throw new PermissionError('Group sharing is disabled by your administrator.');
}
if ($receiverType === ShareReceiverType::LINK) {
$this->assertPublicLinkSharingAllowed();
}
$this->enforceGroupMembersOnlyPolicy($this->userId, $receiverType, $receiver);

$time = new DateTime();
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/Service/ShareServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\Tables\Db\ContextNavigationMapper;
use OCA\Tables\Db\Share;
use OCA\Tables\Db\ShareMapper;
use OCA\Tables\Db\Table;
use OCA\Tables\Db\TableMapper;
use OCA\Tables\Db\ViewMapper;
use OCA\Tables\Errors\PermissionError;
Expand All @@ -21,6 +22,7 @@
use OCA\Tables\Service\PermissionsService;
use OCA\Tables\Service\ShareService;
use OCA\Tables\Service\ValueObject\ShareCreate;
use OCA\Tables\Service\ValueObject\ShareToken;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserManager;
Expand Down Expand Up @@ -145,4 +147,46 @@ public function testAssertPublicShareAccessibleThrowsWhenSenderCannotShare(): vo
$this->expectException(PermissionError::class);
$this->shareService->assertPublicShareAccessible($this->publicShare);
}

public function testCreateLinkShareThrowsWhenPublicLinksDisabled(): void {
$this->shareManager->method('sharingDisabledForUser')->willReturn(false);
$this->shareManager->method('shareApiAllowLinks')->willReturn(false);

$this->mapper->expects($this->never())->method('insert');

$this->expectException(PermissionError::class);
$this->expectExceptionMessage('Public link sharing is disabled by your administrator.');

$table = new Table();
$table->setId(1);
$this->shareService->createLinkShare($table);
}

public function testCreateThrowsWhenPublicLinksDisabledForGenericLinkShare(): void {
$this->shareManager->method('sharingDisabledForUser')->willReturn(false);
$this->shareManager->method('shareApiAllowLinks')->willReturn(false);

$this->mapper->expects($this->never())->method('insert');

$this->expectException(PermissionError::class);
$this->expectExceptionMessage('Public link sharing is disabled by your administrator.');

$this->shareService->create(
new ShareCreate(
1,
'table',
'',
'link',
true,
false,
false,
true,
false,
0,
null,
new ShareToken('tokenBypass2026x'),
)
);
}

}
Loading