Skip to content

Commit 9c4c762

Browse files
authored
Merge pull request #7 from flightphp/security-headers
added security headers by default
2 parents 50fb8d3 + 165ce01 commit 9c4c762

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

app/config/config_sample.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
$app->set('flight.views.extension', '.php'); // View file extension (e.g., '.php', '.latte')
5555
$app->set('flight.content_length', false); // Send content length header. Usually false unless required by proxy
5656

57+
// Generate a CSP nonce for each request and store in $app
58+
$nonce = bin2hex(random_bytes(16));
59+
$app->set('csp_nonce', $nonce);
60+
5761
/**********************************************
5862
* User Configuration *
5963
**********************************************/

app/config/routes.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
<?php
22

33
use app\controllers\ApiExampleController;
4+
use app\middlewares\SecurityHeadersMiddleware;
45
use flight\Engine;
56
use flight\net\Router;
67

78
/**
89
* @var Router $router
910
* @var Engine $app
1011
*/
11-
$router->get('/', function() use ($app) {
12-
$app->render('welcome', [ 'message' => 'You are gonna do great things!' ]);
13-
});
1412

15-
$router->get('/hello-world/@name', function($name) {
16-
echo '<h1>Hello world! Oh hey '.$name.'!</h1>';
17-
});
13+
// This wraps all routes in the group with the SecurityHeadersMiddleware
14+
$router->group('', function(Router $router) use ($app) {
1815

19-
$router->group('/api', function() use ($router) {
20-
$router->get('/users', [ ApiExampleController::class, 'getUsers' ]);
21-
$router->get('/users/@id:[0-9]', [ ApiExampleController::class, 'getUser' ]);
22-
$router->post('/users/@id:[0-9]', [ ApiExampleController::class, 'updateUser' ]);
23-
});
16+
$router->get('/', function() use ($app) {
17+
$app->render('welcome', [ 'message' => 'You are gonna do great things!' ]);
18+
});
19+
20+
$router->get('/hello-world/@name', function($name) {
21+
echo '<h1>Hello world! Oh hey '.$name.'!</h1>';
22+
});
23+
24+
$router->group('/api', function() use ($router) {
25+
$router->get('/users', [ ApiExampleController::class, 'getUsers' ]);
26+
$router->get('/users/@id:[0-9]', [ ApiExampleController::class, 'getUser' ]);
27+
$router->post('/users/@id:[0-9]', [ ApiExampleController::class, 'updateUser' ]);
28+
});
29+
30+
}, [ SecurityHeadersMiddleware::class ]);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace app\middlewares;
5+
6+
use flight\Engine;
7+
use Tracy\Debugger;
8+
9+
class SecurityHeadersMiddleware
10+
{
11+
protected Engine $app;
12+
13+
public function __construct(Engine $app)
14+
{
15+
$this->app = $app;
16+
}
17+
18+
public function before(array $params): void
19+
{
20+
$nonce = $this->app->get('csp_nonce');
21+
22+
// development mode to execute Tracy debug bar CSS
23+
$tracyCssBypass = "'nonce-{$nonce}'";
24+
if(Debugger::$showBar === true) {
25+
$tracyCssBypass = ' \'unsafe-inline\'';
26+
}
27+
28+
$csp = "default-src 'self'; script-src 'self' 'nonce-{$nonce}' 'strict-dynamic'; style-src 'self' {$tracyCssBypass}; img-src 'self' data:;";
29+
$this->app->response()->header('X-Frame-Options', 'SAMEORIGIN');
30+
$this->app->response()->header("Content-Security-Policy", $csp);
31+
$this->app->response()->header('X-XSS-Protection', '1; mode=block');
32+
$this->app->response()->header('X-Content-Type-Options', 'nosniff');
33+
$this->app->response()->header('Referrer-Policy', 'no-referrer-when-downgrade');
34+
$this->app->response()->header('Strict-Transport-Security', 'max-age=31536000; includeSubDomains; preload');
35+
$this->app->response()->header('Permissions-Policy', 'geolocation=()');
36+
}
37+
}

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"start": "php -S localhost:8000 -t public",
3636
"post-create-project-cmd": [
3737
"@php -r \"copy('app/config/config_sample.php', 'app/config/config.php');\"",
38-
"@php -r \"mkdir('app/middlewares/');\"",
3938
"@php -r \"mkdir('app/models/');\"",
4039
"@php -r \"mkdir('app/utils/');\"",
4140
"@php -r \"mkdir('app/cache/');\"",

0 commit comments

Comments
 (0)