TrustedDomainHelperTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright (c) 2015 Lukas Reschke <lukas@owncloud.com>
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later.
  7. * See the COPYING-README file.
  8. */
  9. namespace Test\Security;
  10. use OC\Security\TrustedDomainHelper;
  11. use OCP\IConfig;
  12. /**
  13. * Class TrustedDomainHelperTest
  14. */
  15. class TrustedDomainHelperTest extends \Test\TestCase {
  16. /** @var IConfig */
  17. protected $config;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  21. }
  22. /**
  23. * @dataProvider trustedDomainDataProvider
  24. * @param string $trustedDomains
  25. * @param string $testDomain
  26. * @param bool $result
  27. */
  28. public function testIsTrustedDomain($trustedDomains, $testDomain, $result) {
  29. $this->config->expects($this->at(0))
  30. ->method('getSystemValue')
  31. ->with('overwritehost')
  32. ->willReturn('');
  33. $this->config->expects($this->at(1))
  34. ->method('getSystemValue')
  35. ->with('trusted_domains')
  36. ->willReturn($trustedDomains);
  37. $trustedDomainHelper = new TrustedDomainHelper($this->config);
  38. $this->assertEquals($result, $trustedDomainHelper->isTrustedDomain($testDomain));
  39. }
  40. /**
  41. * @return array
  42. */
  43. public function trustedDomainDataProvider() {
  44. $trustedHostTestList = [
  45. 'host.one.test',
  46. 'host.two.test',
  47. '[1fff:0:a88:85a3::ac1f]',
  48. 'host.three.test:443',
  49. '*.leading.host',
  50. 'trailing.host*',
  51. 'cen*ter',
  52. '*.leadingwith.port:123',
  53. 'trailingwith.port*:456',
  54. 'UPPERCASE.DOMAIN',
  55. 'lowercase.domain',
  56. ];
  57. return [
  58. // empty defaults to false with 8.1
  59. [null, 'host.one.test:8080', false],
  60. ['', 'host.one.test:8080', false],
  61. [[], 'host.one.test:8080', false],
  62. // trust list when defined
  63. [$trustedHostTestList, 'host.two.test:8080', true],
  64. [$trustedHostTestList, 'host.two.test:9999', true],
  65. [$trustedHostTestList, 'host.three.test:8080', false],
  66. [$trustedHostTestList, 'host.two.test:8080:aa:222', false],
  67. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]', true],
  68. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]:801', true],
  69. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]:801:34', false],
  70. [$trustedHostTestList, 'host.three.test:443', true],
  71. [$trustedHostTestList, 'host.three.test:80', false],
  72. [$trustedHostTestList, 'host.three.test', false],
  73. // trust localhost regardless of trust list
  74. [$trustedHostTestList, 'localhost', true],
  75. [$trustedHostTestList, 'localhost:8080', true],
  76. [$trustedHostTestList, '127.0.0.1', true],
  77. [$trustedHostTestList, '127.0.0.1:8080', true],
  78. // do not trust invalid localhosts
  79. [$trustedHostTestList, 'localhost:1:2', false],
  80. [$trustedHostTestList, 'localhost: evil.host', false],
  81. // do not trust casting
  82. [[1], '1', false],
  83. // leading *
  84. [$trustedHostTestList, 'abc.leading.host', true],
  85. [$trustedHostTestList, 'abc.def.leading.host', true],
  86. [$trustedHostTestList, 'abc.def.leading.host.another', false],
  87. [$trustedHostTestList, 'abc.def.leading.host:123', true],
  88. [$trustedHostTestList, 'leading.host', false],
  89. // trailing *
  90. [$trustedHostTestList, 'trailing.host', true],
  91. [$trustedHostTestList, 'trailing.host.abc', true],
  92. [$trustedHostTestList, 'trailing.host.abc.def', true],
  93. [$trustedHostTestList, 'trailing.host.abc:123', true],
  94. [$trustedHostTestList, 'another.trailing.host', false],
  95. // center *
  96. [$trustedHostTestList, 'center', true],
  97. [$trustedHostTestList, 'cenxxxter', true],
  98. [$trustedHostTestList, 'cen.x.y.ter', true],
  99. // with port
  100. [$trustedHostTestList, 'abc.leadingwith.port:123', true],
  101. [$trustedHostTestList, 'abc.leadingwith.port:1234', false],
  102. [$trustedHostTestList, 'trailingwith.port.abc:456', true],
  103. [$trustedHostTestList, 'trailingwith.port.abc:123', false],
  104. // bad hostname
  105. [$trustedHostTestList, '-bad', false],
  106. [$trustedHostTestList, '-bad.leading.host', false],
  107. [$trustedHostTestList, 'bad..der.leading.host', false],
  108. // case sensitivity
  109. [$trustedHostTestList, 'uppercase.domain', true],
  110. [$trustedHostTestList, 'LOWERCASE.DOMAIN', true],
  111. ];
  112. }
  113. public function testIsTrustedDomainOverwriteHost() {
  114. $this->config->expects($this->at(0))
  115. ->method('getSystemValue')
  116. ->with('overwritehost')
  117. ->willReturn('myproxyhost');
  118. $trustedDomainHelper = new TrustedDomainHelper($this->config);
  119. $this->assertTrue($trustedDomainHelper->isTrustedDomain('myproxyhost'));
  120. $this->assertTrue($trustedDomainHelper->isTrustedDomain('myotherhost'));
  121. }
  122. }