BlockLegacyClientPluginTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  9. use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
  10. use OCA\Theming\ThemingDefaults;
  11. use OCP\IConfig;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use Sabre\HTTP\RequestInterface;
  14. use Test\TestCase;
  15. /**
  16. * Class BlockLegacyClientPluginTest
  17. *
  18. * @package OCA\DAV\Tests\unit\Connector\Sabre
  19. */
  20. class BlockLegacyClientPluginTest extends TestCase {
  21. private IConfig&MockObject $config;
  22. private ThemingDefaults&MockObject $themingDefaults;
  23. private BlockLegacyClientPlugin $blockLegacyClientVersionPlugin;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->config = $this->createMock(IConfig::class);
  27. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  28. $this->blockLegacyClientVersionPlugin = new BlockLegacyClientPlugin(
  29. $this->config,
  30. $this->themingDefaults,
  31. );
  32. }
  33. public static function oldDesktopClientProvider(): array {
  34. return [
  35. ['Mozilla/5.0 (Windows) mirall/1.5.0'],
  36. ['Mozilla/5.0 (Bogus Text) mirall/1.6.9'],
  37. ];
  38. }
  39. /**
  40. * @dataProvider oldDesktopClientProvider
  41. */
  42. public function testBeforeHandlerException(string $userAgent): void {
  43. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  44. $this->themingDefaults
  45. ->expects($this->once())
  46. ->method('getSyncClientUrl')
  47. ->willReturn('https://nextcloud.com/install/#install-clients');
  48. $this->config
  49. ->expects($this->once())
  50. ->method('getSystemValue')
  51. ->with('minimum.supported.desktop.version', '2.3.0')
  52. ->willReturn('1.7.0');
  53. $this->expectExceptionMessage('This version of the client is unsupported. Upgrade to <a href="https://nextcloud.com/install/#install-clients">version 1.7.0 or later</a>.');
  54. /** @var RequestInterface|MockObject $request */
  55. $request = $this->createMock('\Sabre\HTTP\RequestInterface');
  56. $request
  57. ->expects($this->once())
  58. ->method('getHeader')
  59. ->with('User-Agent')
  60. ->willReturn($userAgent);
  61. $this->blockLegacyClientVersionPlugin->beforeHandler($request);
  62. }
  63. /**
  64. * Ensure that there is no room for XSS attack through configured URL / version
  65. * @dataProvider oldDesktopClientProvider
  66. */
  67. public function testBeforeHandlerExceptionPreventXSSAttack(string $userAgent): void {
  68. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  69. $this->themingDefaults
  70. ->expects($this->once())
  71. ->method('getSyncClientUrl')
  72. ->willReturn('https://example.com"><script>alter("hacked");</script>');
  73. $this->config
  74. ->expects($this->once())
  75. ->method('getSystemValue')
  76. ->with('minimum.supported.desktop.version', '2.3.0')
  77. ->willReturn('1.7.0 <script>alert("unsafe")</script>');
  78. $this->expectExceptionMessage('This version of the client is unsupported. Upgrade to <a href="https://example.com&quot;&gt;&lt;script&gt;alter(&quot;hacked&quot;);&lt;/script&gt;">version 1.7.0 &lt;script&gt;alert(&quot;unsafe&quot;)&lt;/script&gt; or later</a>.');
  79. /** @var RequestInterface|MockObject $request */
  80. $request = $this->createMock('\Sabre\HTTP\RequestInterface');
  81. $request
  82. ->expects($this->once())
  83. ->method('getHeader')
  84. ->with('User-Agent')
  85. ->willReturn($userAgent);
  86. $this->blockLegacyClientVersionPlugin->beforeHandler($request);
  87. }
  88. public function newAndAlternateDesktopClientProvider(): array {
  89. return [
  90. ['Mozilla/5.0 (Windows) mirall/1.7.0'],
  91. ['Mozilla/5.0 (Bogus Text) mirall/1.9.3'],
  92. ['Mozilla/5.0 (Not Our Client But Old Version) LegacySync/1.1.0'],
  93. ];
  94. }
  95. /**
  96. * @dataProvider newAndAlternateDesktopClientProvider
  97. */
  98. public function testBeforeHandlerSuccess(string $userAgent): void {
  99. /** @var RequestInterface|MockObject $request */
  100. $request = $this->createMock(RequestInterface::class);
  101. $request
  102. ->expects($this->once())
  103. ->method('getHeader')
  104. ->with('User-Agent')
  105. ->willReturn($userAgent);
  106. $this->config
  107. ->expects($this->once())
  108. ->method('getSystemValue')
  109. ->with('minimum.supported.desktop.version', '2.3.0')
  110. ->willReturn('1.7.0');
  111. $this->blockLegacyClientVersionPlugin->beforeHandler($request);
  112. }
  113. public function testBeforeHandlerNoUserAgent(): void {
  114. /** @var RequestInterface|MockObject $request */
  115. $request = $this->createMock(RequestInterface::class);
  116. $request
  117. ->expects($this->once())
  118. ->method('getHeader')
  119. ->with('User-Agent')
  120. ->willReturn(null);
  121. $this->blockLegacyClientVersionPlugin->beforeHandler($request);
  122. }
  123. }