|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Filters; |
| 13 | + |
| 14 | +use CodeIgniter\HTTP\RequestInterface; |
| 15 | +use CodeIgniter\HTTP\ResponseInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Add Common Security Headers |
| 19 | + */ |
| 20 | +class SecureHeaders implements FilterInterface |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var array<string, string> |
| 24 | + */ |
| 25 | + protected $headers = [ |
| 26 | + // https://owasp.org/www-project-secure-headers/#x-frame-options |
| 27 | + 'X-Frame-Options' => 'SAMEORIGIN', |
| 28 | + |
| 29 | + // https://owasp.org/www-project-secure-headers/#x-content-type-options |
| 30 | + 'X-Content-Type-Options' => 'nosniff', |
| 31 | + |
| 32 | + // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/jj542450(v=vs.85)#the-noopen-directive |
| 33 | + 'X-Download-Options' => 'noopen', |
| 34 | + |
| 35 | + // https://owasp.org/www-project-secure-headers/#x-permitted-cross-domain-policies |
| 36 | + 'X-Permitted-Cross-Domain-Policies' => 'none', |
| 37 | + |
| 38 | + // https://owasp.org/www-project-secure-headers/#referrer-policy |
| 39 | + 'Referrer-Policy' => 'same-origin', |
| 40 | + |
| 41 | + // https://owasp.org/www-project-secure-headers/#x-xss-protection |
| 42 | + // If you do not need to support legacy browsers, it is recommended that you use |
| 43 | + // Content-Security-Policy without allowing unsafe-inline scripts instead. |
| 44 | + // 'X-XSS-Protection' => '1; mode=block', |
| 45 | + ]; |
| 46 | + |
| 47 | + /** |
| 48 | + * We don't have anything to do here. |
| 49 | + * |
| 50 | + * @param array|null $arguments |
| 51 | + * |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + public function before(RequestInterface $request, $arguments = null) |
| 55 | + { |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Add security headers. |
| 60 | + * |
| 61 | + * @param array|null $arguments |
| 62 | + * |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) |
| 66 | + { |
| 67 | + foreach ($this->headers as $header => $value) { |
| 68 | + $response->setHeader($header, $value); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments