Issues (197)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/LaravelMicroscopeServiceProvider.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope;
4
5
use Faker\Generator as FakerGenerator;
6
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
7
use Illuminate\Contracts\Queue\Factory as QueueFactoryContract;
8
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
9
use Illuminate\Support\Facades\Event;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Facades\Route;
12
use Illuminate\Support\ServiceProvider;
13
use Illuminate\Support\Str;
14
use Illuminate\View\View;
15
use Imanghafoori\LaravelMicroscope\Checks\CheckClassReferences;
16
use Imanghafoori\LaravelMicroscope\Commands\CheckViews;
17
use Imanghafoori\LaravelMicroscope\ErrorReporters\ConsolePrinterInstaller;
18
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
19
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyBladeCompiler;
20
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyDispatcher;
21
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyFactory;
22
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyGate;
23
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyRouter;
24
use Imanghafoori\LaravelMicroscope\SpyClasses\ViewsData;
25
26
class LaravelMicroscopeServiceProvider extends ServiceProvider
27
{
28
    private static $commandNames = [
29
        Commands\CheckEvents::class,
30
        Commands\CheckGates::class,
31
        Commands\CheckRoutes::class,
32
        Commands\CheckViews::class,
33
        Commands\CheckPsr4::class,
34
        Commands\CheckImports::class,
35
        Commands\CheckAll::class,
36
        Commands\ClassifyStrings::class,
37
        Commands\CheckDD::class,
38
        Commands\CheckEarlyReturns::class,
39
        Commands\CheckCompact::class,
40
        Commands\CheckBladeQueries::class,
41
        Commands\CheckActionComments::class,
42
        Commands\CheckBadPractice::class,
43
        Commands\CheckExtractBladeIncludes::class,
44
        Commands\PrettyPrintRoutes::class,
45
        Commands\CheckExpansions::class,
46
        Commands\CheckDeadControllers::class,
47
        Commands\CheckGenericActionComments::class,
48
        Commands\CheckPsr12::class,
49
        Commands\CheckEndIf::class,
50
    ];
51
52
    public function boot()
53
    {
54
        (app()['env'] !== 'production') && config('microscope.log_unused_view_vars', true) && $this->spyView();
55
56
        if (! $this->canRun()) {
57
            return;
58
        }
59
60
        Event::listen('microscope.start.command', function () {
61
            ! defined('microscope_start') && define('microscope_start', microtime(true));
62
        });
63
64
        $this->resetCountersOnFinish();
65
66
        $this->commands(self::$commandNames);
67
68
        $this->publishes([
69
            __DIR__.'/../config/config.php' => config_path('microscope.php'),
70
        ], 'config');
71
72
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'microscope');
73
74
        ConsolePrinterInstaller::boot();
75
76
        Event::listen('microscope.checking', function ($path, $command) {
77
            $command->line('Checking: '.$path);
78
        });
79
    }
80
81
    public function register()
82
    {
83
        ! defined('T_NAME_QUALIFIED') && define('T_NAME_QUALIFIED', 3030);
84
        ! defined('T_NAME_FULLY_QUALIFIED') && define('T_NAME_FULLY_QUALIFIED', 3031);
85
86
        if (! $this->canRun()) {
87
            return;
88
        }
89
        $this->spyEvents();
90
91
        $this->registerCompiler();
92
93
        $this->loadConfig();
94
95
        app()->singleton(ErrorPrinter::class);
96
        // also we should spy the factory paths.
97
        $this->spyRouter();
98
        if (class_exists('Illuminate\Database\Eloquent\Factory')) {
99
            $this->spyFactory();
100
        }
101
102
        // We need to start spying before the boot process starts.
103
        $command = $_SERVER['argv'][1] ?? null;
104
        // We spy the router in order to have a list of route files.
105
        $checkAll = Str::startsWith('check:all', $command);
106
        ($checkAll || Str::startsWith('check:routes', $command)) && app('router')->spyRouteConflict();
107
        Str::startsWith('check:action_comment', $command) && app('router')->spyRouteConflict();
108
        // ($checkAll || Str::startsWith('check:events', $command)) && $this->spyEvents();
109
        ($checkAll || Str::startsWith('check:gates', $command)) && $this->spyGates();
110
    }
111
112
    private function spyRouter()
113
    {
114
        $router = new SpyRouter(app('events'), app());
115
        $this->app->singleton('router', function ($app) use ($router) {
0 ignored issues
show
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
            return $router;
117
        });
118
        Route::swap($router);
119
    }
120
121
    private function spyFactory()
122
    {
123
        $this->app->singleton(EloquentFactory::class, function ($app) {
124
            return SpyFactory::construct(
125
                $app->make(FakerGenerator::class), $app->databasePath('factories')
126
            );
127
        });
128
    }
129
130
    private function spyGates()
131
    {
132
        $this->app->singleton(GateContract::class, function ($app) {
133
            return new SpyGate($app, function () use ($app) {
134
                return call_user_func($app['auth']->userResolver());
135
            });
136
        });
137
    }
138
139
    private function spyEvents()
140
    {
141
        app()->booting(function () {
142
            $this->app->singleton('events', function ($app) {
143
                return (new SpyDispatcher($app))->setQueueResolver(function () use ($app) {
144
                    return $app->make(QueueFactoryContract::class);
145
                });
146
            });
147
            Event::clearResolvedInstance('events');
148
        });
149
    }
150
151
    public function spyView()
152
    {
153
        app()->singleton('microscope.views', ViewsData::class);
154
155
        \View::creator('*', function (View $view) {
156
            resolve('microscope.views')->add($view);
157
        });
158
159
        app()->terminating(function () {
160
            $spy = resolve('microscope.views');
161
            if (! $spy->main || Str::startsWith($spy->main->getName(), ['errors::'])) {
162
                return;
163
            }
164
165
            $action = $this->getActionName();
166
167
            $uselessVars = array_keys(array_diff_key($spy->getMainVars(), $spy->readTokenizedVars()));
168
            $viewName = $spy->main->getName();
169
170
            $uselessVars && $this->logUnusedViewVars($viewName, $action, $uselessVars);
0 ignored issues
show
Bug Best Practice introduced by
The expression $uselessVars of type array<integer|string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
171
        });
172
    }
173
174
    private function loadConfig()
175
    {
176
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'microscope');
177
    }
178
179
    private function canRun()
180
    {
181
        return $this->app->runningInConsole() && config('microscope.is_enabled', true) && ! $this->app->runningUnitTests() && app()['env'] !== 'production';
182
    }
183
184
    public function getActionName()
185
    {
186
        $cRoute = \Route::getCurrentRoute();
187
188
        return $cRoute ? $cRoute->getActionName() : '';
189
    }
190
191
    private function registerCompiler()
192
    {
193
        $this->app->singleton('microscope.blade.compiler', function () {
194
            return new SpyBladeCompiler($this->app['files'], $this->app['config']['view.compiled']);
195
        });
196
    }
197
198
    private function resetCountersOnFinish()
199
    {
200
        Event::listen('microscope.finished.checks', function () {
201
            CheckViews::$checkedCallsNum = 0;
202
            CheckClassReferences::$refCount = 0;
203
            Psr4Classes::$checkedFilesNum = 0;
204
        });
205
    }
206
207
    private function logUnusedViewVars($viewName, string $action, array $uselessVars)
208
    {
209
        Log::info('Laravel Microscope - The view file "'.$viewName.'"');
210
        Log::info('At "'.$action.'" has some unused variables passed to it: ');
211
        Log::info($uselessVars);
0 ignored issues
show
$uselessVars is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
212
        Log::info('If you do not see these variables passed in a controller, look in view composers.');
213
    }
214
}
215