CoreTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /* Copyright (C) 2010-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/CoreTest.php
  20. * \ingroup test
  21. * \brief PHPUnit test
  22. * \remarks To run this script as CLI: phpunit filename.php
  23. */
  24. global $conf,$user,$langs,$db;
  25. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  26. //require_once 'PHPUnit/Autoload.php';
  27. //require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  28. if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1');
  29. if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1');
  30. if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  31. if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  32. if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1');
  33. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1');
  34. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); // If there is no menu to show
  35. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't need to load the html.form.class.php
  36. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  37. if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session)
  38. /**
  39. * Class to test core functions
  40. *
  41. * @backupGlobals disabled
  42. * @backupStaticAttributes enabled
  43. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  44. */
  45. class CoreTest extends PHPUnit_Framework_TestCase
  46. {
  47. protected $savconf;
  48. protected $savuser;
  49. protected $savlangs;
  50. protected $savdb;
  51. /**
  52. * Constructor
  53. * We save global variables into local variables
  54. *
  55. * @return CoreTest
  56. */
  57. function __construct()
  58. {
  59. parent::__construct();
  60. //$this->sharedFixture
  61. global $conf,$user,$langs,$db;
  62. $this->savconf=$conf;
  63. $this->savuser=$user;
  64. $this->savlangs=$langs;
  65. $this->savdb=$db;
  66. //print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  67. //print " - db ".$db->db;
  68. print "\n";
  69. }
  70. // Static methods
  71. public static function setUpBeforeClass()
  72. {
  73. global $conf,$user,$langs,$db;
  74. //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  75. print __METHOD__."\n";
  76. }
  77. // tear down after class
  78. public static function tearDownAfterClass()
  79. {
  80. global $conf,$user,$langs,$db;
  81. //$db->rollback();
  82. print __METHOD__."\n";
  83. }
  84. /**
  85. * Init phpunit tests
  86. *
  87. * @return void
  88. */
  89. protected function setUp()
  90. {
  91. global $conf,$user,$langs,$db;
  92. $conf=$this->savconf;
  93. $user=$this->savuser;
  94. $langs=$this->savlangs;
  95. $db=$this->savdb;
  96. print __METHOD__."\n";
  97. }
  98. /**
  99. * End phpunit tests
  100. *
  101. * @return void
  102. */
  103. protected function tearDown()
  104. {
  105. print __METHOD__."\n";
  106. }
  107. /**
  108. * testDetectURLROOT
  109. *
  110. * @return void
  111. */
  112. public function testDetectURLROOT()
  113. {
  114. global $dolibarr_main_prod;
  115. global $dolibarr_main_url_root;
  116. global $dolibarr_main_data_root;
  117. global $dolibarr_main_document_root;
  118. global $dolibarr_main_data_root_alt;
  119. global $dolibarr_main_document_root_alt;
  120. global $dolibarr_main_db_host;
  121. global $dolibarr_main_db_port;
  122. global $dolibarr_main_db_type;
  123. global $dolibarr_main_db_prefix;
  124. $testtodo=0;
  125. // Case 1:
  126. // Test for subdir dolibarrnew (that point to htdocs) in root directory /var/www
  127. // URL: http://localhost/dolibarrnew/admin/system/phpinfo.php
  128. // To prepare this test:
  129. // - Create link from htdocs to /var/www/dolibarrnew
  130. // - Put into conf.php $dolibarr_main_document_root='/var/www/dolibarrnew';
  131. if ($testtodo == 1) {
  132. $_SERVER["HTTPS"]='';
  133. $_SERVER["SERVER_NAME"]='localhost';
  134. $_SERVER["SERVER_PORT"]='80';
  135. $_SERVER["DOCUMENT_ROOT"]='/var/www';
  136. $_SERVER["SCRIPT_NAME"]='/dolibarrnew/admin/system/phpinfo.php';
  137. $expectedresult='/dolibarrnew';
  138. }
  139. // Case 2:
  140. // Test for subdir aaa (that point to dolibarr) in root directory /var/www
  141. // URL: http://localhost/aaa/htdocs/admin/system/phpinfo.php
  142. // To prepare this test:
  143. // - Create link from dolibarr to /var/www/aaa
  144. // - Put into conf.php $dolibarr_main_document_root='/var/www/aaa/htdocs';
  145. if ($testtodo == 2) {
  146. $_SERVER["HTTPS"]='';
  147. $_SERVER["SERVER_NAME"]='localhost';
  148. $_SERVER["SERVER_PORT"]='80';
  149. $_SERVER["DOCUMENT_ROOT"]='/var/www';
  150. $_SERVER["SCRIPT_NAME"]='/aaa/htdocs/admin/system/phpinfo.php';
  151. $expectedresult='/aaa/htdocs';
  152. }
  153. // Case 3:
  154. // Test for virtual host localhostdolibarrnew that point to htdocs directory with
  155. // a direct document root
  156. // URL: http://localhostdolibarrnew/admin/system/phpinfo.php
  157. // To prepare this test:
  158. // - Create virtual host localhostdolibarrnew that point to /home/ldestailleur/git/dolibarr/htdocs
  159. // - Put into conf.php $dolibarr_main_document_root='/home/ldestailleur/git/dolibarr/htdocs';
  160. if ($testtodo == 3) {
  161. $_SERVER["HTTPS"]='';
  162. $_SERVER["SERVER_NAME"]='localhostdolibarrnew';
  163. $_SERVER["SERVER_PORT"]='80';
  164. $_SERVER["DOCUMENT_ROOT"]='/home/ldestailleur/git/dolibarr/htdocs';
  165. $_SERVER["SCRIPT_NAME"]='/admin/system/phpinfo.php';
  166. $expectedresult='';
  167. }
  168. // Case 4:
  169. // Test for virtual host localhostdolibarrnew that point to htdocs directory with
  170. // a symbolic link
  171. // URL: http://localhostdolibarrnew/admin/system/phpinfo.php
  172. if ($testtodo == 4) {
  173. $_SERVER["HTTPS"]='';
  174. $_SERVER["SERVER_NAME"]='localhostdolibarrnew';
  175. $_SERVER["SERVER_PORT"]='80';
  176. $_SERVER["DOCUMENT_ROOT"]='/var/www/dolibarr'; // This is a link that point to /home/ldestail/workspace/dolibarr/htdocs
  177. $_SERVER["SCRIPT_NAME"]='/admin/system/phpinfo.php';
  178. $expectedresult='';
  179. }
  180. // Case 5:
  181. // Test for alias /dolibarralias, Test when using nginx, Test when using lighttpd
  182. // URL: http://localhost/dolibarralias/admin/system/phpinfo.php
  183. // To prepare this test:
  184. // - Copy content of dolibarr project into /var/www/dolibarr
  185. // - Put into conf.php $dolibarr_main_document_root='/var/www/dolibarr/htdocs';
  186. // - Put into conf.php $dolibarr_main_url_root='http://localhost/dolibarralias'; (because autodetect will fails in this case)
  187. if ($testtodo == 5) {
  188. $_SERVER["HTTPS"]='';
  189. $_SERVER["SERVER_NAME"]='localhost';
  190. $_SERVER["SERVER_PORT"]='80';
  191. $_SERVER["DOCUMENT_ROOT"]='/var/www';
  192. $_SERVER["SCRIPT_NAME"]='/dolibarralias/admin/system/phpinfo.php';
  193. $expectedresult='/dolibarralias';
  194. }
  195. // Force to rerun filefunc.inc.php
  196. include dirname(__FILE__).'/../../htdocs/filefunc.inc.php';
  197. if ($testtodo != 0)
  198. {
  199. print __METHOD__." DOL_MAIN_URL_ROOT=".DOL_MAIN_URL_ROOT."\n";
  200. print __METHOD__." DOL_URL_ROOT=".DOL_URL_ROOT."\n";
  201. $this->assertEquals($expectedresult, DOL_URL_ROOT);
  202. }
  203. return true;
  204. }
  205. /**
  206. * testSqlAndScriptInject
  207. *
  208. * @return void
  209. */
  210. public function testSqlAndScriptInject()
  211. {
  212. global $dolibarr_main_prod;
  213. global $dolibarr_main_url_root;
  214. global $dolibarr_main_data_root;
  215. global $dolibarr_main_document_root;
  216. global $dolibarr_main_data_root_alt;
  217. global $dolibarr_main_document_root_alt;
  218. global $dolibarr_main_db_host;
  219. global $dolibarr_main_db_port;
  220. global $dolibarr_main_db_type;
  221. global $dolibarr_main_db_prefix;
  222. // This is code copied from main.inc.php !!!!!!!!!!!!!!!
  223. /**
  224. * Security: SQL Injection and XSS Injection (scripts) protection (Filters on GET, POST, PHP_SELF).
  225. *
  226. * @param string $val Value
  227. * @param string $type 1=GET, 0=POST, 2=PHP_SELF
  228. * @return int >0 if there is an injection
  229. */
  230. function test_sql_and_script_inject($val, $type)
  231. {
  232. $inj = 0;
  233. // For SQL Injection (only GET and POST are used to be included into bad escaped SQL requests)
  234. if ($type != 2)
  235. {
  236. $inj += preg_match('/delete\s+from/i', $val);
  237. $inj += preg_match('/create\s+table/i', $val);
  238. $inj += preg_match('/update.+set.+=/i', $val);
  239. $inj += preg_match('/insert\s+into/i', $val);
  240. $inj += preg_match('/select.+from/i', $val);
  241. $inj += preg_match('/union.+select/i', $val);
  242. $inj += preg_match('/into\s+(outfile|dumpfile)/i', $val);
  243. $inj += preg_match('/(\.\.%2f)+/i', $val);
  244. }
  245. // For XSS Injection done by adding javascript with script
  246. // This is all cases a browser consider text is javascript:
  247. // When it found '<script', 'javascript:', '<style', 'onload\s=' on body tag, '="&' on a tag size with old browsers
  248. // All examples on page: http://ha.ckers.org/xss.html#XSScalc
  249. // More on https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet
  250. $inj += preg_match('/<script/i', $val);
  251. $inj += preg_match('/<iframe/i', $val);
  252. $inj += preg_match('/Set\.constructor/i', $val); // ECMA script 6
  253. if (! defined('NOSTYLECHECK')) $inj += preg_match('/<style/i', $val);
  254. $inj += preg_match('/base[\s]+href/si', $val);
  255. $inj += preg_match('/<.*onmouse/si', $val); // onmousexxx can be set on img or any html tag like <img title='...' onmouseover=alert(1)>
  256. $inj += preg_match('/onerror\s*=/i', $val); // onerror can be set on img or any html tag like <img title='...' onerror = alert(1)>
  257. $inj += preg_match('/onfocus\s*=/i', $val); // onfocus can be set on input text html tag like <input type='text' value='...' onfocus = alert(1)>
  258. $inj += preg_match('/onload\s*=/i', $val); // onload can be set on svg tag <svg/onload=alert(1)> or other tag like body <body onload=alert(1)>
  259. //$inj += preg_match('/on[A-Z][a-z]+\*=/', $val); // To lock event handlers onAbort(), ...
  260. $inj += preg_match('/&#58;|&#0000058|&#x3A/i', $val); // refused string ':' encoded (no reason to have it encoded) to lock 'javascript:...'
  261. //if ($type == 1)
  262. //{
  263. $inj += preg_match('/javascript:/i', $val);
  264. $inj += preg_match('/vbscript:/i', $val);
  265. //}
  266. // For XSS Injection done by adding javascript closing html tags like with onmousemove, etc... (closing a src or href tag with not cleaned param)
  267. if ($type == 1) $inj += preg_match('/"/i', $val); // We refused " in GET parameters value
  268. if ($type == 2) $inj += preg_match('/[;"]/', $val); // PHP_SELF is a file system path. It can contains spaces.
  269. return $inj;
  270. }
  271. // Run tests
  272. // More on https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet
  273. // Should be OK
  274. $expectedresult=0;
  275. $_SERVER["PHP_SELF"]='/DIR WITH SPACE/htdocs/admin/index.php?mainmenu=home&leftmenu=setup&username=weservices';
  276. $result=test_sql_and_script_inject($_SERVER["PHP_SELF"], 2);
  277. $this->assertEquals($expectedresult, $result, 'Error on test_sql_and_script_inject 1a');
  278. // Should detect XSS
  279. $expectedresult=1;
  280. $_SERVER["PHP_SELF"]='/DIR WITH SPACE/htdocs/admin/index.php?mainmenu=home&leftmenu=setup&username=weservices;badaction';
  281. $result=test_sql_and_script_inject($_SERVER["PHP_SELF"], 2);
  282. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject 1b');
  283. $test="<img src='1.jpg' onerror =javascript:alert('XSS')>";
  284. $result=test_sql_and_script_inject($test, 0);
  285. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject aaa');
  286. $test="<img src='1.jpg' onerror =javascript:alert('XSS')>";
  287. $result=test_sql_and_script_inject($test, 2);
  288. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject aaa2');
  289. $test='<IMG SRC=# onmouseover="alert(1)">';
  290. $result=test_sql_and_script_inject($test, 0);
  291. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject aaa3');
  292. $test='<IMG SRC onmouseover="alert(1)">';
  293. $result=test_sql_and_script_inject($test, 0);
  294. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject aaa4');
  295. $test='<IMG onmouseover="alert(1)">';
  296. $result=test_sql_and_script_inject($test, 0);
  297. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject aaa5');
  298. $test='<IMG SRC=/ onerror="alert(1)">';
  299. $result=test_sql_and_script_inject($test, 0);
  300. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject aaa6');
  301. $test='<IMG SRC=" &#14; javascript:alert(1);">';
  302. $result=test_sql_and_script_inject($test, 0);
  303. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject aaa7');
  304. $test='<IMG SRC=&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;>';
  305. $result=test_sql_and_script_inject($test, 0);
  306. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject bbb');
  307. $test='<SCRIPT SRC=http://xss.rocks/xss.js></SCRIPT>';
  308. $result=test_sql_and_script_inject($test, 0);
  309. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject ccc');
  310. $test='<IMG SRC="javascript:alert(\'XSS\');">';
  311. $result=test_sql_and_script_inject($test, 1);
  312. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject ddd');
  313. $test='<IMG """><SCRIPT>alert("XSS")</SCRIPT>">';
  314. $result=test_sql_and_script_inject($test, 0);
  315. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject eee');
  316. $test='<!-- Google analytics -->
  317. <script>
  318. (function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){
  319. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  320. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  321. })(window,document,\'script\',\'https://www.google-analytics.com/analytics.js\',\'ga\');
  322. ga(\'create\',\'UA-99999999-9\', \'auto\');
  323. ga(\'send\', \'pageview\');
  324. </script>';
  325. $result=test_sql_and_script_inject($test, 0);
  326. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject eee');
  327. $test="<IMG SRC=\"jav\tascript:alert('XSS');\">"; // Is locked by some brwoser like chrome because the default directive no-referrer-when-downgrade is sent when requesting the SRC and then refused because of browser protection on img src load without referrer.
  328. $test="<IMG SRC=\"jav&#x0D;ascript:alert('XSS');\">"; // Same
  329. $test='<SCRIPT/XSS SRC="http://xss.rocks/xss.js"></SCRIPT>';
  330. $result=test_sql_and_script_inject($test, 0);
  331. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject fff1');
  332. $test='<SCRIPT/SRC="http://xss.rocks/xss.js"></SCRIPT>';
  333. $result=test_sql_and_script_inject($test, 0);
  334. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject fff2');
  335. // This case seems to be filtered by browsers now.
  336. $test='<BODY onload!#$%&()*~+-_.,:;?@[/|\]^`=alert(1)>';
  337. //$result=test_sql_and_script_inject($test, 0);
  338. //$this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject ggg');
  339. $test='<iframe src=http://xss.rocks/scriptlet.html <';
  340. $result=test_sql_and_script_inject($test, 0);
  341. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject hhh');
  342. $test='Set.constructor`alert\x281\x29```';
  343. $result=test_sql_and_script_inject($test, 0);
  344. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on test_sql_and_script_inject iii');
  345. }
  346. }