-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH Add a warning if allowed hosts is not set. (#11612)
Adds ability to "wildcard" allow all hosts, which disables the logging. Adds much needed test coverage.
- Loading branch information
1 parent
db86f83
commit 5fa5a0c
Showing
4 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
tests/php/Control/Middleware/AllowedHostsMiddlewareTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Control\Tests\Middleware; | ||
|
||
use InvalidArgumentException; | ||
use ReflectionClass; | ||
use SilverStripe\Control\HTTPRequest; | ||
use SilverStripe\Control\HTTPResponse; | ||
use SilverStripe\Core\Environment; | ||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\Control\Middleware\AllowedHostsMiddleware; | ||
|
||
class AllowedHostsMiddlewareTest extends SapphireTest | ||
{ | ||
protected $usesDatabase = false; | ||
|
||
public function provideProcess(): array | ||
{ | ||
return [ | ||
'cli allow all' => [ | ||
'allowedHosts' => [], | ||
'isCli' => true, | ||
'allowed' => true, | ||
], | ||
'cli ignores config' => [ | ||
'allowedHosts' => ['example.org'], | ||
'isCli' => true, | ||
'allowed' => true, | ||
], | ||
'HTTP allow all' => [ | ||
'allowedHosts' => [], | ||
'isCli' => false, | ||
'allowed' => true, | ||
], | ||
'HTTP allow all explicit' => [ | ||
'allowedHosts' => ['*'], | ||
'isCli' => false, | ||
'allowed' => true, | ||
], | ||
'HTTP allow explicit host' => [ | ||
'allowedHosts' => ['www.example.com'], | ||
'isCli' => false, | ||
'allowed' => true, | ||
], | ||
'HTTP allow explicit host multiple values' => [ | ||
'allowedHosts' => ['example.com', 'example.org', 'ww.example.org', 'www.example.com'], | ||
'isCli' => false, | ||
'allowed' => true, | ||
], | ||
'HTTP allow explicit host string' => [ | ||
'allowedHosts' => 'example.com,example.org,ww.example.org,www.example.com', | ||
'isCli' => false, | ||
'allowed' => true, | ||
], | ||
'HTTP host mismatch (missing subdomain)' => [ | ||
'allowedHosts' => ['example.com'], | ||
'isCli' => false, | ||
'allowed' => false, | ||
], | ||
'HTTP host mismatch (different tld)' => [ | ||
'allowedHosts' => ['example.org'], | ||
'isCli' => false, | ||
'allowed' => false, | ||
], | ||
'HTTP host mismatch multiple' => [ | ||
'allowedHosts' => ['example.org', 'www.example.org', 'example.com'], | ||
'isCli' => false, | ||
'allowed' => false, | ||
], | ||
'HTTP host mismatch string' => [ | ||
'allowedHosts' => 'example.org,www.example.org,example.com', | ||
'isCli' => false, | ||
'allowed' => false, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideProcess | ||
*/ | ||
public function testProcess(string|array $allowedHosts, bool $isCli, bool $allowed): void | ||
{ | ||
$reflectionEnvironment = new ReflectionClass(Environment::class); | ||
$origIsCli = $reflectionEnvironment->getStaticPropertyValue('isCliOverride'); | ||
$reflectionEnvironment->setStaticPropertyValue('isCliOverride', $isCli); | ||
|
||
try { | ||
$middleware = new AllowedHostsMiddleware(); | ||
$middleware->setAllowedHosts($allowedHosts); | ||
$request = new HTTPRequest('GET', '/'); | ||
$request->addHeader('host', 'www.example.com'); | ||
$defaultResponse = new HTTPResponse(); | ||
|
||
$result = $middleware->process($request, function () use ($defaultResponse) { | ||
return $defaultResponse; | ||
}); | ||
|
||
if ($allowed) { | ||
$this->assertSame(200, $result->getStatusCode()); | ||
$this->assertSame($defaultResponse, $result); | ||
} else { | ||
$this->assertSame(400, $result->getStatusCode()); | ||
$this->assertNotSame($defaultResponse, $result); | ||
} | ||
} finally { | ||
$reflectionEnvironment->setStaticPropertyValue('isCliOverride', $origIsCli); | ||
} | ||
} | ||
|
||
public function testProcessInvalidConfig(): void | ||
{ | ||
$middleware = new AllowedHostsMiddleware(); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The wildcard "*" cannot be used in conjunction with actual hosts.'); | ||
|
||
$middleware->setAllowedHosts(['*', 'www.example.com']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters