mikefrancis / laravel-secureheaders by mikefrancis

SecureHeaders wrapper for Laravel.
350,664
92
4
Package Data
Maintainer Username: mikefrancis
Maintainer Contact: mikeffrancis@gmail.com (Mike Francis)
Package Create Date: 2017-07-17
Package Last Update: 2022-02-07
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2025-02-06 03:03:57
Package Statistics
Total Downloads: 350,664
Monthly Downloads: 10,795
Daily Downloads: 415
Total Stars: 92
Total Watchers: 4
Total Forks: 5
Total Open Issues: 2

Laravel SecureHeaders

Packagist Build Status codecov

SecureHeaders wrapper for Laravel.

Based on aidantwoods/SecureHeaders.

Installation

Require the mikefrancis/laravel-secureheaders package in your composer.json and update your dependencies:

composer require mikefrancis/laravel-secureheaders

If you are using Laravel 5.5+, package discovery is enabled. For Laravel 5.4, add the service provider to your config/app.php providers array:

MikeFrancis\LaravelSecureHeaders\ServiceProvider::class,

Usage

To add more secure headers to your entire application, add the ApplySecureHeaders middleware in the $middleware property of app/Http/Kernel.php class:

protected $middleware = [
    // ...
    \MikeFrancis\LaravelSecureHeaders\ApplySecureHeaders::class,
];

Configuration

Some sensible defaults have been set in config/secure-headers.php but if you'd like to change these, copy the file to your own application's config using the following command:

php artisan vendor:publish --provider="MikeFrancis\LaravelSecureHeaders\ServiceProvider"

A typical configuration might look like this:

<?php

return [
    // Safe Mode
    'safeMode' => false,

    // HSTS Strict-Transport-Security
    'hsts' => [
        'enabled' => true,
    ],

    // Content Security Policy
    'csp' => [
        'default' => [
            'self',
        ],
        'img-src' => [
            '*', // Allow images from anywhere
        ],
        'style-src' => [
            'self',
            'unsafe-inline', // Allow inline styles
            'https://fonts.googleapis.com', // Allow stylesheets from Google Fonts
        ],
        'font-src' => [
            'self',
            'https://fonts.gstatic.com', // Allow fonts from the Google Fonts CDN
        ],
    ],
];

For a full reference of Content Security Policy directives and their values, see content-security-policy.com.