Merge pull request #26788 from nextcloud/backport/26718/stable20
[stable20] Fix ratelimit template
This commit is contained in:
commit
f8f32d79d7
|
@ -27,7 +27,7 @@ namespace OC\AppFramework\Middleware\Security;
|
||||||
use OC\AppFramework\Utility\ControllerMethodReflector;
|
use OC\AppFramework\Utility\ControllerMethodReflector;
|
||||||
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
|
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
|
||||||
use OC\Security\RateLimiting\Limiter;
|
use OC\Security\RateLimiting\Limiter;
|
||||||
use OCP\AppFramework\Http\JSONResponse;
|
use OCP\AppFramework\Http\DataResponse;
|
||||||
use OCP\AppFramework\Http\TemplateResponse;
|
use OCP\AppFramework\Http\TemplateResponse;
|
||||||
use OCP\AppFramework\Middleware;
|
use OCP\AppFramework\Middleware;
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
|
@ -110,21 +110,14 @@ class RateLimitingMiddleware extends Middleware {
|
||||||
public function afterException($controller, $methodName, \Exception $exception) {
|
public function afterException($controller, $methodName, \Exception $exception) {
|
||||||
if ($exception instanceof RateLimitExceededException) {
|
if ($exception instanceof RateLimitExceededException) {
|
||||||
if (stripos($this->request->getHeader('Accept'),'html') === false) {
|
if (stripos($this->request->getHeader('Accept'),'html') === false) {
|
||||||
$response = new JSONResponse(
|
$response = new DataResponse([], $exception->getCode());
|
||||||
[
|
|
||||||
'message' => $exception->getMessage(),
|
|
||||||
],
|
|
||||||
$exception->getCode()
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
$response = new TemplateResponse(
|
$response = new TemplateResponse(
|
||||||
'core',
|
'core',
|
||||||
'403',
|
'429',
|
||||||
[
|
[],
|
||||||
'file' => $exception->getMessage()
|
TemplateResponse::RENDER_AS_GUEST
|
||||||
],
|
);
|
||||||
'guest'
|
|
||||||
);
|
|
||||||
$response->setStatus($exception->getCode());
|
$response->setStatus($exception->getCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -168,7 +168,9 @@ class Base {
|
||||||
if (!is_null($additionalParams)) {
|
if (!is_null($additionalParams)) {
|
||||||
$_ = array_merge($additionalParams, $this->vars);
|
$_ = array_merge($additionalParams, $this->vars);
|
||||||
foreach ($_ as $var => $value) {
|
foreach ($_ as $var => $value) {
|
||||||
${$var} = $value;
|
if (!isset(${$var})) {
|
||||||
|
${$var} = $value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,13 +26,16 @@ use OC\AppFramework\Utility\ControllerMethodReflector;
|
||||||
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
|
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
|
||||||
use OC\Security\RateLimiting\Limiter;
|
use OC\Security\RateLimiting\Limiter;
|
||||||
use OCP\AppFramework\Controller;
|
use OCP\AppFramework\Controller;
|
||||||
use OCP\AppFramework\Http\JSONResponse;
|
use OCP\AppFramework\Http\DataResponse;
|
||||||
use OCP\AppFramework\Http\TemplateResponse;
|
use OCP\AppFramework\Http\TemplateResponse;
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
use OCP\IUser;
|
use OCP\IUser;
|
||||||
use OCP\IUserSession;
|
use OCP\IUserSession;
|
||||||
use Test\TestCase;
|
use Test\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group DB
|
||||||
|
*/
|
||||||
class RateLimitingMiddlewareTest extends TestCase {
|
class RateLimitingMiddlewareTest extends TestCase {
|
||||||
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
|
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
|
||||||
private $request;
|
private $request;
|
||||||
|
@ -250,11 +253,7 @@ class RateLimitingMiddlewareTest extends TestCase {
|
||||||
->willReturn('JSON');
|
->willReturn('JSON');
|
||||||
|
|
||||||
$result = $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new RateLimitExceededException());
|
$result = $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new RateLimitExceededException());
|
||||||
$expected = new JSONResponse(
|
$expected = new DataResponse([], 429
|
||||||
[
|
|
||||||
'message' => 'Rate limit exceeded',
|
|
||||||
],
|
|
||||||
429
|
|
||||||
);
|
);
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
@ -271,13 +270,12 @@ class RateLimitingMiddlewareTest extends TestCase {
|
||||||
$result = $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new RateLimitExceededException());
|
$result = $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new RateLimitExceededException());
|
||||||
$expected = new TemplateResponse(
|
$expected = new TemplateResponse(
|
||||||
'core',
|
'core',
|
||||||
'403',
|
'429',
|
||||||
[
|
[],
|
||||||
'file' => 'Rate limit exceeded',
|
TemplateResponse::RENDER_AS_GUEST
|
||||||
],
|
|
||||||
'guest'
|
|
||||||
);
|
);
|
||||||
$expected->setStatus(429);
|
$expected->setStatus(429);
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
|
$this->assertIsString($result->render());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue