Search by

pieceofcake2 / security-headers

ttsuru

CakePHP 2 plugin that adds the security headers to every response of an application

Package info

github.com/pieceofcake2/security-headers

Homepage

Type:cakephp-plugin

pkg:composer/pieceofcake2/security-headers

Statistics

Installs: 1 291

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.6 2026-10-20 07:33 UTC

README

GitHub License Packagist Version PHP CakePHP PHPStan CI Codecov

A dispatcher filter for pieceofcake2/cakephp applications that adds the security headers to every response: Content-Security-Policy, X-Frame-Options, X-Content-Type-Options, Referrer-Policy, Permissions-Policy, Cross-Origin-Opener-Policy and Cross-Origin-Resource-Policy, and removes X-Powered-By.

The headers that the web server or the CDN should send, such as Strict-Transport-Security, are off by default. The Content Security Policy is sent as Content-Security-Policy-Report-Only by default, so that you can adapt it to your application before enforcing it.

Documentation

It covers every setting and its default, the headers for the web server or the CDN, enforcing the Content Security Policy, and changing the headers in an action.

The Markdown documentation is also included in the Composer package, in the plugin's directory: plugins/SecurityHeaders/docs/README.md (the location depends on your installer-paths).

Requirements

  • PHP 8.0+
  • pieceofcake2/cakephp 2.12+

Installation

composer require pieceofcake2/security-headers

Load the plugin and add the filter in config/bootstrap.php (legacy layout: app/Config/bootstrap.php):

CakePlugin::load('SecurityHeaders');

Configure::write('Dispatcher.filters', [
    'AssetDispatcher',
    'CacheDispatcher',
    'SecurityHeaders.SecurityHeadersFilter',
]);

See Installation for the installer-paths setting.

Usage

Change the settings in config/bootstrap.php:

use PieceOfCake2\SecurityHeaders\ContentSecurityPolicy\GoogleTagManager;

Configure::write('SecurityHeaders', [
    'xFrameOptions' => 'DENY',
    'permissionsPolicy' => 'camera=(), microphone=(), geolocation=(), payment=()',
    // Allows Google Tag Manager and the scripts of a CDN, in addition to those of the site
    'contentSecurityPolicyAllow' => [
        GoogleTagManager::class,
        'cdn' => ['script-src' => ['https://cdn.example.com']],
    ],
]);

A setting is the value of its header, or false not to send it. Classes of the plugin allow the tags, the conversion tags and the embeds of Google, Adobe, Meta (Facebook, Instagram), X, TikTok, LINE, YouTube, Yahoo! JAPAN and Microsoft in the Content Security Policy. See Settings.

Contributing

See the contribution guidelines.

License

Licensed under the MIT License. See LICENSE.