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.
Completed
Push — master ( 2373a8...c42d66 )
by Freek
01:05
created

SslCertificate::getFingerprintSha256()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\SslCertificate;
4
5
use Carbon\Carbon;
6
use Spatie\Macroable\Macroable;
7
8
class SslCertificate
9
{
10
    use Macroable;
11
12
    /** @var array */
13
    protected $rawCertificateFields = [];
14
15
    /** @var string */
16
    protected $fingerprint = '';
17
    /**
18
     * @var string
19
     */
20
    private $fingerprintSha256;
21
22
    public static function download(): Downloader
23
    {
24
        return new Downloader();
25
    }
26
27
    public static function createForHostName(string $url, int $timeout = 30): self
28
    {
29
        $sslCertificate = Downloader::downloadCertificateFromUrl($url, $timeout);
30
31
        return $sslCertificate;
32
    }
33
34
    public function __construct(array $rawCertificateFields, string $fingerprint = '', string $fingerprintSha256 = '')
35
    {
36
        $this->rawCertificateFields = $rawCertificateFields;
37
38
        $this->fingerprint = $fingerprint;
39
        $this->fingerprintSha256 = $fingerprintSha256;
40
    }
41
42
    public function getRawCertificateFields(): array
43
    {
44
        return $this->rawCertificateFields;
45
    }
46
47
    public function getIssuer(): string
48
    {
49
        return $this->rawCertificateFields['issuer']['CN'] ?? '';
50
    }
51
52
    public function getDomain(): string
53
    {
54
        if (! array_key_exists('CN', $this->rawCertificateFields['subject'])) {
55
            return '';
56
        }
57
58
        if (is_string($this->rawCertificateFields['subject']['CN'])) {
59
            return $this->rawCertificateFields['subject']['CN'];
60
        }
61
62
        if (is_array($this->rawCertificateFields['subject']['CN'])) {
63
            return $this->rawCertificateFields['subject']['CN'][0];
64
        }
65
66
        return '';
67
    }
68
69
    public function getSignatureAlgorithm(): string
70
    {
71
        return $this->rawCertificateFields['signatureTypeSN'] ?? '';
72
    }
73
74
    public function getFingerprint(): string
75
    {
76
        return $this->fingerprint;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getFingerprintSha256(): string
83
    {
84
        return $this->fingerprintSha256;
85
    }
86
87
    public function getAdditionalDomains(): array
88
    {
89
        $additionalDomains = explode(', ', $this->rawCertificateFields['extensions']['subjectAltName'] ?? '');
90
91
        return array_map(function (string $domain) {
92
            return str_replace('DNS:', '', $domain);
93
        }, $additionalDomains);
94
    }
95
96
    public function validFromDate(): Carbon
97
    {
98
        return Carbon::createFromTimestampUTC($this->rawCertificateFields['validFrom_time_t']);
99
    }
100
101
    public function expirationDate(): Carbon
102
    {
103
        return Carbon::createFromTimestampUTC($this->rawCertificateFields['validTo_time_t']);
104
    }
105
106
    public function isExpired(): bool
107
    {
108
        return $this->expirationDate()->isPast();
109
    }
110
111
    public function isValid(string $url = null)
112
    {
113
        if (! Carbon::now()->between($this->validFromDate(), $this->expirationDate())) {
0 ignored issues
show
Bug introduced by
The method between does only exist in Carbon\CarbonInterface, but not in Carbon\Traits\Creator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
114
            return false;
115
        }
116
117
        if (! empty($url)) {
118
            return $this->appliesToUrl($url ?? $this->getDomain());
119
        }
120
121
        return true;
122
    }
123
124
    public function isSelfSigned(): bool
125
    {
126
        return $this->getIssuer() === $this->getDomain();
127
    }
128
129
    public function usesSha1Hash(): bool
130
    {
131
        $certificateFields = $this->getRawCertificateFields();
132
133
        if ($certificateFields['signatureTypeSN'] === 'RSA-SHA1') {
134
            return true;
135
        }
136
137
        if ($certificateFields['signatureTypeLN'] === 'sha1WithRSAEncryption') {
138
            return true;
139
        }
140
141
        return false;
142
    }
143
144
    public function isValidUntil(Carbon $carbon, string $url = null): bool
145
    {
146
        if ($this->expirationDate()->lte($carbon)) {
147
            return false;
148
        }
149
150
        return $this->isValid($url);
151
    }
152
153
    public function daysUntilExpirationDate(): int
154
    {
155
        $endDate = $this->expirationDate();
156
157
        $interval = Carbon::now()->diff($endDate);
0 ignored issues
show
Bug introduced by
The method diff does only exist in Carbon\CarbonInterface, but not in Carbon\Traits\Creator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
158
159
        return (int) $interval->format('%r%a');
160
    }
161
162
    public function getDomains(): array
163
    {
164
        $allDomains = $this->getAdditionalDomains();
165
        $allDomains[] = $this->getDomain();
166
        $uniqueDomains = array_unique($allDomains);
167
168
        return array_values(array_filter($uniqueDomains));
169
    }
170
171
    public function appliesToUrl(string $url): bool
172
    {
173
        if(filter_var($url, FILTER_VALIDATE_IP)) {
174
            $host = $url;
175
        } else {
176
            $host = (new Url($url))->getHostName();
177
        }
178
179
        $certificateHosts = $this->getDomains();
180
181
        foreach ($certificateHosts as $certificateHost) {
182
            $certificateHost = str_replace('ip address:', '', strtolower($certificateHost));
183
            if ($host === $certificateHost) {
184
                return true;
185
            }
186
187
            if ($this->wildcardHostCoversHost($certificateHost, $host)) {
188
                return true;
189
            }
190
        }
191
192
        return false;
193
    }
194
195
    protected function wildcardHostCoversHost(string $wildcardHost, string $host): bool
196
    {
197
        if ($host === $wildcardHost) {
198
            return true;
199
        }
200
201
        if (! starts_with($wildcardHost, '*')) {
202
            return false;
203
        }
204
205
        if (substr_count($wildcardHost, '.') < substr_count($host, '.')) {
206
            return false;
207
        }
208
209
        $wildcardHostWithoutWildcard = substr($wildcardHost, 1);
210
211
        $hostWithDottedPrefix = ".{$host}";
212
213
        return ends_with($hostWithDottedPrefix, $wildcardHostWithoutWildcard);
214
    }
215
216
    public function getRawCertificateFieldsJson(): string
217
    {
218
        return json_encode($this->getRawCertificateFields());
219
    }
220
221
    public function getHash(): string
222
    {
223
        return md5($this->getRawCertificateFieldsJson());
224
    }
225
226
    public function __toString(): string
227
    {
228
        return $this->getRawCertificateFieldsJson();
229
    }
230
231
    public function containsDomain(string $domain): bool
232
    {
233
        $certificateHosts = $this->getDomains();
234
235
        foreach ($certificateHosts as $certificateHost) {
236
            if ($certificateHost == $domain) {
237
                return true;
238
            }
239
240
            if (ends_with($domain, '.'.$certificateHost)) {
241
                return true;
242
            }
243
        }
244
245
        return false;
246
    }
247
248
    public function isPreCertificate()
249
    {
250
        return array_key_exists('extensions', $this->rawCertificateFields)
251
                && array_key_exists('ct_precert_poison', $this->rawCertificateFields['extensions']);
252
    }
253
}
254