RateLimitingMiddlewareTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
  5. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  6. *
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace Test\AppFramework\Middleware\Security;
  26. use OC\AppFramework\Middleware\Security\RateLimitingMiddleware;
  27. use OC\AppFramework\Utility\ControllerMethodReflector;
  28. use OC\Security\RateLimiting\Exception\RateLimitExceededException;
  29. use OC\Security\RateLimiting\Limiter;
  30. use OCP\AppFramework\Controller;
  31. use OCP\AppFramework\Http\Attribute\AnonRateLimit;
  32. use OCP\AppFramework\Http\Attribute\UserRateLimit;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\AppFramework\Http\TemplateResponse;
  35. use OCP\IRequest;
  36. use OCP\IUser;
  37. use OCP\IUserSession;
  38. use PHPUnit\Framework\MockObject\MockObject;
  39. use Test\TestCase;
  40. class TestRateLimitController extends Controller {
  41. /**
  42. * @UserRateThrottle(limit=20, period=200)
  43. * @AnonRateThrottle(limit=10, period=100)
  44. */
  45. public function testMethodWithAnnotation() {
  46. }
  47. /**
  48. * @AnonRateThrottle(limit=10, period=100)
  49. */
  50. public function testMethodWithAnnotationFallback() {
  51. }
  52. public function testMethodWithoutAnnotation() {
  53. }
  54. #[UserRateLimit(limit: 20, period: 200)]
  55. #[AnonRateLimit(limit: 10, period: 100)]
  56. public function testMethodWithAttributes() {
  57. }
  58. #[AnonRateLimit(limit: 10, period: 100)]
  59. public function testMethodWithAttributesFallback() {
  60. }
  61. }
  62. /**
  63. * @group DB
  64. */
  65. class RateLimitingMiddlewareTest extends TestCase {
  66. private IRequest|MockObject $request;
  67. private IUserSession|MockObject $userSession;
  68. private ControllerMethodReflector $reflector;
  69. private Limiter|MockObject $limiter;
  70. private RateLimitingMiddleware $rateLimitingMiddleware;
  71. protected function setUp(): void {
  72. parent::setUp();
  73. $this->request = $this->createMock(IRequest::class);
  74. $this->userSession = $this->createMock(IUserSession::class);
  75. $this->reflector = new ControllerMethodReflector();
  76. $this->limiter = $this->createMock(Limiter::class);
  77. $this->rateLimitingMiddleware = new RateLimitingMiddleware(
  78. $this->request,
  79. $this->userSession,
  80. $this->reflector,
  81. $this->limiter
  82. );
  83. }
  84. public function testBeforeControllerWithoutAnnotationForAnon(): void {
  85. $this->limiter
  86. ->expects($this->never())
  87. ->method('registerUserRequest');
  88. $this->limiter
  89. ->expects($this->never())
  90. ->method('registerAnonRequest');
  91. $this->userSession->expects($this->once())
  92. ->method('isLoggedIn')
  93. ->willReturn(false);
  94. /** @var TestRateLimitController|MockObject $controller */
  95. $controller = $this->createMock(TestRateLimitController::class);
  96. $this->reflector->reflect($controller, 'testMethodWithoutAnnotation');
  97. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithoutAnnotation');
  98. }
  99. public function testBeforeControllerWithoutAnnotationForLoggedIn(): void {
  100. $this->limiter
  101. ->expects($this->never())
  102. ->method('registerUserRequest');
  103. $this->limiter
  104. ->expects($this->never())
  105. ->method('registerAnonRequest');
  106. $this->userSession->expects($this->once())
  107. ->method('isLoggedIn')
  108. ->willReturn(true);
  109. /** @var TestRateLimitController|MockObject $controller */
  110. $controller = $this->createMock(TestRateLimitController::class);
  111. $this->reflector->reflect($controller, 'testMethodWithoutAnnotation');
  112. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithoutAnnotation');
  113. }
  114. public function testBeforeControllerForAnon(): void {
  115. $controller = new TestRateLimitController('test', $this->request);
  116. $this->request
  117. ->method('getRemoteAddress')
  118. ->willReturn('127.0.0.1');
  119. $this->userSession
  120. ->expects($this->once())
  121. ->method('isLoggedIn')
  122. ->willReturn(false);
  123. $this->limiter
  124. ->expects($this->never())
  125. ->method('registerUserRequest');
  126. $this->limiter
  127. ->expects($this->once())
  128. ->method('registerAnonRequest')
  129. ->with(get_class($controller) . '::testMethodWithAnnotation', '10', '100', '127.0.0.1');
  130. $this->reflector->reflect($controller, 'testMethodWithAnnotation');
  131. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithAnnotation');
  132. }
  133. public function testBeforeControllerForLoggedIn(): void {
  134. $controller = new TestRateLimitController('test', $this->request);
  135. /** @var IUser|MockObject $user */
  136. $user = $this->createMock(IUser::class);
  137. $this->userSession
  138. ->expects($this->once())
  139. ->method('isLoggedIn')
  140. ->willReturn(true);
  141. $this->userSession
  142. ->expects($this->once())
  143. ->method('getUser')
  144. ->willReturn($user);
  145. $this->limiter
  146. ->expects($this->never())
  147. ->method('registerAnonRequest');
  148. $this->limiter
  149. ->expects($this->once())
  150. ->method('registerUserRequest')
  151. ->with(get_class($controller) . '::testMethodWithAnnotation', '20', '200', $user);
  152. $this->reflector->reflect($controller, 'testMethodWithAnnotation');
  153. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithAnnotation');
  154. }
  155. public function testBeforeControllerAnonWithFallback(): void {
  156. $controller = new TestRateLimitController('test', $this->request);
  157. $this->request
  158. ->expects($this->once())
  159. ->method('getRemoteAddress')
  160. ->willReturn('127.0.0.1');
  161. $this->userSession
  162. ->expects($this->once())
  163. ->method('isLoggedIn')
  164. ->willReturn(true);
  165. $this->limiter
  166. ->expects($this->never())
  167. ->method('registerUserRequest');
  168. $this->limiter
  169. ->expects($this->once())
  170. ->method('registerAnonRequest')
  171. ->with(get_class($controller) . '::testMethodWithAnnotationFallback', '10', '100', '127.0.0.1');
  172. $this->reflector->reflect($controller, 'testMethodWithAnnotationFallback');
  173. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithAnnotationFallback');
  174. }
  175. public function testBeforeControllerAttributesForAnon(): void {
  176. $controller = new TestRateLimitController('test', $this->request);
  177. $this->request
  178. ->method('getRemoteAddress')
  179. ->willReturn('127.0.0.1');
  180. $this->userSession
  181. ->expects($this->once())
  182. ->method('isLoggedIn')
  183. ->willReturn(false);
  184. $this->limiter
  185. ->expects($this->never())
  186. ->method('registerUserRequest');
  187. $this->limiter
  188. ->expects($this->once())
  189. ->method('registerAnonRequest')
  190. ->with(get_class($controller) . '::testMethodWithAttributes', '10', '100', '127.0.0.1');
  191. $this->reflector->reflect($controller, 'testMethodWithAttributes');
  192. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithAttributes');
  193. }
  194. public function testBeforeControllerAttributesForLoggedIn(): void {
  195. $controller = new TestRateLimitController('test', $this->request);
  196. /** @var IUser|MockObject $user */
  197. $user = $this->createMock(IUser::class);
  198. $this->userSession
  199. ->expects($this->once())
  200. ->method('isLoggedIn')
  201. ->willReturn(true);
  202. $this->userSession
  203. ->expects($this->once())
  204. ->method('getUser')
  205. ->willReturn($user);
  206. $this->limiter
  207. ->expects($this->never())
  208. ->method('registerAnonRequest');
  209. $this->limiter
  210. ->expects($this->once())
  211. ->method('registerUserRequest')
  212. ->with(get_class($controller) . '::testMethodWithAttributes', '20', '200', $user);
  213. $this->reflector->reflect($controller, 'testMethodWithAttributes');
  214. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithAttributes');
  215. }
  216. public function testBeforeControllerAttributesAnonWithFallback(): void {
  217. $controller = new TestRateLimitController('test', $this->request);
  218. $this->request
  219. ->expects($this->once())
  220. ->method('getRemoteAddress')
  221. ->willReturn('127.0.0.1');
  222. $this->userSession
  223. ->expects($this->once())
  224. ->method('isLoggedIn')
  225. ->willReturn(true);
  226. $this->limiter
  227. ->expects($this->never())
  228. ->method('registerUserRequest');
  229. $this->limiter
  230. ->expects($this->once())
  231. ->method('registerAnonRequest')
  232. ->with(get_class($controller) . '::testMethodWithAttributesFallback', '10', '100', '127.0.0.1');
  233. $this->reflector->reflect($controller, 'testMethodWithAttributesFallback');
  234. $this->rateLimitingMiddleware->beforeController($controller, 'testMethodWithAttributesFallback');
  235. }
  236. public function testAfterExceptionWithOtherException(): void {
  237. $this->expectException(\Exception::class);
  238. $this->expectExceptionMessage('My test exception');
  239. $controller = new TestRateLimitController('test', $this->request);
  240. $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new \Exception('My test exception'));
  241. }
  242. public function testAfterExceptionWithJsonBody(): void {
  243. $controller = new TestRateLimitController('test', $this->request);
  244. $this->request
  245. ->expects($this->once())
  246. ->method('getHeader')
  247. ->with('Accept')
  248. ->willReturn('JSON');
  249. $result = $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new RateLimitExceededException());
  250. $expected = new DataResponse([], 429
  251. );
  252. $this->assertEquals($expected, $result);
  253. }
  254. public function testAfterExceptionWithHtmlBody(): void {
  255. $controller = new TestRateLimitController('test', $this->request);
  256. $this->request
  257. ->expects($this->once())
  258. ->method('getHeader')
  259. ->with('Accept')
  260. ->willReturn('html');
  261. $result = $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new RateLimitExceededException());
  262. $expected = new TemplateResponse(
  263. 'core',
  264. '429',
  265. [],
  266. TemplateResponse::RENDER_AS_GUEST
  267. );
  268. $expected->setStatus(429);
  269. $this->assertEquals($expected, $result);
  270. $this->assertIsString($result->render());
  271. }
  272. }