Skip to content

Commit c1b9c92

Browse files
authored
Merge pull request #4142 from nextcloud/fix/check-new-file-share-29
[stable29] Add brute force protection for public file creation
2 parents 71e084d + 0c0913e commit c1b9c92

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

lib/Controller/DocumentAPIController.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
use OCA\Richdocuments\Helper;
3131
use OCA\Richdocuments\TemplateManager;
3232
use OCP\AppFramework\Http;
33+
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
34+
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
35+
use OCP\AppFramework\Http\Attribute\PublicPage;
3336
use OCP\AppFramework\Http\DataResponse;
3437
use OCP\AppFramework\Http\JSONResponse;
3538
use OCP\Files\Folder;
@@ -40,29 +43,25 @@
4043
use OCP\Files\Lock\NoLockProviderException;
4144
use OCP\IL10N;
4245
use OCP\IRequest;
46+
use OCP\ISession;
4347
use OCP\PreConditionNotMetException;
4448
use OCP\Share\IManager;
4549
use Psr\Log\LoggerInterface;
4650
use Throwable;
4751

4852
class DocumentAPIController extends \OCP\AppFramework\OCSController {
49-
private $rootFolder;
50-
private $shareManager;
51-
private $templateManager;
52-
private $l10n;
53-
private $logger;
54-
private $lockManager;
55-
private $userId;
56-
57-
public function __construct(IRequest $request, IRootFolder $rootFolder, IManager $shareManager, TemplateManager $templateManager, IL10N $l10n, LoggerInterface $logger, ILockManager $lockManager, $userId) {
53+
public function __construct(
54+
IRequest $request,
55+
private IRootFolder $rootFolder,
56+
private IManager $shareManager,
57+
private TemplateManager $templateManager,
58+
private IL10N $l10n,
59+
private LoggerInterface $logger,
60+
private ILockManager $lockManager,
61+
private ISession $session,
62+
private ?string $userId
63+
) {
5864
parent::__construct(Application::APPNAME, $request);
59-
$this->rootFolder = $rootFolder;
60-
$this->shareManager = $shareManager;
61-
$this->templateManager = $templateManager;
62-
$this->l10n = $l10n;
63-
$this->logger = $logger;
64-
$this->lockManager = $lockManager;
65-
$this->userId = $userId;
6665
}
6766

6867
/**
@@ -71,14 +70,26 @@ public function __construct(IRequest $request, IRootFolder $rootFolder, IManager
7170
* As the server template API for file creation is not available there, we need a dedicated API
7271
* in order to properly create files as public page visitors. This is being called in the new file
7372
* actions in src/view/NewFileMenu.js
74-
*
75-
* @NoAdminRequired
76-
* @PublicPage
7773
*/
74+
#[NoAdminRequired]
75+
#[PublicPage]
76+
#[BruteForceProtection(action: 'richdocumentsCreatePublic')]
7877
public function create(string $mimeType, string $fileName, string $directoryPath = '/', ?string $shareToken = null, ?int $templateId = null): JSONResponse {
7978
try {
8079
if ($shareToken !== null) {
8180
$share = $this->shareManager->getShareByToken($shareToken);
81+
82+
if ($share->getPassword()) {
83+
if (!$this->session->exists('public_link_authenticated')
84+
|| $this->session->get('public_link_authenticated') !== (string)$share->getId()
85+
) {
86+
throw new Exception('Invalid password');
87+
}
88+
}
89+
90+
if (!($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE)) {
91+
throw new Exception('No create permissions');
92+
}
8293
}
8394

8495
$rootFolder = $shareToken !== null ? $share->getNode() : $this->rootFolder->getUserFolder($this->userId);
@@ -156,7 +167,7 @@ public function create(string $mimeType, string $fileName, string $directoryPath
156167
]);
157168
}
158169

159-
#[Http\Attribute\NoAdminRequired]
170+
#[NoAdminRequired]
160171
public function openLocal(int $fileId): DataResponse {
161172
try {
162173
$files = $this->rootFolder->getUserFolder($this->userId)->getById($fileId);

0 commit comments

Comments
 (0)