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/PermissionRegistrar.php (1 issue)

Labels
Severity

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\Cache\CacheManager;
6
use Illuminate\Support\Collection;
7
use Spatie\Permission\Contracts\Role;
8
use Illuminate\Contracts\Auth\Access\Gate;
9
use Spatie\Permission\Contracts\Permission;
10
use Illuminate\Contracts\Auth\Access\Authorizable;
11
12
class PermissionRegistrar
13
{
14
    /** @var \Illuminate\Contracts\Cache\Repository */
15
    protected $cache;
16
17
    /** @var \Illuminate\Cache\CacheManager */
18
    protected $cacheManager;
19
20
    /** @var string */
21
    protected $permissionClass;
22
23
    /** @var string */
24
    protected $roleClass;
25
26
    /** @var \Illuminate\Support\Collection */
27
    protected $permissions;
28
29
    /** @var \DateInterval|int */
30
    public static $cacheExpirationTime;
31
32
    /** @var string */
33
    public static $cacheKey;
34
35
    /** @var string */
36
    public static $cacheModelKey;
37
38
    /**
39
     * PermissionRegistrar constructor.
40
     *
41
     * @param \Illuminate\Cache\CacheManager $cacheManager
42
     */
43
    public function __construct(CacheManager $cacheManager)
44
    {
45
        $this->permissionClass = config('permission.models.permission');
46
        $this->roleClass = config('permission.models.role');
47
48
        $this->cacheManager = $cacheManager;
49
        $this->initializeCache();
50
    }
51
52
    protected function initializeCache()
53
    {
54
        self::$cacheExpirationTime = config('permission.cache.expiration_time', config('permission.cache_expiration_time'));
55
56
        self::$cacheKey = config('permission.cache.key');
57
        self::$cacheModelKey = config('permission.cache.model_key');
58
59
        $this->cache = $this->getCacheStoreFromConfig();
60
    }
61
62
    protected function getCacheStoreFromConfig(): \Illuminate\Contracts\Cache\Repository
63
    {
64
        // the 'default' fallback here is from the permission.php config file, where 'default' means to use config(cache.default)
65
        $cacheDriver = config('permission.cache.store', 'default');
66
67
        // when 'default' is specified, no action is required since we already have the default instance
68
        if ($cacheDriver === 'default') {
69
            return $this->cacheManager->store();
70
        }
71
72
        // if an undefined cache store is specified, fallback to 'array' which is Laravel's closest equiv to 'none'
73
        if (! \array_key_exists($cacheDriver, config('cache.stores'))) {
74
            $cacheDriver = 'array';
75
        }
76
77
        return $this->cacheManager->store($cacheDriver);
78
    }
79
80
    /**
81
     * Register the permission check method on the gate.
82
     * We resolve the Gate fresh here, for benefit of long-running instances.
83
     *
84
     * @return bool
85
     */
86
    public function registerPermissions(): bool
87
    {
88
        app(Gate::class)->before(function (Authorizable $user, string $ability) {
89
            if (method_exists($user, 'checkPermissionTo')) {
90
                return $user->checkPermissionTo($ability) ?: null;
0 ignored issues
show
The method checkPermissionTo() does not seem to exist on object<Illuminate\Contra...th\Access\Authorizable>.

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...
91
            }
92
        });
93
94
        return true;
95
    }
96
97
    /**
98
     * Flush the cache.
99
     */
100
    public function forgetCachedPermissions()
101
    {
102
        $this->permissions = null;
103
104
        return $this->cache->forget(self::$cacheKey);
105
    }
106
107
    /**
108
     * Clear class permissions.
109
     * This is only intended to be called by the PermissionServiceProvider on boot,
110
     * so that long-running instances like Swoole don't keep old data in memory.
111
     */
112
    public function clearClassPermissions()
113
    {
114
        $this->permissions = null;
115
    }
116
117
    /**
118
     * Get the permissions based on the passed params.
119
     *
120
     * @param array $params
121
     *
122
     * @return \Illuminate\Support\Collection
123
     */
124
    public function getPermissions(array $params = []): Collection
125
    {
126
        if ($this->permissions === null) {
127
            $this->permissions = $this->cache->remember(self::$cacheKey, self::$cacheExpirationTime, function () {
128
                return $this->getPermissionClass()
129
                    ->with('roles')
130
                    ->get();
131
            });
132
        }
133
134
        $permissions = clone $this->permissions;
135
136
        foreach ($params as $attr => $value) {
137
            $permissions = $permissions->where($attr, $value);
138
        }
139
140
        return $permissions;
141
    }
142
143
    /**
144
     * Get an instance of the permission class.
145
     *
146
     * @return \Spatie\Permission\Contracts\Permission
147
     */
148
    public function getPermissionClass(): Permission
149
    {
150
        return app($this->permissionClass);
151
    }
152
153
    public function setPermissionClass($permissionClass)
154
    {
155
        $this->permissionClass = $permissionClass;
156
157
        return $this;
158
    }
159
160
    /**
161
     * Get an instance of the role class.
162
     *
163
     * @return \Spatie\Permission\Contracts\Role
164
     */
165
    public function getRoleClass(): Role
166
    {
167
        return app($this->roleClass);
168
    }
169
170
    /**
171
     * Get the instance of the Cache Store.
172
     *
173
     * @return \Illuminate\Contracts\Cache\Store
174
     */
175
    public function getCacheStore(): \Illuminate\Contracts\Cache\Store
176
    {
177
        return $this->cache->getStore();
178
    }
179
}
180