Issues (47)

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/Rand/Rand.php (7 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
 * Part of twLib
4
 * http://www.thomaswhiston.com
5
 * [email protected]
6
 * Created by PhpStorm.
7
 * User: Thomas Whiston
8
 * Date: 04/01/2016
9
 * Time: 23:04
10
 */
11
namespace twhiston\twLib\Rand;
12
13
/**
14
 * Class Rand
15
 * Generate random ints within range or random string
16
 * Will always try to use php7 functions where possible, but if earlier only semi secure int generation is possible
17
 * @package twhiston\twLib
18
 * @deprecated use the symfony polyfill for real secure numbers
19
 */
20
class Rand
21
{
22
23
24
    /**
25
     * Generate random int or throws exception. NON SECURE
26
     * @param $min
27
     * @param $max
28
     * @return int|null
29
     * @throws \twhiston\twLib\TwLibException
30
     */
31
    static public function Int($min, $max)
0 ignored issues
show
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
32
    {
33
34
        //Ensure order correctness
35
        if ($min > $max) {
36
            $temp = $max;
37
            $max = $min;
38
            $min = $temp;
39
        }
40
        $rand = null;
41
        if (phpversion() >= 7) {
42
            //random_int is php7 only
43
            try {
44
                $rand = \random_int($min, $max);
45
            } catch (\Exception $e) {
46
                $rand = \mt_rand($min, $max);
47
            }
48
        } else {
49
            $rand = \mt_rand($min, $max);
50
        }
51
52
        if ($rand === null) {
53
            //We must not be NULL here, we could be 0, but if we are NULL then something went wrong with generation
54
            throw new TwLibException('Rand could not be generated');
55
        }
56
57
        return $rand;
58
    }
59
60
    /**
61
     * Manual secure int version based on http://stackoverflow.com/questions/1313223/replace-rand-with-openssl-random-pseudo-bytes SECURE-ish
62
     * @param $min
63
     * @param $max
64
     * @param bool|TRUE $pedantic
65
     * @return int|null
66
     * @throws \twhiston\twLib\TwLibException
67
     */
68
    static public function SecureInt($min, $max, $pedantic = true)
0 ignored issues
show
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
69
    {
70
71
        //Ensure order correctness
72
        if ($min > $max) {
73
            $temp = $max;
74
            $max = $min;
75
            $min = $temp;
76
        }
77
        $rand = null;
78
        $manual = true;
79
        if (phpversion() >= 7) {
80
            try {
81
                $rand = random_int($min, $max);
82
                $manual = false;
83
            } catch (\Exception $e) {
84
                $manual = true;
85
            }
86
        }
87
88
        if ($manual === true) {
89
            //http://stackoverflow.com/questions/1313223/replace-rand-with-openssl-random-pseudo-bytes
90
            $secure = false;
91
            $diff = $max - $min;
92
            if ($diff <= 0) {
93
                return $min;
94
            } // not so random...
95
            $range = $diff + 1; // because $max is inclusive
96
            $bits = ceil(log(($range), 2));
97
            $bytes = ceil($bits / 8.0);
98
            $bits_max = 1 << $bits;
99
            // e.g. if $range = 3000 (bin: 101110111000)
100
            //  +--------+--------+
101
            //  |....1011|10111000|
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
            //  +--------+--------+
103
            //  bits=12, bytes=2, bits_max=2^12=4096
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
            $num = 0;
0 ignored issues
show
$num is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
105
            do {
106
                $num = hexdec(
107
                        bin2hex(openssl_random_pseudo_bytes($bytes, $secure))
108
                    ) % $bits_max;
109
                if ($secure === false) {
110
                    throw new TwLibException(
111
                        'Non secure value generated. This is a system issue'
112
                    );
113
                }
114
                if ($num >= $range) {
115
                    if ($pedantic) {
116
                        continue;
117
                    } // start over instead of accepting bias
118
                    // else
119
                    $num = $num % $range;  // to hell with security
120
                }
121
                break;
122
            } while (true);
123
            $rand = $num + $min;
124
        }
125
        if ($rand === null) {
126
            //We must not be NULL here, we could be 0, but if we are NULL then something went wrong with generation
127
            throw new TwLibException('Rand could not be generated');
128
        }
129
130
        return $rand;
131
    }
132
133
    /**
134
     * Generate a random string of length. SECURE
135
     * @param $length
136
     * @return null|string
137
     * @throws \twhiston\twLib\TwLibException if string is generated non securely or result is null
138
     */
139
    static public function String($length)
0 ignored issues
show
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
140
    {
141
142
        $string = null;
0 ignored issues
show
$string is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
143
        $length /= 2;
144
        if (phpversion() >= 7) {
145
            $string = bin2hex(random_bytes($length));
146
        } else {
147
            $secure = false;
148
            $string = bin2hex(openssl_random_pseudo_bytes($length, $secure));
149
            if ($secure === false) {
150
                throw new TwLibException(
151
                    'Non secure string generated. This is a system issue'
152
                );
153
            }
154
        }
155
156
        if ($string === null) {
157
            throw new TwLibException('Random string is NULL, PANIC');
158
        }
159
160
        return $string;
161
162
    }
163
164
}