GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Csrf   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A regenerate() 0 3 1
A getToken() 0 7 2
A verify() 0 11 3
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())) {
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getToken() can also be of type boolean. However, the property $token is declared as type string. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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');
0 ignored issues
show
Bug introduced by
The method postGet() does not exist on O2System\Kernel\Cli\Input. Did you maybe mean post()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
            : input()->/** @scrutinizer ignore-call */ postGet('csrf-token');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
95
        if (is_string($token)) {
96
            return hash_equals($this->getToken(), $token);
97
        }
98
99
        return false;
100
    }
101
}