mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 12:12:18 +00:00
Fix unsecure redirect code.
This commit is contained in:
@@ -29,8 +29,11 @@ namespace FireflyIII\Exceptions;
|
||||
use ErrorException;
|
||||
use FireflyIII\Jobs\MailError;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Redirector;
|
||||
use Illuminate\Session\TokenMismatchException;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Validation\ValidationException as LaravelValidationException;
|
||||
@@ -187,4 +190,58 @@ class Handler extends ExceptionHandler
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a validation exception into a response.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param LaravelValidationException $exception
|
||||
*
|
||||
* @return Application|RedirectResponse|Redirector
|
||||
*/
|
||||
protected function invalid($request, LaravelValidationException $exception): Application|RedirectResponse|Redirector
|
||||
{
|
||||
// protect against open redirect when submitting invalid forms.
|
||||
$previous = $this->getPreviousUrl();
|
||||
$redirect = $this->getRedirectUrl($exception);
|
||||
|
||||
return redirect($redirect ?? $previous)
|
||||
->withInput(Arr::except($request->input(), $this->dontFlash))
|
||||
->withErrors($exception->errors(), $request->input('_error_bag', $exception->errorBag));
|
||||
}
|
||||
|
||||
/**
|
||||
* Only return the previousUrl() if it is a valid URL. Return default redirect otherwise.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getPreviousUrl(): string
|
||||
{
|
||||
$safe = route('index');
|
||||
$previous = url()->previous();
|
||||
$previousHost = parse_url($previous, PHP_URL_HOST);
|
||||
$safeHost = parse_url($safe, PHP_URL_HOST);
|
||||
|
||||
return null !== $previousHost && $previousHost === $safeHost ? $previous : $safe;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only return the redirectTo property from the exception if it is a valid URL. Return NULL otherwise.
|
||||
*
|
||||
* @param LaravelValidationException $exception
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function getRedirectUrl(LaravelValidationException $exception): ?string
|
||||
{
|
||||
if (null === $exception->redirectTo) {
|
||||
return null;
|
||||
}
|
||||
$safe = route('index');
|
||||
$previous = $exception->redirectTo;
|
||||
$previousHost = parse_url($previous, PHP_URL_HOST);
|
||||
$safeHost = parse_url($safe, PHP_URL_HOST);
|
||||
|
||||
return null !== $previousHost && $previousHost === $safeHost ? $previous : $safe;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user