Merge pull request #26788 from nextcloud/backport/26718/stable20

[stable20] Fix ratelimit template
This commit is contained in:
Joas Schilling 2021-04-28 09:54:14 +02:00 committed by GitHub
commit f8f32d79d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 26 deletions

View File

@ -27,7 +27,7 @@ namespace OC\AppFramework\Middleware\Security;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
use OC\Security\RateLimiting\Limiter;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Middleware;
use OCP\IRequest;
@ -110,20 +110,13 @@ class RateLimitingMiddleware extends Middleware {
public function afterException($controller, $methodName, \Exception $exception) {
if ($exception instanceof RateLimitExceededException) {
if (stripos($this->request->getHeader('Accept'),'html') === false) {
$response = new JSONResponse(
[
'message' => $exception->getMessage(),
],
$exception->getCode()
);
$response = new DataResponse([], $exception->getCode());
} else {
$response = new TemplateResponse(
'core',
'403',
[
'file' => $exception->getMessage()
],
'guest'
'429',
[],
TemplateResponse::RENDER_AS_GUEST
);
$response->setStatus($exception->getCode());
}

View File

@ -168,9 +168,11 @@ class Base {
if (!is_null($additionalParams)) {
$_ = array_merge($additionalParams, $this->vars);
foreach ($_ as $var => $value) {
if (!isset(${$var})) {
${$var} = $value;
}
}
}
// Include
ob_start();

View File

@ -26,13 +26,16 @@ use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
use OC\Security\RateLimiting\Limiter;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use Test\TestCase;
/**
* @group DB
*/
class RateLimitingMiddlewareTest extends TestCase {
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
private $request;
@ -250,11 +253,7 @@ class RateLimitingMiddlewareTest extends TestCase {
->willReturn('JSON');
$result = $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new RateLimitExceededException());
$expected = new JSONResponse(
[
'message' => 'Rate limit exceeded',
],
429
$expected = new DataResponse([], 429
);
$this->assertEquals($expected, $result);
}
@ -271,13 +270,12 @@ class RateLimitingMiddlewareTest extends TestCase {
$result = $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new RateLimitExceededException());
$expected = new TemplateResponse(
'core',
'403',
[
'file' => 'Rate limit exceeded',
],
'guest'
'429',
[],
TemplateResponse::RENDER_AS_GUEST
);
$expected->setStatus(429);
$this->assertEquals($expected, $result);
$this->assertIsString($result->render());
}
}