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

[stable27] fix(bruteforce-protection): Don't throw a 500 when MaxDelayReached is… #42083

Merged
merged 1 commit into from
Dec 7, 2023
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
17 changes: 17 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
require_once __DIR__ . '/lib/versioncheck.php';

use OCP\Security\Bruteforce\MaxDelayReached;

try {
require_once __DIR__ . '/lib/base.php';

Expand Down Expand Up @@ -67,6 +69,21 @@
exit();
}
OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), 401);
} catch (MaxDelayReached $ex) {
$request = \OC::$server->getRequest();
/**
* Routes with the @CORS annotation and other API endpoints should
* not return a webpage, so we only print the error page when html is accepted,
* otherwise we reply with a JSON array like the BruteForceMiddleware would do.
*/
if (stripos($request->getHeader('Accept'), 'html') === false) {
http_response_code(429);
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['message' => $ex->getMessage()]);
exit();
}
http_response_code(429);
OC_Template::printGuestPage('core', '429');
} catch (Exception $ex) {
\OC::$server->getLogger()->logException($ex, ['app' => 'index']);

Expand Down
6 changes: 5 additions & 1 deletion ocs/v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
exit;
}

use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use OCP\Security\Bruteforce\MaxDelayReached;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

/*
* Try the appframework routes
Expand All @@ -62,6 +63,9 @@
}

OC::$server->get(\OC\Route\Router::class)->match('/ocsapp'.\OC::$server->getRequest()->getRawPathInfo());
} catch (MaxDelayReached $ex) {
$format = \OC::$server->getRequest()->getParam('format', 'xml');
OC_API::respond(new \OC\OCS\Result(null, OCP\AppFramework\Http::STATUS_TOO_MANY_REQUESTS, $ex->getMessage()), $format);
} catch (ResourceNotFoundException $e) {
OC_API::setContentType();

Expand Down
Loading