Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jan 13, 2022
1 parent e08dd17 commit b4a14ff
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 37 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.1, 8.0, 7.4]
laravel: [8.*]
php: [8.1, 8.0]
laravel: [9.*, 8.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
testbench: 7.*
- laravel: 8.*
testbench: ^6.23

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.2|^8.0",
"illuminate/http": "^8.0",
"illuminate/support": "^8.0"
"php": "^8.0",
"illuminate/http": "^8.0|^9.0",
"illuminate/support": "^8.0|^9.0",
"spatie/laravel-package-tools": "^1.11"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.2",
"mockery/mockery": "^1.3.3",
"orchestra/testbench": "^6.0",
"orchestra/testbench": "^6.0|^7.0",
"phpunit/phpunit": "^9.4",
"roave/security-advisories": "dev-master"
},
Expand Down
2 changes: 1 addition & 1 deletion src/AddCspHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class AddCspHeaders
{
public function handle(Request $request, Closure $next, $customPolicyClass = null)
public function handle(Request $request, Closure $next, string $customPolicyClass = null)
{
$response = $next($request);

Expand Down
22 changes: 10 additions & 12 deletions src/CspServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@

use Illuminate\Support\ServiceProvider;
use Spatie\Csp\Nonce\NonceGenerator;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class CspServiceProvider extends ServiceProvider
class CspServiceProvider extends PackageServiceProvider
{
public function boot()
public function configurePackage(Package $package): void
{
if ($this->app->runningInConsole() && function_exists('config_path')) {
$this->publishes([
__DIR__.'/../config/csp.php' => config_path('csp.php'),
], 'config');
}
$package
->name('laravel-csp')
->hasConfigFile();
}

public function packageBooted()
{
$this->app->singleton(NonceGenerator::class, config('csp.nonce_generator'));

$this->app->singleton('csp-nonce', function () {
Expand All @@ -25,9 +28,4 @@ public function boot()
return '<?php echo "nonce=\"" . csp_nonce() . "\""; ?>';
});
}

public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/csp.php', 'csp');
}
}
4 changes: 2 additions & 2 deletions src/Exceptions/InvalidCspPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

class InvalidCspPolicy extends Exception
{
public static function create($class): self
public static function create(object $class): self
{
$className = get_class($class);
$className = $class::class;

return new self("The CSP class `{$className}` is not valid. A valid policy extends ".Policy::class);
}
Expand Down
23 changes: 8 additions & 15 deletions src/Policies/Policy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,13 @@

abstract class Policy
{
protected $directives = [];
protected array $directives = [];

protected $reportOnly = false;
protected bool $reportOnly = false;

abstract public function configure();

/**
* @param string $directive
* @param string|array|bool $values
*
* @return \Spatie\Csp\Policies\Policy
*
* @throws \Spatie\Csp\Exceptions\InvalidDirective
* @throws \Spatie\Csp\Exceptions\InvalidValueSet
*/
public function addDirective(string $directive, $values): self
public function addDirective(string $directive, string|array|bool $values): self
{
$this->guardAgainstInvalidDirectives($directive);
$this->guardAgainstInvalidValues(Arr::wrap($values));
Expand All @@ -41,9 +32,11 @@ public function addDirective(string $directive, $values): self
return $this;
}

$values = array_filter(Arr::flatten(array_map(function ($value) {
return explode(' ', $value);
}, Arr::wrap($values))));
$values = array_filter(
Arr::flatten(
array_map(fn ($value) => explode(' ', $value), Arr::wrap($values))
)
);

if (in_array(Keyword::NONE, $values, true)) {
$this->directives[$directive] = [$this->sanitizeValue(Keyword::NONE)];
Expand Down

0 comments on commit b4a14ff

Please sign in to comment.