SanitizeTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. /**
  3. * SanitizeTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Utility
  16. * @since CakePHP(tm) v 1.2.0.5428
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Sanitize', 'Utility');
  20. /**
  21. * DataTest class
  22. *
  23. * @package Cake.Test.Case.Utility
  24. */
  25. class SanitizeDataTest extends CakeTestModel {
  26. /**
  27. * name property
  28. *
  29. * @var string 'SanitizeDataTest'
  30. */
  31. public $name = 'SanitizeDataTest';
  32. /**
  33. * useTable property
  34. *
  35. * @var string 'data_tests'
  36. */
  37. public $useTable = 'data_tests';
  38. }
  39. /**
  40. * Article class
  41. *
  42. * @package Cake.Test.Case.Utility
  43. */
  44. class SanitizeArticle extends CakeTestModel {
  45. /**
  46. * name property
  47. *
  48. * @var string 'Article'
  49. */
  50. public $name = 'SanitizeArticle';
  51. /**
  52. * useTable property
  53. *
  54. * @var string 'articles'
  55. */
  56. public $useTable = 'articles';
  57. }
  58. /**
  59. * SanitizeTest class
  60. *
  61. * @package Cake.Test.Case.Utility
  62. */
  63. class SanitizeTest extends CakeTestCase {
  64. /**
  65. * autoFixtures property
  66. *
  67. * @var bool false
  68. */
  69. public $autoFixtures = false;
  70. /**
  71. * fixtures property
  72. *
  73. * @var array
  74. */
  75. public $fixtures = array('core.data_test', 'core.article');
  76. /**
  77. * testEscapeAlphaNumeric method
  78. *
  79. * @return void
  80. */
  81. public function testEscapeAlphaNumeric() {
  82. $resultAlpha = Sanitize::escape('abc', 'test');
  83. $this->assertEquals('abc', $resultAlpha);
  84. $resultNumeric = Sanitize::escape('123', 'test');
  85. $this->assertEquals('123', $resultNumeric);
  86. $resultNumeric = Sanitize::escape(1234, 'test');
  87. $this->assertEquals(1234, $resultNumeric);
  88. $resultNumeric = Sanitize::escape(1234.23, 'test');
  89. $this->assertEquals(1234.23, $resultNumeric);
  90. $resultNumeric = Sanitize::escape('#1234.23', 'test');
  91. $this->assertEquals('#1234.23', $resultNumeric);
  92. $resultNull = Sanitize::escape(null, 'test');
  93. $this->assertEquals(null, $resultNull);
  94. $resultNull = Sanitize::escape(false, 'test');
  95. $this->assertEquals(false, $resultNull);
  96. $resultNull = Sanitize::escape(true, 'test');
  97. $this->assertEquals(true, $resultNull);
  98. }
  99. /**
  100. * testClean method
  101. *
  102. * @return void
  103. */
  104. public function testClean() {
  105. $string = 'test & "quote" \'other\' ;.$ symbol.' . "\r" . 'another line';
  106. $expected = 'test &amp; &quot;quote&quot; &#039;other&#039; ;.$ symbol.another line';
  107. $result = Sanitize::clean($string, array('connection' => 'test'));
  108. $this->assertEquals($expected, $result);
  109. $string = 'test & "quote" \'other\' ;.$ symbol.' . "\r" . 'another line';
  110. $expected = 'test & ' . Sanitize::escape('"quote"', 'test') . ' ' . Sanitize::escape('\'other\'', 'test') . ' ;.$ symbol.another line';
  111. $result = Sanitize::clean($string, array('encode' => false, 'connection' => 'test'));
  112. $this->assertEquals($expected, $result);
  113. $string = 'test & "quote" \'other\' ;.$ \\$ symbol.' . "\r" . 'another line';
  114. $expected = 'test & "quote" \'other\' ;.$ $ symbol.another line';
  115. $result = Sanitize::clean($string, array('encode' => false, 'escape' => false, 'connection' => 'test'));
  116. $this->assertEquals($expected, $result);
  117. $string = 'test & "quote" \'other\' ;.$ \\$ symbol.' . "\r" . 'another line';
  118. $expected = 'test & "quote" \'other\' ;.$ \\$ symbol.another line';
  119. $result = Sanitize::clean($string, array('encode' => false, 'escape' => false, 'dollar' => false, 'connection' => 'test'));
  120. $this->assertEquals($expected, $result);
  121. $string = 'test & "quote" \'other\' ;.$ symbol.' . "\r" . 'another line';
  122. $expected = 'test & "quote" \'other\' ;.$ symbol.' . "\r" . 'another line';
  123. $result = Sanitize::clean($string, array('encode' => false, 'escape' => false, 'carriage' => false, 'connection' => 'test'));
  124. $this->assertEquals($expected, $result);
  125. $array = array(array('test & "quote" \'other\' ;.$ symbol.' . "\r" . 'another line'));
  126. $expected = array(array('test &amp; &quot;quote&quot; &#039;other&#039; ;.$ symbol.another line'));
  127. $result = Sanitize::clean($array, array('connection' => 'test'));
  128. $this->assertEquals($expected, $result);
  129. $array = array(array('test & "quote" \'other\' ;.$ \\$ symbol.' . "\r" . 'another line'));
  130. $expected = array(array('test & "quote" \'other\' ;.$ $ symbol.another line'));
  131. $result = Sanitize::clean($array, array('encode' => false, 'escape' => false, 'connection' => 'test'));
  132. $this->assertEquals($expected, $result);
  133. $array = array(array('test odd Ä spacesé'));
  134. $expected = array(array('test odd &Auml; spaces&eacute;'));
  135. $result = Sanitize::clean($array, array('odd_spaces' => false, 'escape' => false, 'connection' => 'test'));
  136. $this->assertEquals($expected, $result);
  137. $array = array(array('\\$', array('key' => 'test & "quote" \'other\' ;.$ \\$ symbol.' . "\r" . 'another line')));
  138. $expected = array(array('$', array('key' => 'test & "quote" \'other\' ;.$ $ symbol.another line')));
  139. $result = Sanitize::clean($array, array('encode' => false, 'escape' => false, 'connection' => 'test'));
  140. $this->assertEquals($expected, $result);
  141. $string = '';
  142. $expected = '';
  143. $result = Sanitize::clean($string, array('connection' => 'test'));
  144. $this->assertEquals($expected, $string);
  145. $data = array(
  146. 'Grant' => array(
  147. 'title' => '2 o clock grant',
  148. 'grant_peer_review_id' => 3,
  149. 'institution_id' => 5,
  150. 'created_by' => 1,
  151. 'modified_by' => 1,
  152. 'created' => '2010-07-15 14:11:00',
  153. 'modified' => '2010-07-19 10:45:41'
  154. ),
  155. 'GrantsMember' => array(
  156. 0 => array(
  157. 'id' => 68,
  158. 'grant_id' => 120,
  159. 'member_id' => 16,
  160. 'program_id' => 29,
  161. 'pi_percent_commitment' => 1
  162. )
  163. )
  164. );
  165. $result = Sanitize::clean($data, array('connection' => 'test'));
  166. $this->assertEquals($data, $result);
  167. }
  168. /**
  169. * testHtml method
  170. *
  171. * @return void
  172. */
  173. public function testHtml() {
  174. $string = '<p>This is a <em>test string</em> & so is this</p>';
  175. $expected = 'This is a test string &amp; so is this';
  176. $result = Sanitize::html($string, array('remove' => true));
  177. $this->assertEquals($expected, $result);
  178. $string = 'The "lazy" dog \'jumped\' & flew over the moon. If (1+1) = 2 <em>is</em> true, (2-1) = 1 is also true';
  179. $expected = 'The &quot;lazy&quot; dog &#039;jumped&#039; &amp; flew over the moon. If (1+1) = 2 &lt;em&gt;is&lt;/em&gt; true, (2-1) = 1 is also true';
  180. $result = Sanitize::html($string);
  181. $this->assertEquals($expected, $result);
  182. $string = 'The "lazy" dog \'jumped\'';
  183. $expected = 'The &quot;lazy&quot; dog \'jumped\'';
  184. $result = Sanitize::html($string, array('quotes' => ENT_COMPAT));
  185. $this->assertEquals($expected, $result);
  186. $string = 'The "lazy" dog \'jumped\'';
  187. $result = Sanitize::html($string, array('quotes' => ENT_NOQUOTES));
  188. $this->assertEquals($string, $result);
  189. $string = 'The "lazy" dog \'jumped\' & flew over the moon. If (1+1) = 2 <em>is</em> true, (2-1) = 1 is also true';
  190. $expected = 'The &quot;lazy&quot; dog &#039;jumped&#039; &amp; flew over the moon. If (1+1) = 2 &lt;em&gt;is&lt;/em&gt; true, (2-1) = 1 is also true';
  191. $result = Sanitize::html($string);
  192. $this->assertEquals($expected, $result);
  193. $string = 'The "lazy" dog & his friend Apple&reg; conquered the world';
  194. $expected = 'The &quot;lazy&quot; dog &amp; his friend Apple&amp;reg; conquered the world';
  195. $result = Sanitize::html($string);
  196. $this->assertEquals($expected, $result);
  197. $string = 'The "lazy" dog & his friend Apple&reg; conquered the world';
  198. $expected = 'The &quot;lazy&quot; dog &amp; his friend Apple&reg; conquered the world';
  199. $result = Sanitize::html($string, array('double' => false));
  200. $this->assertEquals($expected, $result);
  201. }
  202. /**
  203. * testStripWhitespace method
  204. *
  205. * @return void
  206. */
  207. public function testStripWhitespace() {
  208. $string = "This sentence \t\t\t has lots of \n\n white\nspace \rthat \r\n needs to be \t \n trimmed.";
  209. $expected = "This sentence has lots of whitespace that needs to be trimmed.";
  210. $result = Sanitize::stripWhitespace($string);
  211. $this->assertEquals($expected, $result);
  212. $text = 'I love ßá†ö√ letters.';
  213. $result = Sanitize::stripWhitespace($text);
  214. $expected = 'I love ßá†ö√ letters.';
  215. $this->assertEquals($expected, $result);
  216. }
  217. /**
  218. * testParanoid method
  219. *
  220. * @return void
  221. */
  222. public function testParanoid() {
  223. $string = 'I would like to !%@#% & dance & sing ^$&*()-+';
  224. $expected = 'Iwouldliketodancesing';
  225. $result = Sanitize::paranoid($string);
  226. $this->assertEquals($expected, $result);
  227. $string = array('This |s th% s0ng that never ends it g*es',
  228. 'on and on my friends, b^ca#use it is the',
  229. 'so&g th===t never ends.');
  230. $expected = array('This s th% s0ng that never ends it g*es',
  231. 'on and on my friends bcause it is the',
  232. 'sog tht never ends.');
  233. $result = Sanitize::paranoid($string, array('%', '*', '.', ' '));
  234. $this->assertEquals($expected, $result);
  235. $string = "anything' OR 1 = 1";
  236. $expected = 'anythingOR11';
  237. $result = Sanitize::paranoid($string);
  238. $this->assertEquals($expected, $result);
  239. $string = "x' AND email IS NULL; --";
  240. $expected = 'xANDemailISNULL';
  241. $result = Sanitize::paranoid($string);
  242. $this->assertEquals($expected, $result);
  243. $string = "x' AND 1=(SELECT COUNT(*) FROM users); --";
  244. $expected = "xAND1SELECTCOUNTFROMusers";
  245. $result = Sanitize::paranoid($string);
  246. $this->assertEquals($expected, $result);
  247. $string = "x'; DROP TABLE members; --";
  248. $expected = "xDROPTABLEmembers";
  249. $result = Sanitize::paranoid($string);
  250. $this->assertEquals($expected, $result);
  251. }
  252. /**
  253. * testStripImages method
  254. *
  255. * @return void
  256. */
  257. public function testStripImages() {
  258. $string = '<img src="/img/test.jpg" alt="my image" />';
  259. $expected = 'my image<br />';
  260. $result = Sanitize::stripImages($string);
  261. $this->assertEquals($expected, $result);
  262. $string = '<img src="javascript:alert(\'XSS\');" />';
  263. $expected = '';
  264. $result = Sanitize::stripImages($string);
  265. $this->assertEquals($expected, $result);
  266. $string = '<a href="http://www.badsite.com/phising"><img src="/img/test.jpg" alt="test image alt" title="test image title" id="myImage" class="image-left"/></a>';
  267. $expected = '<a href="http://www.badsite.com/phising">test image alt</a><br />';
  268. $result = Sanitize::stripImages($string);
  269. $this->assertEquals($expected, $result);
  270. $string = '<a onclick="medium()" href="http://example.com"><img src="foobar.png" onclick="evilFunction(); return false;"/></a>';
  271. $expected = '<a onclick="medium()" href="http://example.com"></a>';
  272. $result = Sanitize::stripImages($string);
  273. $this->assertEquals($expected, $result);
  274. }
  275. /**
  276. * testStripScripts method
  277. *
  278. * @return void
  279. */
  280. public function testStripScripts() {
  281. $string = '<link href="/css/styles.css" media="screen" rel="stylesheet" />';
  282. $expected = '';
  283. $result = Sanitize::stripScripts($string);
  284. $this->assertEquals($expected, $result);
  285. $string = '<link href="/css/styles.css" media="screen" rel="stylesheet" />' . "\n" .
  286. '<link rel="icon" href="/favicon.ico" type="image/x-icon" />' . "\n" .
  287. '<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />' . "\n" .
  288. '<link rel="alternate" href="/feed.xml" title="RSS Feed" type="application/rss+xml" />';
  289. $expected = "\n" . '<link rel="icon" href="/favicon.ico" type="image/x-icon" />' . "\n" .
  290. '<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />' . "\n" .
  291. '<link rel="alternate" href="/feed.xml" title="RSS Feed" type="application/rss+xml" />';
  292. $result = Sanitize::stripScripts($string);
  293. $this->assertEquals($expected, $result);
  294. $string = '<script type="text/javascript"> alert("hacked!");</script>';
  295. $expected = '';
  296. $result = Sanitize::stripScripts($string);
  297. $this->assertEquals($expected, $result);
  298. $string = '<script> alert("hacked!");</script>';
  299. $expected = '';
  300. $result = Sanitize::stripScripts($string);
  301. $this->assertEquals($expected, $result);
  302. $string = '<style>#content { display:none; }</style>';
  303. $expected = '';
  304. $result = Sanitize::stripScripts($string);
  305. $this->assertEquals($expected, $result);
  306. $string = '<style type="text/css"><!-- #content { display:none; } --></style>';
  307. $expected = '';
  308. $result = Sanitize::stripScripts($string);
  309. $this->assertEquals($expected, $result);
  310. $string = <<<HTML
  311. text
  312. <style type="text/css">
  313. <!--
  314. #content { display:none; }
  315. -->
  316. </style>
  317. text
  318. HTML;
  319. $expected = "text\n\ntext";
  320. $result = Sanitize::stripScripts($string);
  321. $this->assertTextEquals($expected, $result);
  322. $string = <<<HTML
  323. text
  324. <script type="text/javascript">
  325. <!--
  326. alert('wooo');
  327. -->
  328. </script>
  329. text
  330. HTML;
  331. $expected = "text\n\ntext";
  332. $result = Sanitize::stripScripts($string);
  333. $this->assertTextEquals($expected, $result);
  334. }
  335. /**
  336. * testStripAll method
  337. *
  338. * @return void
  339. */
  340. public function testStripAll() {
  341. $string = '<img """><script>alert("xss")</script>"/>';
  342. $expected = '"/>';
  343. $result = Sanitize::stripAll($string);
  344. $this->assertEquals($expected, $result);
  345. $string = '<IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>';
  346. $expected = '';
  347. $result = Sanitize::stripAll($string);
  348. $this->assertEquals($expected, $result);
  349. $string = '<<script>alert("XSS");//<</script>';
  350. $expected = '<';
  351. $result = Sanitize::stripAll($string);
  352. $this->assertEquals($expected, $result);
  353. $string = '<img src="http://google.com/images/logo.gif" onload="window.location=\'http://sam.com/\'" />' . "\n" .
  354. "<p>This is ok \t\n text</p>\n" .
  355. '<link rel="stylesheet" href="/css/master.css" type="text/css" media="screen" title="my sheet" charset="utf-8">' . "\n" .
  356. '<script src="xss.js" type="text/javascript" charset="utf-8"></script>';
  357. $expected = '<p>This is ok text</p>';
  358. $result = Sanitize::stripAll($string);
  359. $this->assertEquals($expected, $result);
  360. }
  361. /**
  362. * testStripTags method
  363. *
  364. * @return void
  365. */
  366. public function testStripTags() {
  367. $string = '<h2>Headline</h2><p><a href="http://example.com">My Link</a> could go to a bad site</p>';
  368. $expected = 'Headline<p>My Link could go to a bad site</p>';
  369. $result = Sanitize::stripTags($string, 'h2', 'a');
  370. $this->assertEquals($expected, $result);
  371. $string = '<script type="text/javascript" src="http://evildomain.com"> </script>';
  372. $expected = ' ';
  373. $result = Sanitize::stripTags($string, 'script');
  374. $this->assertEquals($expected, $result);
  375. $string = '<h2>Important</h2><p>Additional information here <a href="/about"><img src="/img/test.png" /></a>. Read even more here</p>';
  376. $expected = 'Important<p>Additional information here <img src="/img/test.png" />. Read even more here</p>';
  377. $result = Sanitize::stripTags($string, 'h2', 'a');
  378. $this->assertEquals($expected, $result);
  379. $string = '<h2>Important</h2><p>Additional information here <a href="/about"><img src="/img/test.png" /></a>. Read even more here</p>';
  380. $expected = 'Important<p>Additional information here . Read even more here</p>';
  381. $result = Sanitize::stripTags($string, 'h2', 'a', 'img');
  382. $this->assertEquals($expected, $result);
  383. $string = '<b>Important message!</b><br>This message will self destruct!';
  384. $expected = 'Important message!<br>This message will self destruct!';
  385. $result = Sanitize::stripTags($string, 'b');
  386. $this->assertEquals($expected, $result);
  387. $string = '<b>Important message!</b><br />This message will self destruct!';
  388. $expected = 'Important message!<br />This message will self destruct!';
  389. $result = Sanitize::stripTags($string, 'b');
  390. $this->assertEquals($expected, $result);
  391. $string = '<h2 onclick="alert(\'evil\'); onmouseover="badness()">Important</h2><p>Additional information here <a href="/about"><img src="/img/test.png" /></a>. Read even more here</p>';
  392. $expected = 'Important<p>Additional information here . Read even more here</p>';
  393. $result = Sanitize::stripTags($string, 'h2', 'a', 'img');
  394. $this->assertEquals($expected, $result);
  395. }
  396. }