TrustedDomainHelperTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ];
  50. return [
  51. // empty defaults to false with 8.1
  52. [null, 'host.one.test:8080', false],
  53. ['', 'host.one.test:8080', false],
  54. [[], 'host.one.test:8080', false],
  55. // trust list when defined
  56. [$trustedHostTestList, 'host.two.test:8080', true],
  57. [$trustedHostTestList, 'host.two.test:9999', true],
  58. [$trustedHostTestList, 'host.three.test:8080', false],
  59. [$trustedHostTestList, 'host.two.test:8080:aa:222', false],
  60. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]', true],
  61. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]:801', true],
  62. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]:801:34', false],
  63. [$trustedHostTestList, 'host.three.test:443', true],
  64. [$trustedHostTestList, 'host.three.test:80', false],
  65. [$trustedHostTestList, 'host.three.test', false],
  66. // trust localhost regardless of trust list
  67. [$trustedHostTestList, 'localhost', true],
  68. [$trustedHostTestList, 'localhost:8080', true],
  69. [$trustedHostTestList, '127.0.0.1', true],
  70. [$trustedHostTestList, '127.0.0.1:8080', true],
  71. // do not trust invalid localhosts
  72. [$trustedHostTestList, 'localhost:1:2', false],
  73. [$trustedHostTestList, 'localhost: evil.host', false],
  74. // do not trust casting
  75. [[1], '1', false],
  76. // leading *
  77. [$trustedHostTestList, 'abc.leading.host', true],
  78. [$trustedHostTestList, 'abc.def.leading.host', true],
  79. [$trustedHostTestList, 'abc.def.leading.host.another', false],
  80. [$trustedHostTestList, 'abc.def.leading.host:123', true],
  81. [$trustedHostTestList, 'leading.host', false],
  82. // trailing *
  83. [$trustedHostTestList, 'trailing.host', true],
  84. [$trustedHostTestList, 'trailing.host.abc', true],
  85. [$trustedHostTestList, 'trailing.host.abc.def', true],
  86. [$trustedHostTestList, 'trailing.host.abc:123', true],
  87. [$trustedHostTestList, 'another.trailing.host', false],
  88. // center *
  89. [$trustedHostTestList, 'center', true],
  90. [$trustedHostTestList, 'cenxxxter', true],
  91. [$trustedHostTestList, 'cen.x.y.ter', true],
  92. // with port
  93. [$trustedHostTestList, 'abc.leadingwith.port:123', true],
  94. [$trustedHostTestList, 'abc.leadingwith.port:1234', false],
  95. [$trustedHostTestList, 'trailingwith.port.abc:456', true],
  96. [$trustedHostTestList, 'trailingwith.port.abc:123', false],
  97. // bad hostname
  98. [$trustedHostTestList, '-bad', false],
  99. [$trustedHostTestList, '-bad.leading.host', false],
  100. [$trustedHostTestList, 'bad..der.leading.host', false],
  101. ];
  102. }
  103. }