-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecurityChecker.php
177 lines (160 loc) · 5.5 KB
/
SecurityChecker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace Ows\ComposerDependenciesSecurityChecker;
use Composer\Semver\Semver;
use Exception;
/**
* Service to check the security state of a project from a composer.lock file.
*
* Checks performed against the **conflict** section of a composer.json file,
* like the one from this repo: https://github.com/Roave/SecurityAdvisories.
*/
class SecurityChecker
{
/**
* A list of security advisories urls of composer.json files.
*
* @var array
*/
protected $urlsAdvisories = ['https://raw.githubusercontent.com/Roave/SecurityAdvisories/master/composer.json'];
/**
* Static cache for data of each security advisories url.
*
* @var array
*/
protected static $urlsComposerAdvisoriesData;
/**
* @param array $urls_advisories
* A list of security advisories urls.
*/
public function __construct(array $urls_advisories = [])
{
if (!empty($urls_advisories)) {
$this->urlsAdvisories = $urls_advisories;
}
}
/**
* Global function to made several checks on a same composer.lock file.
*
* @param string $composer_lock
* The composer.lock file to check.
* @param bool $exclude_dev
* Only check for production dependencies.
*
* @return array
* An array with the status and the possibles vulnerabilities founded.
* The status can be "ok" or "vulnerable" and in this case, vulnerabilities
* is an array with each vulnerability (array: name, version, links).
*
* @throws \Exception
*/
public function checkComposer($composer_lock, bool $exclude_dev = false)
{
$vulnerabilities = [];
$status = 'ok';
$composer_lock_data = json_decode($composer_lock, true);
foreach ($this->urlsAdvisories as $url) {
$security_advisories_composer_json = $this->fetchAdvisoryComposerJson($url);
$updates = $this->calculateSecurityUpdates(
$composer_lock_data,
$security_advisories_composer_json,
$exclude_dev
);
if (!empty($updates)) {
$vulnerabilities += $updates;
$status = 'vulnerable';
}
}
return ['status' => $status, 'vulnerabilities' => $vulnerabilities];
}
/**
* Fetches the generated composer.json from a security-advisory.
*
* @param string $url
* Url of composer.json of the security-advisory.
*
* @return array
* Data decoded from the composer.json.
*
* @throws \Exception
*/
protected function fetchAdvisoryComposerJson($url)
{
if (!isset(static::$urlsComposerAdvisoriesData[$url])) {
$response = file_get_contents($url);
if ($response) {
$security_advisories = json_decode($response, true);
} else {
$security_advisories = false;
}
static::$urlsComposerAdvisoriesData[$url] = $security_advisories;
}
if (empty(static::$urlsComposerAdvisoriesData[$url])) {
throw new Exception(
"Unable to fetch security-advisories information from $url."
);
}
return static::$urlsComposerAdvisoriesData[$url];
}
/**
* Return available security updates.
*
* @param array $composer_lock_data
* The contents of a composer.lock file.
* @param array $security_advisories_composer_json
* The composer.json array from the security-advisory.
* @param bool $exclude_dev
* Only check for production dependencies.
*
* @return array
* Security updates availables, keyed by package name,
* with those informations:
* - name
* - version
* - links.
*/
protected function calculateSecurityUpdates(array $composer_lock_data, array $security_advisories_composer_json, bool $exclude_dev = false)
{
if ($exclude_dev) {
$packages = $composer_lock_data['packages'];
}
else {
$packages = array_merge(
$composer_lock_data['packages-dev'],
$composer_lock_data['packages']
);
}
$updates = [];
$conflict = $security_advisories_composer_json['conflict'];
foreach ($packages as $package) {
$name = $package['name'];
$version = $package['version'];
if (empty($conflict[$name]) || !Semver::satisfies($version, $conflict[$name])) {
continue;
}
if (mb_strpos($package['type'], 'drupal-') === 0) {
if ($name == 'drupal/core') {
$module_name = 'drupal';
} else {
$module_name = str_replace('drupal/', '', $name);
$version = $package['source']['reference'];
}
$title = $module_name . ' : Version ' . $version;
$link = "https://www.drupal.org/project/$module_name/releases/" . $version;
} else {
$title = $name . ' : Version ' . $version;
$link = "https://packagist.org/packages/$name#" . $version;
}
$updates[$name] = [
'name' => $name,
'version' => $version,
'links' => [
[
'title' => $title,
'link' => $link,
],
],
];
}
return $updates;
}
}