SameSiteCookieMiddlewareTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\AppFramework\Middleware\Security;
  24. use OC\AppFramework\Http\Request;
  25. use OC\AppFramework\Middleware\Security\Exceptions\LaxSameSiteCookieFailedException;
  26. use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
  27. use OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware;
  28. use OC\AppFramework\Utility\ControllerMethodReflector;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http;
  31. use Test\TestCase;
  32. class SameSiteCookieMiddlewareTest extends TestCase {
  33. /** @var SameSiteCookieMiddleware */
  34. private $middleware;
  35. /** @var Request|\PHPUnit\Framework\MockObject\MockObject */
  36. private $request;
  37. /** @var ControllerMethodReflector|\PHPUnit\Framework\MockObject\MockObject */
  38. private $reflector;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->request = $this->createMock(Request::class);
  42. $this->reflector = $this->createMock(ControllerMethodReflector::class);
  43. $this->middleware = new SameSiteCookieMiddleware($this->request, $this->reflector);
  44. }
  45. public function testBeforeControllerNoIndex() {
  46. $this->request->method('getScriptName')
  47. ->willReturn('/ocs/v2.php');
  48. $this->middleware->beforeController($this->createMock(Controller::class), 'foo');
  49. $this->addToAssertionCount(1);
  50. }
  51. public function testBeforeControllerIndexHasAnnotation() {
  52. $this->request->method('getScriptName')
  53. ->willReturn('/index.php');
  54. $this->reflector->method('hasAnnotation')
  55. ->with('NoSameSiteCookieRequired')
  56. ->willReturn(true);
  57. $this->middleware->beforeController($this->createMock(Controller::class), 'foo');
  58. $this->addToAssertionCount(1);
  59. }
  60. public function testBeforeControllerIndexNoAnnotationPassingCheck() {
  61. $this->request->method('getScriptName')
  62. ->willReturn('/index.php');
  63. $this->reflector->method('hasAnnotation')
  64. ->with('NoSameSiteCookieRequired')
  65. ->willReturn(false);
  66. $this->request->method('passesLaxCookieCheck')
  67. ->willReturn(true);
  68. $this->middleware->beforeController($this->createMock(Controller::class), 'foo');
  69. $this->addToAssertionCount(1);
  70. }
  71. public function testBeforeControllerIndexNoAnnotationFailingCheck() {
  72. $this->expectException(LaxSameSiteCookieFailedException::class);
  73. $this->request->method('getScriptName')
  74. ->willReturn('/index.php');
  75. $this->reflector->method('hasAnnotation')
  76. ->with('NoSameSiteCookieRequired')
  77. ->willReturn(false);
  78. $this->request->method('passesLaxCookieCheck')
  79. ->willReturn(false);
  80. $this->middleware->beforeController($this->createMock(Controller::class), 'foo');
  81. }
  82. public function testAfterExceptionNoLaxCookie() {
  83. $ex = new SecurityException();
  84. try {
  85. $this->middleware->afterException($this->createMock(Controller::class), 'foo', $ex);
  86. $this->fail();
  87. } catch (\Exception $e) {
  88. $this->assertSame($ex, $e);
  89. }
  90. }
  91. public function testAfterExceptionLaxCookie() {
  92. $ex = new LaxSameSiteCookieFailedException();
  93. $this->request->method('getRequestUri')
  94. ->willReturn('/myrequri');
  95. $middleware = $this->getMockBuilder(SameSiteCookieMiddleware::class)
  96. ->setConstructorArgs([$this->request, $this->reflector])
  97. ->setMethods(['setSameSiteCookie'])
  98. ->getMock();
  99. $middleware->expects($this->once())
  100. ->method('setSameSiteCookie');
  101. $resp = $middleware->afterException($this->createMock(Controller::class), 'foo', $ex);
  102. $this->assertSame(Http::STATUS_FOUND, $resp->getStatus());
  103. $headers = $resp->getHeaders();
  104. $this->assertSame('/myrequri', $headers['Location']);
  105. }
  106. }