TrustedDomainHelperTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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() {
  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->once())
  29. ->method('getSystemValue')
  30. ->with('trusted_domains')
  31. ->will($this->returnValue($trustedDomains));
  32. $trustedDomainHelper = new TrustedDomainHelper($this->config);
  33. $this->assertEquals($result, $trustedDomainHelper->isTrustedDomain($testDomain));
  34. }
  35. /**
  36. * @return array
  37. */
  38. public function trustedDomainDataProvider() {
  39. $trustedHostTestList = [
  40. 'host.one.test',
  41. 'host.two.test',
  42. '[1fff:0:a88:85a3::ac1f]',
  43. 'host.three.test:443',
  44. '*.leading.host',
  45. 'trailing.host*',
  46. 'cen*ter',
  47. '*.leadingwith.port:123',
  48. 'trailingwith.port*:456',
  49. 'UPPERCASE.DOMAIN',
  50. 'lowercase.domain',
  51. ];
  52. return [
  53. // empty defaults to false with 8.1
  54. [null, 'host.one.test:8080', false],
  55. ['', 'host.one.test:8080', false],
  56. [[], 'host.one.test:8080', false],
  57. // trust list when defined
  58. [$trustedHostTestList, 'host.two.test:8080', true],
  59. [$trustedHostTestList, 'host.two.test:9999', true],
  60. [$trustedHostTestList, 'host.three.test:8080', false],
  61. [$trustedHostTestList, 'host.two.test:8080:aa:222', false],
  62. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]', true],
  63. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]:801', true],
  64. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]:801:34', false],
  65. [$trustedHostTestList, 'host.three.test:443', true],
  66. [$trustedHostTestList, 'host.three.test:80', false],
  67. [$trustedHostTestList, 'host.three.test', false],
  68. // trust localhost regardless of trust list
  69. [$trustedHostTestList, 'localhost', true],
  70. [$trustedHostTestList, 'localhost:8080', true],
  71. [$trustedHostTestList, '127.0.0.1', true],
  72. [$trustedHostTestList, '127.0.0.1:8080', true],
  73. // do not trust invalid localhosts
  74. [$trustedHostTestList, 'localhost:1:2', false],
  75. [$trustedHostTestList, 'localhost: evil.host', false],
  76. // do not trust casting
  77. [[1], '1', false],
  78. // leading *
  79. [$trustedHostTestList, 'abc.leading.host', true],
  80. [$trustedHostTestList, 'abc.def.leading.host', true],
  81. [$trustedHostTestList, 'abc.def.leading.host.another', false],
  82. [$trustedHostTestList, 'abc.def.leading.host:123', true],
  83. [$trustedHostTestList, 'leading.host', false],
  84. // trailing *
  85. [$trustedHostTestList, 'trailing.host', true],
  86. [$trustedHostTestList, 'trailing.host.abc', true],
  87. [$trustedHostTestList, 'trailing.host.abc.def', true],
  88. [$trustedHostTestList, 'trailing.host.abc:123', true],
  89. [$trustedHostTestList, 'another.trailing.host', false],
  90. // center *
  91. [$trustedHostTestList, 'center', true],
  92. [$trustedHostTestList, 'cenxxxter', true],
  93. [$trustedHostTestList, 'cen.x.y.ter', true],
  94. // with port
  95. [$trustedHostTestList, 'abc.leadingwith.port:123', true],
  96. [$trustedHostTestList, 'abc.leadingwith.port:1234', false],
  97. [$trustedHostTestList, 'trailingwith.port.abc:456', true],
  98. [$trustedHostTestList, 'trailingwith.port.abc:123', false],
  99. // bad hostname
  100. [$trustedHostTestList, '-bad', false],
  101. [$trustedHostTestList, '-bad.leading.host', false],
  102. [$trustedHostTestList, 'bad..der.leading.host', false],
  103. // case sensitivity
  104. [$trustedHostTestList, 'uppercase.domain', true],
  105. [$trustedHostTestList, 'LOWERCASE.DOMAIN', true],
  106. ];
  107. }
  108. }