Skip to content

Commit fed38a7

Browse files
DeepDiver1975jnweiger
authored andcommitted
fix: disallow http api requests for user external storages in case disabled (#41250)
(cherry picked from commit 32e12ef)
1 parent 7aef91e commit fed38a7

File tree

3 files changed

+74
-23
lines changed

3 files changed

+74
-23
lines changed

apps/files_external/lib/Controller/UserStoragesController.php

+33-11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use OCP\Files\External\IStorageConfig;
3232
use OCP\Files\External\NotFoundException;
3333
use OCP\Files\External\Service\IUserStoragesService;
34+
use OCP\IConfig;
3435
use OCP\IL10N;
3536
use OCP\ILogger;
3637
use OCP\IRequest;
@@ -44,23 +45,15 @@ class UserStoragesController extends StoragesController {
4445
* @var IUserSession
4546
*/
4647
private $userSession;
48+
private IConfig $config;
4749

48-
/**
49-
* Creates a new user storages controller.
50-
*
51-
* @param string $AppName application name
52-
* @param IRequest $request request object
53-
* @param IL10N $l10n l10n service
54-
* @param IUserStoragesService $userStoragesService storage service
55-
* @param IUserSession $userSession
56-
* @param ILogger $logger
57-
*/
5850
public function __construct(
5951
$AppName,
6052
IRequest $request,
6153
IL10N $l10n,
6254
IUserStoragesService $userStoragesService,
6355
IUserSession $userSession,
56+
IConfig $config,
6457
ILogger $logger
6558
) {
6659
parent::__construct(
@@ -71,6 +64,7 @@ public function __construct(
7164
$logger
7265
);
7366
$this->userSession = $userSession;
67+
$this->config = $config;
7468
}
7569

7670
protected function manipulateStorageConfig(IStorageConfig $storage) {
@@ -88,6 +82,12 @@ protected function manipulateStorageConfig(IStorageConfig $storage) {
8882
* @return DataResponse
8983
*/
9084
public function index() {
85+
if (!$this->isUserMountingAllowed()) {
86+
return new DataResponse(
87+
null,
88+
Http::STATUS_FORBIDDEN
89+
);
90+
}
9191
return parent::index();
9292
}
9393

@@ -122,8 +122,13 @@ public function create(
122122
$backendOptions,
123123
$mountOptions
124124
) {
125+
if (!$this->isUserMountingAllowed()) {
126+
return new DataResponse(
127+
null,
128+
Http::STATUS_FORBIDDEN
129+
);
130+
}
125131
$canCreateNewLocalStorage = \OC::$server->getConfig()->getSystemValue('files_external_allow_create_new_local', false);
126-
127132
if ($backend === 'local' && $canCreateNewLocalStorage === false) {
128133
return new DataResponse(
129134
null,
@@ -183,6 +188,12 @@ public function update(
183188
$mountOptions,
184189
$testOnly = true
185190
) {
191+
if (!$this->isUserMountingAllowed()) {
192+
return new DataResponse(
193+
null,
194+
Http::STATUS_FORBIDDEN
195+
);
196+
}
186197
$storage = $this->createStorage(
187198
$mountPoint,
188199
$backend,
@@ -230,6 +241,17 @@ public function update(
230241
* {@inheritdoc}
231242
*/
232243
public function destroy($id) {
244+
if (!$this->isUserMountingAllowed()) {
245+
return new DataResponse(
246+
null,
247+
Http::STATUS_FORBIDDEN
248+
);
249+
}
250+
233251
return parent::destroy($id);
234252
}
253+
254+
private function isUserMountingAllowed(): bool {
255+
return $this->config->getAppValue('files_external', 'allow_user_mounting', 'yes') === 'yes';
256+
}
235257
}

apps/files_external/tests/Controller/StoragesControllerTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use OCP\Files\External\Service\IGlobalStoragesService;
3232
use OCP\Files\External\Service\IStoragesService;
3333
use OCP\Files\StorageNotAvailableException;
34+
use PHPUnit\Framework\MockObject\MockObject;
3435

3536
abstract class StoragesControllerTest extends \Test\TestCase {
3637
/**
@@ -39,7 +40,7 @@ abstract class StoragesControllerTest extends \Test\TestCase {
3940
protected $controller;
4041

4142
/**
42-
* @var IGlobalStoragesService
43+
* @var IGlobalStoragesService | MockObject
4344
*/
4445
protected $service;
4546

apps/files_external/tests/Controller/UserStoragesControllerTest.php

+39-11
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,56 @@
3131
use OCP\Files\External\IStorageConfig;
3232
use OCP\Files\External\Service\IStoragesService;
3333
use OCP\Files\StorageNotAvailableException;
34+
use OCP\ILogger;
35+
use OCP\IConfig;
36+
use OCP\IUserSession;
37+
use OCP\IL10N;
38+
use OCP\IRequest;
39+
use OCP\Files\External\Service\IUserStoragesService;
3440

3541
class UserStoragesControllerTest extends StoragesControllerTest {
36-
/**
37-
* @var array
38-
*/
39-
private $oldAllowedBackends;
40-
4142
public function setUp(): void {
4243
parent::setUp();
43-
$this->service = $this->createMock('\OCP\Files\External\Service\IUserStoragesService');
44-
44+
$this->service = $this->createMock(IUserStoragesService::class);
4545
$this->service->method('getVisibilityType')
4646
->willReturn(IStoragesBackendService::VISIBILITY_PERSONAL);
4747

48+
$this->config = $this->createMock(IConfig::class);
49+
$this->config->method('getAppValue')->willReturn('yes');
50+
4851
$this->controller = new UserStoragesController(
4952
'files_external',
50-
$this->createMock('\OCP\IRequest'),
51-
$this->createMock('\OCP\IL10N'),
53+
$this->createMock(IRequest::class),
54+
$this->createMock(IL10N::class),
5255
$this->service,
53-
$this->createMock('\OCP\IUserSession'),
54-
$this->createMock('\OCP\ILogger')
56+
$this->createMock(IUserSession::class),
57+
$this->config,
58+
$this->createMock(ILogger::class)
59+
);
60+
}
61+
62+
public function testApiWhenDisabled(): void {
63+
$config = $this->createMock(IConfig::class);
64+
$config->method('getAppValue')->willReturn('no');
65+
66+
$controller = new UserStoragesController(
67+
'files_external',
68+
$this->createMock(IRequest::class),
69+
$this->createMock(IL10N::class),
70+
$this->service,
71+
$this->createMock(IUserSession::class),
72+
$config,
73+
$this->createMock(ILogger::class)
74+
);
75+
76+
$resp = $controller->create(
77+
'',
78+
'',
79+
'',
80+
[],
81+
[],
5582
);
83+
$this->assertEquals(Http::STATUS_FORBIDDEN, $resp->getStatus());
5684
}
5785

5886
public function testAddOrUpdateStorageDisallowedBackend() {

0 commit comments

Comments
 (0)