-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDebugModeAuditService.php
More file actions
189 lines (162 loc) · 5.77 KB
/
DebugModeAuditService.php
File metadata and controls
189 lines (162 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Dgtlss\Warden\Services\Audits;
class DebugModeAuditService extends AbstractAuditService
{
private array $devPackages = [
'barryvdh/laravel-debugbar',
'laravel/telescope',
'laravel/horizon',
'beyondcode/laravel-dump-server',
'laravel/dusk',
];
public function getName(): string
{
return 'debug-mode';
}
public function run(): bool
{
// Check if APP_DEBUG is enabled in production only
if (config('app.env') === 'production' && config('app.debug') === true) {
$this->addFinding([
'package' => 'app-config',
'title' => 'Debug mode is enabled in production',
'severity' => 'critical',
'cve' => null,
'affected_versions' => null
]);
}
// Only check for development packages if we're actually running in production
if ($this->isActuallyProduction()) {
// Check for development packages in vendor/composer/installed.json
$installedPackagesNames = $this->getInstalledPackagesNames();
foreach ($this->devPackages as $devPackage) {
if (in_array($devPackage, $installedPackagesNames)) {
$this->addFinding([
'package' => $devPackage,
'title' => 'Development package detected in production',
'severity' => 'high',
'cve' => null,
'affected_versions' => null
]);
}
}
// Check if Telescope is enabled
if (class_exists(\Laravel\Telescope\Telescope::class) && config('telescope.enabled')) {
$this->addFinding([
'package' => 'laravel/telescope',
'title' => 'Laravel Telescope is enabled in production',
'severity' => 'high',
'cve' => null,
'affected_versions' => null
]);
}
// Check if Horizon is enabled
if (class_exists(\Laravel\Horizon\Horizon::class) && config('horizon.enabled')) {
$this->addFinding([
'package' => 'laravel/horizon',
'title' => 'Laravel Horizon dashboard is enabled in production',
'severity' => 'medium',
'cve' => null,
'affected_versions' => null
]);
}
}
// Check for exposed testing routes only in production
if ($this->isActuallyProduction() && $this->hasExposedTestingRoutes()) {
$this->addFinding([
'package' => 'routes',
'title' => 'Testing routes are exposed',
'severity' => 'high',
'cve' => null,
'affected_versions' => null
]);
}
return true;
}
private function getInstalledPackagesNames(): array
{
$installedPackages = $this->getInstalledPackages();
return isset($installedPackages['packages'])
? array_column($installedPackages['packages'], 'name')
: [];
}
private function getInstalledPackages(): array
{
$installedPath = base_path('vendor/composer/installed.json');
if (!file_exists($installedPath)) {
return [];
}
$installedContents = file_get_contents($installedPath);
return json_decode($installedContents, true);
}
private function hasExposedTestingRoutes(): bool
{
$routeCollection = \Route::getRoutes();
$routes = iterator_to_array($routeCollection, false);
// Check debugbar routes separately as they're allowed when APP_DEBUG is true
foreach ($routes as $route) {
$uri = $route->uri();
if (str_starts_with($uri, '_debugbar')) {
// Only flag debugbar routes as exposed if APP_DEBUG is false and there's no protective middleware
if (!config('app.debug') && !$this->hasProtectiveMiddleware($route)) {
return true;
}
continue;
}
// Check other testing routes that should never be exposed in production
$testingRoutes = [
'telescope',
'horizon',
'_dusk',
];
foreach ($testingRoutes as $testingRoute) {
if (str_starts_with($uri, $testingRoute)) {
return true;
}
}
}
return false;
}
/**
* @param object $route
*/
private function hasProtectiveMiddleware($route): bool
{
$middleware = $route->middleware();
$protectiveMiddleware = [
'auth',
'admin',
'can:',
'ability:',
'role:',
'Barryvdh\Debugbar\Middleware\DebugbarEnabled'
];
foreach ($middleware as $m) {
foreach ($protectiveMiddleware as $protect) {
if (str_starts_with($m, $protect)) {
return true;
}
}
}
return false;
}
private function isActuallyProduction(): bool
{
// Check for common CI/CD environment variables
$ciEnvironments = [
'CI',
'CONTINUOUS_INTEGRATION',
'GITHUB_ACTIONS',
'GITLAB_CI',
'JENKINS_URL',
'TRAVIS',
'CIRCLECI'
];
foreach ($ciEnvironments as $ciEnvironment) {
if (getenv($ciEnvironment) !== false) {
return false;
}
}
return config('app.env') === 'production';
}
}