Skip to content

Commit 4cc4a35

Browse files
benjaminfruehbackportbot[bot]
authored andcommitted
fix(AttachmentService): Validate share password for uploads
Signed-off-by: Benjamin Frueh <benjamin.frueh@gmail.com> [skip ci]
1 parent 3d083e6 commit 4cc4a35

1 file changed

Lines changed: 36 additions & 19 deletions

File tree

lib/Service/AttachmentService.php

Lines changed: 36 additions & 19 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;
@@ -310,9 +312,33 @@ public function uploadAttachment(int $documentId, string $newFileName, $newFileR
310312
* @throws NoUserException
311313
*/
312314
public function uploadAttachmentPublic(?int $documentId, string $newFileName, $newFileResource, string $shareToken): array {
313-
if (!$this->hasUpdatePermissions($shareToken)) {
315+
try {
316+
$share = $this->shareManager->getShareByToken($shareToken);
317+
} catch (ShareNotFound) {
318+
throw new NotFoundException('Share not found');
319+
}
320+
321+
if (!$this->hasUpdatePermissions($share)) {
314322
throw new NotPermittedException('No write permissions');
315323
}
324+
325+
if ($share->getPassword() !== null) {
326+
$key = PublicAuth::DAV_AUTHENTICATED;
327+
328+
if (!$this->session->exists($key)) {
329+
throw new NotPermittedException('Share not authenticated');
330+
}
331+
332+
$allowedShareIds = $this->session->get($key);
333+
if (!is_array($allowedShareIds)) {
334+
throw new NotPermittedException('Share not authenticated');
335+
}
336+
337+
if (!in_array($share->getId(), $allowedShareIds, true)) {
338+
throw new NotPermittedException('Share not authenticated');
339+
}
340+
}
341+
316342
$textFile = $this->getTextFilePublic($documentId, $shareToken);
317343
$saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
318344
$fileName = self::getUniqueFileName($saveDir, $newFileName);
@@ -398,25 +424,16 @@ public static function getUniqueFileName(Folder $dir, string $fileName): string
398424

399425
/**
400426
* Check if the shared access has write permissions
401-
*
402-
* @param string $shareToken
403-
*
404-
* @return bool
405427
*/
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-
}
428+
private function hasUpdatePermissions(IShare $share): bool {
429+
return (
430+
in_array(
431+
$share->getShareType(),
432+
[IShare::TYPE_LINK, IShare::TYPE_EMAIL, IShare::TYPE_ROOM],
433+
true
434+
)
435+
&& $share->getPermissions() & Constants::PERMISSION_UPDATE
436+
&& $share->getNode()->getPermissions() & Constants::PERMISSION_UPDATE);
420437
}
421438

422439
/**

0 commit comments

Comments
 (0)