TrustedDomainHelperTest.php 4.4 KB

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