Skip to content

Commit c9bad43

Browse files
committed
fix(files_sharing): make legacy downloadShare endpoint compatible with legacy behavior
This needs to be able to directly download files if specified to only download a single file and not a folder. Also it was possible to either pass a files array json encoded or a single file not encoded at all. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent ea9e2bb commit c9bad43

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

apps/files_sharing/lib/Controller/ShareController.php

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCA\Files_Sharing\Event\ShareLinkAccessedEvent;
1616
use OCP\Accounts\IAccountManager;
1717
use OCP\AppFramework\AuthPublicShareController;
18+
use OCP\AppFramework\Http;
1819
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
1920
use OCP\AppFramework\Http\Attribute\NoSameSiteCookieRequired;
2021
use OCP\AppFramework\Http\Attribute\OpenAPI;
@@ -30,6 +31,7 @@
3031
use OCP\Files\Folder;
3132
use OCP\Files\IRootFolder;
3233
use OCP\Files\NotFoundException;
34+
use OCP\Files\NotPermittedException;
3335
use OCP\HintException;
3436
use OCP\IConfig;
3537
use OCP\IL10N;
@@ -356,49 +358,75 @@ public function downloadShare(string $token, ?string $files = null, string $path
356358
$share = $this->shareManager->getShareByToken($token);
357359

358360
if (!($share->getPermissions() & Constants::PERMISSION_READ)) {
359-
return new DataResponse('Share has no read permission');
361+
return new DataResponse('Share has no read permission', Http::STATUS_FORBIDDEN);
360362
}
361363

362364
$attributes = $share->getAttributes();
363365
if ($attributes?->getAttribute('permissions', 'download') === false) {
364-
return new DataResponse('Share has no download permission');
366+
return new DataResponse('Share has no download permission', Http::STATUS_FORBIDDEN);
365367
}
366368

367369
if (!$this->validateShare($share)) {
368370
throw new NotFoundException();
369371
}
370372

373+
if ($share->getHideDownload()) {
374+
// download API does not work if hidden - use the DAV endpoint for previews
375+
throw new NotFoundException();
376+
}
377+
371378
$node = $share->getNode();
372-
if ($node instanceof Folder) {
373-
// Directory share
379+
if ($path !== '') {
380+
if (!$node instanceof Folder) {
381+
return new NotFoundResponse();
382+
}
374383

375-
// Try to get the path
376-
if ($path !== '') {
384+
try {
385+
$node = $node->get($path);
386+
} catch (NotFoundException|NotPermittedException) {
387+
$this->emitAccessShareHook($share, 404, 'Share not found');
388+
$this->emitShareAccessEvent($share, self::SHARE_DOWNLOAD, 404, 'Share not found');
389+
return new NotFoundResponse();
390+
}
391+
}
392+
393+
if ($files !== null) {
394+
if (!$node instanceof Folder) {
395+
return new NotFoundResponse();
396+
}
397+
398+
$filesParam = json_decode($files, true);
399+
if (!is_array($filesParam)) {
377400
try {
378-
$node = $node->get($path);
379-
} catch (NotFoundException $e) {
401+
// legacy wise this allows also passing the filename
402+
$node = $node->get($files);
403+
$files = null;
404+
} catch (NotFoundException|NotPermittedException) {
380405
$this->emitAccessShareHook($share, 404, 'Share not found');
381406
$this->emitShareAccessEvent($share, self::SHARE_DOWNLOAD, 404, 'Share not found');
382407
return new NotFoundResponse();
383408
}
384409
}
385-
386-
if ($node instanceof Folder) {
387-
if ($files === null || $files === '') {
388-
if ($share->getHideDownload()) {
389-
throw new NotFoundException('Downloading a folder');
390-
}
391-
}
392-
}
393410
}
394411

395412
$this->emitAccessShareHook($share);
396413
$this->emitShareAccessEvent($share, self::SHARE_DOWNLOAD);
397414

398-
$davUrl = '/public.php/dav/files/' . $token . '/?accept=zip';
415+
$davPath = '';
416+
if ($node !== $share->getNode()) {
417+
$davPath = substr($node->getPath(), strlen($share->getNode()->getPath()));
418+
}
419+
420+
$params = [];
399421
if ($files !== null) {
400-
$davUrl .= '&files=' . $files;
422+
$params['files'] = $files;
401423
}
424+
if ($node instanceof Folder) {
425+
$params['accept'] = 'zip';
426+
}
427+
428+
$davUrl = '/public.php/dav/files/' . $token . $davPath;
429+
$davUrl .= '?' . http_build_query($params);
402430
return new RedirectResponse($this->urlGenerator->getAbsoluteURL($davUrl));
403431
}
404432
}

apps/files_sharing/tests/Controller/ShareControllerTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCP\Accounts\IAccountManager;
1919
use OCP\Accounts\IAccountProperty;
2020
use OCP\Activity\IManager;
21+
use OCP\AppFramework\Http;
2122
use OCP\AppFramework\Http\ContentSecurityPolicy;
2223
use OCP\AppFramework\Http\DataResponse;
2324
use OCP\AppFramework\Http\Template\ExternalShareMenuAction;
@@ -691,7 +692,9 @@ public function testShowShareInvalid(): void {
691692
->with('token')
692693
->willReturn($share);
693694

694-
$this->userManager->method('get')->with('ownerUID')->willReturn($owner);
695+
$this->userManager->method('get')
696+
->with('ownerUID')
697+
->willReturn($owner);
695698

696699
$this->shareController->showShare();
697700
}
@@ -712,7 +715,7 @@ public function testDownloadShareWithCreateOnlyShare(): void {
712715

713716
// Test with a password protected share and no authentication
714717
$response = $this->shareController->downloadShare('validtoken');
715-
$expectedResponse = new DataResponse('Share has no read permission');
718+
$expectedResponse = new DataResponse('Share has no read permission', Http::STATUS_FORBIDDEN);
716719
$this->assertEquals($expectedResponse, $response);
717720
}
718721

@@ -740,7 +743,7 @@ public function testDownloadShareWithoutDownloadPermission(): void {
740743

741744
// Test with a password protected share and no authentication
742745
$response = $this->shareController->downloadShare('validtoken');
743-
$expectedResponse = new DataResponse('Share has no download permission');
746+
$expectedResponse = new DataResponse('Share has no download permission', Http::STATUS_FORBIDDEN);
744747
$this->assertEquals($expectedResponse, $response);
745748
}
746749

0 commit comments

Comments
 (0)