在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):spatie/laravel-csp开源软件地址(OpenSource Url):https://github.com/spatie/laravel-csp开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Set content security policy headers in a Laravel appBy default all scripts on a webpage are allowed to send and fetch data to any site they want. This can be a security problem. Imagine one of your JavaScript dependencies sends all keystrokes, including passwords, to a third party website. It's very easy for someone to hide this malicious behaviour, making it nearly impossible for you to detect it (unless you manually read all the JavaScript code on your site). For a better idea of why you really need to set content security policy headers read this excellent blog post by David Gilbertson. Setting Content Security Policy headers helps solve this problem. These headers dictate which sites your site is allowed to contact. This package makes it easy for you to set the right headers. This readme does not aim to fully explain all the possible usages of CSP and it's directives. We highly recommend that you read Mozilla's documentation on the Content Security Policy) before using this package. If you're an audio visual learner you should check out this video on how to use this package. https://www.laraning.com/videos/spatie-csp-content-security-policy Support usWe invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products. We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall. InstallationYou can install the package via composer: composer require spatie/laravel-csp You can publish the config-file with: php artisan vendor:publish --tag=csp-config This is the contents of the file which will be published at return [
/*
* A policy will determine which CSP headers will be set. A valid CSP policy is
* any class that extends `Spatie\Csp\Policies\Policy`
*/
'policy' => Spatie\Csp\Policies\Basic::class,
/*
* This policy which will be put in report only mode. This is great for testing out
* a new policy or changes to existing csp policy without breaking anything.
*/
'report_only_policy' => '',
/*
* All violations against the policy will be reported to this url.
* A great service you could use for this is https://report-uri.com/
*
* You can override this setting by calling `reportTo` on your policy.
*/
'report_uri' => env('CSP_REPORT_URI', ''),
/*
* Headers will only be added if this setting is set to true.
*/
'enabled' => env('CSP_ENABLED', true),
/*
* The class responsible for generating the nonces used in inline tags and headers.
*/
'nonce_generator' => Spatie\Csp\Nonce\RandomString::class,
]; You can add CSP headers to all responses of your app by registering // app/Http/Kernel.php
...
protected $middlewareGroups = [
'web' => [
...
\Spatie\Csp\AddCspHeaders::class,
], Alternatively you can apply the middleware on the route or route group level. // in a routes file
Route::get('my-page', 'MyController')->middleware(Spatie\Csp\AddCspHeaders::class); You can also pass a policy class as a parameter to the middleware: // in a routes file
Route::get('my-page', 'MyController')->middleware(Spatie\Csp\AddCspHeaders::class . ':' . MyPolicy::class); The given policy will override the one configured in the config file for that specific route or group of routes. UsageThis package allows you to define CSP policies. A CSP policy determines which CSP directives will be set in the headers of the response. An example of a CSP directive is According to the spec certain directive values need to be surrounded by quotes. Examples of this are // in a policy
...
->addDirective(Directive::SCRIPT, Keyword::SELF) // will output `'self'` when outputting headers
->addDirective(Directive::STYLE, 'sha256-hash') // will output `'sha256-hash'` when outputting headers
... You can add multiple policy options in the same directive giving an array as second parameter to // in a policy
...
->addDirective(Directive::SCRIPT, [
Keyword::STRICT_DYNAMIC,
Keyword::SELF,
'www.google.com',
])
->addDirective(Directive::SCRIPT, 'strict-dynamic self www.google.com')
// will both output `'strict_dynamic' 'self' www.google.com` when outputting headers
... There are also a few cases where you don't have to or don't need to specify a value, eg. upgrade-insecure-requests, block-all-mixed-content, ... In this case you can use the following value: // in a policy
...
->addDirective(Directive::UPGRADE_INSECURE_REQUESTS, Value::NO_VALUE)
->addDirective(Directive::BLOCK_ALL_MIXED_CONTENT, Value::NO_VALUE);
... This will output a CSP like this:
Creating policiesIn the namespace Spatie\Csp\Policies;
use Spatie\Csp\Directive;
use Spatie\Csp\Value;
class Basic extends Policy
{
public function configure()
{
$this
->addDirective(Directive::BASE, Keyword::SELF)
->addDirective(Directive::CONNECT, Keyword::SELF)
->addDirective(Directive::DEFAULT, Keyword::SELF)
->addDirective(Directive::FORM_ACTION, Keyword::SELF)
->addDirective(Directive::IMG, Keyword::SELF)
->addDirective(Directive::MEDIA, Keyword::SELF)
->addDirective(Directive::OBJECT, Keyword::NONE)
->addDirective(Directive::SCRIPT, Keyword::SELF)
->addDirective(Directive::STYLE, Keyword::SELF)
->addNonceForDirective(Directive::SCRIPT)
->addNonceForDirective(Directive::STYLE);
}
} You can allow fetching scripts from namespace App\Services\Csp\Policies;
use Spatie\Csp\Directive;
use Spatie\Csp\Policies\Basic;
class MyCustomPolicy extends Basic
{
public function configure()
{
parent::configure();
$this->addDirective(Directive::SCRIPT, 'www.google.com');
}
} Don't forget to set the Using inline scripts and stylesWhen using CSP you must specifically allow the use of inline scripts or styles. The recommended way of doing that with this package is to use a First you must add the nonce to the right directives in your policy: // in a policy
public function configure()
{
$this
->addDirective(Directive::SCRIPT, 'self')
->addDirective(Directive::STYLE, 'self')
->addNonceForDirective(Directive::SCRIPT)
->addNonceForDirective(Directive::STYLE)
...
} Next you must add the nonce to the html:
There are few other options to use inline styles and scripts. Take a look at the CSP docs on the Mozilla developer site to know more. Reporting CSP errorsIn the browserInstead of outright blocking all violations you can put a policy in report only mode. In this case all requests will be made, but all violations will display in your favourite browser's console. To put a policy in report only mode just call public function configure()
{
parent::configure();
$this->reportOnly();
} To an external urlAny violations against to the policy can be reported to a given url. You can set that url in the Using multiple policiesTo test changes to your CSP policy you can specify a second policy in the Using whoopsLaravel comes with whoops, an error handling framework that helps you debug your application with a pretty visualization of exceptions. Whoops uses inline scripts and styles because it can't make any assumptions about the environment it is being used in, so it won't work unless you allow One approach to this problem is to check $this->container->singleton(AppPolicy::class, function ($app) {
return new AppPolicy();
});
app(AppPolicy::class)->addDirective(Directive::SCRIPT, Keyword::UNSAFE_INLINE);
app(AppPolicy::class)->addDirective(Directive::STYLE, Keyword::UNSAFE_INLINE); where Note that Another approach is to overwrite the namespace App\Services\Csp\Policies;
use Illuminate\Http\Request;
use Spatie\Csp;
use Symfony\Component\HttpFoundation\Response;
class MyCustomPolicy extends Csp\Policies\Policy
{
public function configure()
{
// Add directives
}
public function shouldBeApplied(Request $request, Response $response): bool
{
if (config('app.debug') && ($response->isClientError() || $response->isServerError())) {
return false;
}
return parent::shouldBeApplied($request, $response);
}
} This approach completely deactivates the CSP and therefore also works if a strict CSP is used. TestingYou can run all the tests with: composer test ChangelogPlease see CHANGELOG for more information what has changed recently. ContributingPlease see CONTRIBUTING for details. SecurityIf you've found a bug regarding security please mail [email protected] instead of using the issue tracker. CreditsLicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论