This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
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 Rudolf\Component\Auth; |
||
4 | |||
5 | use PDO; |
||
6 | |||
7 | class Session |
||
8 | { |
||
9 | /** |
||
10 | * @var PDO |
||
11 | */ |
||
12 | private $pdo; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $prefix; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $config; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $cookieName = 'auth'; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $table; |
||
33 | |||
34 | /** |
||
35 | * Session constructor. |
||
36 | * |
||
37 | * @param PDO $pdo |
||
38 | * @param $prefix |
||
39 | * @param array $config |
||
40 | */ |
||
41 | public function __construct(PDO $pdo, $prefix, array $config) |
||
42 | { |
||
43 | $this->pdo = $pdo; |
||
44 | $this->prefix = $prefix; |
||
45 | $this->config = $config; |
||
46 | |||
47 | $this->table = $this->prefix.'users_sessions'; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Add user session. |
||
52 | * |
||
53 | * @param array $user |
||
54 | * |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function createSession($user) |
||
58 | { |
||
59 | $ip = $this->getIP(); |
||
60 | $userAgent = $_SERVER['HTTP_USER_AGENT']; |
||
61 | |||
62 | $session['cookie_hash'] = sha1($this->config['site_key'].microtime()); |
||
0 ignored issues
–
show
|
|||
63 | $session['hash'] = $this->cookieHash($session['cookie_hash']); |
||
64 | |||
65 | $session['expire'] = date('Y-m-d H:i:s', strtotime($this->config['session_expire'])); |
||
66 | |||
67 | $cookie = new Cookie($this->cookieName); |
||
68 | |||
69 | if (true === $cookie->isExist()) { |
||
70 | $this->destroySession(); |
||
71 | } |
||
72 | |||
73 | $cookie->setValue($session['cookie_hash']); |
||
74 | $cookie->setExpire(strtotime($session['expire'])); |
||
75 | $cookie->setPath(DIR.'/'); |
||
76 | |||
77 | if (false === $cookie->create()) { |
||
78 | return false; |
||
79 | } |
||
80 | |||
81 | $stmt = $this->pdo->prepare(" |
||
82 | INSERT INTO {$this->table} |
||
83 | (user_id |
||
84 | , hash |
||
85 | , expire |
||
86 | , ip |
||
87 | , useragent |
||
88 | , cookie) |
||
89 | VALUES |
||
90 | (:user_id |
||
91 | , :hash |
||
92 | , :expire |
||
93 | , :ip |
||
94 | , :useragent |
||
95 | , :cookie) |
||
96 | "); |
||
97 | $stmt->bindValue(':user_id', $user['id'], \PDO::PARAM_INT); |
||
98 | $stmt->bindValue(':hash', $session['hash'], \PDO::PARAM_STR); |
||
99 | $stmt->bindValue(':expire', $session['expire'], \PDO::PARAM_STR); |
||
100 | $stmt->bindValue(':ip', $ip, \PDO::PARAM_STR); |
||
101 | $stmt->bindValue(':useragent', $userAgent, \PDO::PARAM_STR); |
||
102 | $stmt->bindValue(':cookie', $session['cookie_hash'], \PDO::PARAM_STR); |
||
103 | |||
104 | if (false === $stmt->execute()) { |
||
105 | return false; |
||
106 | } |
||
107 | |||
108 | return true; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Destroy session. |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function destroySession() |
||
117 | { |
||
118 | $cookie = new Cookie($this->cookieName); |
||
119 | $value = $cookie->getValue(); |
||
120 | $cookie->destroy(); |
||
121 | |||
122 | if (strlen($value) !== 40) { |
||
123 | return false; |
||
124 | } |
||
125 | |||
126 | $stmt = $this->pdo->prepare("DELETE FROM {$this->table} WHERE cookie = :cookie"); |
||
127 | $stmt->bindValue(':cookie', $value, \PDO::PARAM_STR); |
||
128 | |||
129 | return $stmt->execute(); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Check session. |
||
134 | * |
||
135 | * @return bool |
||
136 | */ |
||
137 | public function checkSession() |
||
138 | { |
||
139 | $cookie = new Cookie($this->cookieName); |
||
140 | |||
141 | $cookie_value = $cookie->getValue(); |
||
142 | $ip = $this->getIP(); |
||
143 | $useragent = $_SERVER['HTTP_USER_AGENT']; |
||
0 ignored issues
–
show
$useragent 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 ![]() |
|||
144 | |||
145 | #check length |
||
146 | if (strlen($cookie_value) !== 40) { |
||
147 | return false; |
||
148 | } |
||
149 | |||
150 | $stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE cookie = :cookie"); |
||
151 | $stmt->bindValue(':cookie', $cookie_value, \PDO::PARAM_STR); |
||
152 | $stmt->execute(); |
||
153 | |||
154 | #any session exists |
||
155 | if (0 === $stmt->rowCount()) { |
||
156 | return false; |
||
157 | } |
||
158 | |||
159 | $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
||
160 | |||
161 | #check expire |
||
162 | if (strtotime(date('Y-m-d H:i:s')) > strtotime($row['expire'])) { |
||
163 | $this->destroySession(); |
||
164 | |||
165 | return false; |
||
166 | } |
||
167 | |||
168 | #check ip |
||
169 | if ($ip !== $row['ip']) { |
||
170 | return false; |
||
171 | } |
||
172 | |||
173 | if ($this->cookieHash($cookie_value) !== $row['hash']) { |
||
174 | return false; |
||
175 | } |
||
176 | |||
177 | return true; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Returns the UID. |
||
182 | * |
||
183 | * @param string|bool $hash |
||
184 | * |
||
185 | * @return int|false $uid |
||
186 | */ |
||
187 | View Code Duplication | public function getSessionUID($hash = false) |
|
0 ignored issues
–
show
This method seems to be duplicated in 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. ![]() |
|||
188 | { |
||
189 | if (false === $hash) { |
||
190 | $cookie = new Cookie($this->cookieName); |
||
191 | $hash = $cookie->getValue(); |
||
192 | } |
||
193 | |||
194 | $stmt = $this->pdo->prepare("SELECT user_id FROM {$this->table} WHERE cookie = ?"); |
||
195 | $stmt->execute(array($hash)); |
||
196 | |||
197 | if ($stmt->rowCount() == 0) { |
||
198 | return false; |
||
199 | } |
||
200 | |||
201 | return $stmt->fetch(\PDO::FETCH_ASSOC)['user_id']; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Hash cookie hash. |
||
206 | * |
||
207 | * @param string $cookie |
||
208 | * |
||
209 | * @return string(64) |
||
0 ignored issues
–
show
The doc-type
string(64) could not be parsed: Expected "|" or "end of type", but got "(" at position 6. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. ![]() |
|||
210 | */ |
||
211 | private function cookieHash($cookie) |
||
212 | { |
||
213 | return hash('sha256', $cookie.$this->config['site_key']); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Get user ip address. |
||
218 | * |
||
219 | * @see https://www.chriswiegman.com/2014/05/getting-correct-ip-address-php/ |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getIP() |
||
224 | { |
||
225 | |||
226 | //Just get the headers if we can or else use the SERVER global |
||
227 | if (function_exists('apache_request_headers')) { |
||
228 | $headers = apache_request_headers(); |
||
229 | } else { |
||
230 | $headers = $_SERVER; |
||
231 | } |
||
232 | //Get the forwarded IP if it exists |
||
233 | if (array_key_exists('X-Forwarded-For', $headers) && filter_var($headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
||
234 | $the_ip = $headers['X-Forwarded-For']; |
||
235 | } elseif (array_key_exists('HTTP_X_FORWARDED_FOR', $headers) && filter_var($headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) |
||
236 | ) { |
||
237 | $the_ip = $headers['HTTP_X_FORWARDED_FOR']; |
||
238 | } else { |
||
239 | $the_ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); |
||
240 | } |
||
241 | |||
242 | return $the_ip; |
||
243 | } |
||
244 | } |
||
245 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.