Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
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;
Expand Down Expand Up @@ -110,21 +110,14 @@ public function beforeController($controller, $methodName) {
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'
);
'core',
'429',
[],
TemplateResponse::RENDER_AS_GUEST
);
$response->setStatus($exception->getCode());
}

Expand Down
4 changes: 3 additions & 1 deletion lib/private/Template/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ protected function load($file, $additionalParams = null) {
if (!is_null($additionalParams)) {
$_ = array_merge($additionalParams, $this->vars);
foreach ($_ as $var => $value) {
${$var} = $value;
if (!isset(${$var})) {
${$var} = $value;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
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;
Expand Down Expand Up @@ -250,11 +253,7 @@ public function testAfterExceptionWithJsonBody() {
->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);
}
Expand All @@ -271,13 +270,12 @@ public function testAfterExceptionWithHtmlBody() {
$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());
}
}