RateLimitingMiddlewareTest.php 10 KB

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