This repository was archived by the owner on Mar 1, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.php
247 lines (203 loc) · 7.18 KB
/
Plugin.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
declare(strict_types=1);
/**
* Copyright (c) 2018-2020 Daniel Bannert
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/narrowspark/automatic
*/
namespace Narrowspark\Automatic\Security;
use Composer\Composer;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Plugin\Capability\CommandProvider as CommandProviderContract;
use Composer\Plugin\Capable;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents as ComposerScriptEvents;
use FilesystemIterator;
use Narrowspark\Automatic\Common\AbstractContainer;
use Narrowspark\Automatic\Common\Util;
use Narrowspark\Automatic\Security\Contract\Audit as AuditContract;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
use Symfony\Component\Console\Input\InputInterface;
final class Plugin implements Capable, EventSubscriberInterface, PluginInterface
{
/** @var string */
public const VERSION = '0.13.1';
/** @var string */
public const COMPOSER_EXTRA_KEY = 'audit';
/** @var string */
public const PACKAGE_NAME = 'narrowspark/automatic-security-audit';
/** @var string */
private const NAME = 'no-dev';
/**
* A Container instance.
*
* @var \Narrowspark\Automatic\Common\Contract\Container
*/
private $container;
/**
* Found package vulnerabilities.
*
* @var array[]
*/
private $foundVulnerabilities = [];
/**
* Check if the the plugin is activated.
*
* @var bool
*/
private static $activated = true;
/**
* Check if the package should run in uninstall mode.
*
* @var bool
*/
private $uninstallMode = false;
/**
* {@inheritdoc}
*
* @return mixed[][][]
*/
public static function getSubscribedEvents(): array
{
if (! self::$activated) {
return [];
}
return [
PackageEvents::POST_PACKAGE_INSTALL => [['auditPackage', ~\PHP_INT_MAX]],
PackageEvents::POST_PACKAGE_UPDATE => [['auditPackage', ~\PHP_INT_MAX]],
ComposerScriptEvents::POST_INSTALL_CMD => [['auditComposerLock', \PHP_INT_MAX]],
ComposerScriptEvents::POST_UPDATE_CMD => [['auditComposerLock', \PHP_INT_MAX], ['onPostUpdatePostMessages', ~\PHP_INT_MAX]],
];
}
/**
* {@inheritdoc}
*/
public function activate(Composer $composer, IOInterface $io): void
{
// to avoid issues when Automatic is upgraded, we load all PHP classes now
// that way, we are sure to use all files from the same version.
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__, FilesystemIterator::SKIP_DOTS)) as $file) {
/** @var SplFileInfo $file */
if (\substr($file->getFilename(), -4) === '.php') {
\class_exists(__NAMESPACE__ . \str_replace('/', '\\', \substr($file->getFilename(), \strlen(__DIR__), -4)));
}
}
if (! \class_exists(AbstractContainer::class)) {
require __DIR__ . \DIRECTORY_SEPARATOR . 'alias.php';
}
if (($errorMessage = $this->getErrorMessage()) !== null) {
self::$activated = false;
$io->writeError('<warning>Narrowspark Automatic Security Audit has been disabled. ' . $errorMessage . '</warning>');
return;
}
$this->container = new Container($composer, $io);
/** @var null|\Symfony\Component\Console\Input\InputInterface $input */
$input = $this->container->get(InputInterface::class);
if ($input === null) {
self::$activated = false;
$io->writeError('<warning>Narrowspark Automatic Security Audit has been disabled. No input object found on composer class.</warning>');
return;
}
/** @var AuditContract $audit */
$audit = $this->container->get(AuditContract::class);
$devMode = true;
if ($input->hasOption(self::NAME)) {
$devMode = ! (bool) $input->getOption(self::NAME);
}
$audit->setDevMode($devMode);
}
/**
* {@inheritdoc}
*
* @return string[]
*/
public function getCapabilities(): array
{
return [
CommandProviderContract::class => CommandProvider::class,
];
}
/**
* Execute on composer post-messages event.
*/
public function onPostUpdatePostMessages(Event $event): void
{
if ($this->uninstallMode) {
return;
}
$count = \count(\array_filter($this->foundVulnerabilities));
/** @var IOInterface $io */
$io = $this->container->get(IOInterface::class);
if ($count !== 0) {
$io->write('<error>[!]</> Audit Security Report: ' . \sprintf('%s vulnerabilit%s found - run "composer audit" for more information', $count, $count === 1 ? 'y' : 'ies'));
} else {
$io->write('<fg=black;bg=green>[+]</> Audit Security Report: No known vulnerabilities found');
}
}
/**
* Audit composer package operations.
*/
public function auditPackage(PackageEvent $event): void
{
$operation = $event->getOperation();
if ($operation instanceof UninstallOperation) {
if ($operation->getPackage()->getPrettyName() === self::PACKAGE_NAME) {
$this->uninstallMode = true;
}
return;
}
if ($operation instanceof UpdateOperation) {
$composerPackage = $operation->getTargetPackage();
} else {
$composerPackage = $operation->getPackage();
}
$data = $this->container->get(AuditContract::class)->checkPackage(
$composerPackage->getName(),
$composerPackage->getVersion(),
$this->container->get('security_advisories')
);
if ((\is_countable($data) ? \count($data) : 0) === 0) {
return;
}
$this->foundVulnerabilities += $data[0];
}
/**
* Audit composer.lock.
*/
public function auditComposerLock(): void
{
if ($this->uninstallMode || \count($this->foundVulnerabilities) !== 0) {
return;
}
/** @var \Narrowspark\Automatic\Security\Contract\Audit $audit */
$audit = $this->container->get(AuditContract::class);
$data = $audit->checkLock(Util::getComposerLockFile());
if (\count($data) === 0) {
return;
}
$this->foundVulnerabilities += $data[0];
}
/**
* Check if automatic can be activated.
*/
private function getErrorMessage(): ?string
{
// @codeCoverageIgnoreStart
if (\version_compare(Util::getComposerVersion(), '1.8.0', '<')) {
return \sprintf('Your version "%s" of Composer is too old; Please upgrade', Composer::VERSION);
}
// @codeCoverageIgnoreEnd
return null;
}
}