Skip to content

Commit

Permalink
feat: add validation for baseURL
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Feb 16, 2023
1 parent f08b98d commit e9b6734
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions system/HTTP/SiteURI.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\HTTP;

use BadMethodCallException;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use Config\App;

Expand Down Expand Up @@ -75,6 +76,13 @@ public function __construct(App $configApp)
// baseURL, so let's help them out.
$baseURL = rtrim($configApp->baseURL, '/ ') . '/';

// Validate baseURL
if (filter_var($baseURL, FILTER_VALIDATE_URL) === false) {
throw new ConfigException(
'Config\App::$baseURL is invalid.'
);
}

$this->baseURL = $baseURL;
$this->indexPage = $configApp->indexPage;

Expand Down
11 changes: 11 additions & 0 deletions tests/system/HTTP/SiteURITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\HTTP;

use BadMethodCallException;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use CodeIgniter\Test\CIUnitTestCase;
use Config\App;
Expand Down Expand Up @@ -68,6 +69,16 @@ public function testConstructorIndexPageEmpty()
$this->assertSame('http://example.com/', (string) $uri);
}

public function testConstructorInvalidBaseURL()
{
$this->expectException(ConfigException::class);

$config = new App();
$config->baseURL = 'invalid';

new SiteURI($config);
}

public function testSetPath()
{
$config = new App();
Expand Down

0 comments on commit e9b6734

Please sign in to comment.