SecurityTest.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file test/phpunit/SecurityTest.php
  21. * \ingroup test
  22. * \brief PHPUnit test
  23. * \remarks To run this script as CLI: phpunit filename.php
  24. */
  25. global $conf,$user,$langs,$db;
  26. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  27. //require_once 'PHPUnit/Autoload.php';
  28. if (! defined('NOREQUIRESOC')) {
  29. define('NOREQUIRESOC', '1');
  30. }
  31. if (! defined('NOCSRFCHECK')) {
  32. define('NOCSRFCHECK', '1');
  33. }
  34. if (! defined('NOTOKENRENEWAL')) {
  35. define('NOTOKENRENEWAL', '1');
  36. }
  37. if (! defined('NOREQUIREMENU')) {
  38. define('NOREQUIREMENU', '1'); // If there is no menu to show
  39. }
  40. if (! defined('NOREQUIREHTML')) {
  41. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  42. }
  43. if (! defined('NOREQUIREAJAX')) {
  44. define('NOREQUIREAJAX', '1');
  45. }
  46. if (! defined("NOLOGIN")) {
  47. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  48. }
  49. if (! defined("NOSESSION")) {
  50. define("NOSESSION", '1');
  51. }
  52. require_once dirname(__FILE__).'/../../htdocs/main.inc.php';
  53. require_once dirname(__FILE__).'/../../htdocs/core/lib/security.lib.php';
  54. require_once dirname(__FILE__).'/../../htdocs/core/lib/security2.lib.php';
  55. if (empty($user->id)) {
  56. print "Load permissions for admin user nb 1\n";
  57. $user->fetch(1);
  58. $user->getrights();
  59. }
  60. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  61. /**
  62. * Class for PHPUnit tests
  63. *
  64. * @backupGlobals disabled
  65. * @backupStaticAttributes enabled
  66. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  67. */
  68. class SecurityTest extends PHPUnit\Framework\TestCase
  69. {
  70. protected $savconf;
  71. protected $savuser;
  72. protected $savlangs;
  73. protected $savdb;
  74. /**
  75. * Constructor
  76. * We save global variables into local variables
  77. *
  78. * @param string $name Name
  79. * @return SecurityTest
  80. */
  81. public function __construct($name = '')
  82. {
  83. parent::__construct($name);
  84. //$this->sharedFixture
  85. global $conf,$user,$langs,$db;
  86. $this->savconf=$conf;
  87. $this->savuser=$user;
  88. $this->savlangs=$langs;
  89. $this->savdb=$db;
  90. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  91. //print " - db ".$db->db;
  92. print "\n";
  93. }
  94. /**
  95. * setUpBeforeClass
  96. *
  97. * @return void
  98. */
  99. public static function setUpBeforeClass(): void
  100. {
  101. global $conf,$user,$langs,$db;
  102. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  103. print __METHOD__."\n";
  104. }
  105. /**
  106. * tearDownAfterClass
  107. *
  108. * @return void
  109. */
  110. public static function tearDownAfterClass(): void
  111. {
  112. global $conf,$user,$langs,$db;
  113. $db->rollback();
  114. print __METHOD__."\n";
  115. }
  116. /**
  117. * Init phpunit tests
  118. *
  119. * @return void
  120. */
  121. protected function setUp(): void
  122. {
  123. global $conf,$user,$langs,$db;
  124. $conf=$this->savconf;
  125. $user=$this->savuser;
  126. $langs=$this->savlangs;
  127. $db=$this->savdb;
  128. print __METHOD__."\n";
  129. }
  130. /**
  131. * End phpunit tests
  132. *
  133. * @return void
  134. */
  135. protected function tearDown(): void
  136. {
  137. print __METHOD__."\n";
  138. }
  139. /**
  140. * testSetLang
  141. *
  142. * @return string
  143. */
  144. public function testSetLang()
  145. {
  146. global $conf;
  147. $conf=$this->savconf;
  148. $tmplangs = new Translate('', $conf);
  149. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = "' malicious text with quote";
  150. $tmplangs->setDefaultLang('auto');
  151. print __METHOD__.' $tmplangs->defaultlang='.$tmplangs->defaultlang."\n";
  152. $this->assertEquals($tmplangs->defaultlang, 'malicioustextwithquote_MALICIOUSTEXTWITHQUOTE');
  153. }
  154. /**
  155. * testSqlAndScriptInjectWithPHPUnit
  156. *
  157. * @return void
  158. */
  159. public function testSqlAndScriptInjectWithPHPUnit()
  160. {
  161. // Run tests
  162. // More on https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet
  163. // Should be OK
  164. $expectedresult=0;
  165. /*
  166. $test = '';
  167. $result=testSqlAndScriptInject($test, 0);
  168. $this->assertGreaterThanOrEqual(0, $result, 'Error on testSqlAndScriptInject kkk');
  169. */
  170. $_SERVER["PHP_SELF"]='/DIR WITH SPACE/htdocs/admin/index.php';
  171. $result=testSqlAndScriptInject($_SERVER["PHP_SELF"], 2);
  172. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for PHP_SELF that should be ok');
  173. $test = 'This is a < inside string with < and > also and tag like <a> before the >';
  174. $result=testSqlAndScriptInject($test, 0);
  175. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject expected 0b');
  176. $test = 'This is the union of all for the selection of the best';
  177. $result=testSqlAndScriptInject($test, 0);
  178. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject expected 0c');
  179. $test='/user/perms.php?id=1&action=addrights&entity=1&rights=123&confirm=yes&token=123456789&updatedmodulename=lmscoursetracking';
  180. $result=testSqlAndScriptInject($test, 1);
  181. print "test=".$test." result=".$result."\n";
  182. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject with a valid url');
  183. // Should detect attack
  184. $expectedresult=1;
  185. $_SERVER["PHP_SELF"]='/DIR WITH SPACE/htdocs/admin/index.php/<svg>';
  186. $result=testSqlAndScriptInject($_SERVER["PHP_SELF"], 2);
  187. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject for PHP_SELF that should detect XSS');
  188. $test = 'select @@version';
  189. $result=testSqlAndScriptInject($test, 0);
  190. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for SQL1a. Should find an attack on POST param and did not.');
  191. $test = 'select @@version';
  192. $result=testSqlAndScriptInject($test, 1);
  193. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for SQL1b. Should find an attack on GET param and did not.');
  194. $test = '... update ... set ... =';
  195. $result=testSqlAndScriptInject($test, 1);
  196. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for SQL2a. Should find an attack on GET param and did not.');
  197. $test = "delete\nfrom";
  198. $result=testSqlAndScriptInject($test, 1);
  199. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for SQL2b. Should find an attack on GET param and did not.');
  200. $test = 'action=update& ... set ... =';
  201. $result=testSqlAndScriptInject($test, 1);
  202. $this->assertEquals(0, $result, 'Error on testSqlAndScriptInject for SQL2b. Should not find an attack on GET param and did.');
  203. $test = '... union ... selection ';
  204. $result=testSqlAndScriptInject($test, 1);
  205. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for SQL2c. Should find an attack on GET param and did not.');
  206. $test = 'j&#x61;vascript:';
  207. $result=testSqlAndScriptInject($test, 0);
  208. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for javascript1. Should find an attack and did not.');
  209. $test = 'j&#x61vascript:';
  210. $result=testSqlAndScriptInject($test, 0);
  211. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for javascript2. Should find an attack and did not.');
  212. $test = 'javascript&colon&#x3B;alert(1)';
  213. $result=testSqlAndScriptInject($test, 0);
  214. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for javascript2');
  215. $test="<img src='1.jpg' onerror =javascript:alert('XSS')>";
  216. $result=testSqlAndScriptInject($test, 0);
  217. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa1');
  218. $test="<img src='1.jpg' onerror =javascript:alert('XSS')>";
  219. $result=testSqlAndScriptInject($test, 2);
  220. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa2');
  221. $test='<IMG SRC=# onmouseover="alert(1)">';
  222. $result=testSqlAndScriptInject($test, 0);
  223. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa3');
  224. $test='<IMG SRC onmouseover="alert(1)">';
  225. $result=testSqlAndScriptInject($test, 0);
  226. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa4');
  227. $test='<IMG onmouseover="alert(1)">';
  228. $result=testSqlAndScriptInject($test, 0);
  229. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa5');
  230. $test='<IMG SRC=/ onerror="alert(1)">';
  231. $result=testSqlAndScriptInject($test, 0);
  232. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa6');
  233. $test='<IMG SRC=" &#14; javascript:alert(1);">';
  234. $result=testSqlAndScriptInject($test, 0);
  235. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa7');
  236. $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;>';
  237. $result=testSqlAndScriptInject($test, 0);
  238. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject bbb');
  239. $test='<SCRIPT SRC=http://xss.rocks/xss.js></SCRIPT>';
  240. $result=testSqlAndScriptInject($test, 0);
  241. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject ccc');
  242. $test='<IMG SRC="javascript:alert(\'XSS\');">';
  243. $result=testSqlAndScriptInject($test, 1);
  244. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject ddd');
  245. $test='<IMG """><SCRIPT>alert("XSS")</SCRIPT>">';
  246. $result=testSqlAndScriptInject($test, 0);
  247. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject eee');
  248. $test='<!-- Google analytics -->
  249. <script>
  250. (function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){
  251. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  252. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  253. })(window,document,\'script\',\'https://www.google-analytics.com/analytics.js\',\'ga\');
  254. ga(\'create\',\'UA-99999999-9\', \'auto\');
  255. ga(\'send\', \'pageview\');
  256. </script>';
  257. $result=testSqlAndScriptInject($test, 0);
  258. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject eee');
  259. $test="<IMG SRC=\"jav\tascript:alert('XSS');\">"; // Is locked by some browser 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.
  260. $test="<IMG SRC=\"jav&#x0D;ascript:alert('XSS');\">"; // Same
  261. $test='<SCRIPT/XSS SRC="http://xss.rocks/xss.js"></SCRIPT>';
  262. $result=testSqlAndScriptInject($test, 0);
  263. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject fff1');
  264. $test='<SCRIPT/SRC="http://xss.rocks/xss.js"></SCRIPT>';
  265. $result=testSqlAndScriptInject($test, 0);
  266. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject fff2');
  267. // This case seems to be filtered by browsers now.
  268. $test='<BODY onload!#$%&()*~+-_.,:;?@[/|\]^`=alert(1)>';
  269. //$result=testSqlAndScriptInject($test, 0);
  270. //$this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject ggg');
  271. $test='<iframe src=http://xss.rocks/scriptlet.html <';
  272. $result=testSqlAndScriptInject($test, 0);
  273. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject hhh');
  274. $test='Set.constructor`alert\x281\x29```';
  275. $result=testSqlAndScriptInject($test, 0);
  276. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject iii');
  277. $test="on<!-- ab\nc -->error=alert(1)";
  278. $result=testSqlAndScriptInject($test, 0);
  279. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject jjj');
  280. $test="<img src=x one<a>rror=alert(document.location)";
  281. $result=testSqlAndScriptInject($test, 0);
  282. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject kkk');
  283. $test="<a onpointerdown=alert(document.domain)>XSS</a>";
  284. $result=testSqlAndScriptInject($test, 0);
  285. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject lll');
  286. $test="Text with ' encoded with the numeric html entity converted into text entity &#39; (like when submited by CKEditor)";
  287. $result=testSqlAndScriptInject($test, 0); // result must be 0
  288. $this->assertEquals(0, $result, 'Error on testSqlAndScriptInject mmm, result should be 0 and is not');
  289. $test ='<a href="j&Tab;a&Tab;v&Tab;asc&NewLine;ri&Tab;pt:&lpar;a&Tab;l&Tab;e&Tab;r&Tab;t&Tab;(document.cookie)&rpar;">XSS</a>';
  290. $result=testSqlAndScriptInject($test, 0);
  291. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject nnn, result should be >= 1 and is not');
  292. $test="/dolibarr/htdocs/index.php/".chr('246')."abc"; // Add the char %F6 into the variable
  293. $result=testSqlAndScriptInject($test, 2);
  294. //print "test=".$test." result=".$result."\n";
  295. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject with a non valid UTF8 char');
  296. }
  297. /**
  298. * testGETPOST
  299. *
  300. * @return string
  301. */
  302. public function testGETPOST()
  303. {
  304. global $conf,$user,$langs,$db;
  305. $conf=$this->savconf;
  306. $user=$this->savuser;
  307. $langs=$this->savlangs;
  308. $db=$this->savdb;
  309. // Force default mode
  310. $conf->global->MAIN_RESTRICTHTML_ONLY_VALID_HTML = 0;
  311. $conf->global->MAIN_RESTRICTHTML_REMOVE_ALSO_BAD_ATTRIBUTES = 0;
  312. $_COOKIE["id"]=111;
  313. $_GET["param1"]="222";
  314. $_POST["param1"]="333";
  315. $_GET["param2"]='a/b#e(pr)qq-rr\cc';
  316. $_GET["param3"]='"&#110;a/b#e(pr)qq-rr\cc'; // Same than param2 + " and &#110;
  317. $_GET["param4a"]='..&#47;../dir';
  318. $_GET["param4b"]='..&#92;..\dirwindows';
  319. $_GET["param5"]="a_1-b";
  320. $_POST["param6"]="&quot;&gt;<svg o&#110;load='console.log(&quot;123&quot;)'&gt;";
  321. $_POST["param6b"]='<<<../>../>../svg><<<../>../>../animate =alert(1)>abc';
  322. $_GET["param7"]='"c:\this is a path~1\aaa&#110; &#x&#x31;&#x31;&#x30;;" abc<bad>def</bad>';
  323. $_POST["param8a"]="Hacker<svg o&#110;load='console.log(&quot;123&quot;)'"; // html tag is not closed so it is not detected as html tag but is still harmfull
  324. $_POST['param8b']='<img src=x onerror=alert(document.location) t='; // this is html obfuscated by non closing tag
  325. $_POST['param8c']='< with space after is ok';
  326. $_POST['param8d']='<abc123 is html to clean';
  327. $_POST['param8e']='<123abc is not html to clean'; // other similar case: '<2021-12-12'
  328. $_POST['param8f']='abc<<svg <><<animate onbegin=alert(document.domain) a';
  329. $_POST["param9"]='is_object($object) ? ($object->id < 10 ? round($object->id / 2, 2) : (2 * $user->id) * (int) substr($mysoc->zip, 1, 2)) : \'objnotdefined\'';
  330. $_POST["param10"]='is_object($object) ? ($object->id < 10 ? round($object->id / 2, 2) : (2 * $user->id) * (int) substr($mysoc->zip, 1, 2)) : \'<abc>objnotdefined\'';
  331. $_POST["param11"]=' Name <email@email.com> ';
  332. $_POST["param12"]='<!DOCTYPE html><html>aaa</html>';
  333. $_POST["param13"]='&#110; &#x6E; &gt; &lt; &quot; <a href=\"j&#x61;vascript:alert(document.domain)\">XSS</a>';
  334. $_POST["param13b"]='&#110; &#x6E; &gt; &lt; &quot; <a href=\"j&#x61vascript:alert(document.domain)\">XSS</a>';
  335. $_POST["param14"]="Text with ' encoded with the numeric html entity converted into text entity &#39; (like when submited by CKEditor)";
  336. $_POST["param15"]="<img onerror<=alert(document.domain)> src=>0xbeefed";
  337. //$_POST["param15b"]="<html><head><title>Example HTML</title></head><body><div><p>This is a paragraph.</div><ul><li>Item 1</li><li>Item 2</li></ol></body><html>";
  338. $_POST["param16"]='<a style="z-index: 1000">abc</a>';
  339. $_POST["param17"]='<span style="background-image: url(logout.php)">abc</span>';
  340. $_POST["param18"]='<span style="background-image: url(...?...action=aaa)">abc</span>';
  341. $_POST["param19"]='<a href="j&Tab;a&Tab;v&Tab;asc&NewLine;ri&Tab;pt:&lpar;alert(document.cookie)&rpar;">XSS</a>';
  342. //$_POST["param19"]='<a href="javascript:alert(document.cookie)">XSS</a>';
  343. $result=GETPOST('id', 'int'); // Must return nothing
  344. print __METHOD__." result=".$result."\n";
  345. $this->assertEquals($result, '');
  346. $result=GETPOST("param1", 'int');
  347. print __METHOD__." result=".$result."\n";
  348. $this->assertEquals($result, 222, 'Test on param1 with no 3rd param');
  349. $result=GETPOST("param1", 'int', 2);
  350. print __METHOD__." result=".$result."\n";
  351. $this->assertEquals($result, 333, 'Test on param1 with 3rd param = 2');
  352. // Test with alpha
  353. $result=GETPOST("param2", 'alpha');
  354. print __METHOD__." result=".$result."\n";
  355. $this->assertEquals($result, $_GET["param2"], 'Test on param2');
  356. $result=GETPOST("param3", 'alpha'); // Must return string sanitized from char "
  357. print __METHOD__." result=".$result."\n";
  358. $this->assertEquals($result, 'na/b#e(pr)qq-rr\cc', 'Test on param3');
  359. $result=GETPOST("param4a", 'alpha'); // Must return string sanitized from ../
  360. print __METHOD__." result=".$result."\n";
  361. $this->assertEquals($result, 'dir');
  362. $result=GETPOST("param4b", 'alpha'); // Must return string sanitized from ../
  363. print __METHOD__." result=".$result."\n";
  364. $this->assertEquals($result, 'dirwindows');
  365. // Test with aZ09
  366. $result=GETPOST("param1", 'aZ09');
  367. print __METHOD__." result=".$result."\n";
  368. $this->assertEquals($result, $_GET["param1"]);
  369. $result=GETPOST("param2", 'aZ09'); // Must return '' as string contains car not in aZ09 definition
  370. print __METHOD__." result=".$result."\n";
  371. $this->assertEquals($result, '');
  372. $result=GETPOST("param3", 'aZ09'); // Must return '' as string contains car not in aZ09 definition
  373. print __METHOD__." result=".$result."\n";
  374. $this->assertEquals($result, '');
  375. $result=GETPOST("param4a", 'aZ09'); // Must return '' as string contains car not in aZ09 definition
  376. print __METHOD__." result=".$result."\n";
  377. $this->assertEquals('', $result);
  378. $result=GETPOST("param4b", 'aZ09'); // Must return '' as string contains car not in aZ09 definition
  379. print __METHOD__." result=".$result."\n";
  380. $this->assertEquals('', $result);
  381. $result=GETPOST("param5", 'aZ09');
  382. print __METHOD__." result=".$result."\n";
  383. $this->assertEquals($_GET["param5"], $result);
  384. // Test with nohtml
  385. $result=GETPOST("param6", 'nohtml');
  386. print __METHOD__." result=".$result."\n";
  387. $this->assertEquals('">', $result);
  388. // Test with alpha = alphanohtml. We must convert the html entities like &#110; and disable all entities
  389. $result=GETPOST("param6", 'alphanohtml');
  390. print __METHOD__." result=".$result."\n";
  391. $this->assertEquals('>', $result);
  392. $result=GETPOST("param6b", 'alphanohtml');
  393. print __METHOD__." result=".$result."\n";
  394. $this->assertEquals('abc', $result);
  395. $result=GETPOST("param8a", 'alphanohtml');
  396. print __METHOD__." result=".$result."\n";
  397. $this->assertEquals("Hackersvg onload='console.log(123)'", $result);
  398. $result=GETPOST("param8b", 'alphanohtml');
  399. print __METHOD__." result=".$result."\n";
  400. $this->assertEquals('img src=x onerror=alert(document.location) t=', $result, 'Test a string with non closing html tag with alphanohtml');
  401. $result=GETPOST("param8c", 'alphanohtml');
  402. print __METHOD__." result=".$result."\n";
  403. $this->assertEquals($_POST['param8c'], $result, 'Test a string with non closing html tag with alphanohtml');
  404. $result=GETPOST("param8d", 'alphanohtml');
  405. print __METHOD__." result=".$result."\n";
  406. $this->assertEquals('abc123 is html to clean', $result, 'Test a string with non closing html tag with alphanohtml');
  407. $result=GETPOST("param8e", 'alphanohtml');
  408. print __METHOD__." result=".$result."\n";
  409. $this->assertEquals($_POST['param8e'], $result, 'Test a string with non closing html tag with alphanohtml');
  410. $result=GETPOST("param8f", 'alphanohtml');
  411. print __METHOD__." result=".$result."\n";
  412. $this->assertEquals('abcsvg animate onbegin=alert(document.domain) a', $result, 'Test a string with html tag open with several <');
  413. $result=GETPOST("param9", 'alphanohtml');
  414. print __METHOD__." result=".$result."\n";
  415. $this->assertEquals($_POST["param9"], $result);
  416. $result=GETPOST("param10", 'alphanohtml');
  417. print __METHOD__." result=".$result."\n";
  418. $this->assertEquals($_POST["param9"], $result, 'We should get param9 after processing param10');
  419. $result=GETPOST("param11", 'alphanohtml');
  420. print __METHOD__." result=".$result."\n";
  421. $this->assertEquals("Name", $result, 'Test an email string with alphanohtml');
  422. $result=GETPOST("param13", 'alphanohtml');
  423. print __METHOD__." result=".$result."\n";
  424. $this->assertEquals('n n > < XSS', $result, 'Test that html entities are decoded with alpha');
  425. // Test with alphawithlgt
  426. $result=GETPOST("param11", 'alphawithlgt');
  427. print __METHOD__." result=".$result."\n";
  428. $this->assertEquals(trim($_POST["param11"]), $result, 'Test an email string with alphawithlgt');
  429. // Test with restricthtml: we must remove html open/close tag and content but not htmlentities (we can decode html entities for ascii chars like &#110;)
  430. $result=GETPOST("param6", 'restricthtml');
  431. print __METHOD__." result param6=".$result."\n";
  432. $this->assertEquals('&quot;&gt;', $result);
  433. $result=GETPOST("param7", 'restricthtml');
  434. print __METHOD__." result param7 = ".$result."\n";
  435. $this->assertEquals('"c:\this is a path~1\aaan &#x;;;;" abcdef', $result);
  436. $result=GETPOST("param8e", 'restricthtml');
  437. print __METHOD__." result param8e = ".$result."\n";
  438. $this->assertEquals('', $result);
  439. $result=GETPOST("param12", 'restricthtml');
  440. print __METHOD__." result=".$result."\n";
  441. $this->assertEquals(trim($_POST["param12"]), $result, 'Test a string with DOCTYPE and restricthtml');
  442. $result=GETPOST("param13", 'restricthtml');
  443. print __METHOD__." result=".$result."\n";
  444. $this->assertEquals('n n &gt; &lt; &quot; <a href=\"alert(document.domain)\">XSS</a>', $result, 'Test 13 that HTML entities are decoded with restricthtml, but only for common alpha chars');
  445. $result=GETPOST("param13b", 'restricthtml');
  446. print __METHOD__." result=".$result."\n";
  447. $this->assertEquals('n n &gt; &lt; &quot; <a href=\"alert(document.domain)\">XSS</a>', $result, 'Test 13b that HTML entities are decoded with restricthtml, but only for common alpha chars');
  448. $result=GETPOST("param14", 'restricthtml');
  449. print __METHOD__." result=".$result."\n";
  450. $this->assertEquals("Text with ' encoded with the numeric html entity converted into text entity &#39; (like when submited by CKEditor)", $result, 'Test 14');
  451. $result=GETPOST("param15", 'restricthtml'); // param15 = <img onerror<=alert(document.domain)> src=>0xbeefed that is a dangerous string
  452. print __METHOD__." result=".$result."\n";
  453. $this->assertEquals("<img onerror=alert(document.domain) src=>0xbeefed", $result, 'Test 15'); // The GETPOST return a harmull string
  454. $result=GETPOST("param19", 'restricthtml');
  455. print __METHOD__." result=".$result."\n";
  456. $this->assertEquals('<a href="&lpar;alert(document.cookie)&rpar;">XSS</a>', $result, 'Test 19');
  457. // Test with restricthtml + MAIN_RESTRICTHTML_ONLY_VALID_HTML to test disabling of bad atrributes
  458. $conf->global->MAIN_RESTRICTHTML_ONLY_VALID_HTML = 1;
  459. $result=GETPOST("param15", 'restricthtml'); // param15 = <img onerror<=alert(document.domain)> src=>0xbeefed that is a dangerous string
  460. print __METHOD__." result=".$result."\n";
  461. $this->assertEquals('InvalidHTMLString', $result, 'Test 15b'); // With some PHP and libxml version, we got this when parsong invalid HTML
  462. //$this->assertEquals('<img onerror> src=&gt;0xbeefed', $result, 'Test 15b'); // On other we got a HTML that has been cleaned
  463. unset($conf->global->MAIN_RESTRICTHTML_ONLY_VALID_HTML);
  464. // Test with restricthtml + MAIN_RESTRICTHTML_REMOVE_ALSO_BAD_ATTRIBUTES to test disabling of bad atrributes
  465. $conf->global->MAIN_RESTRICTHTML_REMOVE_ALSO_BAD_ATTRIBUTES = 1;
  466. $result=GETPOST("param15", 'restricthtml');
  467. print __METHOD__." result=".$result."\n";
  468. $this->assertEquals('<img src="">0xbeefed', $result, 'Test 15c');
  469. $result=GETPOST('param16', 'restricthtml');
  470. print __METHOD__." result=".$result."\n";
  471. $this->assertEquals('<a style=" 1000">abc</a>', $result, 'Test tag a with forbidden attribute z-index');
  472. $result=GETPOST('param17', 'restricthtml');
  473. print __METHOD__." result=".$result."\n";
  474. $this->assertEquals('<span style="background-image: url()">abc</span>', $result, 'Test anytag with a forbidden value for attribute');
  475. $result=GETPOST('param18', 'restricthtml');
  476. print __METHOD__." result=".$result."\n";
  477. $this->assertEquals('<span style="background-image: url(...?...aaa)">abc</span>', $result, 'Test anytag with a forbidden value for attribute');
  478. unset($conf->global->MAIN_RESTRICTHTML_REMOVE_ALSO_BAD_ATTRIBUTES);
  479. // Special test for GETPOST of backtopage, backtolist or backtourl parameter
  480. $_POST["backtopage"]='//www.google.com';
  481. $result=GETPOST("backtopage");
  482. print __METHOD__." result=".$result."\n";
  483. $this->assertEquals('www.google.com', $result, 'Test for backtopage param');
  484. $_POST["backtopage"]='https:https://www.google.com';
  485. $result=GETPOST("backtopage");
  486. print __METHOD__." result=".$result."\n";
  487. $this->assertEquals('www.google.com', $result, 'Test for backtopage param');
  488. $_POST["backtolist"]='::HTTPS://www.google.com';
  489. $result=GETPOST("backtolist");
  490. print __METHOD__." result=".$result."\n";
  491. $this->assertEquals('www.google.com', $result, 'Test for backtopage param');
  492. $_POST["backtopage"]='http:www.google.com';
  493. $result=GETPOST("backtopage");
  494. print __METHOD__." result=".$result."\n";
  495. $this->assertEquals('httpwww.google.com', $result, 'Test for backtopage param');
  496. $_POST["backtopage"]='/mydir/mypage.php?aa=a%10a';
  497. $result=GETPOST("backtopage");
  498. print __METHOD__." result=".$result."\n";
  499. $this->assertEquals('/mydir/mypage.php?aa=a%10a', $result, 'Test for backtopage param');
  500. $_POST["backtopage"]='javascripT&javascript#javascriptxjavascript3a alert(1)';
  501. $result=GETPOST("backtopage");
  502. print __METHOD__." result=".$result."\n";
  503. $this->assertEquals('x3aalert(1)', $result, 'Test for backtopage param');
  504. $conf->global->MAIN_SECURITY_MAX_IMG_IN_HTML_CONTENT = 3;
  505. $_POST["pagecontentwithlinks"]='<img src="aaa"><img src="bbb"><img src="ccc"><span style="background: url(/ddd)"></span>';
  506. $result=GETPOST("pagecontentwithlinks", 'restricthtml');
  507. print __METHOD__." result=".$result."\n";
  508. $this->assertEquals('TooManyLinksIntoHTMLString', $result, 'Test on limit on GETPOST fails');
  509. return $result;
  510. }
  511. /**
  512. * testEncodeDecode
  513. *
  514. * @return number
  515. */
  516. public function testEncodeDecode()
  517. {
  518. $stringtotest="This is a string to test encode/decode. This is a string to test encode/decode. This is a string to test encode/decode.";
  519. $encodedstring=dol_encode($stringtotest);
  520. $decodedstring=dol_decode($encodedstring);
  521. print __METHOD__." encodedstring=".$encodedstring." ".base64_encode($stringtotest)."\n";
  522. $this->assertEquals($stringtotest, $decodedstring, 'Use dol_encode/decode with no parameter');
  523. $encodedstring=dol_encode($stringtotest, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
  524. $decodedstring=dol_decode($encodedstring, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
  525. print __METHOD__." encodedstring=".$encodedstring." ".base64_encode($stringtotest)."\n";
  526. $this->assertEquals($stringtotest, $decodedstring, 'Use dol_encode/decode with a key parameter');
  527. return 0;
  528. }
  529. /**
  530. * testDolStringOnlyTheseHtmlTags
  531. *
  532. * @return number
  533. */
  534. public function testDolHTMLEntityDecode()
  535. {
  536. $stringtotest = 'a &colon; b &quot; c &#039; d &apos; e &eacute;';
  537. $decodedstring = dol_html_entity_decode($stringtotest, ENT_QUOTES);
  538. $this->assertEquals('a &colon; b " c \' d &apos; e é', $decodedstring, 'Function did not sanitize correclty');
  539. $stringtotest = 'a &colon; b &quot; c &#039; d &apos; e &eacute;';
  540. $decodedstring = dol_html_entity_decode($stringtotest, ENT_QUOTES|ENT_HTML5);
  541. $this->assertEquals('a : b " c \' d \' e é', $decodedstring, 'Function did not sanitize correclty');
  542. return 0;
  543. }
  544. /**
  545. * testDolStringOnlyTheseHtmlTags
  546. *
  547. * @return number
  548. */
  549. public function testDolStringOnlyTheseHtmlTags()
  550. {
  551. $stringtotest = '<a href="javascript:aaa">bbbڴ';
  552. $decodedstring = dol_string_onlythesehtmltags($stringtotest, 1, 1, 1);
  553. $this->assertEquals('<a href="aaa">bbbڴ', $decodedstring, 'Function did not sanitize correclty with test 1');
  554. $stringtotest = '<a href="java'.chr(0).'script:aaa">bbbڴ';
  555. $decodedstring = dol_string_onlythesehtmltags($stringtotest, 1, 1, 1);
  556. $this->assertEquals('<a href="aaa">bbbڴ', $decodedstring, 'Function did not sanitize correclty with test 2');
  557. $stringtotest = '<a href="javascript&colon;aaa">bbbڴ';
  558. $decodedstring = dol_string_onlythesehtmltags($stringtotest, 1, 1, 1);
  559. $this->assertEquals('<a href="aaa">bbbڴ', $decodedstring, 'Function did not sanitize correclty with test 3');
  560. $stringtotest = 'text <link href="aaa"> text';
  561. $decodedstring = dol_string_onlythesehtmltags($stringtotest, 1, 1, 1, 0, array(), 0);
  562. $this->assertEquals('text text', $decodedstring, 'Function did not sanitize correclty with test 4a');
  563. $stringtotest = 'text <link href="aaa"> text';
  564. $decodedstring = dol_string_onlythesehtmltags($stringtotest, 1, 1, 1, 0, array(), 1);
  565. $this->assertEquals('text <link href="aaa"> text', $decodedstring, 'Function did not sanitize correclty with test 4b');
  566. return 0;
  567. }
  568. /**
  569. * testDolStringOnlyTheseHtmlAttributes
  570. *
  571. * @return number
  572. */
  573. public function testDolStringOnlyTheseHtmlAttributes()
  574. {
  575. $stringtotest = 'eée';
  576. $decodedstring = dol_string_onlythesehtmlattributes($stringtotest);
  577. $this->assertEquals('e&eacute;e', $decodedstring, 'Function did not sanitize correclty with test 1');
  578. $stringtotest = '<div onload="ee"><a href="123"><span class="abc">abc</span></a></div>';
  579. $decodedstring = dol_string_onlythesehtmlattributes($stringtotest);
  580. $decodedstring = preg_replace("/\n$/", "", $decodedstring);
  581. $this->assertEquals('<div><a href="123"><span class="abc">abc</span></a></div>', $decodedstring, 'Function did not sanitize correclty with test 2');
  582. return 0;
  583. }
  584. /**
  585. * testGetRandomPassword
  586. *
  587. * @return number
  588. */
  589. public function testGetRandomPassword()
  590. {
  591. global $conf;
  592. $genpass1=getRandomPassword(true); // Should be a string return by dol_hash (if no option set, will be md5)
  593. print __METHOD__." genpass1=".$genpass1."\n";
  594. $this->assertEquals(strlen($genpass1), 32);
  595. $genpass1=getRandomPassword(true, array('I')); // Should be a string return by dol_hash (if no option set, will be md5)
  596. print __METHOD__." genpass1=".$genpass1."\n";
  597. $this->assertEquals(strlen($genpass1), 32);
  598. $conf->global->USER_PASSWORD_GENERATED='None';
  599. $genpass2=getRandomPassword(false); // Should return an empty string
  600. print __METHOD__." genpass2=".$genpass2."\n";
  601. $this->assertEquals($genpass2, '');
  602. $conf->global->USER_PASSWORD_GENERATED='Standard';
  603. $genpass3=getRandomPassword(false); // Should return a password of 12 chars
  604. print __METHOD__." genpass3=".$genpass3."\n";
  605. $this->assertEquals(strlen($genpass3), 12);
  606. return 0;
  607. }
  608. /**
  609. * testRestrictedArea
  610. *
  611. * @return void
  612. */
  613. public function testRestrictedArea()
  614. {
  615. global $conf,$user,$langs,$db;
  616. $conf=$this->savconf;
  617. $user=$this->savuser;
  618. $langs=$this->savlangs;
  619. $db=$this->savdb;
  620. //$dummyuser=new User($db);
  621. //$result=restrictedArea($dummyuser,'societe');
  622. $result=restrictedArea($user, 'societe');
  623. $this->assertEquals(1, $result);
  624. }
  625. /**
  626. * testGetRandomPassword
  627. *
  628. * @return number
  629. */
  630. public function testGetURLContent()
  631. {
  632. global $conf;
  633. include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  634. $url = 'ftp://mydomain.com';
  635. $tmp = getURLContent($url);
  636. print __METHOD__." url=".$url."\n";
  637. $this->assertGreaterThan(0, strpos($tmp['curl_error_msg'], 'not supported')); // Test error if return does not contains 'not supported'
  638. $url = 'https://www.dolibarr.fr'; // This is a redirect 301 page
  639. $tmp = getURLContent($url, 'GET', '', 0); // We do NOT follow
  640. print __METHOD__." url=".$url."\n";
  641. $this->assertEquals(301, $tmp['http_code'], 'Should GET url 301 response and stop here');
  642. $url = 'https://www.dolibarr.fr'; // This is a redirect 301 page
  643. $tmp = getURLContent($url); // We DO follow a page with return 300 so result should be 200
  644. print __METHOD__." url=".$url."\n";
  645. $this->assertEquals(200, $tmp['http_code'], 'Should GET url 301 with a follow -> 200 but we get '.$tmp['http_code']);
  646. $url = 'http://localhost';
  647. $tmp = getURLContent($url, 'GET', '', 0, array(), array('http', 'https'), 0); // Only external URL
  648. print __METHOD__." url=".$url."\n";
  649. $this->assertEquals(400, $tmp['http_code'], 'Should GET url to '.$url.' that resolves to a local URL'); // Test we receive an error because localtest.me is not an external URL
  650. $url = 'http://127.0.0.1';
  651. $tmp = getURLContent($url, 'GET', '', 0, array(), array('http', 'https'), 0); // Only external URL
  652. print __METHOD__." url=".$url."\n";
  653. $this->assertEquals(400, $tmp['http_code'], 'Should GET url to '.$url.' that is a local URL'); // Test we receive an error because 127.0.0.1 is not an external URL
  654. $url = 'http://127.0.2.1';
  655. $tmp = getURLContent($url, 'GET', '', 0, array(), array('http', 'https'), 0); // Only external URL
  656. print __METHOD__." url=".$url."\n";
  657. $this->assertEquals(400, $tmp['http_code'], 'Should GET url to '.$url.' that is a local URL'); // Test we receive an error because 127.0.2.1 is not an external URL
  658. $url = 'https://169.254.0.1';
  659. $tmp = getURLContent($url, 'GET', '', 0, array(), array('http', 'https'), 0); // Only external URL
  660. print __METHOD__." url=".$url."\n";
  661. $this->assertEquals(400, $tmp['http_code'], 'Should GET url to '.$url.' that is a local URL'); // Test we receive an error because 169.254.0.1 is not an external URL
  662. $url = 'http://[::1]';
  663. $tmp = getURLContent($url, 'GET', '', 0, array(), array('http', 'https'), 0); // Only external URL
  664. print __METHOD__." url=".$url."\n";
  665. $this->assertEquals(400, $tmp['http_code'], 'Should GET url to '.$url.' that is a local URL'); // Test we receive an error because [::1] is not an external URL
  666. /*$url = 'localtest.me';
  667. $tmp = getURLContent($url, 'GET', '', 0, array(), array('http', 'https'), 0); // Only external URL
  668. print __METHOD__." url=".$url."\n";
  669. $this->assertEquals(400, $tmp['http_code'], 'Should GET url to '.$url.' that resolves to a local URL'); // Test we receive an error because localtest.me is not an external URL
  670. */
  671. $url = 'http://192.0.0.192';
  672. $tmp = getURLContent($url, 'GET', '', 0, array(), array('http', 'https'), 0); // Only external URL but on an IP in blacklist
  673. print __METHOD__." url=".$url." tmp['http_code'] = ".$tmp['http_code']."\n";
  674. $this->assertEquals(400, $tmp['http_code'], 'Access should be refused and was not'); // Test we receive an error because ip is in blacklist
  675. return 0;
  676. }
  677. /**
  678. * testDolSanitizeUrl
  679. *
  680. * @return void
  681. */
  682. public function testDolSanitizeUrl()
  683. {
  684. global $conf,$user,$langs,$db;
  685. $conf=$this->savconf;
  686. $user=$this->savuser;
  687. $langs=$this->savlangs;
  688. $db=$this->savdb;
  689. $test = 'javascripT&javascript#x3a alert(1)';
  690. $result=dol_sanitizeUrl($test);
  691. $this->assertEquals('x3a alert(1)', $result, 'Test on dol_sanitizeUrl A');
  692. $test = 'javajavascriptscript&cjavascriptolon;alert(1)';
  693. $result=dol_sanitizeUrl($test);
  694. $this->assertEquals('alert(1)', $result, 'Test on dol_sanitizeUrl B');
  695. $test = '/javas:cript/google.com';
  696. $result=dol_sanitizeUrl($test);
  697. $this->assertEquals('google.com', $result, 'Test on dol_sanitizeUrl C');
  698. }
  699. /**
  700. * testDolSanitizeEmail
  701. *
  702. * @return void
  703. */
  704. public function testDolSanitizeEmail()
  705. {
  706. global $conf,$user,$langs,$db;
  707. $conf=$this->savconf;
  708. $user=$this->savuser;
  709. $langs=$this->savlangs;
  710. $db=$this->savdb;
  711. $test = 'aaa@mycompany.com <My name>, bbb@mycompany.com <Another name>';
  712. $result=dol_sanitizeEmail($test);
  713. $this->assertEquals($test, $result, 'Test on dol_sanitizeEmail A');
  714. $test = "aaa@mycompany.com <My name>,\nbbb@mycompany.com <Another name>";
  715. $result=dol_sanitizeEmail($test);
  716. $this->assertEquals('aaa@mycompany.com <My name>,bbb@mycompany.com <Another name>', $result, 'Test on dol_sanitizeEmail B');
  717. $test = 'aaa@mycompany.com <My name>,\nbbb@mycompany.com <Another name>';
  718. $result=dol_sanitizeEmail($test);
  719. $this->assertEquals('aaa@mycompany.com <My name>,nbbb@mycompany.com <Another name>', $result, 'Test on dol_sanitizeEmail C');
  720. $test = 'aaa@mycompany.com <My name>, "bcc:bbb"@mycompany.com <Another name>';
  721. $result=dol_sanitizeEmail($test);
  722. $this->assertEquals('aaa@mycompany.com <My name>, bccbbb@mycompany.com <Another name>', $result, 'Test on dol_sanitizeEmail D');
  723. }
  724. /**
  725. * testDolSanitizeFileName
  726. *
  727. * @return void
  728. */
  729. public function testDolSanitizeFileName()
  730. {
  731. global $conf,$user,$langs,$db;
  732. $conf=$this->savconf;
  733. $user=$this->savuser;
  734. $langs=$this->savlangs;
  735. $db=$this->savdb;
  736. //$dummyuser=new User($db);
  737. //$result=restrictedArea($dummyuser,'societe');
  738. $result=dol_sanitizeFileName('bad file | evilaction');
  739. $this->assertEquals('bad file _ evilaction', $result);
  740. $result=dol_sanitizeFileName('bad file -evilparam --evilparam ---evilparam ----evilparam');
  741. $this->assertEquals('bad file _evilparam _evilparam _evilparam _evilparam', $result);
  742. }
  743. /**
  744. * testDolEval
  745. *
  746. * @return void
  747. */
  748. public function testDolEval()
  749. {
  750. global $conf,$user,$langs,$db;
  751. $conf=$this->savconf;
  752. $user=$this->savuser;
  753. $langs=$this->savlangs;
  754. $db=$this->savdb;
  755. $result=dol_eval('1==1', 1, 0);
  756. print "result = ".$result."\n";
  757. $this->assertTrue($result);
  758. $result=dol_eval('1==2', 1, 0);
  759. print "result = ".$result."\n";
  760. $this->assertFalse($result);
  761. include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  762. include_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
  763. $s = '(($reloadedobj = new Task($db)) && ($reloadedobj->fetchNoCompute($object->id) > 0) && ($secondloadedobj = new Project($db)) && ($secondloadedobj->fetchNoCompute($reloadedobj->fk_project) > 0)) ? $secondloadedobj->ref : "Parent project not found"';
  764. $result=dol_eval($s, 1, 1, '2');
  765. print "result = ".$result."\n";
  766. $this->assertEquals('Parent project not found', $result);
  767. $s = '(($reloadedobj = new Task($db)) && ($reloadedobj->fetchNoCompute($object->id) > 0) && ($secondloadedobj = new Project($db)) && ($secondloadedobj->fetchNoCompute($reloadedobj->fk_project) > 0)) ? $secondloadedobj->ref : \'Parent project not found\'';
  768. $result=dol_eval($s, 1, 1, '2');
  769. print "result = ".$result."\n";
  770. $this->assertEquals('Parent project not found', $result);
  771. $result=dol_eval('$a=function() { }; $a;', 1, 1, '');
  772. print "result = ".$result."\n";
  773. $this->assertContains('Bad string syntax to evaluate', $result);
  774. $result=dol_eval('$a=exec("ls");', 1, 1);
  775. print "result = ".$result."\n";
  776. $this->assertContains('Bad string syntax to evaluate', $result);
  777. $result=dol_eval('$a=exec ("ls")', 1, 1);
  778. print "result = ".$result."\n";
  779. $this->assertContains('Bad string syntax to evaluate', $result);
  780. $result=dol_eval('$a="test"; $$a;', 1, 0);
  781. print "result = ".$result."\n";
  782. $this->assertContains('Bad string syntax to evaluate', $result);
  783. $result=dol_eval('`ls`', 1, 0);
  784. print "result = ".$result."\n";
  785. $this->assertContains('Bad string syntax to evaluate', $result);
  786. $result=dol_eval("('ex'.'ec')('echo abc')", 1, 0);
  787. print "result = ".$result."\n";
  788. $this->assertContains('Bad string syntax to evaluate', $result);
  789. $result=dol_eval("sprintf(\"%s%s\", \"ex\", \"ec\")('echo abc')", 1, 0);
  790. print "result = ".$result."\n";
  791. $this->assertContains('Bad string syntax to evaluate', $result);
  792. $result=dol_eval("90402.38+267678+0", 1, 1, 1);
  793. print "result = ".$result."\n";
  794. $this->assertEquals('358080.38', $result);
  795. global $leftmenu; // Used into strings to eval
  796. $leftmenu = 'AAA';
  797. $result=dol_eval('$conf->currency && preg_match(\'/^(AAA|BBB)/\',$leftmenu)', 1, 1, '1');
  798. print "result = ".$result."\n";
  799. $this->assertTrue($result);
  800. // Same with syntax error
  801. $leftmenu = 'XXX';
  802. $result=dol_eval('$conf->currency && preg_match(\'/^(AAA|BBB)/\',$leftmenu)', 1, 1, '1');
  803. print "result = ".$result."\n";
  804. $this->assertFalse($result);
  805. // Case with param onlysimplestring = 1
  806. $result=dol_eval('1 && getDolGlobalInt("doesnotexist1") && $conf->global->MAIN_FEATURES_LEVEL', 1, 0); // Should return false and not a 'Bad string syntax to evaluate ...'
  807. print "result = ".$result."\n";
  808. $this->assertFalse($result);
  809. $result=dol_eval("(\$a.'aa')", 1, 0);
  810. print "result = ".$result."\n";
  811. $this->assertContains('Bad string syntax to evaluate', $result);
  812. }
  813. /**
  814. * testCheckLoginPassEntity
  815. *
  816. * @return void
  817. */
  818. public function testCheckLoginPassEntity()
  819. {
  820. $login=checkLoginPassEntity('loginbidon', 'passwordbidon', 1, array('dolibarr'));
  821. print __METHOD__." login=".$login."\n";
  822. $this->assertEquals($login, '');
  823. $login=checkLoginPassEntity('admin', 'passwordbidon', 1, array('dolibarr'));
  824. print __METHOD__." login=".$login."\n";
  825. $this->assertEquals($login, '');
  826. $login=checkLoginPassEntity('admin', 'admin', 1, array('dolibarr')); // Should works because admin/admin exists
  827. print __METHOD__." login=".$login."\n";
  828. $this->assertEquals($login, 'admin', 'The test to check if pass of user "admin" is "admin" has failed');
  829. $login=checkLoginPassEntity('admin', 'admin', 1, array('http','dolibarr')); // Should work because of second authentication method
  830. print __METHOD__." login=".$login."\n";
  831. $this->assertEquals($login, 'admin');
  832. $login=checkLoginPassEntity('admin', 'admin', 1, array('forceuser'));
  833. print __METHOD__." login=".$login."\n";
  834. $this->assertEquals('', $login, 'Error'); // Expected '' because should failed because login 'auto' does not exists
  835. }
  836. }