Halte die Anmeldung auf der im Browser geöffneten Adresse.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
63f2be565f
commit
fa706e5080
7 files changed
+119
-3
No files matched your search
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class IgnoreEmptyForwardedHeaders
|
||||
{
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
foreach ([
|
||||
'X-Forwarded-Host',
|
||||
'X-Forwarded-Port',
|
||||
'X-Forwarded-Proto',
|
||||
'X-Forwarded-For',
|
||||
'X-Forwarded-Prefix',
|
||||
] as $header) {
|
||||
if (! $request->headers->has($header)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (trim((string) $request->headers->get($header)) === '') {
|
||||
$request->headers->remove($header);
|
||||
}
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class SanitizeIntendedUrl
|
||||
{
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$intended = $request->session()->get('url.intended');
|
||||
|
||||
if (is_string($intended) && ! $this->matchesOpenedHost($intended, $request)) {
|
||||
$request->session()->forget('url.intended');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
private function matchesOpenedHost(string $url, Request $request): bool
|
||||
{
|
||||
if ($url === '' || filter_var($url, FILTER_VALIDATE_URL) === false) {
|
||||
return str_starts_with($url, '/');
|
||||
}
|
||||
|
||||
$host = parse_url($url, PHP_URL_HOST);
|
||||
|
||||
if (! is_string($host) || strcasecmp($host, $request->getHost()) !== 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$port = parse_url($url, PHP_URL_PORT);
|
||||
$requestPort = (int) $request->getPort();
|
||||
|
||||
if ($port !== null) {
|
||||
return (int) $port === $requestPort;
|
||||
}
|
||||
|
||||
return in_array($requestPort, [80, 443], true);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Providers\Filament;
|
||||
|
||||
use App\Http\Middleware\SanitizeIntendedUrl;
|
||||
use Filament\Auth\MultiFactor\App\AppAuthentication;
|
||||
use Filament\Http\Middleware\Authenticate;
|
||||
use Filament\Http\Middleware\AuthenticateSession;
|
||||
@@ -49,6 +50,7 @@ public function panel(Panel $panel): Panel
|
||||
EncryptCookies::class,
|
||||
AddQueuedCookiesToResponse::class,
|
||||
StartSession::class,
|
||||
SanitizeIntendedUrl::class,
|
||||
AuthenticateSession::class,
|
||||
ShareErrorsFromSession::class,
|
||||
PreventRequestForgery::class,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Middleware\IgnoreEmptyForwardedHeaders;
|
||||
use App\Http\Middleware\SanitizeIntendedUrl;
|
||||
use App\Http\Middleware\UseRequestRootUrl;
|
||||
use App\Http\Middleware\UseStaffSessionCookie;
|
||||
use Illuminate\Foundation\Application;
|
||||
@@ -17,6 +19,8 @@
|
||||
$middleware->trustProxies(at: '*');
|
||||
$middleware->prepend(UseStaffSessionCookie::class);
|
||||
$middleware->prepend(UseRequestRootUrl::class);
|
||||
$middleware->prepend(IgnoreEmptyForwardedHeaders::class);
|
||||
$middleware->web(append: SanitizeIntendedUrl::class);
|
||||
$middleware->redirectGuestsTo(function (Request $request) {
|
||||
if ($request->is('verwaltung', 'verwaltung/*')) {
|
||||
return '/verwaltung/login';
|
||||
|
||||
@@ -19,8 +19,8 @@ server {
|
||||
set $forwarded_proto $scheme;
|
||||
}
|
||||
fastcgi_param HTTP_X_FORWARDED_PROTO $forwarded_proto;
|
||||
fastcgi_param HTTP_X_FORWARDED_HOST $http_x_forwarded_host;
|
||||
fastcgi_param HTTP_X_FORWARDED_PORT $http_x_forwarded_port;
|
||||
fastcgi_param HTTP_X_FORWARDED_HOST $http_x_forwarded_host if_not_empty;
|
||||
fastcgi_param HTTP_X_FORWARDED_PORT $http_x_forwarded_port if_not_empty;
|
||||
fastcgi_param HTTP_X_FORWARDED_FOR $proxy_add_x_forwarded_for;
|
||||
fastcgi_read_timeout 120s;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ ## Bedienbar
|
||||
|
||||
Der Systemverwalter vergibt diese Rollen und kann sie dem eigenen Konto geben. Ein Konto darf mehrere Rollen tragen.
|
||||
|
||||
Auf der Testinstanz steht `APP_URL` noch auf dem Platzhalter. Solange das so ist, baut die Anwendung Links aus der Adresse, die der Browser tatsächlich geöffnet hat.
|
||||
Auf der Testinstanz steht `APP_URL` noch auf dem Platzhalter. Solange das so ist, baut die Anwendung Links aus der Adresse, die der Browser tatsächlich geöffnet hat. Ein Zugriff direkt über die IP, ohne Reverse Proxy, bleibt dabei auf dieser Adresse; die Anmeldung springt nicht auf `verwaltung/` als Host.
|
||||
|
||||
## Testdaten
|
||||
|
||||
|
||||
@@ -14,6 +14,42 @@ class AdministrationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_guest_redirect_keeps_the_address_opened_in_the_browser(): void
|
||||
{
|
||||
config(['app.url' => 'https://CHANGE-ME.example']);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'X-Forwarded-Host' => '',
|
||||
'X-Forwarded-Port' => '',
|
||||
'X-Forwarded-Proto' => '',
|
||||
])->get('http://192.168.2.144:3011/verwaltung');
|
||||
|
||||
$response->assertRedirect('http://192.168.2.144:3011/verwaltung/login');
|
||||
|
||||
$intended = session('url.intended');
|
||||
$this->assertIsString($intended);
|
||||
$this->assertSame('192.168.2.144', parse_url($intended, PHP_URL_HOST));
|
||||
$this->assertSame(3011, parse_url($intended, PHP_URL_PORT));
|
||||
$this->assertSame('/verwaltung', parse_url($intended, PHP_URL_PATH));
|
||||
}
|
||||
|
||||
public function test_login_drops_an_intended_address_that_leaves_the_opened_host(): void
|
||||
{
|
||||
config(['app.url' => 'https://CHANGE-ME.example']);
|
||||
|
||||
$this->withSession(['url.intended' => 'http:///verwaltung'])
|
||||
->get('http://192.168.2.144:3011/verwaltung/login')
|
||||
->assertOk();
|
||||
|
||||
$this->assertNull(session('url.intended'));
|
||||
|
||||
$this->withSession(['url.intended' => 'http://192.168.2.144:3011/verwaltung'])
|
||||
->get('http://192.168.2.144:3011/verwaltung/login')
|
||||
->assertOk();
|
||||
|
||||
$this->assertSame('http://192.168.2.144:3011/verwaltung', session('url.intended'));
|
||||
}
|
||||
|
||||
public function test_guests_and_members_cannot_open_the_administration(): void
|
||||
{
|
||||
$this->get('/verwaltung')->assertRedirect('/verwaltung/login');
|
||||
|
||||
Reference in new issue
Block a user