Skip to content
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
58 changes: 38 additions & 20 deletions lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace OCA\Text\Service;

use OC\User\NoUserException;
use OCA\DAV\Connector\Sabre\PublicAuth;
use OCA\Files_Sharing\SharedStorage;
use OCA\Text\Controller\AttachmentController;
use OCA\Text\Db\Session;
Expand All @@ -40,6 +41,7 @@
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IPreview;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\Lock\LockedException;
use OCP\Share\Exceptions\ShareNotFound;
Expand All @@ -52,7 +54,8 @@
private ShareManager $shareManager,
private IPreview $previewManager,
private IMimeTypeDetector $mimeTypeDetector,
private IURLGenerator $urlGenerator) {
private IURLGenerator $urlGenerator,
private ISession $session) {
}

/**
Expand Down Expand Up @@ -310,9 +313,33 @@
* @throws NoUserException
*/
public function uploadAttachmentPublic(?int $documentId, string $newFileName, $newFileResource, string $shareToken): array {
if (!$this->hasUpdatePermissions($shareToken)) {
try {
$share = $this->shareManager->getShareByToken($shareToken);
} catch (ShareNotFound) {
throw new NotFoundException('Share not found');
}

if (!$this->hasUpdatePermissions($share)) {
throw new NotPermittedException('No write permissions');
}

if ($share->getPassword() !== null) {

Check failure on line 326 in lib/Service/AttachmentService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

RedundantConditionGivenDocblockType

lib/Service/AttachmentService.php:326:7: RedundantConditionGivenDocblockType: Docblock-defined type string can never contain null (see https://psalm.dev/156)
$key = PublicAuth::DAV_AUTHENTICATED;

if (!$this->session->exists($key)) {
throw new NotPermittedException('Share not authenticated');
}

$allowedShareIds = $this->session->get($key);
if (!is_array($allowedShareIds)) {
throw new NotPermittedException('Share not authenticated');
}

if (!in_array($share->getId(), $allowedShareIds, true)) {
throw new NotPermittedException('Share not authenticated');
}
}

$textFile = $this->getTextFilePublic($documentId, $shareToken);
$saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
$fileName = self::getUniqueFileName($saveDir, $newFileName);
Expand Down Expand Up @@ -398,25 +425,16 @@

/**
* Check if the shared access has write permissions
*
* @param string $shareToken
*
* @return bool
*/
private function hasUpdatePermissions(string $shareToken): bool {
try {
$share = $this->shareManager->getShareByToken($shareToken);
return (
in_array(
$share->getShareType(),
[IShare::TYPE_LINK, IShare::TYPE_EMAIL, IShare::TYPE_ROOM],
true
)
&& $share->getPermissions() & Constants::PERMISSION_UPDATE
&& $share->getNode()->getPermissions() & Constants::PERMISSION_UPDATE);
} catch (ShareNotFound|NotFoundException $e) {
return false;
}
private function hasUpdatePermissions(IShare $share): bool {
return (
in_array(
$share->getShareType(),
[IShare::TYPE_LINK, IShare::TYPE_EMAIL, IShare::TYPE_ROOM],
true
)
&& $share->getPermissions() & Constants::PERMISSION_UPDATE
&& $share->getNode()->getPermissions() & Constants::PERMISSION_UPDATE);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ abstract public function setWantsNotification(bool $wantsNotification): void;
abstract public function setNotificationTarget(?string $notificationTarget): void;
}
}

namespace OCA\DAV\Connector\Sabre {
class PublicAuth {
public const DAV_AUTHENTICATED = '';
}
}
Loading