Fix: OAuth-Client-Fehler lösen keine Exception-Mail mehr aus

Bots klopfen /oauth/authorize ohne gültige OAuth-Parameter ab; Passport
wirft dann korrekt einen 4xx-Fehler. Da der Handler bei jeder gemeldeten
Exception mailt und die API ausschliesslich Personal Access Tokens nutzt
(Authorize-Flow nie legitim genutzt), war jede dieser Mails Rauschen.

OAuthServerException wandert in $dontReport – Endpoint-Verhalten bleibt
unveraendert, nur der Mail-Versand fuer diese Client-Fehler entfaellt.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-08-26 10:25:17 +00:00
parent 7265d9cd19
commit 037ec4d34b
13 changed files with 15 additions and 13 deletions

View file

@ -3,10 +3,12 @@
namespace App\Exceptions; namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Mail;
use Symfony\Component\ErrorHandler\Exception\FlattenException; use Laravel\Passport\Exceptions\OAuthServerException;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer; use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Response;
use Throwable;
class Handler extends ExceptionHandler class Handler extends ExceptionHandler
{ {
@ -16,7 +18,10 @@ class Handler extends ExceptionHandler
* @var array * @var array
*/ */
protected $dontReport = [ protected $dontReport = [
// // OAuth-Client-Fehler (z. B. Bots, die /oauth/authorize ohne gültige
// Parameter abklopfen). Werden sauber als 4xx an den Aufrufer gerendert
// und sind keine Applikationsfehler – lösen daher keine Exception-Mail aus.
OAuthServerException::class,
]; ];
/** /**
@ -32,7 +37,6 @@ class Handler extends ExceptionHandler
/** /**
* Report or log an exception. * Report or log an exception.
* *
* @param \Throwable $exception
* @return void * @return void
* *
* @throws \Exception * @throws \Exception
@ -49,10 +53,9 @@ class Handler extends ExceptionHandler
* Render an exception into an HTTP response. * Render an exception into an HTTP response.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \Throwable $exception * @return Response
* @return \Symfony\Component\HttpFoundation\Response
* *
* @throws \Throwable * @throws Throwable
*/ */
public function render($request, Throwable $exception) public function render($request, Throwable $exception)
{ {
@ -67,24 +70,23 @@ class Handler extends ExceptionHandler
$css = $handler->getStylesheet(); $css = $handler->getStylesheet();
$content = $handler->getBody($e); $content = $handler->getBody($e);
//Mail::to(config('app.exception_mail'))->send(new MailContact($contact)); // Mail::to(config('app.exception_mail'))->send(new MailContact($contact));
// Verwende normale Mail-Klasse statt Facade, um Probleme bei der Initialisierung zu vermeiden // Verwende normale Mail-Klasse statt Facade, um Probleme bei der Initialisierung zu vermeiden
$to = config('app.exception_mail'); $to = config('app.exception_mail');
$subject = 'gruene-seele Exception: ' . \Request::fullUrl(); $subject = 'gruene-seele Exception: '.\Request::fullUrl();
if ($to) { if ($to) {
\Mail::send('emails.exception', compact('css', 'content'), function ($message) use ($to, $subject) { \Mail::send('emails.exception', compact('css', 'content'), function ($message) use ($to, $subject) {
$message $message
->to($to) ->to($to)
->subject($subject) ->subject($subject);
;
}); });
} }
} catch (Throwable $ex) { } catch (Throwable $ex) {
// Einfache Fehlerprotokollierung ohne Facade // Einfache Fehlerprotokollierung ohne Facade
file_put_contents( file_put_contents(
storage_path('logs/laravel-' . date('Y-m-d') . '.log'), storage_path('logs/laravel-'.date('Y-m-d').'.log'),
'[' . date('Y-m-d H:i:s') . '] exception-handler-error: ' . $ex->getMessage() . "\n", '['.date('Y-m-d H:i:s').'] exception-handler-error: '.$ex->getMessage()."\n",
FILE_APPEND FILE_APPEND
); );
} }