|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit; |
| 4 | + |
| 5 | +use BookStack\Exceptions\HttpFetchException; |
| 6 | +use BookStack\Util\SsrUrlValidator; |
| 7 | +use Tests\TestCase; |
| 8 | + |
| 9 | +class SsrUrlValidatorTest extends TestCase |
| 10 | +{ |
| 11 | + public function test_allowed() |
| 12 | + { |
| 13 | + $testMap = [ |
| 14 | + // Single values |
| 15 | + ['config' => '', 'url' => '', 'result' => false], |
| 16 | + ['config' => '', 'url' => 'https://example.com', 'result' => false], |
| 17 | + ['config' => ' ', 'url' => 'https://example.com', 'result' => false], |
| 18 | + ['config' => '*', 'url' => '', 'result' => false], |
| 19 | + ['config' => '*', 'url' => 'https://example.com', 'result' => true], |
| 20 | + ['config' => 'https://*', 'url' => 'https://example.com', 'result' => true], |
| 21 | + ['config' => 'http://*', 'url' => 'https://example.com', 'result' => false], |
| 22 | + ['config' => 'https://*example.com', 'url' => 'https://example.com', 'result' => true], |
| 23 | + ['config' => 'https://*ample.com', 'url' => 'https://example.com', 'result' => true], |
| 24 | + ['config' => 'https://*.example.com', 'url' => 'https://example.com', 'result' => false], |
| 25 | + ['config' => 'https://*.example.com', 'url' => 'https://test.example.com', 'result' => true], |
| 26 | + ['config' => '*//example.com', 'url' => 'https://example.com', 'result' => true], |
| 27 | + ['config' => '*//example.com', 'url' => 'http://example.com', 'result' => true], |
| 28 | + ['config' => 'https://example.com', 'url' => 'https://example.com/a/b/c?test=cat', 'result' => true], |
| 29 | + ['config' => 'https://example.com', 'url' => 'https://example.co.uk', 'result' => false], |
| 30 | + |
| 31 | + // Escapes |
| 32 | + ['config' => 'https://(.*?).com', 'url' => 'https://example.com', 'result' => false], |
| 33 | + ['config' => 'https://example.com', 'url' => 'https://example.co.uk#https://example.com', 'result' => false], |
| 34 | + |
| 35 | + // Multi values |
| 36 | + ['config' => '*//example.org *//example.com', 'url' => 'https://example.com', 'result' => true], |
| 37 | + ['config' => '*//example.org *//example.com', 'url' => 'https://example.com/a/b/c?test=cat#hello', 'result' => true], |
| 38 | + ['config' => '*.example.org *.example.com', 'url' => 'https://example.co.uk', 'result' => false], |
| 39 | + ['config' => ' *.example.org *.example.com ', 'url' => 'https://example.co.uk', 'result' => false], |
| 40 | + ['config' => '* *.example.com', 'url' => 'https://example.co.uk', 'result' => true], |
| 41 | + ['config' => '*//example.org *//example.com *//example.co.uk', 'url' => 'https://example.co.uk', 'result' => true], |
| 42 | + ['config' => '*//example.org *//example.com *//example.co.uk', 'url' => 'https://example.net', 'result' => false], |
| 43 | + ]; |
| 44 | + |
| 45 | + foreach ($testMap as $test) { |
| 46 | + $result = (new SsrUrlValidator($test['config']))->allowed($test['url']); |
| 47 | + $this->assertEquals($test['result'], $result, "Failed asserting url '{$test['url']}' with config '{$test['config']}' results " . ($test['result'] ? 'true' : 'false')); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public function test_enssure_allowed() |
| 52 | + { |
| 53 | + $result = (new SsrUrlValidator('https://example.com'))->ensureAllowed('https://example.com'); |
| 54 | + $this->assertNull($result); |
| 55 | + |
| 56 | + $this->expectException(HttpFetchException::class); |
| 57 | + (new SsrUrlValidator('https://example.com'))->ensureAllowed('https://test.example.com'); |
| 58 | + } |
| 59 | +} |
0 commit comments