Skip to content

Commit b275c47

Browse files
committed
Event improvement
1 parent 2d21955 commit b275c47

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,18 @@ Instead of transforming malicious input, you may configure the middleware to ter
8181

8282
### Dispatch event
8383

84-
You may configure the middleware to dispatch an event whenever malicious input has been found. Setting the `middleware.dispatch_event_on_malicious_input` to `true` will dispatch an `ProtoneMedia\LaravelXssProtection\Events\MaliciousInputFound` event with the malicious keys and full request.
84+
You may configure the middleware to dispatch an event whenever malicious input has been found. Setting the `middleware.dispatch_event_on_malicious_input` to `true` will dispatch an `ProtoneMedia\LaravelXssProtection\Events\MaliciousInputFound` event with the sanitized keys, the original request and the sanitized request.
85+
86+
```php
87+
use Illuminate\Support\Facades\Event;
88+
use ProtoneMedia\LaravelXssProtection\Events\MaliciousInputFound;
89+
90+
Event::listen(function (MaliciousInputFound $event) {
91+
$event->sanitizedKeys;
92+
$event->originalRequest;
93+
$event->sanitizedRequest;
94+
});
95+
```
8596

8697
## Changelog
8798

src/Events/MaliciousInputFound.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
class MaliciousInputFound
88
{
9-
public function __construct(public array $keys, public Request $request)
9+
public function __construct(
10+
public array $sanitizedKeys,
11+
public Request $originalRequest,
12+
public Request $sanitizedRequest
13+
)
1014
{
1115
}
1216
}

src/Middleware/XssCleanInput.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ public function handle($request, Closure $next)
7979
}
8080
}
8181

82+
$originalRequest = clone $request;
83+
8284
$this->clean($request);
8385

8486
if (count($this->sanitizedKeys) === 0) {
8587
return $next($request);
8688
}
8789

8890
if ($this->enabledInConfig('dispatch_event_on_malicious_input')) {
89-
event(new MaliciousInputFound($this->sanitizedKeys, $request));
91+
event(new MaliciousInputFound($this->sanitizedKeys, $originalRequest, $request));
9092
}
9193

9294
if ($this->enabledInConfig('terminate_request_on_malicious_input')) {

tests/MiddlewareTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
$middleware->handle($request, fn ($request) => $request);
3636

3737
Event::assertDispatched(function (MaliciousInputFound $event) use ($request) {
38-
return $event->request === $request && $event->keys === ['key'];
38+
return $event->sanitizedRequest === $request
39+
&& $event->originalRequest->input('key') === 'test<script>script</script>'
40+
&& $event->sanitizedKeys === ['key'];
3941
});
4042
});
4143

0 commit comments

Comments
 (0)