1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Security\Protections;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* Class Csrf
|
20
|
|
|
*
|
21
|
|
|
* @package O2System\Security\Protections
|
22
|
|
|
*/
|
23
|
|
|
class Csrf
|
24
|
|
|
{
|
25
|
|
|
/**
|
26
|
|
|
* Csrf::$token
|
27
|
|
|
*
|
28
|
|
|
* Active CSRF protection token.
|
29
|
|
|
*
|
30
|
|
|
* @var string
|
31
|
|
|
*/
|
32
|
|
|
private $token;
|
33
|
|
|
|
34
|
|
|
// ------------------------------------------------------------------------
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* Csrf::__construct
|
38
|
|
|
*/
|
39
|
|
|
public function __construct()
|
40
|
|
|
{
|
41
|
|
|
if (false === ($this->token = $this->getToken())) {
|
|
|
|
|
42
|
|
|
$this->regenerate();
|
43
|
|
|
}
|
44
|
|
|
}
|
45
|
|
|
|
46
|
|
|
// ------------------------------------------------------------------------
|
47
|
|
|
|
48
|
|
|
/**
|
49
|
|
|
* Csrf::getToken
|
50
|
|
|
*
|
51
|
|
|
* Gets session CSRF protection token.
|
52
|
|
|
*
|
53
|
|
|
* @return string|bool Returns FALSE if CSRF protection token is not set.
|
54
|
|
|
*/
|
55
|
|
|
public function getToken()
|
56
|
|
|
{
|
57
|
|
|
if (isset($_SESSION[ 'csrfToken' ])) {
|
58
|
|
|
return $_SESSION[ 'csrfToken' ];
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
return false;
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
// ------------------------------------------------------------------------
|
65
|
|
|
|
66
|
|
|
/**
|
67
|
|
|
* Csrf::regenerate
|
68
|
|
|
*
|
69
|
|
|
* Regenerate CSRF protection token.
|
70
|
|
|
*
|
71
|
|
|
* @return void
|
72
|
|
|
*/
|
73
|
|
|
public function regenerate()
|
74
|
|
|
{
|
75
|
|
|
$_SESSION[ 'csrfToken' ] = $this->token = 'CSRF-' . bin2hex(random_bytes(32));
|
76
|
|
|
}
|
77
|
|
|
|
78
|
|
|
// ------------------------------------------------------------------------
|
79
|
|
|
|
80
|
|
|
/**
|
81
|
|
|
* Csrf::verify
|
82
|
|
|
*
|
83
|
|
|
* Checks if the posted CSRF protection token is valid.
|
84
|
|
|
*
|
85
|
|
|
* @param string $token
|
86
|
|
|
*
|
87
|
|
|
* @return bool
|
88
|
|
|
*/
|
89
|
|
|
public function verify($token = null)
|
90
|
|
|
{
|
91
|
|
|
$token = isset($token)
|
92
|
|
|
? $token
|
93
|
|
|
: input()->postGet('csrf-token');
|
|
|
|
|
94
|
|
|
|
95
|
|
|
if (is_string($token)) {
|
96
|
|
|
return hash_equals($this->getToken(), $token);
|
97
|
|
|
}
|
98
|
|
|
|
99
|
|
|
return false;
|
100
|
|
|
}
|
101
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.