Skip to content
Open
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
6 changes: 6 additions & 0 deletions apps/dav/lib/Connector/Sabre/PublicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function __construct(

/**
* @throws NotAuthenticated
* @throws NotFound
* @throws MaxDelayReached
* @throws ServiceUnavailable
*/
Expand Down Expand Up @@ -85,6 +86,11 @@ public function check(RequestInterface $request, ResponseInterface $response): a
} catch (NotAuthenticated|MaxDelayReached $e) {
$this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
throw $e;
} catch (ShareNotFound) {
$this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
throw new NotFound();
} catch (NotFound $e) {
throw $e;
} catch (PreconditionFailed $e) {
$response->setHeader(
'Location',
Expand Down
65 changes: 65 additions & 0 deletions apps/dav/tests/unit/Connector/Sabre/PublicAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use OCP\Share\IShare;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Sabre\HTTP\Request;
use Sabre\HTTP\Response;

/**
* Class PublicAuthTest
Expand Down Expand Up @@ -126,6 +128,69 @@ public function testCheckTokenInvalidShare(): void {
self::invokePrivate($this->auth, 'checkToken');
}

public function testCheckShareNotFound(): void {
$this->request->method('getPathInfo')
->willReturn('/dav/files/GX9HSGQrGE');
$this->request->method('getRemoteAddress')
->willReturn('1.2.3.4');

$this->shareManager->method('getShareByToken')
->with('GX9HSGQrGE')
->willThrowException(new ShareNotFound());

$this->throttler->expects($this->once())
->method('registerAttempt')
->with('public_dav_auth', '1.2.3.4');
$this->logger->expects($this->never())
->method('error');

$this->expectException(\Sabre\DAV\Exception\NotFound::class);
$this->auth->check(new Request('GET', '/dav/files/GX9HSGQrGE'), new Response());
}

public function testCheckShareNotFoundWithoutStrictCookie(): void {
$cookies = $_COOKIE;
$_COOKIE = ['foo' => 'bar'];

$this->request->method('getPathInfo')
->willReturn('/dav/files/GX9HSGQrGE');
$this->request->method('getRemoteAddress')
->willReturn('1.2.3.4');
$this->request->method('passesStrictCookieCheck')
->willReturn(false);

$this->shareManager->method('getShareByToken')
->with('GX9HSGQrGE')
->willThrowException(new ShareNotFound());

$this->throttler->expects($this->once())
->method('registerAttempt')
->with('public_dav_auth', '1.2.3.4');
$this->logger->expects($this->never())
->method('error');

try {
$this->expectException(\Sabre\DAV\Exception\NotFound::class);
$this->auth->check(new Request('GET', '/dav/files/GX9HSGQrGE'), new Response());
} finally {
$_COOKIE = $cookies;
}
}

public function testCheckUnexpectedException(): void {
$this->request->method('getPathInfo')
->willReturn('/dav/files/GX9HSGQrGE');

$this->shareManager->method('getShareByToken')
->willThrowException(new \RuntimeException('Database is gone'));

$this->logger->expects($this->once())
->method('error');

$this->expectException(\Sabre\DAV\Exception\ServiceUnavailable::class);
$this->auth->check(new Request('GET', '/dav/files/GX9HSGQrGE'), new Response());
}

public function testCheckTokenAlreadyAuthenticated(): void {
$this->request->method('getPathInfo')
->willReturn('/dav/files/GX9HSGQrGE');
Expand Down
Loading