You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

request.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Thomas Müller <thomas.mueller@tmit.eu>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_Request extends \Test\TestCase {
  9. protected function setUp() {
  10. parent::setUp();
  11. OC::$server->getConfig()->setSystemValue('overwritewebroot', '/domain.tld/ownCloud');
  12. OC::$server->getConfig()->setSystemValue('trusted_proxies', array());
  13. OC::$server->getConfig()->setSystemValue('forwarded_for_headers', array());
  14. }
  15. protected function tearDown() {
  16. OC::$server->getConfig()->setSystemValue('overwritewebroot', '');
  17. OC::$server->getConfig()->setSystemValue('trusted_proxies', array());
  18. OC::$server->getConfig()->setSystemValue('forwarded_for_headers', array());
  19. parent::tearDown();
  20. }
  21. public function testScriptNameOverWrite() {
  22. $_SERVER['REMOTE_ADDR'] = '10.0.0.1';
  23. $_SERVER['SCRIPT_FILENAME'] = __FILE__;
  24. $scriptName = OC_Request::scriptName();
  25. $this->assertEquals('/domain.tld/ownCloud/tests/lib/request.php', $scriptName);
  26. }
  27. public function testGetRemoteAddress() {
  28. $_SERVER['REMOTE_ADDR'] = '10.0.0.2';
  29. $_SERVER['HTTP_X_FORWARDED'] = '10.4.0.5, 10.4.0.4';
  30. $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.0.233';
  31. // Without having specified a trusted remote address
  32. $this->assertEquals('10.0.0.2', OC_Request::getRemoteAddress());
  33. // With specifying a trusted remote address but no trusted header
  34. OC::$server->getConfig()->setSystemValue('trusted_proxies', array('10.0.0.2'));
  35. $this->assertEquals('10.0.0.2', OC_Request::getRemoteAddress());
  36. // With specifying a trusted remote address and trusted headers
  37. OC::$server->getConfig()->setSystemValue('trusted_proxies', array('10.0.0.2'));
  38. OC::$server->getConfig()->setSystemValue('forwarded_for_headers', array('HTTP_X_FORWARDED'));
  39. $this->assertEquals('10.4.0.5', OC_Request::getRemoteAddress());
  40. OC::$server->getConfig()->setSystemValue('forwarded_for_headers', array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED'));
  41. $this->assertEquals('192.168.0.233', OC_Request::getRemoteAddress());
  42. // With specifying multiple trusted remote addresses and trusted headers
  43. OC::$server->getConfig()->setSystemValue('trusted_proxies', array('10.3.4.2', '10.0.0.2', '127.0.3.3'));
  44. OC::$server->getConfig()->setSystemValue('forwarded_for_headers', array('HTTP_X_FORWARDED'));
  45. $this->assertEquals('10.4.0.5', OC_Request::getRemoteAddress());
  46. OC::$server->getConfig()->setSystemValue('forwarded_for_headers', array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED'));
  47. $this->assertEquals('192.168.0.233', OC_Request::getRemoteAddress());
  48. }
  49. /**
  50. * @dataProvider rawPathInfoProvider
  51. * @param $expected
  52. * @param $requestUri
  53. * @param $scriptName
  54. */
  55. public function testRawPathInfo($expected, $requestUri, $scriptName) {
  56. $_SERVER['REQUEST_URI'] = $requestUri;
  57. $_SERVER['SCRIPT_NAME'] = $scriptName;
  58. $rawPathInfo = OC_Request::getRawPathInfo();
  59. $this->assertEquals($expected, $rawPathInfo);
  60. }
  61. function rawPathInfoProvider() {
  62. return array(
  63. array('/core/ajax/translations.php', 'index.php/core/ajax/translations.php', 'index.php'),
  64. array('/core/ajax/translations.php', '/index.php/core/ajax/translations.php', '/index.php'),
  65. array('/core/ajax/translations.php', '//index.php/core/ajax/translations.php', '/index.php'),
  66. array('', '/oc/core', '/oc/core/index.php'),
  67. array('', '/oc/core/', '/oc/core/index.php'),
  68. array('', '/oc/core/index.php', '/oc/core/index.php'),
  69. array('/core/ajax/translations.php', '/core/ajax/translations.php', 'index.php'),
  70. array('/core/ajax/translations.php', '//core/ajax/translations.php', '/index.php'),
  71. array('/core/ajax/translations.php', '/oc/core/ajax/translations.php', '/oc/index.php'),
  72. array('/1', '/oc/core/1', '/oc/core/index.php'),
  73. );
  74. }
  75. /**
  76. * @dataProvider rawPathInfoThrowsExceptionProvider
  77. * @expectedException Exception
  78. *
  79. * @param $requestUri
  80. * @param $scriptName
  81. */
  82. public function testRawPathInfoThrowsException($requestUri, $scriptName) {
  83. $_SERVER['REQUEST_URI'] = $requestUri;
  84. $_SERVER['SCRIPT_NAME'] = $scriptName;
  85. OC_Request::getRawPathInfo();
  86. }
  87. function rawPathInfoThrowsExceptionProvider() {
  88. return array(
  89. array('/oc/core1', '/oc/core/index.php'),
  90. );
  91. }
  92. /**
  93. * @dataProvider userAgentProvider
  94. */
  95. public function testUserAgent($testAgent, $userAgent, $matches) {
  96. $_SERVER['HTTP_USER_AGENT'] = $testAgent;
  97. $this->assertEquals($matches, OC_Request::isUserAgent($userAgent));
  98. }
  99. function userAgentProvider() {
  100. return array(
  101. array(
  102. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
  103. OC_Request::USER_AGENT_IE,
  104. true
  105. ),
  106. array(
  107. 'Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Firefox/24.0',
  108. OC_Request::USER_AGENT_IE,
  109. false
  110. ),
  111. array(
  112. 'Mozilla/5.0 (Linux; Android 4.4; Nexus 4 Build/KRT16S) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36',
  113. OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  114. true
  115. ),
  116. array(
  117. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
  118. OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  119. false
  120. ),
  121. // test two values
  122. array(
  123. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
  124. array(
  125. OC_Request::USER_AGENT_IE,
  126. OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  127. ),
  128. true
  129. ),
  130. array(
  131. 'Mozilla/5.0 (Linux; Android 4.4; Nexus 4 Build/KRT16S) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36',
  132. array(
  133. OC_Request::USER_AGENT_IE,
  134. OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  135. ),
  136. true
  137. ),
  138. array(
  139. 'Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Firefox/24.0',
  140. OC_Request::USER_AGENT_FREEBOX,
  141. false
  142. ),
  143. array(
  144. 'Mozilla/5.0',
  145. OC_Request::USER_AGENT_FREEBOX,
  146. true
  147. ),
  148. array(
  149. 'Fake Mozilla/5.0',
  150. OC_Request::USER_AGENT_FREEBOX,
  151. false
  152. ),
  153. );
  154. }
  155. public function testInsecureServerHost() {
  156. unset($_SERVER['HTTP_X_FORWARDED_HOST']);
  157. unset($_SERVER['HTTP_HOST']);
  158. unset($_SERVER['SERVER_NAME']);
  159. $_SERVER['SERVER_NAME'] = 'from.server.name:8080';
  160. $host = OC_Request::insecureServerHost();
  161. $this->assertEquals('from.server.name:8080', $host);
  162. $_SERVER['HTTP_HOST'] = 'from.host.header:8080';
  163. $host = OC_Request::insecureServerHost();
  164. $this->assertEquals('from.host.header:8080', $host);
  165. $_SERVER['HTTP_X_FORWARDED_HOST'] = 'from.forwarded.host:8080';
  166. $host = OC_Request::insecureServerHost();
  167. $this->assertEquals('from.forwarded.host:8080', $host);
  168. $_SERVER['HTTP_X_FORWARDED_HOST'] = 'from.forwarded.host2:8080,another.one:9000';
  169. $host = OC_Request::insecureServerHost();
  170. $this->assertEquals('from.forwarded.host2:8080', $host);
  171. // clean up
  172. unset($_SERVER['HTTP_X_FORWARDED_HOST']);
  173. unset($_SERVER['HTTP_HOST']);
  174. unset($_SERVER['SERVER_NAME']);
  175. }
  176. public function testGetOverwriteHost() {
  177. unset($_SERVER['REMOTE_ADDR']);
  178. OC_Config::deleteKey('overwritecondaddr');
  179. OC_Config::deleteKey('overwritehost');
  180. $host = OC_Request::getOverwriteHost();
  181. $this->assertNull($host);
  182. OC_Config::setValue('overwritehost', '');
  183. $host = OC_Request::getOverwriteHost();
  184. $this->assertNull($host);
  185. OC_Config::setValue('overwritehost', 'host.one.test:8080');
  186. $host = OC_Request::getOverwriteHost();
  187. $this->assertEquals('host.one.test:8080', $host);
  188. $_SERVER['REMOTE_ADDR'] = 'somehost.test:8080';
  189. OC_Config::setValue('overwritecondaddr', '^somehost\..*$');
  190. $host = OC_Request::getOverwriteHost();
  191. $this->assertEquals('host.one.test:8080', $host);
  192. OC_Config::setValue('overwritecondaddr', '^somethingelse.*$');
  193. $host = OC_Request::getOverwriteHost();
  194. $this->assertNull($host);
  195. // clean up
  196. unset($_SERVER['REMOTE_ADDR']);
  197. OC_Config::deleteKey('overwritecondaddr');
  198. OC_Config::deleteKey('overwritehost');
  199. }
  200. public function hostWithPortProvider() {
  201. return array(
  202. array('localhost:500', 'localhost'),
  203. array('foo.com', 'foo.com'),
  204. array('[1fff:0:a88:85a3::ac1f]:801', '[1fff:0:a88:85a3::ac1f]'),
  205. array('[1fff:0:a88:85a3::ac1f]', '[1fff:0:a88:85a3::ac1f]')
  206. );
  207. }
  208. /**
  209. * @dataProvider hostWithPortProvider
  210. */
  211. public function testGetDomainWithoutPort($hostWithPort, $host) {
  212. $this->assertEquals($host, OC_Request::getDomainWithoutPort($hostWithPort));
  213. }
  214. /**
  215. * @dataProvider trustedDomainDataProvider
  216. */
  217. public function testIsTrustedDomain($trustedDomains, $testDomain, $result) {
  218. OC_Config::deleteKey('trusted_domains');
  219. if ($trustedDomains !== null) {
  220. OC_Config::setValue('trusted_domains', $trustedDomains);
  221. }
  222. $this->assertEquals($result, OC_Request::isTrustedDomain($testDomain));
  223. // clean up
  224. OC_Config::deleteKey('trusted_domains');
  225. }
  226. public function trustedDomainDataProvider() {
  227. $trustedHostTestList = array('host.one.test', 'host.two.test', '[1fff:0:a88:85a3::ac1f]');
  228. return array(
  229. // empty defaults to true
  230. array(null, 'host.one.test:8080', true),
  231. array('', 'host.one.test:8080', true),
  232. array(array(), 'host.one.test:8080', true),
  233. // trust list when defined
  234. array($trustedHostTestList, 'host.two.test:8080', true),
  235. array($trustedHostTestList, 'host.two.test:9999', true),
  236. array($trustedHostTestList, 'host.three.test:8080', false),
  237. array($trustedHostTestList, 'host.two.test:8080:aa:222', false),
  238. array($trustedHostTestList, '[1fff:0:a88:85a3::ac1f]', true),
  239. array($trustedHostTestList, '[1fff:0:a88:85a3::ac1f]:801', true),
  240. array($trustedHostTestList, '[1fff:0:a88:85a3::ac1f]:801:34', false),
  241. // trust localhost regardless of trust list
  242. array($trustedHostTestList, 'localhost', true),
  243. array($trustedHostTestList, 'localhost:8080', true),
  244. array($trustedHostTestList, '127.0.0.1', true),
  245. array($trustedHostTestList, '127.0.0.1:8080', true),
  246. // do not trust invalid localhosts
  247. array($trustedHostTestList, 'localhost:1:2', false),
  248. array($trustedHostTestList, 'localhost: evil.host', false),
  249. );
  250. }
  251. public function testServerHost() {
  252. OC_Config::deleteKey('overwritecondaddr');
  253. OC_Config::setValue('overwritehost', 'overwritten.host:8080');
  254. OC_Config::setValue(
  255. 'trusted_domains',
  256. array(
  257. 'trusted.host:8080',
  258. 'second.trusted.host:8080'
  259. )
  260. );
  261. $_SERVER['HTTP_HOST'] = 'trusted.host:8080';
  262. // CLI always gives localhost
  263. $oldCLI = OC::$CLI;
  264. OC::$CLI = true;
  265. $host = OC_Request::serverHost();
  266. $this->assertEquals('localhost', $host);
  267. OC::$CLI = false;
  268. // overwritehost overrides trusted domain
  269. $host = OC_Request::serverHost();
  270. $this->assertEquals('overwritten.host:8080', $host);
  271. // trusted domain returned when used
  272. OC_Config::deleteKey('overwritehost');
  273. $host = OC_Request::serverHost();
  274. $this->assertEquals('trusted.host:8080', $host);
  275. // trusted domain returned when untrusted one in header
  276. $_SERVER['HTTP_HOST'] = 'untrusted.host:8080';
  277. OC_Config::deleteKey('overwritehost');
  278. $host = OC_Request::serverHost();
  279. $this->assertEquals('trusted.host:8080', $host);
  280. // clean up
  281. OC_Config::deleteKey('overwritecondaddr');
  282. OC_Config::deleteKey('overwritehost');
  283. unset($_SERVER['HTTP_HOST']);
  284. OC::$CLI = $oldCLI;
  285. }
  286. }