Spaces:
No application file
No application file
File size: 5,438 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
<?php
declare(strict_types=1);
namespace Mautic\Middleware\Tests;
use Mautic\CoreBundle\Test\AbstractMauticTestCase;
use Mautic\Middleware\HSTSMiddleware;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\ExpectationFailedException as PHPUnitException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class HSTSMiddlewareTest extends AbstractMauticTestCase
{
public const HSTS_KEY = 'strict-transport-security';
protected \ReflectionProperty $addHSTS;
protected \ReflectionProperty $includeDubDomains;
protected \ReflectionProperty $preload;
protected HSTSMiddleware $middleware;
protected \ReflectionClass $middlewareReflection;
/**
* @throws \ReflectionException
*/
protected function setUp(): void
{
parent::setUp();
$this->middleware = new HSTSMiddleware($this->client->getKernel());
$this->middlewareReflection = new \ReflectionClass($this->middleware);
$this->addHSTS = $this->middlewareReflection->getProperty('enableHSTS');
$this->addHSTS->setAccessible(true);
$this->includeDubDomains = $this->middlewareReflection->getProperty('includeDubDomains');
$this->includeDubDomains->setAccessible(true);
$this->preload = $this->middlewareReflection->getProperty('preload');
$this->preload->setAccessible(true);
}
protected function testResponseHeaders(): void
{
$response = $this->getMiddlewareResponse();
Assert::assertNotEmpty($response->headers);
}
public function testHSTSEnabled(): void
{
$this->setHSTS(true);
$response = $this->getMiddlewareResponse();
Assert::assertTrue(
$response->headers->has(self::HSTS_KEY),
'Strict-Transport-Security is enabled but is missing from the response headers'
);
}
public function testHSTSDisabled(): void
{
$this->setHSTS(false);
$response = $this->getMiddlewareResponse();
Assert::assertFalse(
$response->headers->has(self::HSTS_KEY),
'Strict-Transport-Security is disabled but is present in response headers'
);
}
public function testIncludeSubdomainsEnabled(): void
{
$needle = 'includeSubDomains';
$this->setHSTS(true);
$this->setIncludeDubDomainsValue(true);
$response = $this->getMiddlewareResponse();
Assert::assertStringContainsString(
$needle,
$response->headers->get(self::HSTS_KEY),
'Option include Subdomains is enabled but is missing from the HSTS value'
);
}
public function testIncludeSubdomainsDisabled(): void
{
$needle = 'includeSubDomains';
$this->setHSTS(true);
$this->setIncludeDubDomainsValue(false);
$response = $this->getMiddlewareResponse();
Assert::assertStringNotContainsStringIgnoringCase(
$needle,
$this->getHSTSValue($response),
'Option include Subdomains is disabled but is present in HSTS value'
);
}
public function testPreloadEnabled(): void
{
$needle = 'preload';
$this->setHSTS(true);
$this->setPreloadValue(true);
$response = $this->getMiddlewareResponse();
Assert::assertStringContainsString(
$needle,
$response->headers->get(self::HSTS_KEY),
'Option preload is enabled but is missing from the HSTS value'
);
}
public function testPreloadDisabled(): void
{
$needle = 'preload';
$this->setHSTS(true);
$this->setPreloadValue(false);
$response = $this->getMiddlewareResponse();
Assert::assertStringNotContainsStringIgnoringCase(
$needle,
$this->getHSTSValue($response),
'Option preload is disabled but is present in HSTS value'
);
}
/**
* @throws \ReflectionException
*/
public function testExpireTime(): void
{
$this->setHSTS(true);
$expireTimeValue = 12345;
$expireTime = $this->middlewareReflection->getProperty('expireTime');
$expireTime->setAccessible(true);
$expireTime->setValue($this->middleware, $expireTimeValue);
$response = $this->getMiddlewareResponse();
Assert::assertMatchesRegularExpression(
'/max-age='.$expireTimeValue.'(; includeSubDomains)?/',
$this->getHSTSValue($response),
'Expire time does not match the configuration'
);
}
private function setHSTS(bool $value): void
{
$this->addHSTS->setValue($this->middleware, $value);
}
private function setIncludeDubDomainsValue(bool $value): void
{
$this->includeDubDomains->setValue($this->middleware, $value);
}
private function setPreloadValue(bool $value): void
{
$this->preload->setValue($this->middleware, $value);
}
private function getHSTSValue(Response $response): string
{
return $response->headers->get(self::HSTS_KEY) ?? '';
}
private function getMiddlewareResponse(): Response
{
try {
return $this->middleware->handle(Request::create('s/login', Request::METHOD_GET));
} catch (\Exception $e) {
throw new PHPUnitException($e->getMessage());
}
}
}
|