RateLimitingMiddlewareTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\AppFramework\Middleware\Security;
  8. use OC\AppFramework\Middleware\Security\RateLimitingMiddleware;
  9. use OC\AppFramework\Utility\ControllerMethodReflector;
  10. use OC\Security\RateLimiting\Exception\RateLimitExceededException;
  11. use OC\Security\RateLimiting\Limiter;
  12. use OCP\AppFramework\Controller;
  13. use OCP\AppFramework\Http\Attribute\AnonRateLimit;
  14. use OCP\AppFramework\Http\Attribute\UserRateLimit;
  15. use OCP\AppFramework\Http\DataResponse;
  16. use OCP\AppFramework\Http\TemplateResponse;
  17. use OCP\IRequest;
  18. use OCP\ISession;
  19. use OCP\IUser;
  20. use OCP\IUserSession;
  21. use PHPUnit\Framework\MockObject\MockObject;
  22. use Test\TestCase;
  23. class TestRateLimitController extends Controller {
  24. /**
  25. * @UserRateThrottle(limit=20, period=200)
  26. * @AnonRateThrottle(limit=10, period=100)
  27. */
  28. public function testMethodWithAnnotation() {
  29. }
  30. /**
  31. * @AnonRateThrottle(limit=10, period=100)
  32. */
  33. public function testMethodWithAnnotationFallback() {
  34. }
  35. public function testMethodWithoutAnnotation() {
  36. }
  37. #[UserRateLimit(limit: 20, period: 200)]
  38. #[AnonRateLimit(limit: 10, period: 100)]
  39. public function testMethodWithAttributes() {
  40. }
  41. #[AnonRateLimit(limit: 10, period: 100)]
  42. public function testMethodWithAttributesFallback() {
  43. }
  44. }
  45. /**
  46. * @group DB
  47. */
  48. class RateLimitingMiddlewareTest extends TestCase {
  49. private IRequest|MockObject $request;
  50. private IUserSession|MockObject $userSession;
  51. private ControllerMethodReflector $reflector;
  52. private Limiter|MockObject $limiter;
  53. private ISession|MockObject $session;
  54. private RateLimitingMiddleware $rateLimitingMiddleware;
  55. protected function setUp(): void {
  56. parent::setUp();
  57. $this->request = $this->createMock(IRequest::class);
  58. $this->userSession = $this->createMock(IUserSession::class);
  59. $this->reflector = new ControllerMethodReflector();
  60. $this->limiter = $this->createMock(Limiter::class);
  61. $this->session = $this->createMock(ISession::class);
  62. $this->rateLimitingMiddleware = new RateLimitingMiddleware(
  63. $this->request,
  64. $this->userSession,
  65. $this->reflector,
  66. $this->limiter,
  67. $this->session
  68. );
  69. }
  70. public function testBeforeControllerWithoutAnnotationForAnon(): void {
  71. $this->limiter
  72. ->expects($this->never())
  73. ->method('registerUserRequest');
  74. $this->limiter
  75. ->expects($this->never())
  76. ->method('registerAnonRequest');
  77. $this->userSession->expects($this->once())
  78. ->method('isLoggedIn')
  79. ->willReturn(false);
  80. /** @var TestRateLimitController|MockObject $controller */
  81. $controller = $this->createMock(TestRateLimitController::class);
  82. $this->reflector->reflect($controller, 'testMethodWithoutAnnotation');
  83. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithoutAnnotation');
  84. }
  85. public function testBeforeControllerWithoutAnnotationForLoggedIn(): void {
  86. $this->limiter
  87. ->expects($this->never())
  88. ->method('registerUserRequest');
  89. $this->limiter
  90. ->expects($this->never())
  91. ->method('registerAnonRequest');
  92. $this->userSession->expects($this->once())
  93. ->method('isLoggedIn')
  94. ->willReturn(true);
  95. /** @var TestRateLimitController|MockObject $controller */
  96. $controller = $this->createMock(TestRateLimitController::class);
  97. $this->reflector->reflect($controller, 'testMethodWithoutAnnotation');
  98. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithoutAnnotation');
  99. }
  100. public function testBeforeControllerForAnon(): void {
  101. $controller = new TestRateLimitController('test', $this->request);
  102. $this->request
  103. ->method('getRemoteAddress')
  104. ->willReturn('127.0.0.1');
  105. $this->userSession
  106. ->expects($this->once())
  107. ->method('isLoggedIn')
  108. ->willReturn(false);
  109. $this->limiter
  110. ->expects($this->never())
  111. ->method('registerUserRequest');
  112. $this->limiter
  113. ->expects($this->once())
  114. ->method('registerAnonRequest')
  115. ->with(get_class($controller) . '::testMethodWithAnnotation', '10', '100', '127.0.0.1');
  116. $this->reflector->reflect($controller, 'testMethodWithAnnotation');
  117. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithAnnotation');
  118. }
  119. public function testBeforeControllerForLoggedIn(): void {
  120. $controller = new TestRateLimitController('test', $this->request);
  121. /** @var IUser|MockObject $user */
  122. $user = $this->createMock(IUser::class);
  123. $this->userSession
  124. ->expects($this->once())
  125. ->method('isLoggedIn')
  126. ->willReturn(true);
  127. $this->userSession
  128. ->expects($this->once())
  129. ->method('getUser')
  130. ->willReturn($user);
  131. $this->limiter
  132. ->expects($this->never())
  133. ->method('registerAnonRequest');
  134. $this->limiter
  135. ->expects($this->once())
  136. ->method('registerUserRequest')
  137. ->with(get_class($controller) . '::testMethodWithAnnotation', '20', '200', $user);
  138. $this->reflector->reflect($controller, 'testMethodWithAnnotation');
  139. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithAnnotation');
  140. }
  141. public function testBeforeControllerAnonWithFallback(): void {
  142. $controller = new TestRateLimitController('test', $this->request);
  143. $this->request
  144. ->expects($this->once())
  145. ->method('getRemoteAddress')
  146. ->willReturn('127.0.0.1');
  147. $this->userSession
  148. ->expects($this->once())
  149. ->method('isLoggedIn')
  150. ->willReturn(true);
  151. $this->limiter
  152. ->expects($this->never())
  153. ->method('registerUserRequest');
  154. $this->limiter
  155. ->expects($this->once())
  156. ->method('registerAnonRequest')
  157. ->with(get_class($controller) . '::testMethodWithAnnotationFallback', '10', '100', '127.0.0.1');
  158. $this->reflector->reflect($controller, 'testMethodWithAnnotationFallback');
  159. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithAnnotationFallback');
  160. }
  161. public function testBeforeControllerAttributesForAnon(): void {
  162. $controller = new TestRateLimitController('test', $this->request);
  163. $this->request
  164. ->method('getRemoteAddress')
  165. ->willReturn('127.0.0.1');
  166. $this->userSession
  167. ->expects($this->once())
  168. ->method('isLoggedIn')
  169. ->willReturn(false);
  170. $this->limiter
  171. ->expects($this->never())
  172. ->method('registerUserRequest');
  173. $this->limiter
  174. ->expects($this->once())
  175. ->method('registerAnonRequest')
  176. ->with(get_class($controller) . '::testMethodWithAttributes', '10', '100', '127.0.0.1');
  177. $this->reflector->reflect($controller, 'testMethodWithAttributes');
  178. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithAttributes');
  179. }
  180. public function testBeforeControllerAttributesForLoggedIn(): void {
  181. $controller = new TestRateLimitController('test', $this->request);
  182. /** @var IUser|MockObject $user */
  183. $user = $this->createMock(IUser::class);
  184. $this->userSession
  185. ->expects($this->once())
  186. ->method('isLoggedIn')
  187. ->willReturn(true);
  188. $this->userSession
  189. ->expects($this->once())
  190. ->method('getUser')
  191. ->willReturn($user);
  192. $this->limiter
  193. ->expects($this->never())
  194. ->method('registerAnonRequest');
  195. $this->limiter
  196. ->expects($this->once())
  197. ->method('registerUserRequest')
  198. ->with(get_class($controller) . '::testMethodWithAttributes', '20', '200', $user);
  199. $this->reflector->reflect($controller, 'testMethodWithAttributes');
  200. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithAttributes');
  201. }
  202. public function testBeforeControllerAttributesAnonWithFallback(): void {
  203. $controller = new TestRateLimitController('test', $this->request);
  204. $this->request
  205. ->expects($this->once())
  206. ->method('getRemoteAddress')
  207. ->willReturn('127.0.0.1');
  208. $this->userSession
  209. ->expects($this->once())
  210. ->method('isLoggedIn')
  211. ->willReturn(true);
  212. $this->limiter
  213. ->expects($this->never())
  214. ->method('registerUserRequest');
  215. $this->limiter
  216. ->expects($this->once())
  217. ->method('registerAnonRequest')
  218. ->with(get_class($controller) . '::testMethodWithAttributesFallback', '10', '100', '127.0.0.1');
  219. $this->reflector->reflect($controller, 'testMethodWithAttributesFallback');
  220. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithAttributesFallback');
  221. }
  222. public function testAfterExceptionWithOtherException(): void {
  223. $this->expectException(\Exception::class);
  224. $this->expectExceptionMessage('My test exception');
  225. $controller = new TestRateLimitController('test', $this->request);
  226. $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new \Exception('My test exception'));
  227. }
  228. public function testAfterExceptionWithJsonBody(): void {
  229. $controller = new TestRateLimitController('test', $this->request);
  230. $this->request
  231. ->expects($this->once())
  232. ->method('getHeader')
  233. ->with('Accept')
  234. ->willReturn('JSON');
  235. $result = $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new RateLimitExceededException());
  236. $expected = new DataResponse([], 429
  237. );
  238. $this->assertEquals($expected, $result);
  239. }
  240. public function testAfterExceptionWithHtmlBody(): void {
  241. $controller = new TestRateLimitController('test', $this->request);
  242. $this->request
  243. ->expects($this->once())
  244. ->method('getHeader')
  245. ->with('Accept')
  246. ->willReturn('html');
  247. $result = $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new RateLimitExceededException());
  248. $expected = new TemplateResponse(
  249. 'core',
  250. '429',
  251. [],
  252. TemplateResponse::RENDER_AS_GUEST
  253. );
  254. $expected->setStatus(429);
  255. $this->assertEquals($expected, $result);
  256. $this->assertIsString($result->render());
  257. }
  258. }