Skip to content

Commit cf9e05e

Browse files
committed
refactor(ViewOnlyPlugin): Cleanup checks
Signed-off-by: provokateurin <kate@provokateurin.de>
1 parent 515768b commit cf9e05e

2 files changed

Lines changed: 21 additions & 26 deletions

File tree

apps/dav/lib/DAV/ViewOnlyPlugin.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,26 @@ public function checkViewOnly(RequestInterface $request): bool {
8080
}
8181

8282
$storage = $node->getStorage();
83-
8483
if (!$storage->instanceOfStorage(ISharedStorage::class)) {
8584
return true;
8685
}
8786

88-
// Extract extra permissions
8987
/** @var ISharedStorage $storage */
9088
$share = $storage->getShare();
91-
$attributes = $share->getAttributes();
92-
if ($attributes === null) {
93-
return true;
94-
}
95-
96-
// We have two options here, if download is disabled, but viewing is allowed,
97-
// we still allow the GET request to return the file content.
98-
$canDownload = $attributes->getAttribute('permissions', 'download');
99-
if (!$share->canSeeContent()) {
100-
throw new Forbidden('Access to this shared resource has been denied because its download permission is disabled.');
101-
}
102-
103-
// If download is disabled, we disable the COPY and MOVE methods even if the
104-
// shareapi_allow_view_without_download is set to true.
105-
if ($request->getMethod() !== 'GET' && ($canDownload !== null && !$canDownload)) {
106-
throw new Forbidden('Access to this shared resource has been denied because its download permission is disabled.');
89+
switch ($request->getMethod()) {
90+
case 'GET':
91+
// If download is disabled, but viewing is allowed, we still allow the GET method to return the file content.
92+
if (!$share->canSeeContent()) {
93+
throw new Forbidden('Access to this shared resource has been denied because its download permission is disabled.');
94+
}
95+
break;
96+
case 'COPY':
97+
case 'MOVE':
98+
// If download is disabled, we disable the COPY and MOVE methods even if the shareapi_allow_view_without_download is set to true.
99+
if (!$share->canDownload()) {
100+
throw new Forbidden('Access to this shared resource has been denied because its download permission is disabled.');
101+
}
102+
break;
107103
}
108104
} catch (NotFound $e) {
109105
// File not found

apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use OCP\Files\Storage\ISharedStorage;
2121
use OCP\Files\Storage\IStorage;
2222
use OCP\IUser;
23-
use OCP\Share\IAttributes;
2423
use OCP\Share\IShare;
2524
use PHPUnit\Framework\MockObject\MockObject;
2625
use Sabre\DAV\Server;
@@ -136,6 +135,11 @@ public function testCanGet(bool $isVersion, ?bool $attrEnabled, bool $expectCanD
136135
$davNode->method('getNode')->willReturn($nodeInfo);
137136
}
138137

138+
$this->request
139+
->expects($this->once())
140+
->method('getMethod')
141+
->willReturn('GET');
142+
139143
$this->request->expects($this->once())->method('getPath')->willReturn($davPath);
140144

141145
$this->tree->expects($this->once())
@@ -151,16 +155,11 @@ public function testCanGet(bool $isVersion, ?bool $attrEnabled, bool $expectCanD
151155
$storage->method('instanceOfStorage')->with(ISharedStorage::class)->willReturn(true);
152156
$storage->method('getShare')->willReturn($share);
153157

154-
$extAttr = $this->createMock(IAttributes::class);
155-
$share->method('getAttributes')->willReturn($extAttr);
156-
$extAttr->expects($this->once())
157-
->method('getAttribute')
158-
->with('permissions', 'download')
159-
->willReturn($attrEnabled);
158+
$share->method('canDownload')->willReturn($attrEnabled ?? true);
160159

161160
$share->expects($this->once())
162161
->method('canSeeContent')
163-
->willReturn($allowViewWithoutDownload);
162+
->willReturn($allowViewWithoutDownload && ($attrEnabled ?? true));
164163

165164
if (!$expectCanDownloadFile) {
166165
$this->expectException(Forbidden::class);

0 commit comments

Comments
 (0)