Issues (34)

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/Models/ApiCredential.php (3 issues)

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 Bytesfield\KeyManager\Models;
4
5
use Bytesfield\KeyManager\Traits\WithSearchableEncryptedAttribute;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Factories\HasFactory;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\BelongsTo;
10
use Illuminate\Database\Eloquent\SoftDeletes;
11
12
class ApiCredential extends Model
13
{
14
    use SoftDeletes;
15
    use HasFactory;
16
    use WithSearchableEncryptedAttribute;
17
18
    protected $guarded = [];
19
20
    protected $table = 'key_api_credentials';
21
22
    /**
23
     * Statues for api credential.
24
     */
25
    public const STATUSES = [
26
        'ACTIVE' => 'active',
27
        'SUSPENDED' => 'suspended',
28
    ];
29
30
    /**
31
     * Public key prefix.
32
     */
33
    public const PUBLIC_KEY_PREFIX = 'api_key_pub';
34
35
    /**
36
     * Private key prefix.
37
     */
38
    public const PRIVATE_KEY_PREFIX = 'api_key_prv';
39
40
    /**
41
     * The hidden columns.
42
     *
43
     * @var string[]
44
     */
45
    protected $hidden = [
46
        'secret_hash', 'deleted_at',
47
    ];
48
49
    /**
50
     * The encrypted field or column.
51
     *
52
     * @var string
53
     */
54
    protected $encrypted = 'private_key';
55
56
    /**
57
     * The blind index field or column.
58
     *
59
     * @var string
60
     */
61
    protected $blindIndex = 'secret_hash';
62
63
    /**
64
     * Get the client for the api key.
65
     *
66
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
67
     */
68
    public function client(): BelongsTo
69
    {
70
        return $this->belongsTo(Client::class, 'id');
71
    }
72
73
    /**
74
     * Sets the value for private key column.
75
     *
76
     * @param string $key
77
     *
78
     * @return void
79
     */
80
    public function setPrivateKeyAttribute(string $key): void
81
    {
82
        $this->attributes['private_key'] = $this->encrypt($key);
83
    }
84
85
    /**
86
     * Sets the value secret hash column.
87
     *
88
     * @param string|null $hash
89
     *
90
     * @throws \ParagonIE\CipherSweet\Exception\BlindIndexNotFoundException
91
     * @throws \ParagonIE\CipherSweet\Exception\CryptoOperationException
92
     * @throws \SodiumException
93
     *
94
     * @return void
95
     */
96
    public function setSecretHashAttribute(?string $hash = null): void
97
    {
98
        $this->attributes['secret_hash'] = $this->blindIndexValue();
99
    }
100
101
    /**
102
     * Get the value for private key column.
103
     *
104
     * @param string $encryptedKey
105
     *
106
     * @return mixed
107
     */
108
    public function getPrivateKeyAttribute(string $encryptedKey)
109
    {
110
        return $this->decrypt($encryptedKey);
111
    }
112
113
    /**
114
     * Add where clause for status equals active.
115
     *
116
     * @param \Illuminate\Database\Eloquent\Builder $query
117
     *
118
     * @return \Illuminate\Database\Eloquent\Builder
119
     */
120
    public function scopeActive(Builder $query): Builder
121
    {
122
        return $query->where('status', self::STATUSES['ACTIVE']);
123
    }
124
125
    /**
126
     * Add where clause for status equals active.
127
     *
128
     * @param \Illuminate\Database\Eloquent\Builder $query
129
     *
130
     * @return \Illuminate\Database\Eloquent\Builder
131
     */
132
    public function scopeSuspended(Builder $query): Builder
133
    {
134
        return $query->where('status', self::STATUSES['SUSPENDED']);
135
    }
136
137
    /**
138
     * Find client active api keys by private key.
139
     *
140
     * @param string $privateKey
141
     *
142
     * @return static|null
143
     */
144
    public static function findActiveByPrivateKey(string $privateKey): ?self
145
    {
146
        return static::query()
147
            ->with('client')
148
            ->active()
149
            ->whereBlindIndex($privateKey)
150
            ->first();
151
    }
152
153
    /**
154
     * Find client suspended api keys by private key.
155
     *
156
     * @param string $privateKey
157
     *
158
     * @return static|null
159
     */
160
    public static function findSuspendedByPrivateKey(string $privateKey): ?self
161
    {
162
        return static::query()
163
            ->with('client')
164
            ->suspended()
165
            ->whereBlindIndex($privateKey)
166
            ->first();
167
    }
168
169
    /**
170
     * Find client api keys by private key.
171
     *
172
     * @param string $privateKey
173
     *
174
     * @return static|null
175
     */
176
    public static function findByPrivateKey(string $privateKey): ?self
177
    {
178
        return self::withTrashed()
0 ignored issues
show
The method with does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloq...\Database\Query\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
179
            ->with('client')
180
            ->whereBlindIndex($privateKey)
181
            ->first();
182
    }
183
184
    /**
185
     * Generates the public and secret key pairs for both live and test domains.
186
     * @throws \Exception
187
     *
188
     * @return array
189
     */
190
    public static function generateKeyPairArray(): array
191
    {
192
        [$publicKey, $privateKey] = self::generatePublicPrivateKeys();
0 ignored issues
show
The variable $publicKey does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $privateKey does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
193
194
        return [
195
            'public_key' => self::PUBLIC_KEY_PREFIX.'_'.$publicKey,
196
            'private_key' => self::PRIVATE_KEY_PREFIX.'_'.$privateKey,
197
            'secret_hash' => null,
198
            'status' => self::STATUSES['ACTIVE'],
199
        ];
200
    }
201
202
    /**
203
     * Generate unique public and secret key pairs.
204
     * @throws \Exception
205
     *
206
     * @return array
207
     */
208
    private static function generatePublicPrivateKeys(): array
209
    {
210
        do {
211
            $privateKey = bin2hex(random_bytes(20));
212
            $publicKey = bin2hex(random_bytes(20));
213
214
            $secretHash = (new self())->getBlindIndexValueFor($privateKey);
215
            $hashExists = self::where('secret_hash', $secretHash)->withTrashed()->exists();
216
        } while ($hashExists);
217
218
        return [$publicKey, $privateKey];
219
    }
220
}
221