SecurityTest.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. // Should detect attack
  180. $expectedresult=1;
  181. $_SERVER["PHP_SELF"]='/DIR WITH SPACE/htdocs/admin/index.php/<svg>';
  182. $result=testSqlAndScriptInject($_SERVER["PHP_SELF"], 2);
  183. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject for PHP_SELF that should detect XSS');
  184. $test = 'select @@version';
  185. $result=testSqlAndScriptInject($test, 0);
  186. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for SQL1a. Should find an attack on POST param and did not.');
  187. $test = 'select @@version';
  188. $result=testSqlAndScriptInject($test, 1);
  189. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for SQL1b. Should find an attack on GET param and did not.');
  190. $test = '... update ... set ... =';
  191. $result=testSqlAndScriptInject($test, 1);
  192. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for SQL2a. Should find an attack on GET param and did not.');
  193. $test = "delete\nfrom";
  194. $result=testSqlAndScriptInject($test, 1);
  195. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for SQL2b. Should find an attack on GET param and did not.');
  196. $test = 'action=update& ... set ... =';
  197. $result=testSqlAndScriptInject($test, 1);
  198. $this->assertEquals(0, $result, 'Error on testSqlAndScriptInject for SQL2b. Should not find an attack on GET param and did.');
  199. $test = '... union ... selection ';
  200. $result=testSqlAndScriptInject($test, 1);
  201. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for SQL2c. Should find an attack on GET param and did not.');
  202. $test = 'j&#x61;vascript:';
  203. $result=testSqlAndScriptInject($test, 0);
  204. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for javascript1. Should find an attack and did not.');
  205. $test = 'j&#x61vascript:';
  206. $result=testSqlAndScriptInject($test, 0);
  207. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for javascript2. Should find an attack and did not.');
  208. $test = 'javascript&colon&#x3B;alert(1)';
  209. $result=testSqlAndScriptInject($test, 0);
  210. $this->assertEquals($expectedresult, $result, 'Error on testSqlAndScriptInject for javascript2');
  211. $test="<img src='1.jpg' onerror =javascript:alert('XSS')>";
  212. $result=testSqlAndScriptInject($test, 0);
  213. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa1');
  214. $test="<img src='1.jpg' onerror =javascript:alert('XSS')>";
  215. $result=testSqlAndScriptInject($test, 2);
  216. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa2');
  217. $test='<IMG SRC=# onmouseover="alert(1)">';
  218. $result=testSqlAndScriptInject($test, 0);
  219. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa3');
  220. $test='<IMG SRC onmouseover="alert(1)">';
  221. $result=testSqlAndScriptInject($test, 0);
  222. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa4');
  223. $test='<IMG onmouseover="alert(1)">';
  224. $result=testSqlAndScriptInject($test, 0);
  225. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa5');
  226. $test='<IMG SRC=/ onerror="alert(1)">';
  227. $result=testSqlAndScriptInject($test, 0);
  228. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa6');
  229. $test='<IMG SRC=" &#14; javascript:alert(1);">';
  230. $result=testSqlAndScriptInject($test, 0);
  231. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject aaa7');
  232. $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;>';
  233. $result=testSqlAndScriptInject($test, 0);
  234. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject bbb');
  235. $test='<SCRIPT SRC=http://xss.rocks/xss.js></SCRIPT>';
  236. $result=testSqlAndScriptInject($test, 0);
  237. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject ccc');
  238. $test='<IMG SRC="javascript:alert(\'XSS\');">';
  239. $result=testSqlAndScriptInject($test, 1);
  240. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject ddd');
  241. $test='<IMG """><SCRIPT>alert("XSS")</SCRIPT>">';
  242. $result=testSqlAndScriptInject($test, 0);
  243. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject eee');
  244. $test='<!-- Google analytics -->
  245. <script>
  246. (function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){
  247. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  248. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  249. })(window,document,\'script\',\'https://www.google-analytics.com/analytics.js\',\'ga\');
  250. ga(\'create\',\'UA-99999999-9\', \'auto\');
  251. ga(\'send\', \'pageview\');
  252. </script>';
  253. $result=testSqlAndScriptInject($test, 0);
  254. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject eee');
  255. $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.
  256. $test="<IMG SRC=\"jav&#x0D;ascript:alert('XSS');\">"; // Same
  257. $test='<SCRIPT/XSS SRC="http://xss.rocks/xss.js"></SCRIPT>';
  258. $result=testSqlAndScriptInject($test, 0);
  259. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject fff1');
  260. $test='<SCRIPT/SRC="http://xss.rocks/xss.js"></SCRIPT>';
  261. $result=testSqlAndScriptInject($test, 0);
  262. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject fff2');
  263. // This case seems to be filtered by browsers now.
  264. $test='<BODY onload!#$%&()*~+-_.,:;?@[/|\]^`=alert(1)>';
  265. //$result=testSqlAndScriptInject($test, 0);
  266. //$this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject ggg');
  267. $test='<iframe src=http://xss.rocks/scriptlet.html <';
  268. $result=testSqlAndScriptInject($test, 0);
  269. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject hhh');
  270. $test='Set.constructor`alert\x281\x29```';
  271. $result=testSqlAndScriptInject($test, 0);
  272. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject iii');
  273. $test="on<!-- ab\nc -->error=alert(1)";
  274. $result=testSqlAndScriptInject($test, 0);
  275. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject jjj');
  276. $test="<img src=x one<a>rror=alert(document.location)";
  277. $result=testSqlAndScriptInject($test, 0);
  278. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject kkk');
  279. $test="<a onpointerdown=alert(document.domain)>XSS</a>";
  280. $result=testSqlAndScriptInject($test, 0);
  281. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject lll');
  282. $test="Text with ' encoded with the numeric html entity converted into text entity &#39; (like when submited by CKEditor)";
  283. $result=testSqlAndScriptInject($test, 0); // result must be 0
  284. $this->assertEquals(0, $result, 'Error on testSqlAndScriptInject mmm, result should be 0 and is not');
  285. $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>';
  286. $result=testSqlAndScriptInject($test, 0);
  287. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject nnn, result should be >= 1 and is not');
  288. $test="/dolibarr/htdocs/index.php/".chr('246')."abc"; // Add the char %F6 into the variable
  289. $result=testSqlAndScriptInject($test, 2);
  290. //print "test=".$test." result=".$result."\n";
  291. $this->assertGreaterThanOrEqual($expectedresult, $result, 'Error on testSqlAndScriptInject with a non valid UTF8 char');
  292. }
  293. /**
  294. * testGETPOST
  295. *
  296. * @return string
  297. */
  298. public function testGETPOST()
  299. {
  300. global $conf,$user,$langs,$db;
  301. $conf=$this->savconf;
  302. $user=$this->savuser;
  303. $langs=$this->savlangs;
  304. $db=$this->savdb;
  305. // Force default mode
  306. $conf->global->MAIN_RESTRICTHTML_ONLY_VALID_HTML = 0;
  307. $conf->global->MAIN_RESTRICTHTML_REMOVE_ALSO_BAD_ATTRIBUTES = 0;
  308. $_COOKIE["id"]=111;
  309. $_GET["param1"]="222";
  310. $_POST["param1"]="333";
  311. $_GET["param2"]='a/b#e(pr)qq-rr\cc';
  312. $_GET["param3"]='"&#110;a/b#e(pr)qq-rr\cc'; // Same than param2 + " and &#110;
  313. $_GET["param4a"]='..&#47;../dir';
  314. $_GET["param4b"]='..&#92;..\dirwindows';
  315. $_GET["param5"]="a_1-b";
  316. $_POST["param6"]="&quot;&gt;<svg o&#110;load='console.log(&quot;123&quot;)'&gt;";
  317. $_POST["param6b"]='<<<../>../>../svg><<<../>../>../animate =alert(1)>abc';
  318. $_GET["param7"]='"c:\this is a path~1\aaa&#110; &#x&#x31;&#x31;&#x30;;" abc<bad>def</bad>';
  319. $_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
  320. $_POST['param8b']='<img src=x onerror=alert(document.location) t='; // this is html obfuscated by non closing tag
  321. $_POST['param8c']='< with space after is ok';
  322. $_POST['param8d']='<abc123 is html to clean';
  323. $_POST['param8e']='<123abc is not html to clean'; // other similar case: '<2021-12-12'
  324. $_POST['param8f']='abc<<svg <><<animate onbegin=alert(document.domain) a';
  325. $_POST["param9"]='is_object($object) ? ($object->id < 10 ? round($object->id / 2, 2) : (2 * $user->id) * (int) substr($mysoc->zip, 1, 2)) : \'objnotdefined\'';
  326. $_POST["param10"]='is_object($object) ? ($object->id < 10 ? round($object->id / 2, 2) : (2 * $user->id) * (int) substr($mysoc->zip, 1, 2)) : \'<abc>objnotdefined\'';
  327. $_POST["param11"]=' Name <email@email.com> ';
  328. $_POST["param12"]='<!DOCTYPE html><html>aaa</html>';
  329. $_POST["param13"]='&#110; &#x6E; &gt; &lt; &quot; <a href=\"j&#x61;vascript:alert(document.domain)\">XSS</a>';
  330. $_POST["param13b"]='&#110; &#x6E; &gt; &lt; &quot; <a href=\"j&#x61vascript:alert(document.domain)\">XSS</a>';
  331. $_POST["param14"]="Text with ' encoded with the numeric html entity converted into text entity &#39; (like when submited by CKEditor)";
  332. $_POST["param15"]="<img onerror<=alert(document.domain)> src=>0xbeefed";
  333. //$_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>";
  334. $_POST["param16"]='<a style="z-index: 1000">abc</a>';
  335. $_POST["param17"]='<span style="background-image: url(logout.php)">abc</span>';
  336. $_POST["param18"]='<span style="background-image: url(...?...action=aaa)">abc</span>';
  337. $_POST["param19"]='<a href="j&Tab;a&Tab;v&Tab;asc&NewLine;ri&Tab;pt:&lpar;alert(document.cookie)&rpar;">XSS</a>';
  338. //$_POST["param19"]='<a href="javascript:alert(document.cookie)">XSS</a>';
  339. $result=GETPOST('id', 'int'); // Must return nothing
  340. print __METHOD__." result=".$result."\n";
  341. $this->assertEquals($result, '');
  342. $result=GETPOST("param1", 'int');
  343. print __METHOD__." result=".$result."\n";
  344. $this->assertEquals($result, 222, 'Test on param1 with no 3rd param');
  345. $result=GETPOST("param1", 'int', 2);
  346. print __METHOD__." result=".$result."\n";
  347. $this->assertEquals($result, 333, 'Test on param1 with 3rd param = 2');
  348. // Test with alpha
  349. $result=GETPOST("param2", 'alpha');
  350. print __METHOD__." result=".$result."\n";
  351. $this->assertEquals($result, $_GET["param2"], 'Test on param2');
  352. $result=GETPOST("param3", 'alpha'); // Must return string sanitized from char "
  353. print __METHOD__." result=".$result."\n";
  354. $this->assertEquals($result, 'na/b#e(pr)qq-rr\cc', 'Test on param3');
  355. $result=GETPOST("param4a", 'alpha'); // Must return string sanitized from ../
  356. print __METHOD__." result=".$result."\n";
  357. $this->assertEquals($result, 'dir');
  358. $result=GETPOST("param4b", 'alpha'); // Must return string sanitized from ../
  359. print __METHOD__." result=".$result."\n";
  360. $this->assertEquals($result, 'dirwindows');
  361. // Test with aZ09
  362. $result=GETPOST("param1", 'aZ09');
  363. print __METHOD__." result=".$result."\n";
  364. $this->assertEquals($result, $_GET["param1"]);
  365. $result=GETPOST("param2", 'aZ09'); // Must return '' as string contains car not in aZ09 definition
  366. print __METHOD__." result=".$result."\n";
  367. $this->assertEquals($result, '');
  368. $result=GETPOST("param3", 'aZ09'); // Must return '' as string contains car not in aZ09 definition
  369. print __METHOD__." result=".$result."\n";
  370. $this->assertEquals($result, '');
  371. $result=GETPOST("param4a", 'aZ09'); // Must return '' as string contains car not in aZ09 definition
  372. print __METHOD__." result=".$result."\n";
  373. $this->assertEquals('', $result);
  374. $result=GETPOST("param4b", 'aZ09'); // Must return '' as string contains car not in aZ09 definition
  375. print __METHOD__." result=".$result."\n";
  376. $this->assertEquals('', $result);
  377. $result=GETPOST("param5", 'aZ09');
  378. print __METHOD__." result=".$result."\n";
  379. $this->assertEquals($_GET["param5"], $result);
  380. // Test with nohtml
  381. $result=GETPOST("param6", 'nohtml');
  382. print __METHOD__." result=".$result."\n";
  383. $this->assertEquals('">', $result);
  384. // Test with alpha = alphanohtml. We must convert the html entities like &#110; and disable all entities
  385. $result=GETPOST("param6", 'alphanohtml');
  386. print __METHOD__." result=".$result."\n";
  387. $this->assertEquals('>', $result);
  388. $result=GETPOST("param6b", 'alphanohtml');
  389. print __METHOD__." result=".$result."\n";
  390. $this->assertEquals('abc', $result);
  391. $result=GETPOST("param8a", 'alphanohtml');
  392. print __METHOD__." result=".$result."\n";
  393. $this->assertEquals("Hackersvg onload='console.log(123)'", $result);
  394. $result=GETPOST("param8b", 'alphanohtml');
  395. print __METHOD__." result=".$result."\n";
  396. $this->assertEquals('img src=x onerror=alert(document.location) t=', $result, 'Test a string with non closing html tag with alphanohtml');
  397. $result=GETPOST("param8c", 'alphanohtml');
  398. print __METHOD__." result=".$result."\n";
  399. $this->assertEquals($_POST['param8c'], $result, 'Test a string with non closing html tag with alphanohtml');
  400. $result=GETPOST("param8d", 'alphanohtml');
  401. print __METHOD__." result=".$result."\n";
  402. $this->assertEquals('abc123 is html to clean', $result, 'Test a string with non closing html tag with alphanohtml');
  403. $result=GETPOST("param8e", 'alphanohtml');
  404. print __METHOD__." result=".$result."\n";
  405. $this->assertEquals($_POST['param8e'], $result, 'Test a string with non closing html tag with alphanohtml');
  406. $result=GETPOST("param8f", 'alphanohtml');
  407. print __METHOD__." result=".$result."\n";
  408. $this->assertEquals('abcsvg animate onbegin=alert(document.domain) a', $result, 'Test a string with html tag open with several <');
  409. $result=GETPOST("param9", 'alphanohtml');
  410. print __METHOD__." result=".$result."\n";
  411. $this->assertEquals($_POST["param9"], $result);
  412. $result=GETPOST("param10", 'alphanohtml');
  413. print __METHOD__." result=".$result."\n";
  414. $this->assertEquals($_POST["param9"], $result, 'We should get param9 after processing param10');
  415. $result=GETPOST("param11", 'alphanohtml');
  416. print __METHOD__." result=".$result."\n";
  417. $this->assertEquals("Name", $result, 'Test an email string with alphanohtml');
  418. $result=GETPOST("param13", 'alphanohtml');
  419. print __METHOD__." result=".$result."\n";
  420. $this->assertEquals('n n > < XSS', $result, 'Test that html entities are decoded with alpha');
  421. // Test with alphawithlgt
  422. $result=GETPOST("param11", 'alphawithlgt');
  423. print __METHOD__." result=".$result."\n";
  424. $this->assertEquals(trim($_POST["param11"]), $result, 'Test an email string with alphawithlgt');
  425. // 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;)
  426. $result=GETPOST("param6", 'restricthtml');
  427. print __METHOD__." result param6=".$result."\n";
  428. $this->assertEquals('&quot;&gt;', $result);
  429. $result=GETPOST("param7", 'restricthtml');
  430. print __METHOD__." result param7 = ".$result."\n";
  431. $this->assertEquals('"c:\this is a path~1\aaan &#x;;;;" abcdef', $result);
  432. $result=GETPOST("param8e", 'restricthtml');
  433. print __METHOD__." result param8e = ".$result."\n";
  434. $this->assertEquals('', $result);
  435. $result=GETPOST("param12", 'restricthtml');
  436. print __METHOD__." result=".$result."\n";
  437. $this->assertEquals(trim($_POST["param12"]), $result, 'Test a string with DOCTYPE and restricthtml');
  438. $result=GETPOST("param13", 'restricthtml');
  439. print __METHOD__." result=".$result."\n";
  440. $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');
  441. $result=GETPOST("param13b", 'restricthtml');
  442. print __METHOD__." result=".$result."\n";
  443. $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');
  444. $result=GETPOST("param14", 'restricthtml');
  445. print __METHOD__." result=".$result."\n";
  446. $this->assertEquals("Text with ' encoded with the numeric html entity converted into text entity &#39; (like when submited by CKEditor)", $result, 'Test 14');
  447. $result=GETPOST("param15", 'restricthtml'); // param15 = <img onerror<=alert(document.domain)> src=>0xbeefed that is a dangerous string
  448. print __METHOD__." result=".$result."\n";
  449. $this->assertEquals("<img onerror=alert(document.domain) src=>0xbeefed", $result, 'Test 15'); // The GETPOST return a harmull string
  450. $result=GETPOST("param19", 'restricthtml');
  451. print __METHOD__." result=".$result."\n";
  452. $this->assertEquals('<a href="&lpar;alert(document.cookie)&rpar;">XSS</a>', $result, 'Test 19');
  453. // Test with restricthtml + MAIN_RESTRICTHTML_ONLY_VALID_HTML to test disabling of bad atrributes
  454. $conf->global->MAIN_RESTRICTHTML_ONLY_VALID_HTML = 1;
  455. $result=GETPOST("param15", 'restricthtml'); // param15 = <img onerror<=alert(document.domain)> src=>0xbeefed that is a dangerous string
  456. print __METHOD__." result=".$result."\n";
  457. $this->assertEquals('InvalidHTMLString', $result, 'Test 15b'); // With some PHP and libxml version, we got this when parsong invalid HTML
  458. //$this->assertEquals('<img onerror> src=&gt;0xbeefed', $result, 'Test 15b'); // On other we got a HTML that has been cleaned
  459. unset($conf->global->MAIN_RESTRICTHTML_ONLY_VALID_HTML);
  460. // Test with restricthtml + MAIN_RESTRICTHTML_REMOVE_ALSO_BAD_ATTRIBUTES to test disabling of bad atrributes
  461. $conf->global->MAIN_RESTRICTHTML_REMOVE_ALSO_BAD_ATTRIBUTES = 1;
  462. $result=GETPOST("param15", 'restricthtml');
  463. print __METHOD__." result=".$result."\n";
  464. $this->assertEquals('<img src="">0xbeefed', $result, 'Test 15c');
  465. $result=GETPOST('param16', 'restricthtml');
  466. print __METHOD__." result=".$result."\n";
  467. $this->assertEquals('<a style=" 1000">abc</a>', $result, 'Test tag a with forbidden attribute z-index');
  468. $result=GETPOST('param17', 'restricthtml');
  469. print __METHOD__." result=".$result."\n";
  470. $this->assertEquals('<span style="background-image: url()">abc</span>', $result, 'Test anytag with a forbidden value for attribute');
  471. $result=GETPOST('param18', 'restricthtml');
  472. print __METHOD__." result=".$result."\n";
  473. $this->assertEquals('<span style="background-image: url(...?...aaa)">abc</span>', $result, 'Test anytag with a forbidden value for attribute');
  474. unset($conf->global->MAIN_RESTRICTHTML_REMOVE_ALSO_BAD_ATTRIBUTES);
  475. // Special test for GETPOST of backtopage, backtolist or backtourl parameter
  476. $_POST["backtopage"]='//www.google.com';
  477. $result=GETPOST("backtopage");
  478. print __METHOD__." result=".$result."\n";
  479. $this->assertEquals('www.google.com', $result, 'Test for backtopage param');
  480. $_POST["backtopage"]='https:https://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["backtolist"]='::HTTPS://www.google.com';
  485. $result=GETPOST("backtolist");
  486. print __METHOD__." result=".$result."\n";
  487. $this->assertEquals('www.google.com', $result, 'Test for backtopage param');
  488. $_POST["backtopage"]='http:www.google.com';
  489. $result=GETPOST("backtopage");
  490. print __METHOD__." result=".$result."\n";
  491. $this->assertEquals('httpwww.google.com', $result, 'Test for backtopage param');
  492. $_POST["backtopage"]='/mydir/mypage.php?aa=a%10a';
  493. $result=GETPOST("backtopage");
  494. print __METHOD__." result=".$result."\n";
  495. $this->assertEquals('/mydir/mypage.php?aa=a%10a', $result, 'Test for backtopage param');
  496. $_POST["backtopage"]='javascripT&javascript#javascriptxjavascript3a alert(1)';
  497. $result=GETPOST("backtopage");
  498. print __METHOD__." result=".$result."\n";
  499. $this->assertEquals('x3aalert(1)', $result, 'Test for backtopage param');
  500. $conf->global->MAIN_SECURITY_MAX_IMG_IN_HTML_CONTENT = 3;
  501. $_POST["pagecontentwithlinks"]='<img src="aaa"><img src="bbb"><img src="ccc"><span style="background: url(/ddd)"></span>';
  502. $result=GETPOST("pagecontentwithlinks", 'restricthtml');
  503. print __METHOD__." result=".$result."\n";
  504. $this->assertEquals('TooManyLinksIntoHTMLString', $result, 'Test on limit on GETPOST fails');
  505. return $result;
  506. }
  507. /**
  508. * testEncodeDecode
  509. *
  510. * @return number
  511. */
  512. public function testEncodeDecode()
  513. {
  514. $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.";
  515. $encodedstring=dol_encode($stringtotest);
  516. $decodedstring=dol_decode($encodedstring);
  517. print __METHOD__." encodedstring=".$encodedstring." ".base64_encode($stringtotest)."\n";
  518. $this->assertEquals($stringtotest, $decodedstring, 'Use dol_encode/decode with no parameter');
  519. $encodedstring=dol_encode($stringtotest, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
  520. $decodedstring=dol_decode($encodedstring, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
  521. print __METHOD__." encodedstring=".$encodedstring." ".base64_encode($stringtotest)."\n";
  522. $this->assertEquals($stringtotest, $decodedstring, 'Use dol_encode/decode with a key parameter');
  523. return 0;
  524. }
  525. /**
  526. * testDolStringOnlyTheseHtmlTags
  527. *
  528. * @return number
  529. */
  530. public function testDolHTMLEntityDecode()
  531. {
  532. $stringtotest = 'a &colon; b &quot; c &#039; d &apos; e &eacute;';
  533. $decodedstring = dol_html_entity_decode($stringtotest, ENT_QUOTES);
  534. $this->assertEquals('a &colon; b " c \' d &apos; e é', $decodedstring, 'Function did not sanitize correclty');
  535. $stringtotest = 'a &colon; b &quot; c &#039; d &apos; e &eacute;';
  536. $decodedstring = dol_html_entity_decode($stringtotest, ENT_QUOTES|ENT_HTML5);
  537. $this->assertEquals('a : b " c \' d \' e é', $decodedstring, 'Function did not sanitize correclty');
  538. return 0;
  539. }
  540. /**
  541. * testDolStringOnlyTheseHtmlTags
  542. *
  543. * @return number
  544. */
  545. public function testDolStringOnlyTheseHtmlTags()
  546. {
  547. $stringtotest = '<a href="javascript:aaa">bbbڴ';
  548. $decodedstring = dol_string_onlythesehtmltags($stringtotest, 1, 1, 1);
  549. $this->assertEquals('<a href="aaa">bbbڴ', $decodedstring, 'Function did not sanitize correclty with test 1');
  550. $stringtotest = '<a href="java'.chr(0).'script:aaa">bbbڴ';
  551. $decodedstring = dol_string_onlythesehtmltags($stringtotest, 1, 1, 1);
  552. $this->assertEquals('<a href="aaa">bbbڴ', $decodedstring, 'Function did not sanitize correclty with test 2');
  553. $stringtotest = '<a href="javascript&colon;aaa">bbbڴ';
  554. $decodedstring = dol_string_onlythesehtmltags($stringtotest, 1, 1, 1);
  555. $this->assertEquals('<a href="aaa">bbbڴ', $decodedstring, 'Function did not sanitize correclty with test 3');
  556. $stringtotest = 'text <link href="aaa"> text';
  557. $decodedstring = dol_string_onlythesehtmltags($stringtotest, 1, 1, 1, 0, array(), 0);
  558. $this->assertEquals('text text', $decodedstring, 'Function did not sanitize correclty with test 4a');
  559. $stringtotest = 'text <link href="aaa"> text';
  560. $decodedstring = dol_string_onlythesehtmltags($stringtotest, 1, 1, 1, 0, array(), 1);
  561. $this->assertEquals('text <link href="aaa"> text', $decodedstring, 'Function did not sanitize correclty with test 4b');
  562. return 0;
  563. }
  564. /**
  565. * testDolStringOnlyTheseHtmlAttributes
  566. *
  567. * @return number
  568. */
  569. public function testDolStringOnlyTheseHtmlAttributes()
  570. {
  571. $stringtotest = 'eée';
  572. $decodedstring = dol_string_onlythesehtmlattributes($stringtotest);
  573. $this->assertEquals('e&eacute;e', $decodedstring, 'Function did not sanitize correclty with test 1');
  574. $stringtotest = '<div onload="ee"><a href="123"><span class="abc">abc</span></a></div>';
  575. $decodedstring = dol_string_onlythesehtmlattributes($stringtotest);
  576. $decodedstring = preg_replace("/\n$/", "", $decodedstring);
  577. $this->assertEquals('<div><a href="123"><span class="abc">abc</span></a></div>', $decodedstring, 'Function did not sanitize correclty with test 2');
  578. return 0;
  579. }
  580. /**
  581. * testGetRandomPassword
  582. *
  583. * @return number
  584. */
  585. public function testGetRandomPassword()
  586. {
  587. global $conf;
  588. $genpass1=getRandomPassword(true); // Should be a string return by dol_hash (if no option set, will be md5)
  589. print __METHOD__." genpass1=".$genpass1."\n";
  590. $this->assertEquals(strlen($genpass1), 32);
  591. $genpass1=getRandomPassword(true, array('I')); // Should be a string return by dol_hash (if no option set, will be md5)
  592. print __METHOD__." genpass1=".$genpass1."\n";
  593. $this->assertEquals(strlen($genpass1), 32);
  594. $conf->global->USER_PASSWORD_GENERATED='None';
  595. $genpass2=getRandomPassword(false); // Should return an empty string
  596. print __METHOD__." genpass2=".$genpass2."\n";
  597. $this->assertEquals($genpass2, '');
  598. $conf->global->USER_PASSWORD_GENERATED='Standard';
  599. $genpass3=getRandomPassword(false); // Should return a password of 12 chars
  600. print __METHOD__." genpass3=".$genpass3."\n";
  601. $this->assertEquals(strlen($genpass3), 12);
  602. return 0;
  603. }
  604. /**
  605. * testRestrictedArea
  606. *
  607. * @return void
  608. */
  609. public function testRestrictedArea()
  610. {
  611. global $conf,$user,$langs,$db;
  612. $conf=$this->savconf;
  613. $user=$this->savuser;
  614. $langs=$this->savlangs;
  615. $db=$this->savdb;
  616. //$dummyuser=new User($db);
  617. //$result=restrictedArea($dummyuser,'societe');
  618. $result=restrictedArea($user, 'societe');
  619. $this->assertEquals(1, $result);
  620. }
  621. /**
  622. * testGetRandomPassword
  623. *
  624. * @return number
  625. */
  626. public function testGetURLContent()
  627. {
  628. global $conf;
  629. include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  630. $url = 'ftp://mydomain.com';
  631. $tmp = getURLContent($url);
  632. print __METHOD__." url=".$url."\n";
  633. $this->assertGreaterThan(0, strpos($tmp['curl_error_msg'], 'not supported')); // Test error if return does not contains 'not supported'
  634. $url = 'https://www.dolibarr.fr'; // This is a redirect 301 page
  635. $tmp = getURLContent($url, 'GET', '', 0); // We do NOT follow
  636. print __METHOD__." url=".$url."\n";
  637. $this->assertEquals(301, $tmp['http_code'], 'Should GET url 301 response and stop here');
  638. $url = 'https://www.dolibarr.fr'; // This is a redirect 301 page
  639. $tmp = getURLContent($url); // We DO follow a page with return 300 so result should be 200
  640. print __METHOD__." url=".$url."\n";
  641. $this->assertEquals(200, $tmp['http_code'], 'Should GET url 301 with a follow -> 200 but we get '.$tmp['http_code']);
  642. $url = 'http://localhost';
  643. $tmp = getURLContent($url, 'GET', '', 0, array(), array('http', 'https'), 0); // Only external URL
  644. print __METHOD__." url=".$url."\n";
  645. $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
  646. $url = 'http://127.0.0.1';
  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 is a local URL'); // Test we receive an error because 127.0.0.1 is not an external URL
  650. $url = 'http://127.0.2.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.2.1 is not an external URL
  654. $url = 'https://169.254.0.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 169.254.0.1 is not an external URL
  658. $url = 'http://[::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 [::1] is not an external URL
  662. /*$url = 'localtest.me';
  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 resolves to a local URL'); // Test we receive an error because localtest.me is not an external URL
  666. */
  667. $url = 'http://192.0.0.192';
  668. $tmp = getURLContent($url, 'GET', '', 0, array(), array('http', 'https'), 0); // Only external URL but on an IP in blacklist
  669. print __METHOD__." url=".$url." tmp['http_code'] = ".$tmp['http_code']."\n";
  670. $this->assertEquals(400, $tmp['http_code'], 'Access should be refused and was not'); // Test we receive an error because ip is in blacklist
  671. return 0;
  672. }
  673. /**
  674. * testDolSanitizeUrl
  675. *
  676. * @return void
  677. */
  678. public function testDolSanitizeUrl()
  679. {
  680. global $conf,$user,$langs,$db;
  681. $conf=$this->savconf;
  682. $user=$this->savuser;
  683. $langs=$this->savlangs;
  684. $db=$this->savdb;
  685. $test = 'javascripT&javascript#x3a alert(1)';
  686. $result=dol_sanitizeUrl($test);
  687. $this->assertEquals('x3a alert(1)', $result, 'Test on dol_sanitizeUrl A');
  688. $test = 'javajavascriptscript&cjavascriptolon;alert(1)';
  689. $result=dol_sanitizeUrl($test);
  690. $this->assertEquals('alert(1)', $result, 'Test on dol_sanitizeUrl B');
  691. $test = '/javas:cript/google.com';
  692. $result=dol_sanitizeUrl($test);
  693. $this->assertEquals('google.com', $result, 'Test on dol_sanitizeUrl C');
  694. }
  695. /**
  696. * testDolSanitizeEmail
  697. *
  698. * @return void
  699. */
  700. public function testDolSanitizeEmail()
  701. {
  702. global $conf,$user,$langs,$db;
  703. $conf=$this->savconf;
  704. $user=$this->savuser;
  705. $langs=$this->savlangs;
  706. $db=$this->savdb;
  707. $test = 'aaa@mycompany.com <My name>, bbb@mycompany.com <Another name>';
  708. $result=dol_sanitizeEmail($test);
  709. $this->assertEquals($test, $result, 'Test on dol_sanitizeEmail A');
  710. $test = "aaa@mycompany.com <My name>,\nbbb@mycompany.com <Another name>";
  711. $result=dol_sanitizeEmail($test);
  712. $this->assertEquals('aaa@mycompany.com <My name>,bbb@mycompany.com <Another name>', $result, 'Test on dol_sanitizeEmail B');
  713. $test = 'aaa@mycompany.com <My name>,\nbbb@mycompany.com <Another name>';
  714. $result=dol_sanitizeEmail($test);
  715. $this->assertEquals('aaa@mycompany.com <My name>,nbbb@mycompany.com <Another name>', $result, 'Test on dol_sanitizeEmail C');
  716. $test = 'aaa@mycompany.com <My name>, "bcc:bbb"@mycompany.com <Another name>';
  717. $result=dol_sanitizeEmail($test);
  718. $this->assertEquals('aaa@mycompany.com <My name>, bccbbb@mycompany.com <Another name>', $result, 'Test on dol_sanitizeEmail D');
  719. }
  720. /**
  721. * testDolSanitizeFileName
  722. *
  723. * @return void
  724. */
  725. public function testDolSanitizeFileName()
  726. {
  727. global $conf,$user,$langs,$db;
  728. $conf=$this->savconf;
  729. $user=$this->savuser;
  730. $langs=$this->savlangs;
  731. $db=$this->savdb;
  732. //$dummyuser=new User($db);
  733. //$result=restrictedArea($dummyuser,'societe');
  734. $result=dol_sanitizeFileName('bad file | evilaction');
  735. $this->assertEquals('bad file _ evilaction', $result);
  736. $result=dol_sanitizeFileName('bad file -evilparam --evilparam ---evilparam ----evilparam');
  737. $this->assertEquals('bad file _evilparam _evilparam _evilparam _evilparam', $result);
  738. }
  739. /**
  740. * testDolEval
  741. *
  742. * @return void
  743. */
  744. public function testDolEval()
  745. {
  746. global $conf,$user,$langs,$db;
  747. $conf=$this->savconf;
  748. $user=$this->savuser;
  749. $langs=$this->savlangs;
  750. $db=$this->savdb;
  751. $result=dol_eval('1==1', 1, 0);
  752. print "result = ".$result."\n";
  753. $this->assertTrue($result);
  754. $result=dol_eval('1==2', 1, 0);
  755. print "result = ".$result."\n";
  756. $this->assertFalse($result);
  757. include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  758. include_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
  759. $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"';
  760. $result=dol_eval($s, 1, 1, '2');
  761. print "result = ".$result."\n";
  762. $this->assertEquals('Parent project not found', $result);
  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. $result=dol_eval('$a=function() { }; $a;', 1, 1, '');
  768. print "result = ".$result."\n";
  769. $this->assertContains('Bad string syntax to evaluate', $result);
  770. $result=dol_eval('$a=exec("ls");', 1, 1);
  771. print "result = ".$result."\n";
  772. $this->assertContains('Bad string syntax to evaluate', $result);
  773. $result=dol_eval('$a=exec ("ls")', 1, 1);
  774. print "result = ".$result."\n";
  775. $this->assertContains('Bad string syntax to evaluate', $result);
  776. $result=dol_eval('$a="test"; $$a;', 1, 0);
  777. print "result = ".$result."\n";
  778. $this->assertContains('Bad string syntax to evaluate', $result);
  779. $result=dol_eval('`ls`', 1, 0);
  780. print "result = ".$result."\n";
  781. $this->assertContains('Bad string syntax to evaluate', $result);
  782. $result=dol_eval("('ex'.'ec')('echo abc')", 1, 0);
  783. print "result = ".$result."\n";
  784. $this->assertContains('Bad string syntax to evaluate', $result);
  785. $result=dol_eval("sprintf(\"%s%s\", \"ex\", \"ec\")('echo abc')", 1, 0);
  786. print "result = ".$result."\n";
  787. $this->assertContains('Bad string syntax to evaluate', $result);
  788. $result=dol_eval("90402.38+267678+0", 1, 1, 1);
  789. print "result = ".$result."\n";
  790. $this->assertEquals('358080.38', $result);
  791. global $leftmenu; // Used into strings to eval
  792. $leftmenu = 'AAA';
  793. $result=dol_eval('$conf->currency && preg_match(\'/^(AAA|BBB)/\',$leftmenu)', 1, 1, '1');
  794. print "result = ".$result."\n";
  795. $this->assertTrue($result);
  796. // Same with syntax error
  797. $leftmenu = 'XXX';
  798. $result=dol_eval('$conf->currency && preg_match(\'/^(AAA|BBB)/\',$leftmenu)', 1, 1, '1');
  799. print "result = ".$result."\n";
  800. $this->assertFalse($result);
  801. // Case with param onlysimplestring = 1
  802. $result=dol_eval('1 && getDolGlobalInt("doesnotexist1") && $conf->global->MAIN_FEATURES_LEVEL', 1, 0); // Should return false and not a 'Bad string syntax to evaluate ...'
  803. print "result = ".$result."\n";
  804. $this->assertFalse($result);
  805. $result=dol_eval("(\$a.'aa')", 1, 0);
  806. print "result = ".$result."\n";
  807. $this->assertContains('Bad string syntax to evaluate', $result);
  808. }
  809. /**
  810. * testCheckLoginPassEntity
  811. *
  812. * @return void
  813. */
  814. public function testCheckLoginPassEntity()
  815. {
  816. $login=checkLoginPassEntity('loginbidon', 'passwordbidon', 1, array('dolibarr'));
  817. print __METHOD__." login=".$login."\n";
  818. $this->assertEquals($login, '');
  819. $login=checkLoginPassEntity('admin', 'passwordbidon', 1, array('dolibarr'));
  820. print __METHOD__." login=".$login."\n";
  821. $this->assertEquals($login, '');
  822. $login=checkLoginPassEntity('admin', 'admin', 1, array('dolibarr')); // Should works because admin/admin exists
  823. print __METHOD__." login=".$login."\n";
  824. $this->assertEquals($login, 'admin', 'The test to check if pass of user "admin" is "admin" has failed');
  825. $login=checkLoginPassEntity('admin', 'admin', 1, array('http','dolibarr')); // Should work because of second authentication method
  826. print __METHOD__." login=".$login."\n";
  827. $this->assertEquals($login, 'admin');
  828. $login=checkLoginPassEntity('admin', 'admin', 1, array('forceuser'));
  829. print __METHOD__." login=".$login."\n";
  830. $this->assertEquals('', $login, 'Error'); // Expected '' because should failed because login 'auto' does not exists
  831. }
  832. }