Skip to content

Commit e1812e4

Browse files
committed
fix: always validate share token if provided
Signed-off-by: Benjamin Frueh <benjamin.frueh@gmail.com>
1 parent 5395499 commit e1812e4

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

lib/Middleware/SessionMiddleware.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,9 @@ private function assertDocumentSession(ISessionAwareController $controller): voi
116116
*/
117117
private function assertUserOrShareToken(ISessionAwareController $controller): void {
118118
$documentId = (int)$this->request->getParam('documentId');
119-
if (null !== $userId = $this->userSession->getUser()?->getUID()) {
120-
// Check if user has access to document
121-
if (count($this->rootFolder->getUserFolder($userId)->getById($documentId)) === 0) {
122-
throw new InvalidSessionException();
123-
}
124-
$controller->setUserId($userId);
125-
} elseif ('' !== $shareToken = (string)$this->request->getParam('shareToken')) {
119+
$shareToken = (string)$this->request->getParam('shareToken');
120+
121+
if ($shareToken !== '') {
126122
try {
127123
$share = $this->shareManager->getShareByToken($shareToken);
128124
} catch (ShareNotFound) {
@@ -136,8 +132,9 @@ private function assertUserOrShareToken(ISessionAwareController $controller): vo
136132

137133
/** @psalm-suppress RedundantConditionGivenDocblockType */
138134
if ($share->getPassword() !== null) {
139-
$shareId = $this->session->get('public_link_authenticated');
140-
if ($share->getId() !== $shareId) {
135+
$shareIds = $this->session->get('public_link_authenticated');
136+
$shareIds = is_array($shareIds) ? $shareIds : [$shareIds];
137+
if (!in_array($share->getId(), $shareIds, true)) {
141138
throw new InvalidSessionException();
142139
}
143140
}
@@ -150,6 +147,12 @@ private function assertUserOrShareToken(ISessionAwareController $controller): vo
150147
if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
151148
throw new InvalidSessionException();
152149
}
150+
} elseif (null !== $userId = $this->userSession->getUser()?->getUID()) {
151+
// Check if user has access to document
152+
if (count($this->rootFolder->getUserFolder($userId)->getById($documentId)) === 0) {
153+
throw new InvalidSessionException();
154+
}
155+
$controller->setUserId($userId);
153156
} else {
154157
throw new InvalidSessionException();
155158
}

0 commit comments

Comments
 (0)