|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Util; |
| 4 | + |
| 5 | +use BookStack\Util\UrlFilter; |
| 6 | +use Tests\TestCase; |
| 7 | + |
| 8 | +class UrlFilterTest extends TestCase |
| 9 | +{ |
| 10 | + public function test_it_finds_invalid_urls() |
| 11 | + { |
| 12 | + $urls = [ |
| 13 | + 'javascript:alert("bunny")', |
| 14 | + ' javascript:alert("bunny")', |
| 15 | + 'JavaScript:alert("bunny")', |
| 16 | + "\t\n\t\nJavaScript:alert(\"bunny\")", |
| 17 | + 'data:text/html;bunny<a></a>', |
| 18 | + 'Data:text/html;bunny<a></a>', |
| 19 | + 'Data:text/html;bunny<a></a>', |
| 20 | + "http://example.com\0javascript:alert(1)", |
| 21 | + ]; |
| 22 | + |
| 23 | + foreach ($urls as $url) { |
| 24 | + $filter = new UrlFilter($url); |
| 25 | + $this->assertFalse($filter->isAllowed(), "Failed to detect invalid url: {$url}"); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public function test_clean() |
| 30 | + { |
| 31 | + $expectedOutputByInput = [ |
| 32 | + 'javascript:alert("bunny")' => '#badlink', |
| 33 | + ' javascript:alert("bunny")' => '#badlink', |
| 34 | + 'JavaScript:alert("bunny")' => '#badlink', |
| 35 | + "\t\n\t\nJavaScript:alert(\"bunny\")" => '#badlink', |
| 36 | + 'data:text/html;bunny<a></a>' => '#badlink', |
| 37 | + 'Data:text/html;bunny<a></a>' => '#badlink', |
| 38 | + 'Data:text/html;bunny<a></a>' => '#badlink', |
| 39 | + "http://example.com\0javascript:alert(1)" => '#badlink', |
| 40 | + "Java\tScript:alert(\"bunny\")" => '#badlink', |
| 41 | + |
| 42 | + 'https://example.com' => 'https://example.com', |
| 43 | + 'https://example.com/a/b' => 'https://example.com/a/b', |
| 44 | + 'https://example.com/a/b?a=b#ab' => 'https://example.com/a/b?a=b#ab', |
| 45 | + 'https://example.com/a/b?a=b#ab&c=d' => 'https://example.com/a/b?a=b#ab&c=d', |
| 46 | + 'https://example.com:5050' => 'https://example.com:5050', |
| 47 | + 'https://example.com:5050/a/b' => 'https://example.com:5050/a/b', |
| 48 | + 'https://example.com:5050/a/b?a=b#ab' => 'https://example.com:5050/a/b?a=b#ab', |
| 49 | + 'https://user@example.com:5011/a/b?a=b' => 'https://user@example.com:5011/a/b?a=b', |
| 50 | + 'https://user:pass@example.com:5011/a/b?a=b' => 'https://user:pass@example.com:5011/a/b?a=b', |
| 51 | + |
| 52 | + '//example.com' => 'https://example.com', |
| 53 | + 'a/b/c' => 'a/b/c', |
| 54 | + '/a/b/c' => '/a/b/c', |
| 55 | + |
| 56 | + 'tel:123456789' => 'tel:123456789', |
| 57 | + 'TEL:123456789' => 'tel:123456789', |
| 58 | + 'maiLto:a@b.c' => 'mailto:a@b.c', |
| 59 | + 'file://a/b/c' => 'file://a/b/c', |
| 60 | + 'ftp://a/b/c' => 'ftp://a/b/c', |
| 61 | + 'nntp://a/b/c' => 'nntp://a/b/c', |
| 62 | + 'news:a/b/c' => 'news:a/b/c', |
| 63 | + ]; |
| 64 | + |
| 65 | + foreach ($expectedOutputByInput as $input => $expected) { |
| 66 | + $filter = new UrlFilter($input); |
| 67 | + $output = $filter->clean(); |
| 68 | + $this->assertEquals($expected, $output, "Failed to clean url: {$input}"); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments