Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throttle on public DAV endpoint #27610

Merged
merged 2 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/dav/appinfo/v1/publicwebdav.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
$authBackend = new OCA\DAV\Connector\PublicAuth(
\OC::$server->getRequest(),
\OC::$server->getShareManager(),
\OC::$server->getSession()
\OC::$server->getSession(),
\OC::$server->getBruteForceThrottler()
);
$authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend);

Expand Down
16 changes: 15 additions & 1 deletion apps/dav/lib/Connector/PublicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
namespace OCA\DAV\Connector;

use OC\Security\Bruteforce\Throttler;
use OCP\IRequest;
use OCP\ISession;
use OCP\Share\Exceptions\ShareNotFound;
Expand All @@ -42,6 +43,7 @@
* @package OCA\DAV\Connector
*/
class PublicAuth extends AbstractBasic {
private const BRUTEFORCE_ACTION = 'public_webdav_auth';

/** @var \OCP\Share\IShare */
private $share;
Expand All @@ -55,17 +57,23 @@ class PublicAuth extends AbstractBasic {
/** @var IRequest */
private $request;

/** @var Throttler */
private $throttler;

/**
* @param IRequest $request
* @param IManager $shareManager
* @param ISession $session
* @param Throttler $throttler
*/
public function __construct(IRequest $request,
IManager $shareManager,
ISession $session) {
ISession $session,
Throttler $throttler) {
$this->request = $request;
$this->shareManager = $shareManager;
$this->session = $session;
$this->throttler = $throttler;

// setup realm
$defaults = new \OCP\Defaults();
Expand All @@ -85,9 +93,12 @@ public function __construct(IRequest $request,
* @throws \Sabre\DAV\Exception\NotAuthenticated
*/
protected function validateUserPass($username, $password) {
$this->throttler->sleepDelayOrThrowOnMax($this->request->getRemoteAddress(), self::BRUTEFORCE_ACTION);

try {
$share = $this->shareManager->getShareByToken($username);
} catch (ShareNotFound $e) {
$this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
return false;
}

Expand All @@ -112,11 +123,14 @@ protected function validateUserPass($username, $password) {
header('WWW-Authenticate: DummyBasic realm="' . $this->realm . '"');
throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
}

$this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
return false;
}
} elseif ($share->getShareType() === IShare::TYPE_REMOTE) {
return true;
} else {
$this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
return false;
}
} else {
Expand Down
9 changes: 8 additions & 1 deletion apps/dav/tests/unit/Connector/PublicAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
namespace OCA\DAV\Tests\unit\Connector;

use OC\Security\Bruteforce\Throttler;
use OCP\IRequest;
use OCP\ISession;
use OCP\Share\Exceptions\ShareNotFound;
Expand All @@ -49,6 +50,8 @@ class PublicAuthTest extends \Test\TestCase {
private $shareManager;
/** @var \OCA\DAV\Connector\PublicAuth */
private $auth;
/** @var Throttler|\PHPUnit\Framework\MockObject\MockObject */
private $throttler;

/** @var string */
private $oldUser;
Expand All @@ -65,11 +68,15 @@ protected function setUp(): void {
$this->shareManager = $this->getMockBuilder(IManager::class)
->disableOriginalConstructor()
->getMock();
$this->throttler = $this->getMockBuilder(Throttler::class)
->disableOriginalConstructor()
->getMock();

$this->auth = new \OCA\DAV\Connector\PublicAuth(
$this->request,
$this->shareManager,
$this->session
$this->session,
$this->throttler
);

// Store current user
Expand Down