CORSMiddlewareTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later. See the COPYING file.
  7. *
  8. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  9. * @copyright Bernhard Posselt 2014
  10. */
  11. namespace Test\AppFramework\Middleware\Security;
  12. use OC\AppFramework\Http\Request;
  13. use OC\AppFramework\Middleware\Security\CORSMiddleware;
  14. use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
  15. use OC\AppFramework\Utility\ControllerMethodReflector;
  16. use OC\Security\Bruteforce\Throttler;
  17. use OC\User\Session;
  18. use OCP\AppFramework\Controller;
  19. use OCP\AppFramework\Http\JSONResponse;
  20. use OCP\AppFramework\Http\Response;
  21. use OCP\IConfig;
  22. use OCP\IRequestId;
  23. class CORSMiddlewareTest extends \Test\TestCase {
  24. /** @var ControllerMethodReflector */
  25. private $reflector;
  26. /** @var Session|\PHPUnit\Framework\MockObject\MockObject */
  27. private $session;
  28. /** @var Throttler */
  29. private $throttler;
  30. /** @var Controller */
  31. private $controller;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->reflector = new ControllerMethodReflector();
  35. $this->session = $this->createMock(Session::class);
  36. $this->throttler = $this->createMock(Throttler::class);
  37. $this->controller = $this->createMock(Controller::class);
  38. }
  39. /**
  40. * @CORS
  41. */
  42. public function testSetCORSAPIHeader() {
  43. $request = new Request(
  44. [
  45. 'server' => [
  46. 'HTTP_ORIGIN' => 'test'
  47. ]
  48. ],
  49. $this->createMock(IRequestId::class),
  50. $this->createMock(IConfig::class)
  51. );
  52. $this->reflector->reflect($this, __FUNCTION__);
  53. $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
  54. $response = $middleware->afterController($this->controller, __FUNCTION__, new Response());
  55. $headers = $response->getHeaders();
  56. $this->assertEquals('test', $headers['Access-Control-Allow-Origin']);
  57. }
  58. public function testNoAnnotationNoCORSHEADER() {
  59. $request = new Request(
  60. [
  61. 'server' => [
  62. 'HTTP_ORIGIN' => 'test'
  63. ]
  64. ],
  65. $this->createMock(IRequestId::class),
  66. $this->createMock(IConfig::class)
  67. );
  68. $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
  69. $response = $middleware->afterController($this->controller, __FUNCTION__, new Response());
  70. $headers = $response->getHeaders();
  71. $this->assertFalse(array_key_exists('Access-Control-Allow-Origin', $headers));
  72. }
  73. /**
  74. * @CORS
  75. */
  76. public function testNoOriginHeaderNoCORSHEADER() {
  77. $request = new Request(
  78. [],
  79. $this->createMock(IRequestId::class),
  80. $this->createMock(IConfig::class)
  81. );
  82. $this->reflector->reflect($this, __FUNCTION__);
  83. $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
  84. $response = $middleware->afterController($this->controller, __FUNCTION__, new Response());
  85. $headers = $response->getHeaders();
  86. $this->assertFalse(array_key_exists('Access-Control-Allow-Origin', $headers));
  87. }
  88. /**
  89. * @CORS
  90. */
  91. public function testCorsIgnoredIfWithCredentialsHeaderPresent() {
  92. $this->expectException(\OC\AppFramework\Middleware\Security\Exceptions\SecurityException::class);
  93. $request = new Request(
  94. [
  95. 'server' => [
  96. 'HTTP_ORIGIN' => 'test'
  97. ]
  98. ],
  99. $this->createMock(IRequestId::class),
  100. $this->createMock(IConfig::class)
  101. );
  102. $this->reflector->reflect($this, __FUNCTION__);
  103. $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
  104. $response = new Response();
  105. $response->addHeader('AcCess-control-Allow-Credentials ', 'TRUE');
  106. $middleware->afterController($this->controller, __FUNCTION__, $response);
  107. }
  108. /**
  109. * CORS must not be enforced for anonymous users on public pages
  110. *
  111. * @CORS
  112. * @PublicPage
  113. */
  114. public function testNoCORSOnAnonymousPublicPage() {
  115. $request = new Request(
  116. [],
  117. $this->createMock(IRequestId::class),
  118. $this->createMock(IConfig::class)
  119. );
  120. $this->reflector->reflect($this, __FUNCTION__);
  121. $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
  122. $this->session->expects($this->once())
  123. ->method('isLoggedIn')
  124. ->willReturn(false);
  125. $this->session->expects($this->never())
  126. ->method('logout');
  127. $this->session->expects($this->never())
  128. ->method('logClientIn')
  129. ->with($this->equalTo('user'), $this->equalTo('pass'))
  130. ->willReturn(true);
  131. $this->reflector->reflect($this, __FUNCTION__);
  132. $middleware->beforeController($this->controller, __FUNCTION__);
  133. }
  134. /**
  135. * Even on public pages users logged in using session cookies,
  136. * that do not provide a valid CSRF token are disallowed
  137. *
  138. * @CORS
  139. * @PublicPage
  140. */
  141. public function testCORSShouldNeverAllowCookieAuth() {
  142. $request = new Request(
  143. [],
  144. $this->createMock(IRequestId::class),
  145. $this->createMock(IConfig::class)
  146. );
  147. $this->reflector->reflect($this, __FUNCTION__);
  148. $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
  149. $this->session->expects($this->once())
  150. ->method('isLoggedIn')
  151. ->willReturn(true);
  152. $this->session->expects($this->once())
  153. ->method('logout');
  154. $this->session->expects($this->never())
  155. ->method('logClientIn')
  156. ->with($this->equalTo('user'), $this->equalTo('pass'))
  157. ->willReturn(true);
  158. $this->expectException(SecurityException::class);
  159. $middleware->beforeController($this->controller, __FUNCTION__);
  160. }
  161. /**
  162. * @CORS
  163. */
  164. public function testCORSShouldRelogin() {
  165. $request = new Request(
  166. ['server' => [
  167. 'PHP_AUTH_USER' => 'user',
  168. 'PHP_AUTH_PW' => 'pass'
  169. ]],
  170. $this->createMock(IRequestId::class),
  171. $this->createMock(IConfig::class)
  172. );
  173. $this->session->expects($this->once())
  174. ->method('logout');
  175. $this->session->expects($this->once())
  176. ->method('logClientIn')
  177. ->with($this->equalTo('user'), $this->equalTo('pass'))
  178. ->willReturn(true);
  179. $this->reflector->reflect($this, __FUNCTION__);
  180. $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
  181. $middleware->beforeController($this->controller, __FUNCTION__);
  182. }
  183. /**
  184. * @CORS
  185. */
  186. public function testCORSShouldFailIfPasswordLoginIsForbidden() {
  187. $this->expectException(\OC\AppFramework\Middleware\Security\Exceptions\SecurityException::class);
  188. $request = new Request(
  189. ['server' => [
  190. 'PHP_AUTH_USER' => 'user',
  191. 'PHP_AUTH_PW' => 'pass'
  192. ]],
  193. $this->createMock(IRequestId::class),
  194. $this->createMock(IConfig::class)
  195. );
  196. $this->session->expects($this->once())
  197. ->method('logout');
  198. $this->session->expects($this->once())
  199. ->method('logClientIn')
  200. ->with($this->equalTo('user'), $this->equalTo('pass'))
  201. ->will($this->throwException(new \OC\Authentication\Exceptions\PasswordLoginForbiddenException));
  202. $this->reflector->reflect($this, __FUNCTION__);
  203. $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
  204. $middleware->beforeController($this->controller, __FUNCTION__);
  205. }
  206. /**
  207. * @CORS
  208. */
  209. public function testCORSShouldNotAllowCookieAuth() {
  210. $this->expectException(\OC\AppFramework\Middleware\Security\Exceptions\SecurityException::class);
  211. $request = new Request(
  212. ['server' => [
  213. 'PHP_AUTH_USER' => 'user',
  214. 'PHP_AUTH_PW' => 'pass'
  215. ]],
  216. $this->createMock(IRequestId::class),
  217. $this->createMock(IConfig::class)
  218. );
  219. $this->session->expects($this->once())
  220. ->method('logout');
  221. $this->session->expects($this->once())
  222. ->method('logClientIn')
  223. ->with($this->equalTo('user'), $this->equalTo('pass'))
  224. ->willReturn(false);
  225. $this->reflector->reflect($this, __FUNCTION__);
  226. $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
  227. $middleware->beforeController($this->controller, __FUNCTION__);
  228. }
  229. public function testAfterExceptionWithSecurityExceptionNoStatus() {
  230. $request = new Request(
  231. ['server' => [
  232. 'PHP_AUTH_USER' => 'user',
  233. 'PHP_AUTH_PW' => 'pass'
  234. ]],
  235. $this->createMock(IRequestId::class),
  236. $this->createMock(IConfig::class)
  237. );
  238. $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
  239. $response = $middleware->afterException($this->controller, __FUNCTION__, new SecurityException('A security exception'));
  240. $expected = new JSONResponse(['message' => 'A security exception'], 500);
  241. $this->assertEquals($expected, $response);
  242. }
  243. public function testAfterExceptionWithSecurityExceptionWithStatus() {
  244. $request = new Request(
  245. ['server' => [
  246. 'PHP_AUTH_USER' => 'user',
  247. 'PHP_AUTH_PW' => 'pass'
  248. ]],
  249. $this->createMock(IRequestId::class),
  250. $this->createMock(IConfig::class)
  251. );
  252. $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
  253. $response = $middleware->afterException($this->controller, __FUNCTION__, new SecurityException('A security exception', 501));
  254. $expected = new JSONResponse(['message' => 'A security exception'], 501);
  255. $this->assertEquals($expected, $response);
  256. }
  257. public function testAfterExceptionWithRegularException() {
  258. $this->expectException(\Exception::class);
  259. $this->expectExceptionMessage('A regular exception');
  260. $request = new Request(
  261. ['server' => [
  262. 'PHP_AUTH_USER' => 'user',
  263. 'PHP_AUTH_PW' => 'pass'
  264. ]],
  265. $this->createMock(IRequestId::class),
  266. $this->createMock(IConfig::class)
  267. );
  268. $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler);
  269. $middleware->afterException($this->controller, __FUNCTION__, new \Exception('A regular exception'));
  270. }
  271. }