Skip to content

Commit 6b89754

Browse files
authored
Merge pull request #7933 from nextcloud/fix/attachments/validate-public-share-password
Improve share token handling in AttachmentService
2 parents 7270b61 + 98ee05f commit 6b89754

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

lib/Service/AttachmentService.php

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace OCA\Text\Service;
1111

1212
use OC\User\NoUserException;
13+
use OCA\DAV\Connector\Sabre\PublicAuth;
1314
use OCA\Files_Sharing\SharedStorage;
1415
use OCA\Text\Controller\AttachmentController;
1516
use OCA\Text\Db\Session;
@@ -25,6 +26,7 @@
2526
use OCP\Files\SimpleFS\ISimpleFile;
2627
use OCP\FilesMetadata\IFilesMetadataManager;
2728
use OCP\IPreview;
29+
use OCP\ISession;
2830
use OCP\IURLGenerator;
2931
use OCP\Lock\LockedException;
3032
use OCP\Share\Exceptions\ShareNotFound;
@@ -41,6 +43,7 @@ public function __construct(
4143
private IURLGenerator $urlGenerator,
4244
private IFilenameValidator $filenameValidator,
4345
private IFilesMetadataManager $filesMetadataManager,
46+
private ISession $session,
4447
) {
4548
}
4649

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

430457
/**
431458
* Check if the shared access has write permissions
432-
*
433-
* @param string $shareToken
434-
*
435-
* @return bool
436459
*/
437-
private function hasUpdatePermissions(string $shareToken): bool {
438-
try {
439-
$share = $this->shareManager->getShareByToken($shareToken);
440-
return (
441-
in_array(
442-
$share->getShareType(),
443-
[IShare::TYPE_LINK, IShare::TYPE_EMAIL, IShare::TYPE_ROOM],
444-
true
445-
)
446-
&& $share->getPermissions() & Constants::PERMISSION_UPDATE
447-
&& $share->getNode()->getPermissions() & Constants::PERMISSION_UPDATE);
448-
} catch (ShareNotFound|NotFoundException $e) {
449-
return false;
450-
}
460+
private function hasUpdatePermissions(IShare $share): bool {
461+
return (
462+
in_array(
463+
$share->getShareType(),
464+
[IShare::TYPE_LINK, IShare::TYPE_EMAIL, IShare::TYPE_ROOM],
465+
true
466+
)
467+
&& $share->getPermissions() & Constants::PERMISSION_UPDATE
468+
&& $share->getNode()->getPermissions() & Constants::PERMISSION_UPDATE);
451469
}
452470

453471
/**

tests/stub.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,9 @@ abstract class IVersion {
7272
abstract public function getSourceFile(): \OCP\Files\File;
7373
}
7474
}
75+
76+
namespace OCA\DAV\Connector\Sabre {
77+
class PublicAuth {
78+
public const DAV_AUTHENTICATED = '';
79+
}
80+
}

0 commit comments

Comments
 (0)