Issues (194)

Security Analysis    no vulnerabilities found

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/Utility/RedisUtility.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 Ps2alerts\Api\Utility;
4
5
use Ps2alerts\Api\Contract\RedisAwareInterface;
6
use Ps2alerts\Api\Contract\RedisAwareTrait;
7
8
class RedisUtility implements RedisAwareInterface
9
{
10
    use RedisAwareTrait;
11
12
    protected $flag = 'NOT-USED';
13
    protected $misses = 0;
14
    protected $hits =  0;
15
16
    /**
17
     * Sets the Redis Missed Flag
18
     *
19
     * @param string $flag
20
     */
21
    public function setFlag($flag)
22
    {
23
        $this->flag = $flag;
24
    }
25
26
    /**
27
     * Sets the Redis Missed Flag, which the response class will pull out to send to the headers
28
     *
29
     * @return string
30
     */
31
    public function getFlag()
32
    {
33
        return $this->flag;
34
    }
35
36
    /**
37
     * Sets the Redis Missed Flag, which the response class will pull out to send to the headers
38
     *
39
     * @return int
40
     */
41
    public function getMissCount()
42
    {
43
        return $this->misses;
44
    }
45
46
    /**
47
     * Sets the Redis Missed Flag, which the response class will pull out to send to the headers
48
     *
49
     * @return int
50
     */
51
    public function getHitCount()
52
    {
53
        return $this->hits;
54
    }
55
56
    /**
57
     * Checks redis for a entry and returns it decoded if exists
58
     *
59
     * @param string $store Redis store to pull data from
60
     * @param string $type player|outfit
61
     * @param string $id   ID of player or outfit
62
     * @param string $encodeType Flag to change format of data
63
     *
64
     * @return string|boolean
65
     */
66
    public function checkRedis($store = 'api', $type, $id, $encodeType = 'array')
67
    {
68
        $redis = $this->getRedisDriver();
69
70
        $key = "ps2alerts:{$store}:{$type}:{$id}";
71
72
        if ($redis->exists($key)) {
73
            if ($encodeType === 'object') {
74
                $data = json_decode($redis->get($key));
75
            } else if ($encodeType === 'array') {
76
                $data = json_decode($redis->get($key), true);
77
            }
78
79
            $this->checkFlagScenario('hit');
80
            $this->hits++;
81
82
            return $data;
0 ignored issues
show
The variable $data does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
83
        }
84
85
        $this->checkFlagScenario('miss');
86
        $this->misses++;
87
88
        return false;
89
    }
90
91
    /**
92
     * Stores the complete information in Redis
93
     *
94
     * @param  string  $namespace Split between cache and API
95
     * @param  string  $type
96
     * @param  string  $id
97
     * @param  string  $data
98
     * @param  integer $time Time in seconds to store data
99
     *
100
     * @throws \Exception
101
     *
102
     * @return boolean
103
     */
104
    public function storeInRedis($namespace = 'api', $type, $id, $data, $time = 0)
105
    {
106
        $redis = $this->getRedisDriver();
107
        $key = "ps2alerts:{$namespace}:{$type}:{$id}";
108
109
        $data = json_encode($data);
110
111
        // Check for errors #BRINGJSONEXCEPTIONS!
112
        if (json_last_error() !== JSON_ERROR_NONE) {
113
            throw new \Exception();
114
        }
115
116
        if (!$time) {
117
            $time = 3600 * 24; // 1 day
118
        }
119
120
        $this->checkFlagScenario('store');
121
122
        return $redis->setEx($key, $time, $data);
123
    }
124
125
126
    /**
127
     * Sets the missed flag for Redis based on scenario.
128
     *
129
     * @param string $mode
130
     *
131
     * @return void
132
     */
133
    public function checkFlagScenario(string $mode)
134
    {
135
        /*
136
            If all keys are missing: Miss
137
            If one key is found: Partial-Hit
138
            If ALL keys are found: Hit
139
        */
140
141
        if ($this->getFlag() === 'NOT-USED' || $this->getFlag() !== 'MISS - STORED') {
142
            if ($mode === 'miss') {
143
                $this->setFlag('MISS');
144
            }
145
            if ($mode === 'store') {
146
                $this->setFlag('MISS - STORED');
147
            }
148
            if ($mode === 'hit') {
149
                $this->setFlag('HIT');
150
            }
151
        }
152
153
        // If we've had a miss and we've previously marked as a hit, downgrade to Partial Hit.
154
        if ($mode === 'miss' && $this->getFlag() === 'HIT') {
155
            $this->setFlag('PARTIAL-HIT');
156
        }
157
158
        // Already set to Miss
159
    }
160
}