ForwardedForHeadersTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Tests;
  8. use OCA\Settings\SetupChecks\ForwardedForHeaders;
  9. use OCP\IConfig;
  10. use OCP\IL10N;
  11. use OCP\IRequest;
  12. use OCP\IURLGenerator;
  13. use OCP\SetupCheck\SetupResult;
  14. use Test\TestCase;
  15. class ForwardedForHeadersTest extends TestCase {
  16. private IL10N $l10n;
  17. private IConfig $config;
  18. private IURLGenerator $urlGenerator;
  19. private IRequest $request;
  20. private ForwardedForHeaders $check;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->l10n = $this->getMockBuilder(IL10N::class)
  24. ->disableOriginalConstructor()->getMock();
  25. $this->l10n->expects($this->any())
  26. ->method('t')
  27. ->willReturnCallback(function ($message, array $replace) {
  28. return vsprintf($message, $replace);
  29. });
  30. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  31. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  32. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  33. $this->check = new ForwardedForHeaders(
  34. $this->l10n,
  35. $this->config,
  36. $this->urlGenerator,
  37. $this->request,
  38. );
  39. }
  40. /**
  41. * @dataProvider dataForwardedForHeadersWorking
  42. */
  43. public function testForwardedForHeadersWorking(array $trustedProxies, string $remoteAddrNotForwarded, string $remoteAddr, string $result): void {
  44. $this->config->expects($this->once())
  45. ->method('getSystemValue')
  46. ->with('trusted_proxies', [])
  47. ->willReturn($trustedProxies);
  48. $this->request->expects($this->atLeastOnce())
  49. ->method('getHeader')
  50. ->willReturnMap([
  51. ['REMOTE_ADDR', $remoteAddrNotForwarded],
  52. ['X-Forwarded-Host', '']
  53. ]);
  54. $this->request->expects($this->any())
  55. ->method('getRemoteAddress')
  56. ->willReturn($remoteAddr);
  57. $this->assertEquals(
  58. $result,
  59. $this->check->run()->getSeverity()
  60. );
  61. }
  62. public function dataForwardedForHeadersWorking(): array {
  63. return [
  64. // description => trusted proxies, getHeader('REMOTE_ADDR'), getRemoteAddr, expected result
  65. 'no trusted proxies' => [[], '2.2.2.2', '2.2.2.2', SetupResult::SUCCESS],
  66. 'trusted proxy, remote addr not trusted proxy' => [['1.1.1.1'], '2.2.2.2', '2.2.2.2', SetupResult::SUCCESS],
  67. '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],
  68. '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],
  69. ];
  70. }
  71. public function testForwardedHostPresentButTrustedProxiesNotAnArray(): void {
  72. $this->config->expects($this->once())
  73. ->method('getSystemValue')
  74. ->with('trusted_proxies', [])
  75. ->willReturn('1.1.1.1');
  76. $this->request->expects($this->atLeastOnce())
  77. ->method('getHeader')
  78. ->willReturnMap([
  79. ['REMOTE_ADDR', '1.1.1.1'],
  80. ['X-Forwarded-Host', 'nextcloud.test']
  81. ]);
  82. $this->request->expects($this->any())
  83. ->method('getRemoteAddress')
  84. ->willReturn('1.1.1.1');
  85. $this->assertEquals(
  86. SetupResult::ERROR,
  87. $this->check->run()->getSeverity()
  88. );
  89. }
  90. public function testForwardedHostPresentButTrustedProxiesEmpty(): void {
  91. $this->config->expects($this->once())
  92. ->method('getSystemValue')
  93. ->with('trusted_proxies', [])
  94. ->willReturn([]);
  95. $this->request->expects($this->atLeastOnce())
  96. ->method('getHeader')
  97. ->willReturnMap([
  98. ['REMOTE_ADDR', '1.1.1.1'],
  99. ['X-Forwarded-Host', 'nextcloud.test']
  100. ]);
  101. $this->request->expects($this->any())
  102. ->method('getRemoteAddress')
  103. ->willReturn('1.1.1.1');
  104. $this->assertEquals(
  105. SetupResult::ERROR,
  106. $this->check->run()->getSeverity()
  107. );
  108. }
  109. }