diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 1b4e26054889..26bda6c84a46 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -1,119 +1,12 @@ [ - EncryptCookies::class, - AddQueuedCookiesToResponse::class, - VerifyCsrfToken::class, - CheckLocale::class, - CheckUserIsActivated::class, - CheckForTwoFactor::class, - IssueFreshApiTokenIfTwoFactorComplete::class, - CheckColorSettings::class, - AuthenticateSession::class, - SubstituteBindings::class, - ], - - 'api' => [ - 'auth:api', - CheckUserIsActivated::class, - EnforceApiTwoFactorEnrollment::class, - EnforceApiUserAgent::class, - CheckLocale::class, - LogAuthedUserHeader::class, - SetPaginationDefaults::class, - SubstituteBindings::class, - ], - - 'health' => [ - - ], - ]; - - /** - * The application's route middleware. - * - * These middleware may be assigned to groups or used individually. - * - * @var array - */ - protected $routeMiddleware = [ - 'auth' => Authenticate::class, - 'authorize' => CheckPermissions::class, - 'auth.basic' => AuthenticateWithBasicAuth::class, - 'can' => Authorize::class, - 'guest' => RedirectIfAuthenticated::class, - 'throttle' => ThrottleRequests::class, - 'api-throttle' => SetAPIResponseHeaders::class, - 'health' => null, - ]; -} +/* + * This file isn't in use any more, and its use is discouraged in the Laravel world. + * + * Now, instead, you should go look at bootstrap/app.php + * + * + * + * + * + */ \ No newline at end of file diff --git a/app/Http/Middleware/TrimStrings.php b/app/Http/Middleware/TrimStrings.php deleted file mode 100644 index a8a252df4c0a..000000000000 --- a/app/Http/Middleware/TrimStrings.php +++ /dev/null @@ -1,19 +0,0 @@ -allSubdomainsOfApplicationUrl(), - ]; - } -} diff --git a/app/Http/Middleware/TrustProxies.php b/app/Http/Middleware/TrustProxies.php index cff6c1d8dcfd..9093b505b401 100644 --- a/app/Http/Middleware/TrustProxies.php +++ b/app/Http/Middleware/TrustProxies.php @@ -55,4 +55,4 @@ protected function headers() { return $this->headerBitmask; } -} +} \ No newline at end of file diff --git a/bootstrap/app.php b/bootstrap/app.php index f2801adf6f14..ef3aa30aec7a 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -1,36 +1,123 @@ withEvents(false) + ->withMiddleware(function (Middleware $middleware) { + // --- Global stack --- + // ValidatePathEncoding, InvokeDeferredCallbacks, *NOT* TrustProxies, HandleCors, + // PreventRequestsDuringMaintenance, ValidatePostSize, ConvertEmptyStringsToNull + // all come from Laravel's own defaults now, so future additions there show up + // automatically. Only Snipe-IT's own additions are listed explicitly below. + $middleware->trustHosts(); + $middleware->prepend(TrustProxies::class); + // this was overridden, inherits from the parents but makes some changes. + // to keep this change small enough, we keep it for now + $middleware->trimStrings(except: [ + 'current_password', + 'password', + 'password_confirmation', + ]); -$app->singleton( - Illuminate\Contracts\Http\Kernel::class, - App\Http\Kernel::class -); + // Order matters: NoSessionStore must run before StartSession (it may force + // the array session driver for /health); CheckForSetup/CheckForDebug need + // the session/auth state StartSession sets up. These run globally (not just + // in the 'web' group) because /health uses Route::withoutMiddleware(['web']) + // and still needs them. + $middleware->append([ + NoSessionStore::class, + StartSession::class, + ShareErrorsFromSession::class, + CheckForSetup::class, + CheckForDebug::class, + SecurityHeaders::class, + PreventBackHistory::class, + ]); + + // --- Groups (explicit, not merged with Laravel's group defaults: Snipe-IT's + // web/api groups intentionally diverge - e.g. StartSession/ShareErrorsFromSession + // are global instead of web-group-only, and VerifyCsrfToken is a custom subclass + // incompatible with the new validateCsrfTokens() helper) --- + $middleware->group('web', [ + EncryptCookies::class, + AddQueuedCookiesToResponse::class, + VerifyCsrfToken::class, + CheckLocale::class, + CheckUserIsActivated::class, + CheckForTwoFactor::class, + IssueFreshApiTokenIfTwoFactorComplete::class, + CheckColorSettings::class, + AuthenticateSession::class, + SubstituteBindings::class, + ]); + $middleware->group('api', [ + 'auth:api', + CheckUserIsActivated::class, + EnforceApiTwoFactorEnrollment::class, + EnforceApiUserAgent::class, + CheckLocale::class, + LogAuthedUserHeader::class, + SetPaginationDefaults::class, + SubstituteBindings::class, + ]); + + $middleware->group('health', []); + + $middleware->alias([ + 'auth' => Authenticate::class, + 'authorize' => CheckPermissions::class, + 'auth.basic' => AuthenticateWithBasicAuth::class, + 'can' => Authorize::class, + 'guest' => RedirectIfAuthenticated::class, + 'throttle' => ThrottleRequests::class, + 'api-throttle' => SetAPIResponseHeaders::class, + ]); + }) + ->create(); + +// Keep Snipe-IT's own Console Kernel (custom schedule + command/route loading) and +// Exception Handler (SCIM, 2FA, API-JSON rendering) instead of Laravel's closure-based +// withSchedule()/withExceptions() APIs - there's no "silently missing default" risk for +// either of these the way there was for HTTP middleware, so rewriting them adds risk +// without fixing anything. $app->singleton( Illuminate\Contracts\Console\Kernel::class, App\Console\Kernel::class @@ -41,15 +128,4 @@ App\Exceptions\Handler::class ); -/* -|-------------------------------------------------------------------------- -| Return The Application -|-------------------------------------------------------------------------- -| -| This script returns the application instance. The instance is given to -| the calling script so we can separate the building of the instances -| from the actual running of the application and sending responses. -| -*/ - return $app;