Skip to content

Commit

Permalink
Merge pull request #4145 from nextcloud/fix/check-new-file-share-26
Browse files Browse the repository at this point in the history
[stable26] Add brute force protection for public file creation
  • Loading branch information
elzody authored Oct 18, 2024
2 parents 917fe2f + 83dd63a commit 931b420
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/Controller/DocumentAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use OCP\Files\Lock\NoLockProviderException;
use OCP\IL10N;
use OCP\IRequest;
use OCP\ISession;
use OCP\PreConditionNotMetException;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;
Expand All @@ -52,16 +53,18 @@ class DocumentAPIController extends \OCP\AppFramework\OCSController {
private $l10n;
private $logger;
private $lockManager;
private $session;
private $userId;

public function __construct(IRequest $request, IRootFolder $rootFolder, IManager $shareManager, TemplateManager $templateManager, IL10N $l10n, LoggerInterface $logger, ILockManager $lockManager, $userId) {
public function __construct(IRequest $request, IRootFolder $rootFolder, IManager $shareManager, TemplateManager $templateManager, IL10N $l10n, LoggerInterface $logger, ILockManager $lockManager, ISession $session, $userId) {
parent::__construct(Application::APPNAME, $request);
$this->rootFolder = $rootFolder;
$this->shareManager = $shareManager;
$this->templateManager = $templateManager;
$this->l10n = $l10n;
$this->logger = $logger;
$this->lockManager = $lockManager;
$this->session = $session;
$this->userId = $userId;
}

Expand All @@ -74,11 +77,24 @@ public function __construct(IRequest $request, IRootFolder $rootFolder, IManager
*
* @NoAdminRequired
* @PublicPage
* @BruteForceProtection(action=richdocumentsCreatePublic)
*/
public function create(string $mimeType, string $fileName, string $directoryPath = '/', string $shareToken = null, ?int $templateId = null): JSONResponse {
try {
if ($shareToken !== null) {
$share = $this->shareManager->getShareByToken($shareToken);

if ($share->getPassword()) {
if (!$this->session->exists('public_link_authenticated')
|| $this->session->get('public_link_authenticated') !== (string)$share->getId()
) {
throw new Exception('Invalid password');
}
}

if (!($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE)) {
throw new Exception('No create permissions');
}
}

$rootFolder = $shareToken !== null ? $share->getNode() : $this->rootFolder->getUserFolder($this->userId);
Expand Down

0 comments on commit 931b420

Please sign in to comment.