]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix ratelimit template
authorJoas Schilling <coding@schilljs.com>
Fri, 23 Apr 2021 08:21:14 +0000 (10:21 +0200)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Tue, 27 Apr 2021 13:39:52 +0000 (13:39 +0000)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/AppFramework/Middleware/Security/RateLimitingMiddleware.php
tests/lib/AppFramework/Middleware/Security/RateLimitingMiddlewareTest.php

index 712becb3be53cd8aae8a93fdd8ee09bd9d1b907e..f5960880546c33878e6cdd5a6fb0ffd8cdba677d 100644 (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,21 +110,14 @@ 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'
-                                       );
+                                       'core',
+                                       '429',
+                                       [],
+                                       TemplateResponse::RENDER_AS_GUEST
+                               );
                                $response->setStatus($exception->getCode());
                        }
 
index 0d27a9a070f365066fc720d31ca4a551f254f19b..aa713b99156feb782cfa84a9cf938f0d8052b0ee 100644 (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());
        }
 }