Skip to content

Commit

Permalink
Use graham-campbell/security-core instead of `graham-campbell/secur…
Browse files Browse the repository at this point in the history
…ity`
  • Loading branch information
pascalbaljet committed Mar 14, 2024
1 parent 35a20b4 commit be2e5ba
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 45 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
}
],
"require": {
"php": "^8.1|^8.2|^8.3",
"graham-campbell/security": "^11.0",
"php": "^8.2|^8.3",
"graham-campbell/security-core": "^4.0",
"illuminate/contracts": "^10.0|^11.0",
"spatie/laravel-package-tools": "^1.9.2"
},
"require-dev": {
"laravel/pint": "^1.14",
"nunomaduro/collision": "^7.0|^8.0",
"orchestra/testbench": "^8.0|^9.0",
"pestphp/pest": "^2.0",
Expand Down
11 changes: 11 additions & 0 deletions config/xss-protection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@

'dispatch_event_on_malicious_input' => false,
],

// Additional configuration for the underlying voku/anti-xss package
// See: https://github.com/GrahamCampbell/Laravel-Security/blob/11.1/config/security.php
'anti_xss' => [
'evil' => [
'attributes' => null,
'tags' => null,
],

'replacement' => null,
],
];
3 changes: 1 addition & 2 deletions src/Events/MaliciousInputFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public function __construct(
public array $sanitizedKeys,
public Request $originalRequest,
public Request $sanitizedRequest
)
{
) {
}
}
33 changes: 7 additions & 26 deletions src/Middleware/XssCleanInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@

class XssCleanInput extends TransformsRequest
{
/**
* The security instance.
*
* @var \GrahamCampbell\SecurityCore\Security
*/
protected $security;

/**
* The Blade echo cleaner instance.
*
* @var \ProtoneMedia\LaravelXssProtection\Cleaners\BladeEchoes
*/
protected $bladeEchoCleaner;

/**
* All of the registered skip callbacks.
*
Expand Down Expand Up @@ -63,22 +49,20 @@ class XssCleanInput extends TransformsRequest
/**
* Create a new instance.
*
* @param \GrahamCampbell\SecurityCore\Security $security
* @param \ProtoneMedia\LaravelXssProtection\Cleaners\BladeEchoes $bladeEchoCleaner
*
* @return void
*/
public function __construct(Security $security, BladeEchoes $bladeEchoCleaner)
{
$this->security = $security;
$this->bladeEchoCleaner = $bladeEchoCleaner;
public function __construct(
protected Security $security,
protected BladeEchoes $bladeEchoCleaner
) {
//
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
Expand Down Expand Up @@ -149,7 +133,7 @@ protected function transform($key, $value)

$output = $this->security->clean((string) $value);

if (!$this->enabledInConfig('allow_blade_echoes')) {
if (! $this->enabledInConfig('allow_blade_echoes')) {
$output = $this->bladeEchoCleaner->clean((string) $output);
}

Expand All @@ -165,8 +149,7 @@ protected function transform($key, $value)
/**
* Returns a boolean whether an option has been enabled.
*
* @param string $key
* @return boolean
* @param string $key
*/
private function enabledInConfig($key): bool
{
Expand All @@ -176,7 +159,6 @@ private function enabledInConfig($key): bool
/**
* Register a callback that instructs the middleware to be skipped.
*
* @param \Closure $callback
* @return void
*/
public static function skipWhen(Closure $callback)
Expand All @@ -187,7 +169,6 @@ public static function skipWhen(Closure $callback)
/**
* Register a callback that instructs the middleware to be skipped.
*
* @param \Closure $callback
* @return void
*/
public static function skipKeyWhen(Closure $callback)
Expand Down
9 changes: 9 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ProtoneMedia\LaravelXssProtection;

use GrahamCampbell\SecurityCore\Security;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -18,4 +19,12 @@ public function configurePackage(Package $package): void
->name('laravel-xss-protection')
->hasConfigFile();
}

public function packageBooted()
{
$this->app->singleton(Security::class, fn () => Security::create(
config('xss-protection.anti_xss.evil'),
config('xss-protection.anti_xss.replacement')
));
}
}
28 changes: 14 additions & 14 deletions tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@

it('doesnt interfere with booleans, numbers and null values', function () {
$request = Request::createFromGlobals()->merge([
'yes' => true,
'no' => false,
'one' => 1,
'pi' => 3.14,
'yes' => true,
'no' => false,
'one' => 1,
'pi' => 3.14,
'null' => null,
]);

Expand Down Expand Up @@ -143,11 +143,11 @@ class ExceptXssCleanInput extends XssCleanInput
}

$request = Request::createFromGlobals()->merge([
'key' => 'test<script>script</script>',
'key' => 'test<script>script</script>',
'allow' => 'test<script>script</script>',

'nested' => [
'key' => 'test<script>script</script>',
'key' => 'test<script>script</script>',
'allowed' => 'test<script>script</script>',
],
]);
Expand All @@ -166,12 +166,12 @@ class ExceptXssCleanInput extends XssCleanInput
it('can trim blade echoes', function () {
$request = Request::createFromGlobals()->merge([
'key' => 'test',
'a' => '{{ $test }}',
'b' => '{!! $test !!}',
'c' => '{{{ $test }}}',
'd' => 'd{{ $test }}',
'e' => 'e{!! $test !!}',
'f' => 'f{{{ $test }}}',
'a' => '{{ $test }}',
'b' => '{!! $test !!}',
'c' => '{{{ $test }}}',
'd' => 'd{{ $test }}',
'e' => 'e{!! $test !!}',
'f' => 'f{{{ $test }}}',
]);

config(['xss-protection.middleware.completely_replace_malicious_input' => false]);
Expand All @@ -198,11 +198,11 @@ class ExceptXssCleanInput extends XssCleanInput
});

$request = Request::createFromGlobals()->merge([
'key' => 'test<script>script</script>',
'key' => 'test<script>script</script>',
'allow' => 'test<script>script</script>',

'nested' => [
'key' => 'test<script>script</script>',
'key' => 'test<script>script</script>',
'allowed' => 'test<script>script</script>',
],
]);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected function setUp(): void
parent::setUp();

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'ProtoneMedia\\LaravelXssProtection\\Database\\Factories\\' . class_basename($modelName) . 'Factory'
fn (string $modelName) => 'ProtoneMedia\\LaravelXssProtection\\Database\\Factories\\'.class_basename($modelName).'Factory'
);
}

Expand Down

0 comments on commit be2e5ba

Please sign in to comment.