Issues (12)

Security Analysis    no request data  

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/ImpersonateServiceProvider.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 Lab404\Impersonate;
4
5
use Illuminate\Auth\AuthManager;
6
use Illuminate\Auth\Events\Login;
7
use Illuminate\Auth\Events\Logout;
8
use Illuminate\Foundation\Application;
9
use Illuminate\Support\Facades\Event;
10
use Illuminate\View\Compilers\BladeCompiler;
11
use Lab404\Impersonate\Guard\SessionGuard;
12
use Lab404\Impersonate\Middleware\ProtectFromImpersonation;
13
use Lab404\Impersonate\Services\ImpersonateManager;
14
15
/**
16
 * Class ServiceProvider
17
 *
18
 * @package Lab404\Impersonate
19
 */
20
class ImpersonateServiceProvider extends \Illuminate\Support\ServiceProvider
21
{
22
    /** @var string $configName */
23
    protected $configName = 'laravel-impersonate';
24
25
    /**
26
     * Register the service provider.
27
     *
28
     * @return void
29
     */
30
    public function register()
31
    {
32
        $this->mergeConfig();
33
34
        $this->app->bind(ImpersonateManager::class, ImpersonateManager::class);
35
36
        $this->app->singleton(ImpersonateManager::class, function ($app) {
37
            return new ImpersonateManager($app);
38
        });
39
40
        $this->app->alias(ImpersonateManager::class, 'impersonate');
41
42
        $this->registerRoutesMacro();
43
        $this->registerBladeDirectives();
44
        $this->registerMiddleware();
45
        $this->registerAuthDriver();
46
    }
47
48
    /**
49
     * Bootstrap the application events.
50
     *
51
     * @return void
52
     */
53
    public function boot()
54
    {
55
        $this->publishConfig();
56
57
        // We want to remove data from storage on real login and logout
58
        Event::listen(Login::class, function ($event) {
0 ignored issues
show
The parameter $event 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...
59
            app('impersonate')->clear();
60
        });
61
        Event::listen(Logout::class, function ($event) {
0 ignored issues
show
The parameter $event 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...
62
            app('impersonate')->clear();
63
        });
64
    }
65
66
    /**
67
     * Register plugin blade directives.
68
     *
69
     * @param void
70
     * @return  void
71
     */
72
    protected function registerBladeDirectives()
73
    {
74
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
75
            $bladeCompiler->directive('impersonating', function () {
76
                return "<?php if (is_impersonating()) : ?>";
77
            });
78
79
            $bladeCompiler->directive('endImpersonating', function () {
80
                return '<?php endif; ?>';
81
            });
82
83
            $bladeCompiler->directive('canImpersonate', function () {
84
                return "<?php if (can_impersonate()) : ?>";
85
            });
86
87
            $bladeCompiler->directive('endCanImpersonate', function () {
88
                return '<?php endif; ?>';
89
            });
90
91
            $bladeCompiler->directive('canBeImpersonated', function ($expression) {
92
                return "<?php if (can_be_impersonated({$expression})) : ?>";
93
            });
94
95
            $bladeCompiler->directive('endCanBeImpersonated', function () {
96
                return '<?php endif; ?>';
97
            });
98
        });
99
    }
100
101
    /**
102
     * Register routes macro.
103
     *
104
     * @param void
105
     * @return  void
106
     */
107
    protected function registerRoutesMacro()
108
    {
109
        $router = $this->app['router'];
110
111
        $router->macro('impersonate', function () use ($router) {
112
            $router->get('/impersonate/take/{id}/{guardName?}',
113
                '\Lab404\Impersonate\Controllers\[email protected]')->name('impersonate');
114
            $router->get('/impersonate/leave',
115
                '\Lab404\Impersonate\Controllers\[email protected]')->name('impersonate.leave');
116
        });
117
    }
118
119
    /**
120
     * @param void
121
     * @return  void
122
     */
123
    protected function registerAuthDriver()
124
    {
125
        /** @var AuthManager $auth */
126
        $auth = $this->app['auth'];
127
128
        $auth->extend('session', function (Application $app, $name, array $config) use ($auth) {
129
            $provider = $auth->createUserProvider($config['provider']);
130
131
            $guard = new SessionGuard($name, $provider, $app['session.store']);
0 ignored issues
show
It seems like $provider defined by $auth->createUserProvider($config['provider']) on line 129 can be null; however, Illuminate\Auth\SessionGuard::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
132
133
            if (method_exists($guard, 'setCookieJar')) {
134
                $guard->setCookieJar($app['cookie']);
135
            }
136
137
            if (method_exists($guard, 'setDispatcher')) {
138
                $guard->setDispatcher($app['events']);
139
            }
140
141
            if (method_exists($guard, 'setRequest')) {
142
                $guard->setRequest($app->refresh('request', $guard, 'setRequest'));
143
            }
144
145
            return $guard;
146
        });
147
    }
148
149
    /**
150
     * Register plugin middleware.
151
     *
152
     * @param void
153
     * @return  void
154
     */
155
    public function registerMiddleware()
156
    {
157
        $this->app['router']->aliasMiddleware('impersonate.protect', ProtectFromImpersonation::class);
158
    }
159
160
    /**
161
     * Merge config file.
162
     *
163
     * @param void
164
     * @return  void
165
     */
166
    protected function mergeConfig()
167
    {
168
        $configPath = __DIR__ . '/../config/' . $this->configName . '.php';
169
170
        $this->mergeConfigFrom($configPath, $this->configName);
171
    }
172
173
    /**
174
     * Publish config file.
175
     *
176
     * @param void
177
     * @return  void
178
     */
179
    protected function publishConfig()
180
    {
181
        $configPath = __DIR__ . '/../config/' . $this->configName . '.php';
182
183
        $this->publishes([$configPath => config_path($this->configName . '.php')], 'impersonate');
184
    }
185
}