Issues (62)

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/PermissionServiceProvider.php (12 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 Spatie\Permission;
4
5
use Illuminate\Routing\Route;
6
use Illuminate\Support\Collection;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Support\ServiceProvider;
9
use Illuminate\View\Compilers\BladeCompiler;
10
use Spatie\Permission\Contracts\Role as RoleContract;
11
use Spatie\Permission\Contracts\Permission as PermissionContract;
12
13
class PermissionServiceProvider extends ServiceProvider
14
{
15
    public function boot(PermissionRegistrar $permissionLoader, Filesystem $filesystem)
16
    {
17
        if (function_exists('config_path')) { // function not available and 'publish' not relevant in Lumen
18
            $this->publishes([
19
                __DIR__.'/../config/permission.php' => config_path('permission.php'),
20
            ], 'config');
21
22
            $this->publishes([
23
                __DIR__.'/../database/migrations/create_permission_tables.php.stub' => $this->getMigrationFileName($filesystem),
24
            ], 'migrations');
25
        }
26
27
        $this->registerMacroHelpers();
28
29
        $this->commands([
30
            Commands\CacheReset::class,
31
            Commands\CreateRole::class,
32
            Commands\CreatePermission::class,
33
            Commands\Show::class,
34
        ]);
35
36
        $this->registerModelBindings();
37
38
        $permissionLoader->clearClassPermissions();
39
        $permissionLoader->registerPermissions();
40
41
        $this->app->singleton(PermissionRegistrar::class, function ($app) use ($permissionLoader) {
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...
42
            return $permissionLoader;
43
        });
44
    }
45
46
    public function register()
47
    {
48
        $this->mergeConfigFrom(
49
            __DIR__.'/../config/permission.php',
50
            'permission'
51
        );
52
53
        $this->registerBladeExtensions();
54
    }
55
56
    protected function registerModelBindings()
57
    {
58
        $config = $this->app->config['permission.models'];
0 ignored issues
show
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
59
60
        if (! $config) {
61
            return;
62
        }
63
64
        $this->app->bind(PermissionContract::class, $config['permission']);
65
        $this->app->bind(RoleContract::class, $config['role']);
66
    }
67
68
    protected function registerBladeExtensions()
69
    {
70
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
71 View Code Duplication
            $bladeCompiler->directive('role', function ($arguments) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
                list($role, $guard) = explode(',', $arguments.',');
73
74
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
75
            });
76 View Code Duplication
            $bladeCompiler->directive('elserole', function ($arguments) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
                list($role, $guard) = explode(',', $arguments.',');
78
79
                return "<?php elseif(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
80
            });
81
            $bladeCompiler->directive('endrole', function () {
82
                return '<?php endif; ?>';
83
            });
84
85 View Code Duplication
            $bladeCompiler->directive('hasrole', function ($arguments) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
                list($role, $guard) = explode(',', $arguments.',');
87
88
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
89
            });
90
            $bladeCompiler->directive('endhasrole', function () {
91
                return '<?php endif; ?>';
92
            });
93
94 View Code Duplication
            $bladeCompiler->directive('hasanyrole', function ($arguments) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
                list($roles, $guard) = explode(',', $arguments.',');
96
97
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
98
            });
99
            $bladeCompiler->directive('endhasanyrole', function () {
100
                return '<?php endif; ?>';
101
            });
102
103 View Code Duplication
            $bladeCompiler->directive('hasallroles', function ($arguments) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
                list($roles, $guard) = explode(',', $arguments.',');
105
106
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
107
            });
108
            $bladeCompiler->directive('endhasallroles', function () {
109
                return '<?php endif; ?>';
110
            });
111
112 View Code Duplication
            $bladeCompiler->directive('unlessrole', function ($arguments) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
                list($role, $guard) = explode(',', $arguments.',');
114
115
                return "<?php if(!auth({$guard})->check() || ! auth({$guard})->user()->hasRole({$role})): ?>";
116
            });
117
            $bladeCompiler->directive('endunlessrole', function () {
118
                return '<?php endif; ?>';
119
            });
120
        });
121
    }
122
123
    protected function registerMacroHelpers()
124
    {
125
        if (! method_exists(Route::class, 'macro')) { // Lumen
126
            return;
127
        }
128
129 View Code Duplication
        Route::macro('role', function ($roles = []) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
            if (! is_array($roles)) {
131
                $roles = [$roles];
132
            }
133
134
            $roles = implode('|', $roles);
135
136
            $this->middleware("role:$roles");
0 ignored issues
show
The method middleware() does not seem to exist on object<Spatie\Permission...missionServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
138
            return $this;
139
        });
140
141 View Code Duplication
        Route::macro('permission', function ($permissions = []) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
            if (! is_array($permissions)) {
143
                $permissions = [$permissions];
144
            }
145
146
            $permissions = implode('|', $permissions);
147
148
            $this->middleware("permission:$permissions");
0 ignored issues
show
The method middleware() does not seem to exist on object<Spatie\Permission...missionServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
150
            return $this;
151
        });
152
    }
153
154
    /**
155
     * Returns existing migration file if found, else uses the current timestamp.
156
     *
157
     * @param Filesystem $filesystem
158
     * @return string
159
     */
160
    protected function getMigrationFileName(Filesystem $filesystem): string
161
    {
162
        $timestamp = date('Y_m_d_His');
163
164
        return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
165
            ->flatMap(function ($path) use ($filesystem) {
166
                return $filesystem->glob($path.'*_create_permission_tables.php');
167
            })->push($this->app->databasePath()."/migrations/{$timestamp}_create_permission_tables.php")
168
            ->first();
169
    }
170
}
171