|
| 1 | +# Content Security Policy Middleware |
| 2 | + |
| 3 | +[](https://github.com/stevenmaguire/laravel-middleware-csp/releases) |
| 4 | +[](LICENSE.md) |
| 5 | +[](https://travis-ci.org/stevenmaguire/laravel-middleware-csp) |
| 6 | +[](https://scrutinizer-ci.com/g/stevenmaguire/laravel-middleware-csp/code-structure) |
| 7 | +[](https://scrutinizer-ci.com/g/stevenmaguire/laravel-middleware-csp) |
| 8 | +[](https://packagist.org/packages/stevenmaguire/laravel-middleware-csp) |
| 9 | + |
| 10 | +Provides support for enforcing Content Security Policy with headers in Laravel responses. |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +Via Composer |
| 15 | + |
| 16 | +``` bash |
| 17 | +$ composer require stevenmaguire/laravel-middleware-csp |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +### Register as route middleware |
| 23 | + |
| 24 | +``` php |
| 25 | +// within app/Http/Kernal.php |
| 26 | + |
| 27 | +protected $routeMiddleware = [ |
| 28 | + // |
| 29 | + 'secure.content' => \Stevenmaguire\Http\Middleware\Laravel\EnforceContentSecurity::class, |
| 30 | + // |
| 31 | +]; |
| 32 | +``` |
| 33 | + |
| 34 | +### Apply content security policy to routes |
| 35 | + |
| 36 | +The following will apply all default profiles to the `gallery` route. |
| 37 | + |
| 38 | +``` php |
| 39 | +// within app/Http/routes.php |
| 40 | + |
| 41 | +Route::get('gallery', ['middleware' => 'secure.content', function () { |
| 42 | + return 'pictures!'; |
| 43 | +}]); |
| 44 | +``` |
| 45 | + |
| 46 | +The following will apply all default profiles and a specific `flickr` profile to the `gallery` route. |
| 47 | + |
| 48 | +``` php |
| 49 | +// within app/Http/routes.php |
| 50 | + |
| 51 | +Route::get('gallery', ['middleware' => 'secure.content:flickr', function () { |
| 52 | + return 'pictures!'; |
| 53 | +}]); |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +### Apply content security policy to controllers |
| 58 | + |
| 59 | +The following will apply all default profiles to all methods within the `GalleryController`. |
| 60 | + |
| 61 | +``` php |
| 62 | +// within app/Http/Controllers/GalleryController.php |
| 63 | + |
| 64 | +public function __construct() |
| 65 | +{ |
| 66 | + $this->middleware('secure.content'); |
| 67 | +} |
| 68 | +``` |
| 69 | +The following will apply all default profiles and a specific `google` profile to all methods within the `GalleryController`. |
| 70 | + |
| 71 | +``` php |
| 72 | +// within app/Http/Controllers/GalleryController.php |
| 73 | + |
| 74 | +public function __construct() |
| 75 | +{ |
| 76 | + $this->middleware('secure.content:google'); |
| 77 | +} |
| 78 | +``` |
| 79 | +You can include any number of specific profiles to any middleware decoration. For instance, the following will apply default, `google`, `flickr`, and `my_custom` profiles to all methods within the `GalleryController`. |
| 80 | + |
| 81 | +``` php |
| 82 | +// within app/Http/Controllers/GalleryController.php |
| 83 | + |
| 84 | +public function __construct() |
| 85 | +{ |
| 86 | + $this->middleware('secure.content:google,flickr,my_custom'); |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +### Create content security profiles |
| 91 | + |
| 92 | +The default location for content security profiles is `security.content`. If you wish to use this default configuration, ensure your project includes the appropriate configuration files. |
| 93 | + |
| 94 | +The structure of this configuration array is important. The middleware expects to find a `default` key with a string value and a `profiles` key with an array value. |
| 95 | + |
| 96 | +``` php |
| 97 | +// within config/security.php |
| 98 | + |
| 99 | +return [ |
| 100 | + 'content' => [ |
| 101 | + 'default' => '', |
| 102 | + 'profiles' => [], |
| 103 | + ], |
| 104 | +]; |
| 105 | + |
| 106 | +``` |
| 107 | +The `profiles` array contains the security profiles for your application. Each profile name must be unique and is expected to have a value of an array. |
| 108 | + |
| 109 | +``` php |
| 110 | +// within config/security.php |
| 111 | + |
| 112 | +return [ |
| 113 | + 'content' => [ |
| 114 | + 'default' => '', |
| 115 | + 'profiles' => [ |
| 116 | + 'profile_one' => [], |
| 117 | + 'profile_two' => [], |
| 118 | + 'profile_three' => [], |
| 119 | + ], |
| 120 | + ], |
| 121 | +]; |
| 122 | + |
| 123 | +``` |
| 124 | +Each profile array should contain keys that correspond to Content Security Policy directives. The value of each of these directives can be a string, comma-separated string, or array of strings. Each string value should correspond to the domain associated with your directive and profile. |
| 125 | + |
| 126 | +``` php |
| 127 | +// within config/security.php |
| 128 | + |
| 129 | +return [ |
| 130 | + 'content' => [ |
| 131 | + 'default' => '', |
| 132 | + 'profiles' => [ |
| 133 | + 'profile_one' => [ |
| 134 | + 'base-uri' => 'https://domain.com,http://google.com', |
| 135 | + ], |
| 136 | + 'profile_two' => [ |
| 137 | + 'font-src' => 'https://domain.com', |
| 138 | + 'base-uri' => [ |
| 139 | + "'self'", |
| 140 | + 'http://google.com' |
| 141 | + ], |
| 142 | + ], |
| 143 | + 'profile_three' => [ |
| 144 | + 'font-src' => [ |
| 145 | + "'self'" |
| 146 | + ], |
| 147 | + ], |
| 148 | + ], |
| 149 | + ], |
| 150 | +]; |
| 151 | + |
| 152 | +``` |
| 153 | +The `default` key value should be a string, comma-separated string, or array of strings that correspond to the unique profile names that you would like to enforce on all responses with minimal content security applied. |
| 154 | + |
| 155 | +``` php |
| 156 | +// within config/security.php |
| 157 | + |
| 158 | +return [ |
| 159 | + 'content' => [ |
| 160 | + 'default' => 'profile_one', |
| 161 | + 'profiles' => [ |
| 162 | + 'profile_one' => [ |
| 163 | + 'base-uri' => 'https://domain.com,http://google.com', |
| 164 | + ], |
| 165 | + 'profile_two' => [ |
| 166 | + 'font-src' => 'https://domain.com', |
| 167 | + 'base-uri' => [ |
| 168 | + "'self'", |
| 169 | + 'http://google.com' |
| 170 | + ], |
| 171 | + ], |
| 172 | + 'profile_three' => [ |
| 173 | + 'font-src' => [ |
| 174 | + "'self'" |
| 175 | + ], |
| 176 | + ], |
| 177 | + ], |
| 178 | + ], |
| 179 | +]; |
| 180 | + |
| 181 | +``` |
| 182 | + |
| 183 | +Here is a real-world example: |
| 184 | + |
| 185 | +``` php |
| 186 | +// within config/security.php |
| 187 | + |
| 188 | +return [ |
| 189 | + 'content' => [ |
| 190 | + 'default' => 'global', |
| 191 | + 'profiles' => [ |
| 192 | + 'global' => [ |
| 193 | + 'base-uri' => "'self'", |
| 194 | + 'font-src' => [ |
| 195 | + "'self'", |
| 196 | + 'fonts.gstatic.com' |
| 197 | + ], |
| 198 | + 'img-src' => "'self'", |
| 199 | + 'script-src' => "'self'", |
| 200 | + 'style-src' => [ |
| 201 | + "'self'", |
| 202 | + "'unsafe-inline'", |
| 203 | + 'fonts.googleapis.com' |
| 204 | + ], |
| 205 | + ], |
| 206 | + 'flickr' => [ |
| 207 | + 'img-src' => [ |
| 208 | + 'https://*.staticflickr.com', |
| 209 | + ], |
| 210 | + ], |
| 211 | + ], |
| 212 | + ], |
| 213 | +]; |
| 214 | + |
| 215 | +``` |
| 216 | + |
| 217 | +## Testing |
| 218 | + |
| 219 | +``` bash |
| 220 | +$ ./vendor/bin/phpunit |
| 221 | +``` |
| 222 | + |
| 223 | +## Contributing |
| 224 | + |
| 225 | +Please see [CONTRIBUTING](https://github.com/stevenmaguire/laravel-middleware-csp/blob/master/CONTRIBUTING.md) for details. |
| 226 | + |
| 227 | +## Credits |
| 228 | + |
| 229 | +- [Steven Maguire](https://github.com/stevenmaguire) |
| 230 | +- [All Contributors](https://github.com/stevenmaguire/laravel-middleware-csp/contributors) |
| 231 | + |
| 232 | +## License |
| 233 | + |
| 234 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments