Skip to content

Commit

Permalink
Fix handleInvalidData allowing possible return of the next request in…
Browse files Browse the repository at this point in the history
… the middleware chain
  • Loading branch information
Lukasss93 committed Sep 10, 2023
1 parent 0adca00 commit 710545f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Middleware/ValidateWebAppData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct(protected Nutgram $bot)
{
}

public function handle(Request $request, Closure $next)
public function handle(Request $request, Closure $next): mixed
{
try {
$initData = $request->input('initData', '');
Expand All @@ -22,11 +22,11 @@ public function handle(Request $request, Closure $next)
$request->attributes->add(['webAppData' => $data]);
return $next($request);
} catch (InvalidDataException) {
$this->handleInvalidData($request, $next);
return $this->handleInvalidData($request, $next);
}
}

protected function handleInvalidData(Request $request, Closure $next): void
protected function handleInvalidData(Request $request, Closure $next): mixed
{
abort(403);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@
$middleware->handle($this->request, function ($request) {
});
})->throws(HttpException::class);

it('fails to validate web app data + custom action', function () {
$middleware = new class($this->bot) extends ValidateWebAppData {
protected function handleInvalidData(Request $request, Closure $next): mixed
{
$request->attributes->add(['webAppData' => null]);
return $next($request);
}
};

$middleware->handle($this->request, function ($request) {
expect($request->get('webAppData'))->toBeNull();
});
});

0 comments on commit 710545f

Please sign in to comment.