Skip to content

Commit cfca01c

Browse files
committed
fix: disable direct link if link shares are disabled
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 0131bd9 commit cfca01c

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

apps/dav/lib/Controller/DirectController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use OCP\IRequest;
2626
use OCP\IURLGenerator;
2727
use OCP\Security\ISecureRandom;
28+
use OCP\Share\IManager;
2829

2930
class DirectController extends OCSController {
3031

@@ -38,6 +39,7 @@ public function __construct(
3839
private ITimeFactory $timeFactory,
3940
private IURLGenerator $urlGenerator,
4041
private IEventDispatcher $eventDispatcher,
42+
private IManager $shareManager,
4143
) {
4244
parent::__construct($appName, $request);
4345
}
@@ -56,6 +58,10 @@ public function __construct(
5658
*/
5759
#[NoAdminRequired]
5860
public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse {
61+
if (!$this->shareManager->shareApiAllowLinks()) {
62+
throw new OCSForbiddenException('Creating direct links is disabled');
63+
}
64+
5965
$userFolder = $this->rootFolder->getUserFolder($this->userId);
6066

6167
$file = $userFolder->getFirstNodeById($fileId);

apps/dav/tests/unit/Controller/DirectControllerTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCA\DAV\Db\DirectMapper;
1414
use OCP\AppFramework\Http\DataResponse;
1515
use OCP\AppFramework\OCS\OCSBadRequestException;
16+
use OCP\AppFramework\OCS\OCSForbiddenException;
1617
use OCP\AppFramework\OCS\OCSNotFoundException;
1718
use OCP\AppFramework\Utility\ITimeFactory;
1819
use OCP\EventDispatcher\IEventDispatcher;
@@ -22,6 +23,7 @@
2223
use OCP\IRequest;
2324
use OCP\IURLGenerator;
2425
use OCP\Security\ISecureRandom;
26+
use OCP\Share\IManager;
2527
use PHPUnit\Framework\MockObject\MockObject;
2628
use Test\TestCase;
2729

@@ -32,6 +34,7 @@ class DirectControllerTest extends TestCase {
3234
private ITimeFactory&MockObject $timeFactory;
3335
private IURLGenerator&MockObject $urlGenerator;
3436
private IEventDispatcher&MockObject $eventDispatcher;
37+
private IManager&MockObject $shareManager;
3538

3639
private DirectController $controller;
3740

@@ -44,6 +47,7 @@ protected function setUp(): void {
4447
$this->timeFactory = $this->createMock(ITimeFactory::class);
4548
$this->urlGenerator = $this->createMock(IURLGenerator::class);
4649
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
50+
$this->shareManager = $this->createMock(IManager::class);
4751

4852
$this->controller = new DirectController(
4953
'dav',
@@ -54,11 +58,15 @@ protected function setUp(): void {
5458
$this->random,
5559
$this->timeFactory,
5660
$this->urlGenerator,
57-
$this->eventDispatcher
61+
$this->eventDispatcher,
62+
$this->shareManager,
5863
);
5964
}
6065

6166
public function testGetUrlNonExistingFileId(): void {
67+
$this->shareManager->method('shareApiAllowLinks')
68+
->willReturn(true);
69+
6270
$userFolder = $this->createMock(Folder::class);
6371
$this->rootFolder->method('getUserFolder')
6472
->with('awesomeUser')
@@ -73,6 +81,9 @@ public function testGetUrlNonExistingFileId(): void {
7381
}
7482

7583
public function testGetUrlForFolder(): void {
84+
$this->shareManager->method('shareApiAllowLinks')
85+
->willReturn(true);
86+
7687
$userFolder = $this->createMock(Folder::class);
7788
$this->rootFolder->method('getUserFolder')
7889
->with('awesomeUser')
@@ -89,6 +100,9 @@ public function testGetUrlForFolder(): void {
89100
}
90101

91102
public function testGetUrlValid(): void {
103+
$this->shareManager->method('shareApiAllowLinks')
104+
->willReturn(true);
105+
92106
$userFolder = $this->createMock(Folder::class);
93107
$this->rootFolder->method('getUserFolder')
94108
->with('awesomeUser')
@@ -135,4 +149,23 @@ public function testGetUrlValid(): void {
135149
'url' => 'https://my.nextcloud/remote.php/direct/superduperlongtoken',
136150
], $result->getData());
137151
}
152+
153+
public function testGetUrlNoLinkShares(): void {
154+
$this->shareManager->method('shareApiAllowLinks')
155+
->willReturn(false);
156+
157+
$userFolder = $this->createMock(Folder::class);
158+
$this->rootFolder->method('getUserFolder')
159+
->with('awesomeUser')
160+
->willReturn($userFolder);
161+
162+
$file = $this->createMock(File::class);
163+
164+
$userFolder->method('getFirstNodeById')
165+
->with(101)
166+
->willReturn($file);
167+
168+
$this->expectException(OCSForbiddenException::class);
169+
$this->controller->getUrl(101);
170+
}
138171
}

0 commit comments

Comments
 (0)