From 230fbd6f42946e8173c973cce51f54e78e5b8404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Tobi=C5=A1ka?= Date: Sat, 21 Feb 2026 11:28:54 +0100 Subject: [PATCH] Fix audit to respect custom Laravel paths and add Route facade import - Add proper Route facade import in DebugModeAuditService (aliases may not be set from Laravel 11 up) - Update PHPStan bootstrap stubs for new method calls - Fixes #17, Use app()->environmentPath() and app()->environmentFile() instead of hardcoded base_path('.env') in EnvAuditService - Fixes #16, Use storage_path() instead of base_path('storage/...') in StorageAuditService --- phpstan-bootstrap.php | 15 +++++++++++++ src/Services/Audits/DebugModeAuditService.php | 4 +++- src/Services/Audits/EnvAuditService.php | 5 +++-- src/Services/Audits/StorageAuditService.php | 22 ++++++++----------- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/phpstan-bootstrap.php b/phpstan-bootstrap.php index d5fd219..7982709 100644 --- a/phpstan-bootstrap.php +++ b/phpstan-bootstrap.php @@ -14,6 +14,16 @@ public function version(): string { return 'fake-laravel-version'; } + + public function environmentPath(): string + { + return __DIR__ . '/../'; + } + + public function environmentFile(): string + { + return '.env'; + } } if (!class_exists(\Illuminate\Foundation\Application::class)) { @@ -45,6 +55,11 @@ public function uri(): string { return ''; } public function middleware(): array { return []; } } +if (!interface_exists(\Illuminate\Routing\RouteCollectionInterface::class)) { + interface WardenPHPStanFakeRouteCollectionInterface extends \IteratorAggregate {} + class_alias(WardenPHPStanFakeRouteCollectionInterface::class, \Illuminate\Routing\RouteCollectionInterface::class); +} + class WardenPHPStanFakeRouteFacade { /** @return array */ diff --git a/src/Services/Audits/DebugModeAuditService.php b/src/Services/Audits/DebugModeAuditService.php index 69f3c0a..1b7cb4b 100644 --- a/src/Services/Audits/DebugModeAuditService.php +++ b/src/Services/Audits/DebugModeAuditService.php @@ -2,6 +2,8 @@ namespace Dgtlss\Warden\Services\Audits; +use Illuminate\Support\Facades\Route; + class DebugModeAuditService extends AbstractAuditService { private array $devPackages = [ @@ -107,7 +109,7 @@ private function getInstalledPackages(): array private function hasExposedTestingRoutes(): bool { - $routeCollection = \Route::getRoutes(); + $routeCollection = Route::getRoutes(); // Check debugbar routes separately as they're allowed when APP_DEBUG is true foreach ($routeCollection as $route) { diff --git a/src/Services/Audits/EnvAuditService.php b/src/Services/Audits/EnvAuditService.php index d1b8f01..f8cff29 100644 --- a/src/Services/Audits/EnvAuditService.php +++ b/src/Services/Audits/EnvAuditService.php @@ -18,8 +18,9 @@ public function getName(): string public function run(): bool { - // Check if .env exists - if (!file_exists(base_path('.env'))) { + // Check if .env exists (respect custom environment path) + $envPath = app()->environmentPath() . DIRECTORY_SEPARATOR . app()->environmentFile(); + if (!file_exists($envPath)) { $this->addFinding([ 'package' => 'environment', 'title' => 'Missing .env file', diff --git a/src/Services/Audits/StorageAuditService.php b/src/Services/Audits/StorageAuditService.php index fcf5e93..377e854 100644 --- a/src/Services/Audits/StorageAuditService.php +++ b/src/Services/Audits/StorageAuditService.php @@ -4,15 +4,6 @@ class StorageAuditService extends AbstractAuditService { - /** - * @var array - */ - private array $directories = [ - 'storage/framework', - 'storage/logs', - 'bootstrap/cache', - ]; - public function getName(): string { return 'storage'; @@ -20,13 +11,18 @@ public function getName(): string public function run(): bool { - foreach ($this->directories as $directory) { - $path = base_path($directory); + $directories = [ + storage_path('framework'), + storage_path('logs'), + base_path('bootstrap/cache'), + ]; + + foreach ($directories as $path) { if (!file_exists($path)) { $this->addFinding([ 'package' => 'storage', - 'title' => 'Missing directory: ' . $directory, + 'title' => 'Missing directory: ' . $path, 'severity' => 'high', 'cve' => null, 'affected_versions' => null @@ -37,7 +33,7 @@ public function run(): bool if (!is_writable($path)) { $this->addFinding([ 'package' => 'storage', - 'title' => 'Directory not writable: ' . $directory, + 'title' => 'Directory not writable: ' . $path, 'severity' => 'high', 'cve' => null, 'affected_versions' => null