-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add security headers to the default response (#110)
- Loading branch information
1 parent
d768aa6
commit c8e3636
Showing
4 changed files
with
118 additions
and
1 deletion.
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
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,38 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PhpList\RestBundle\ViewHandler; | ||
|
||
use FOS\RestBundle\View\View; | ||
use FOS\RestBundle\View\ViewHandler; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* This class is used to add headers to the default response. | ||
* | ||
* @author Xheni Myrtaj <[email protected]> | ||
*/ | ||
class SecuredViewHandler | ||
{ | ||
/** | ||
* @param ViewHandler $viewHandler | ||
* @param View $view | ||
* @param Request $request | ||
* @param string $format | ||
* | ||
* @return Response | ||
*/ | ||
public function createResponse(ViewHandler $handler, View $view, Request $request, string $format): Response | ||
{ | ||
$view->setHeaders( | ||
[ | ||
'X-Content-Type-Options' => 'nosniff', | ||
'Content-Security-Policy' => "default-src 'none'", | ||
'X-Frame-Options' => 'DENY', | ||
] | ||
); | ||
|
||
return $handler->createResponse($view, $request, $format); | ||
} | ||
} |
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,69 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PhpList\RestBundle\Tests\System\Controller; | ||
|
||
use GuzzleHttp\Client; | ||
use PhpList\Core\TestingSupport\Traits\SymfonyServerTrait; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* Test for security headers | ||
* | ||
* @author Xheni Myrtaj <[email protected]> | ||
*/ | ||
class SecuredViewHandlerTest extends TestCase | ||
{ | ||
use SymfonyServerTrait; | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
private $httpClient = null; | ||
|
||
protected function setUp() | ||
{ | ||
$this->httpClient = new Client(['http_errors' => false]); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
$this->stopSymfonyServer(); | ||
} | ||
|
||
/** | ||
* @return string[][] | ||
*/ | ||
public function environmentDataProvider(): array | ||
{ | ||
return [ | ||
'test' => ['test'], | ||
'dev' => ['dev'], | ||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
* @param string $environment | ||
* @dataProvider environmentDataProvider | ||
*/ | ||
public function testSecurityHeaders(string $environment) | ||
{ | ||
$this->startSymfonyServer($environment); | ||
|
||
$response = $this->httpClient->get( | ||
'/api/v2/sessions', | ||
['base_uri' => $this->getBaseUrl()] | ||
); | ||
$expectedHeaders = [ | ||
'X-Content-Type-Options' => 'nosniff', | ||
'Content-Security-Policy' => "default-src 'none'", | ||
'X-Frame-Options' => 'DENY', | ||
]; | ||
|
||
foreach ($expectedHeaders as $key => $value) { | ||
static::assertSame([$value], $response->getHeader($key)); | ||
} | ||
} | ||
} |