Skip to content

Commit

Permalink
Event improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Feb 2, 2022
1 parent 2d21955 commit b275c47
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,18 @@ Instead of transforming malicious input, you may configure the middleware to ter

### Dispatch event

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.
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.

```php
use Illuminate\Support\Facades\Event;
use ProtoneMedia\LaravelXssProtection\Events\MaliciousInputFound;

Event::listen(function (MaliciousInputFound $event) {
$event->sanitizedKeys;
$event->originalRequest;
$event->sanitizedRequest;
});
```

## Changelog

Expand Down
6 changes: 5 additions & 1 deletion src/Events/MaliciousInputFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

class MaliciousInputFound
{
public function __construct(public array $keys, public Request $request)
public function __construct(
public array $sanitizedKeys,
public Request $originalRequest,
public Request $sanitizedRequest
)
{
}
}
4 changes: 3 additions & 1 deletion src/Middleware/XssCleanInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ public function handle($request, Closure $next)
}
}

$originalRequest = clone $request;

$this->clean($request);

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

if ($this->enabledInConfig('dispatch_event_on_malicious_input')) {
event(new MaliciousInputFound($this->sanitizedKeys, $request));
event(new MaliciousInputFound($this->sanitizedKeys, $originalRequest, $request));
}

if ($this->enabledInConfig('terminate_request_on_malicious_input')) {
Expand Down
4 changes: 3 additions & 1 deletion tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
$middleware->handle($request, fn ($request) => $request);

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

Expand Down

0 comments on commit b275c47

Please sign in to comment.