Skip to content

Commit 572752e

Browse files
authored
Merge pull request #7 from laravel-shift/l11-compatibility
Laravel 11.x Compatibility
2 parents 0379f9b + 72d4d83 commit 572752e

File tree

9 files changed

+79
-60
lines changed

9 files changed

+79
-60
lines changed

.github/workflows/run-tests.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@ name: run-tests
22

33
on:
44
push:
5-
branches: [main]
5+
branches:
6+
- main
67
pull_request:
7-
branches: [main]
8+
branches:
9+
- main
810

911
jobs:
1012
test:
1113
runs-on: ${{ matrix.os }}
14+
1215
strategy:
1316
fail-fast: true
1417
matrix:
1518
os: [ubuntu-latest]
16-
php: [8.3, 8.2, 8.1]
17-
laravel: [10.*]
19+
php: [8.3, 8.2]
20+
laravel: ["10.*", "11.*"]
1821
stability: [prefer-lowest, prefer-stable]
1922
include:
2023
- laravel: 10.*
2124
testbench: ^8.0
25+
- laravel: 11.*
26+
testbench: ^9.0
2227

2328
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
2429

README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
Laravel Middleware to protect your app against Cross-site scripting (XSS). It sanitizes request input by utilising the [Laravel Security](https://github.com/GrahamCampbell/Laravel-Security) package, and it can sanatize [Blade echo statements](https://laravel.com/docs/8.x/blade#displaying-data) as well.
88

9-
* PHP 8.1 and higher
10-
* Laravel 10
9+
* PHP 8.2 and higher
10+
* Laravel 10 and higher
1111

1212
## Sponsor this package!
1313

@@ -108,6 +108,21 @@ Event::listen(function (MaliciousInputFound $event) {
108108
});
109109
```
110110

111+
### Additional configuration for `voku/anti-xss`
112+
113+
As of version 1.6.0, you may provide additional configuration for the `voku/anti-xss` package. You may do this by filling the `middleware.anti_xss` key. This is similar to the [Laravel Security](https://github.com/GrahamCampbell/Laravel-Security) package, which this package used to rely on.
114+
115+
```php
116+
'anti_xss' => [
117+
'evil' => [
118+
'attributes' => ['href'],
119+
'tags' => ['video'],
120+
],
121+
122+
'replacement' => '*redacted*',
123+
]
124+
```
125+
111126
## Changelog
112127

113128
Please see [CHANGELOG](CHANGELOG.md) for more information about what has changed recently.
@@ -124,17 +139,15 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
124139

125140
## Other Laravel packages
126141

127-
* [`Laravel Analytics Event Tracking`](https://github.com/protonemedia/laravel-analytics-event-tracking): Laravel package to easily send events to Google Analytics.
128142
* [`Laravel Blade On Demand`](https://github.com/protonemedia/laravel-blade-on-demand): Laravel package to compile Blade templates in memory.
129143
* [`Laravel Cross Eloquent Search`](https://github.com/protonemedia/laravel-cross-eloquent-search): Laravel package to search through multiple Eloquent models.
130144
* [`Laravel Eloquent Scope as Select`](https://github.com/protonemedia/laravel-eloquent-scope-as-select): Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes and constraints by adding them as a subquery.
131-
* [`Laravel Eloquent Where Not`](https://github.com/protonemedia/laravel-eloquent-where-not): This Laravel package allows you to flip/invert an Eloquent scope, or really any query constraint.
132-
* [`Laravel FFMpeg`](https://github.com/protonemedia/laravel-ffmpeg): This package provides integration with FFmpeg for Laravel. The storage of the files is handled by Laravel's Filesystem.
133-
* [`Laravel Form Components`](https://github.com/protonemedia/laravel-form-components): Blade components to rapidly build forms with Tailwind CSS Custom Forms and Bootstrap 4. Supports validation, model binding, default values, translations, includes default vendor styling and fully customizable!
145+
* [`Laravel FFMpeg`](https://github.com/protonemedia/laravel-ffmpeg): This package provides an integration with FFmpeg for Laravel. The storage of the files is handled by Laravel's Filesystem.
146+
* [`Laravel MinIO Testing Tools`](https://github.com/protonemedia/laravel-minio-testing-tools): Run your tests against a MinIO S3 server.
134147
* [`Laravel Mixins`](https://github.com/protonemedia/laravel-mixins): A collection of Laravel goodies.
135148
* [`Laravel Paddle`](https://github.com/protonemedia/laravel-paddle): Paddle.com API integration for Laravel with support for webhooks/events.
149+
* [`Laravel Task Runner`](https://github.com/protonemedia/laravel-task-runner): Write Shell scripts like Blade Components and run them locally or on a remote server.
136150
* [`Laravel Verify New Email`](https://github.com/protonemedia/laravel-verify-new-email): This package adds support for verifying new email addresses: when a user updates its email address, it won't replace the old one until the new one is verified.
137-
* [`Laravel WebDAV`](https://github.com/protonemedia/laravel-webdav): WebDAV driver for Laravel's Filesystem.
138151

139152
## Security
140153

composer.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^8.1|^8.2|^8.3",
20-
"graham-campbell/security": "^11.0",
21-
"illuminate/contracts": "^10.0",
19+
"php": "^8.2|^8.3",
20+
"graham-campbell/security-core": "^4.0",
21+
"illuminate/contracts": "^10.0|^11.0",
2222
"spatie/laravel-package-tools": "^1.9.2"
2323
},
2424
"require-dev": {
25-
"nunomaduro/collision": "^7.0",
26-
"orchestra/testbench": "^8.0",
25+
"laravel/pint": "^1.14",
26+
"nunomaduro/collision": "^7.0|^8.0",
27+
"orchestra/testbench": "^8.0|^9.0",
2728
"pestphp/pest": "^2.0",
2829
"pestphp/pest-plugin-laravel": "^2.0",
2930
"phpunit/phpunit": "^10.4"
@@ -58,4 +59,4 @@
5859
},
5960
"minimum-stability": "dev",
6061
"prefer-stable": true
61-
}
62+
}

config/xss-protection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,15 @@
1818

1919
'dispatch_event_on_malicious_input' => false,
2020
],
21+
22+
// Additional configuration for the underlying voku/anti-xss package
23+
// See: https://github.com/GrahamCampbell/Laravel-Security/blob/11.1/config/security.php
24+
'anti_xss' => [
25+
'evil' => [
26+
'attributes' => null,
27+
'tags' => null,
28+
],
29+
30+
'replacement' => null,
31+
],
2132
];

src/Events/MaliciousInputFound.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public function __construct(
1010
public array $sanitizedKeys,
1111
public Request $originalRequest,
1212
public Request $sanitizedRequest
13-
)
14-
{
13+
) {
1514
}
1615
}

src/Middleware/XssCleanInput.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,6 @@
1111

1212
class XssCleanInput extends TransformsRequest
1313
{
14-
/**
15-
* The security instance.
16-
*
17-
* @var \GrahamCampbell\SecurityCore\Security
18-
*/
19-
protected $security;
20-
21-
/**
22-
* The Blade echo cleaner instance.
23-
*
24-
* @var \ProtoneMedia\LaravelXssProtection\Cleaners\BladeEchoes
25-
*/
26-
protected $bladeEchoCleaner;
27-
2814
/**
2915
* All of the registered skip callbacks.
3016
*
@@ -63,22 +49,20 @@ class XssCleanInput extends TransformsRequest
6349
/**
6450
* Create a new instance.
6551
*
66-
* @param \GrahamCampbell\SecurityCore\Security $security
67-
* @param \ProtoneMedia\LaravelXssProtection\Cleaners\BladeEchoes $bladeEchoCleaner
6852
*
6953
* @return void
7054
*/
71-
public function __construct(Security $security, BladeEchoes $bladeEchoCleaner)
72-
{
73-
$this->security = $security;
74-
$this->bladeEchoCleaner = $bladeEchoCleaner;
55+
public function __construct(
56+
protected Security $security,
57+
protected BladeEchoes $bladeEchoCleaner
58+
) {
59+
//
7560
}
7661

7762
/**
7863
* Handle an incoming request.
7964
*
8065
* @param \Illuminate\Http\Request $request
81-
* @param \Closure $next
8266
* @return mixed
8367
*/
8468
public function handle($request, Closure $next)
@@ -149,7 +133,7 @@ protected function transform($key, $value)
149133

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

152-
if (!$this->enabledInConfig('allow_blade_echoes')) {
136+
if (! $this->enabledInConfig('allow_blade_echoes')) {
153137
$output = $this->bladeEchoCleaner->clean((string) $output);
154138
}
155139

@@ -165,8 +149,7 @@ protected function transform($key, $value)
165149
/**
166150
* Returns a boolean whether an option has been enabled.
167151
*
168-
* @param string $key
169-
* @return boolean
152+
* @param string $key
170153
*/
171154
private function enabledInConfig($key): bool
172155
{
@@ -176,7 +159,6 @@ private function enabledInConfig($key): bool
176159
/**
177160
* Register a callback that instructs the middleware to be skipped.
178161
*
179-
* @param \Closure $callback
180162
* @return void
181163
*/
182164
public static function skipWhen(Closure $callback)
@@ -187,7 +169,6 @@ public static function skipWhen(Closure $callback)
187169
/**
188170
* Register a callback that instructs the middleware to be skipped.
189171
*
190-
* @param \Closure $callback
191172
* @return void
192173
*/
193174
public static function skipKeyWhen(Closure $callback)

src/ServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ProtoneMedia\LaravelXssProtection;
44

5+
use GrahamCampbell\SecurityCore\Security;
56
use Spatie\LaravelPackageTools\Package;
67
use Spatie\LaravelPackageTools\PackageServiceProvider;
78

@@ -18,4 +19,12 @@ public function configurePackage(Package $package): void
1819
->name('laravel-xss-protection')
1920
->hasConfigFile();
2021
}
22+
23+
public function packageBooted()
24+
{
25+
$this->app->singleton(Security::class, fn () => Security::create(
26+
config('xss-protection.anti_xss.evil'),
27+
config('xss-protection.anti_xss.replacement')
28+
));
29+
}
2130
}

tests/MiddlewareTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@
6161

6262
it('doesnt interfere with booleans, numbers and null values', function () {
6363
$request = Request::createFromGlobals()->merge([
64-
'yes' => true,
65-
'no' => false,
66-
'one' => 1,
67-
'pi' => 3.14,
64+
'yes' => true,
65+
'no' => false,
66+
'one' => 1,
67+
'pi' => 3.14,
6868
'null' => null,
6969
]);
7070

@@ -143,11 +143,11 @@ class ExceptXssCleanInput extends XssCleanInput
143143
}
144144

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

149149
'nested' => [
150-
'key' => 'test<script>script</script>',
150+
'key' => 'test<script>script</script>',
151151
'allowed' => 'test<script>script</script>',
152152
],
153153
]);
@@ -166,12 +166,12 @@ class ExceptXssCleanInput extends XssCleanInput
166166
it('can trim blade echoes', function () {
167167
$request = Request::createFromGlobals()->merge([
168168
'key' => 'test',
169-
'a' => '{{ $test }}',
170-
'b' => '{!! $test !!}',
171-
'c' => '{{{ $test }}}',
172-
'd' => 'd{{ $test }}',
173-
'e' => 'e{!! $test !!}',
174-
'f' => 'f{{{ $test }}}',
169+
'a' => '{{ $test }}',
170+
'b' => '{!! $test !!}',
171+
'c' => '{{{ $test }}}',
172+
'd' => 'd{{ $test }}',
173+
'e' => 'e{!! $test !!}',
174+
'f' => 'f{{{ $test }}}',
175175
]);
176176

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

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

204204
'nested' => [
205-
'key' => 'test<script>script</script>',
205+
'key' => 'test<script>script</script>',
206206
'allowed' => 'test<script>script</script>',
207207
],
208208
]);

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ protected function setUp(): void
1313
parent::setUp();
1414

1515
Factory::guessFactoryNamesUsing(
16-
fn (string $modelName) => 'ProtoneMedia\\LaravelXssProtection\\Database\\Factories\\' . class_basename($modelName) . 'Factory'
16+
fn (string $modelName) => 'ProtoneMedia\\LaravelXssProtection\\Database\\Factories\\'.class_basename($modelName).'Factory'
1717
);
1818
}
1919

0 commit comments

Comments
 (0)