Skip to content

Commit f05824c

Browse files
committed
Set security headers to response
Signed-off-by: Xheni Myrtaj <myrtajxheni@gmail.com>
1 parent 97a8ee8 commit f05824c

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@
128128
"messages": {
129129
"Symfony\\Component\\HttpKernel\\Exception\\BadRequestHttpException": true
130130
}
131+
},
132+
"service": {
133+
"view_handler": "my.secure_view_handler"
131134

132135
}
133136
}

config/services.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ services:
44
public: true
55
autowire: true
66
tags: ['controller.service_arguments']
7+
8+
my.secure_handler:
9+
class: \PhpList\RestBundle\ViewHandler\SecuredViewHandler
10+
11+
my.secure_view_handler:
12+
parent: fos_rest.view_handler.default
13+
calls:
14+
- ['registerHandler', [ 'json', ["@my.secure_handler", 'createResponse'] ] ]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PhpList\RestBundle\ViewHandler;
5+
6+
use FOS\RestBundle\View\View;
7+
use FOS\RestBundle\View\ViewHandler;
8+
use Symfony\Component\HttpFoundation\Request;
9+
use Symfony\Component\HttpFoundation\Response;
10+
11+
/**
12+
* This class is used to add headers to the default response.
13+
* @author Xheni Myrtaj <xheni@phplist.com> .
14+
*/
15+
class SecuredViewHandler
16+
{
17+
18+
/**
19+
* @param ViewHandler $viewHandler
20+
* @param View $view
21+
* @param Request $request
22+
* @param string $format
23+
*
24+
* @return Response
25+
*/
26+
public function createResponse(ViewHandler $handler, View $view, Request $request, $format)
27+
{
28+
$view->setHeaders([
29+
'X-Content-Type-Options' => 'nosniff',
30+
'Content-Security-Policy' => "default-src 'none'",
31+
'X-Frame-Options' => 'DENY'
32+
]);
33+
34+
return $handler->createResponse($view, $request, $format);
35+
}
36+
}

0 commit comments

Comments
 (0)