-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6237431
commit b31476b
Showing
6 changed files
with
112 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
<?php | ||
|
||
namespace Spatie\Csp\Profiles\Profile; | ||
namespace Spatie\Csp\Profiles; | ||
|
||
use Spatie\Csp\Directive; | ||
|
||
class Basic extends Profile | ||
{ | ||
public function registerDirectives() | ||
{ | ||
$this | ||
->addDirective(Directive::DEFAULT, 'none') | ||
->addDirective(Directive::CONNECT, 'self') | ||
->addDirective(Directive::FORM, 'self') | ||
->addDirective(Directive::IMG, 'self') | ||
->addDirective(Directive::SCRIPT, 'self') | ||
->addDirective(Directive::STYLE, 'self') | ||
->addDirective(Directive::MEDIA, 'self'); | ||
->addDirective(Directive::CONNECT, "'self'") | ||
->addDirective(Directive::DEFAULT, "'self'") | ||
->addDirective(Directive::FORM, "'self'") | ||
->addDirective(Directive::IMG, "'self'") | ||
->addDirective(Directive::MEDIA, "'self'") | ||
->addDirective(Directive::SCRIPT, "'self'") | ||
->addDirective(Directive::STYLE, "'self'"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Spatie\Csp\Tests; | ||
|
||
use Illuminate\Support\Facades\Route; | ||
use Spatie\Csp\AddCspHeaders; | ||
use Symfony\Component\HttpFoundation\HeaderBag; | ||
|
||
class AddCspHeadersTest extends TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
Route::get('test', function () { | ||
return 'ok'; | ||
})->middleware(AddCspHeaders::class); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_set_the_basic_csp_headers() | ||
{ | ||
$headers = $this->getResponseHeaders(); | ||
|
||
$this->assertContains("default-src 'self';", $headers->get('Content-Security-Policy')); | ||
} | ||
|
||
/** @test */ | ||
public function it_wont_set_any_headers_if_not_enabled_in_the_config() | ||
{ | ||
config(['csp.enabled' => false]); | ||
|
||
$headers = $this->getResponseHeaders(); | ||
|
||
$this->assertNull($headers->get('Content-Security-Policy')); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_be_set_in_report_only_mode_via_the_config() | ||
{ | ||
config(['csp.report_only' => true]); | ||
|
||
$headers = $this->getResponseHeaders(); | ||
|
||
$this->assertNotNull($headers->get('Content-Security-Policy-Report-Only')); | ||
|
||
$this->assertNull($headers->get('Content-Security-Policy')); | ||
} | ||
|
||
/** @test */ | ||
public function a_report_uri_can_be_set_in_the_config() | ||
{ | ||
config(['csp.report_uri' => 'https://report-uri.com']); | ||
|
||
$headers = $this->getResponseHeaders(); | ||
|
||
$this | ||
->assertCspHeaderContains($headers, 'report-uri https://report-uri.com;') | ||
->assertCspHeaderContains($headers, 'report-to {"url":"https:\/\/report-uri.com","group-name":"Basic","max-age":18144000};'); | ||
} | ||
|
||
protected function assertCspHeaderContains(HeaderBag $headerBag, string $needle): self | ||
{ | ||
$this->assertContains($needle, $headerBag->get('Content-Security-Policy')); | ||
|
||
return $this; | ||
} | ||
|
||
protected function getResponseHeaders(): HeaderBag | ||
{ | ||
return $this | ||
->get('test') | ||
->assertSuccessful() | ||
->headers; | ||
} | ||
} |