Skip to content

Commit 088b233

Browse files
authored
Merge pull request #7938 from nextcloud/backport/7933/stable28
[stable28] Improve share token handling in AttachmentService
2 parents 3d083e6 + 91f8905 commit 088b233

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

lib/Service/AttachmentService.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
namespace OCA\Text\Service;
2828

2929
use OC\User\NoUserException;
30+
use OCA\DAV\Connector\Sabre\PublicAuth;
3031
use OCA\Files_Sharing\SharedStorage;
3132
use OCA\Text\Controller\AttachmentController;
3233
use OCA\Text\Db\Session;
@@ -40,6 +41,7 @@
4041
use OCP\Files\NotPermittedException;
4142
use OCP\Files\SimpleFS\ISimpleFile;
4243
use OCP\IPreview;
44+
use OCP\ISession;
4345
use OCP\IURLGenerator;
4446
use OCP\Lock\LockedException;
4547
use OCP\Share\Exceptions\ShareNotFound;
@@ -52,7 +54,8 @@ public function __construct(private IRootFolder $rootFolder,
5254
private ShareManager $shareManager,
5355
private IPreview $previewManager,
5456
private IMimeTypeDetector $mimeTypeDetector,
55-
private IURLGenerator $urlGenerator) {
57+
private IURLGenerator $urlGenerator,
58+
private ISession $session) {
5659
}
5760

5861
/**
@@ -310,9 +313,33 @@ public function uploadAttachment(int $documentId, string $newFileName, $newFileR
310313
* @throws NoUserException
311314
*/
312315
public function uploadAttachmentPublic(?int $documentId, string $newFileName, $newFileResource, string $shareToken): array {
313-
if (!$this->hasUpdatePermissions($shareToken)) {
316+
try {
317+
$share = $this->shareManager->getShareByToken($shareToken);
318+
} catch (ShareNotFound) {
319+
throw new NotFoundException('Share not found');
320+
}
321+
322+
if (!$this->hasUpdatePermissions($share)) {
314323
throw new NotPermittedException('No write permissions');
315324
}
325+
326+
if ($share->getPassword() !== null) {
327+
$key = PublicAuth::DAV_AUTHENTICATED;
328+
329+
if (!$this->session->exists($key)) {
330+
throw new NotPermittedException('Share not authenticated');
331+
}
332+
333+
$allowedShareIds = $this->session->get($key);
334+
if (!is_array($allowedShareIds)) {
335+
throw new NotPermittedException('Share not authenticated');
336+
}
337+
338+
if (!in_array($share->getId(), $allowedShareIds, true)) {
339+
throw new NotPermittedException('Share not authenticated');
340+
}
341+
}
342+
316343
$textFile = $this->getTextFilePublic($documentId, $shareToken);
317344
$saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
318345
$fileName = self::getUniqueFileName($saveDir, $newFileName);
@@ -398,25 +425,16 @@ public static function getUniqueFileName(Folder $dir, string $fileName): string
398425

399426
/**
400427
* Check if the shared access has write permissions
401-
*
402-
* @param string $shareToken
403-
*
404-
* @return bool
405428
*/
406-
private function hasUpdatePermissions(string $shareToken): bool {
407-
try {
408-
$share = $this->shareManager->getShareByToken($shareToken);
409-
return (
410-
in_array(
411-
$share->getShareType(),
412-
[IShare::TYPE_LINK, IShare::TYPE_EMAIL, IShare::TYPE_ROOM],
413-
true
414-
)
415-
&& $share->getPermissions() & Constants::PERMISSION_UPDATE
416-
&& $share->getNode()->getPermissions() & Constants::PERMISSION_UPDATE);
417-
} catch (ShareNotFound|NotFoundException $e) {
418-
return false;
419-
}
429+
private function hasUpdatePermissions(IShare $share): bool {
430+
return (
431+
in_array(
432+
$share->getShareType(),
433+
[IShare::TYPE_LINK, IShare::TYPE_EMAIL, IShare::TYPE_ROOM],
434+
true
435+
)
436+
&& $share->getPermissions() & Constants::PERMISSION_UPDATE
437+
&& $share->getNode()->getPermissions() & Constants::PERMISSION_UPDATE);
420438
}
421439

422440
/**

tests/stub.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ abstract public function setWantsNotification(bool $wantsNotification): void;
4949
abstract public function setNotificationTarget(?string $notificationTarget): void;
5050
}
5151
}
52+
53+
namespace OCA\DAV\Connector\Sabre {
54+
class PublicAuth {
55+
public const DAV_AUTHENTICATED = '';
56+
}
57+
}

0 commit comments

Comments
 (0)