Skip to content

Commit

Permalink
Harden Headers (#2721)
Browse files Browse the repository at this point in the history
* Basic security headers

* Remove XSS Header (not relevent)

* Fix config name

* Use Arr::get()

* Add tests

* Re-fix the StoreConfig step for fresh installs

Co-authored-by: luceos <luceos@users.noreply.github.com>
Co-authored-by: Alexander Skvortsov <askvortsov1@users.noreply.github.com>
  • Loading branch information
3 people authored May 3, 2021
1 parent 9711af4 commit 7eea247
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Admin/AdminServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public function register()
HttpMiddleware\SetLocale::class,
'flarum.admin.route_resolver',
HttpMiddleware\CheckCsrfToken::class,
Middleware\RequireAdministrateAbility::class
Middleware\RequireAdministrateAbility::class,
HttpMiddleware\ReferrerPolicyHeader::class,
HttpMiddleware\ContentTypeOptionsHeader::class
];
});

Expand Down
2 changes: 2 additions & 0 deletions src/Forum/ForumServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public function register()
HttpMiddleware\CheckCsrfToken::class,
HttpMiddleware\ShareErrorsFromSession::class,
HttpMiddleware\FlarumPromotionHeader::class,
HttpMiddleware\ReferrerPolicyHeader::class,
HttpMiddleware\ContentTypeOptionsHeader::class
];
});

Expand Down
25 changes: 25 additions & 0 deletions src/Http/Middleware/ContentTypeOptionsHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Http\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface;

class ContentTypeOptionsHeader implements Middleware
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);

return $response->withAddedHeader('X-Content-Type-Options', 'nosniff');
}
}
34 changes: 34 additions & 0 deletions src/Http/Middleware/ReferrerPolicyHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Http\Middleware;

use Flarum\Foundation\Config;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface;

class ReferrerPolicyHeader implements Middleware
{
protected $policy = '';

public function __construct(Config $config)
{
$this->policy = Arr::get($config, 'headers.referrerPolicy') ?? 'same-origin';
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);

return $response->withAddedHeader('Referrer-Policy', $this->policy);
}
}
5 changes: 4 additions & 1 deletion src/Install/Steps/StoreConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ private function buildConfig()
{
return [
'debug' => $this->debugMode,
'poweredByHeader' => true,
'database' => $this->dbConfig->toArray(),
'url' => (string) $this->baseUrl,
'paths' => $this->getPathsConfig(),
'headers' => [
'poweredByHeader' => true,
'referrerPolicy' => 'same-origin',
]
];
}

Expand Down
28 changes: 28 additions & 0 deletions tests/integration/middleware/ContentTypeOptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Tests\integration\middleware;

use Flarum\Testing\integration\TestCase;

class ContentTypeOptionsTest extends TestCase
{
/**
* @test
*/
public function has_content_type_options_header()
{
$response = $this->send(
$this->request('GET', '/')
);
$this->assertEquals(200, $response->getStatusCode());
$this->assertArrayHasKey('X-Content-Type-Options', $response->getHeaders());
$this->assertEquals('nosniff', $response->getHeader('X-Content-Type-Options')[0]);
}
}
39 changes: 39 additions & 0 deletions tests/integration/middleware/ReferrerPolicyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Tests\integration\middleware;

use Flarum\Testing\integration\TestCase;

class ReferrerPolicyTest extends TestCase
{
/**
* @test
*/
public function has_referer_header()
{
$response = $this->send(
$this->request('GET', '/')
);
$this->assertEquals(200, $response->getStatusCode());
$this->assertArrayHasKey('Referrer-Policy', $response->getHeaders());
}

/**
* @test
*/
public function has_default_referer_policy()
{
$response = $this->send(
$this->request('GET', '/')
);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('same-origin', $response->getHeader('Referrer-Policy')[0]);
}
}

0 comments on commit 7eea247

Please sign in to comment.