ForwardedForHeadersTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Côme Chilliet <come.chilliet@nextcloud.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 OCA\Settings\Tests;
  26. use OCA\Settings\SetupChecks\ForwardedForHeaders;
  27. use OCP\IConfig;
  28. use OCP\IL10N;
  29. use OCP\IRequest;
  30. use OCP\IURLGenerator;
  31. use OCP\SetupCheck\SetupResult;
  32. use Test\TestCase;
  33. class ForwardedForHeadersTest extends TestCase {
  34. private IL10N $l10n;
  35. private IConfig $config;
  36. private IURLGenerator $urlGenerator;
  37. private IRequest $request;
  38. private ForwardedForHeaders $check;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->l10n = $this->getMockBuilder(IL10N::class)
  42. ->disableOriginalConstructor()->getMock();
  43. $this->l10n->expects($this->any())
  44. ->method('t')
  45. ->willReturnCallback(function ($message, array $replace) {
  46. return vsprintf($message, $replace);
  47. });
  48. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  49. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  50. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  51. $this->check = new ForwardedForHeaders(
  52. $this->l10n,
  53. $this->config,
  54. $this->urlGenerator,
  55. $this->request,
  56. );
  57. }
  58. /**
  59. * @dataProvider dataForwardedForHeadersWorking
  60. */
  61. public function testForwardedForHeadersWorking(array $trustedProxies, string $remoteAddrNotForwarded, string $remoteAddr, string $result): void {
  62. $this->config->expects($this->once())
  63. ->method('getSystemValue')
  64. ->with('trusted_proxies', [])
  65. ->willReturn($trustedProxies);
  66. $this->request->expects($this->atLeastOnce())
  67. ->method('getHeader')
  68. ->willReturnMap([
  69. ['REMOTE_ADDR', $remoteAddrNotForwarded],
  70. ['X-Forwarded-Host', '']
  71. ]);
  72. $this->request->expects($this->any())
  73. ->method('getRemoteAddress')
  74. ->willReturn($remoteAddr);
  75. $this->assertEquals(
  76. $result,
  77. $this->check->run()->getSeverity()
  78. );
  79. }
  80. public function dataForwardedForHeadersWorking(): array {
  81. return [
  82. // description => trusted proxies, getHeader('REMOTE_ADDR'), getRemoteAddr, expected result
  83. 'no trusted proxies' => [[], '2.2.2.2', '2.2.2.2', SetupResult::SUCCESS],
  84. 'trusted proxy, remote addr not trusted proxy' => [['1.1.1.1'], '2.2.2.2', '2.2.2.2', SetupResult::SUCCESS],
  85. 'trusted proxy, remote addr is trusted proxy, x-forwarded-for working' => [['1.1.1.1'], '1.1.1.1', '2.2.2.2', SetupResult::SUCCESS],
  86. 'trusted proxy, remote addr is trusted proxy, x-forwarded-for not set' => [['1.1.1.1'], '1.1.1.1', '1.1.1.1', SetupResult::WARNING],
  87. ];
  88. }
  89. public function testForwardedHostPresentButTrustedProxiesNotAnArray(): void {
  90. $this->config->expects($this->once())
  91. ->method('getSystemValue')
  92. ->with('trusted_proxies', [])
  93. ->willReturn('1.1.1.1');
  94. $this->request->expects($this->atLeastOnce())
  95. ->method('getHeader')
  96. ->willReturnMap([
  97. ['REMOTE_ADDR', '1.1.1.1'],
  98. ['X-Forwarded-Host', 'nextcloud.test']
  99. ]);
  100. $this->request->expects($this->any())
  101. ->method('getRemoteAddress')
  102. ->willReturn('1.1.1.1');
  103. $this->assertEquals(
  104. SetupResult::ERROR,
  105. $this->check->run()->getSeverity()
  106. );
  107. }
  108. public function testForwardedHostPresentButTrustedProxiesEmpty(): void {
  109. $this->config->expects($this->once())
  110. ->method('getSystemValue')
  111. ->with('trusted_proxies', [])
  112. ->willReturn([]);
  113. $this->request->expects($this->atLeastOnce())
  114. ->method('getHeader')
  115. ->willReturnMap([
  116. ['REMOTE_ADDR', '1.1.1.1'],
  117. ['X-Forwarded-Host', 'nextcloud.test']
  118. ]);
  119. $this->request->expects($this->any())
  120. ->method('getRemoteAddress')
  121. ->willReturn('1.1.1.1');
  122. $this->assertEquals(
  123. SetupResult::ERROR,
  124. $this->check->run()->getSeverity()
  125. );
  126. }
  127. }