Fix Display\ResultsTest test on Windows
[phpmyadmin.git] / test / classes / SanitizeTest.php
blob9b889f731c1c0266d03640d0b391a48c8c3e6d67
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Tests for methods in Sanitize class
6 * @package PhpMyAdmin-test
7 */
8 declare(strict_types=1);
10 namespace PhpMyAdmin\Tests;
12 use PhpMyAdmin\Sanitize;
13 use PHPUnit\Framework\TestCase;
15 /**
16 * Tests for methods in Sanitize class
18 * @package PhpMyAdmin-test
20 class SanitizeTest extends TestCase
22 /**
23 * Tests for proper escaping of XSS.
25 * @return void
27 public function testXssInHref()
29 $this->assertEquals(
30 '[a@javascript:alert(\'XSS\');@target]link</a>',
31 Sanitize::sanitizeMessage('[a@javascript:alert(\'XSS\');@target]link[/a]')
35 /**
36 * Tests correct generating of link redirector.
38 * @return void
40 public function testLink()
42 $lang = $GLOBALS['lang'];
44 unset($GLOBALS['server']);
45 unset($GLOBALS['lang']);
46 $this->assertEquals(
47 '<a href="./url.php?url=https%3A%2F%2Fwww.phpmyadmin.net%2F" target="target">link</a>',
48 Sanitize::sanitizeMessage('[a@https://www.phpmyadmin.net/@target]link[/a]')
51 $GLOBALS['lang'] = $lang;
54 /**
55 * Tests links to documentation.
57 * @param string $link link
58 * @param string $expected expected result
60 * @return void
62 * @dataProvider docLinks
64 public function testDoc($link, $expected): void
66 $this->assertEquals(
67 '<a href="./url.php?url=https%3A%2F%2Fdocs.phpmyadmin.net%2Fen%2Flatest%2F' . $expected . '" target="documentation">doclink</a>',
68 Sanitize::sanitizeMessage('[doc@' . $link . ']doclink[/doc]')
72 /**
73 * Data provider for sanitize [doc@foo] markup
75 * @return array
77 public function docLinks()
79 return [
81 'foo',
82 'setup.html%23foo',
85 'cfg_TitleTable',
86 'config.html%23cfg_TitleTable',
89 'faq3-11',
90 'faq.html%23faq3-11',
93 'bookmarks@',
94 'bookmarks.html',
99 /**
100 * Tests link target validation.
102 * @return void
104 public function testInvalidTarget()
106 $this->assertEquals(
107 '[a@./Documentation.html@INVALID9]doc</a>',
108 Sanitize::sanitizeMessage('[a@./Documentation.html@INVALID9]doc[/a]')
113 * Tests XSS escaping after valid link.
115 * @return void
117 public function testLinkDocXss()
119 $this->assertEquals(
120 '[a@./Documentation.html" onmouseover="alert(foo)"]doc</a>',
121 Sanitize::sanitizeMessage('[a@./Documentation.html" onmouseover="alert(foo)"]doc[/a]')
126 * Tests proper handling of multi link code.
128 * @return void
130 public function testLinkAndXssInHref()
132 $this->assertEquals(
133 '<a href="./url.php?url=https%3A%2F%2Fdocs.phpmyadmin.net%2F">doc</a>[a@javascript:alert(\'XSS\');@target]link</a>',
134 Sanitize::sanitizeMessage('[a@https://docs.phpmyadmin.net/]doc[/a][a@javascript:alert(\'XSS\');@target]link[/a]')
139 * Test escaping of HTML tags
141 * @return void
143 public function testHtmlTags()
145 $this->assertEquals(
146 '&lt;div onclick=""&gt;',
147 Sanitize::sanitizeMessage('<div onclick="">')
152 * Tests basic BB code.
154 * @return void
156 public function testBBCode()
158 $this->assertEquals(
159 '<strong>strong</strong>',
160 Sanitize::sanitizeMessage('[strong]strong[/strong]')
165 * Tests output escaping.
167 * @return void
169 public function testEscape()
171 $this->assertEquals(
172 '&lt;strong&gt;strong&lt;/strong&gt;',
173 Sanitize::sanitizeMessage('[strong]strong[/strong]', true)
178 * Test for Sanitize::sanitizeFilename
180 * @return void
182 public function testSanitizeFilename()
184 $this->assertEquals(
185 'File_name_123',
186 Sanitize::sanitizeFilename('File_name 123')
191 * Test for Sanitize::getJsValue
193 * @param string $key Key
194 * @param string $value Value
195 * @param string $expected Expected output
197 * @dataProvider variables
199 * @return void
201 public function testGetJsValue($key, $value, $expected): void
203 $this->assertEquals($expected, Sanitize::getJsValue($key, $value));
204 $this->assertEquals('foo = 100', Sanitize::getJsValue('foo', '100', false));
205 $array = [
206 '1',
207 '2',
208 '3',
210 $this->assertEquals(
211 "foo = [\"1\",\"2\",\"3\",];\n",
212 Sanitize::getJsValue('foo', $array)
214 $this->assertEquals(
215 "foo = \"bar\\\"baz\";\n",
216 Sanitize::getJsValue('foo', 'bar"baz')
221 * Test for Sanitize::jsFormat
223 * @return void
225 public function testJsFormat()
227 $this->assertEquals("`foo`", Sanitize::jsFormat('foo'));
231 * Provider for testFormat
233 * @return array
235 public function variables()
237 return [
239 'foo',
240 true,
241 "foo = true;\n",
244 'foo',
245 false,
246 "foo = false;\n",
249 'foo',
250 100,
251 "foo = 100;\n",
254 'foo',
256 "foo = 0;\n",
259 'foo',
260 'text',
261 "foo = \"text\";\n",
264 'foo',
265 'quote"',
266 "foo = \"quote\\\"\";\n",
269 'foo',
270 'apostroph\'',
271 "foo = \"apostroph\\'\";\n",
277 * Sanitize::escapeJsString tests
279 * @param string $target expected output
280 * @param string $source string to be escaped
282 * @return void
284 * @dataProvider escapeDataProvider
286 public function testEscapeJsString($target, $source): void
288 $this->assertEquals($target, Sanitize::escapeJsString($source));
292 * Data provider for testEscape
294 * @return array data for testEscape test case
296 public function escapeDataProvider()
298 return [
300 '\\\';',
301 '\';',
304 '\r\n\\\'<scrIpt></\' + \'script>',
305 "\r\n'<scrIpt></sCRIPT>",
308 '\\\';[XSS]',
309 '\';[XSS]',
312 '</\' + \'script></head><body>[HTML]',
313 '</SCRIPT></head><body>[HTML]',
316 '\"\\\'\\\\\\\'\"',
317 '"\'\\\'"',
320 "\\\\\'\'\'\'\'\'\'\'\'\'\'\'\\\\",
321 "\\''''''''''''\\",
327 * Test for removeRequestVars
329 * @return void
331 public function testRemoveRequestVars()
333 $_REQUEST['foo'] = 'bar';
334 $_REQUEST['allow'] = 'all';
335 $_REQUEST['second'] = 1;
336 $allow_list = [
337 'allow',
338 'second',
340 Sanitize::removeRequestVars($allow_list);
341 $this->assertArrayNotHasKey('foo', $_REQUEST);
342 $this->assertArrayNotHasKey('second', $_REQUEST);
343 $this->assertArrayHasKey('allow', $_REQUEST);