-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathSecurityChecker.php
More file actions
136 lines (114 loc) · 3.62 KB
/
Copy pathSecurityChecker.php
File metadata and controls
136 lines (114 loc) · 3.62 KB
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
<?php
namespace PHPCensor\Plugin;
use PHPCensor;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Model\BuildError;
use PHPCensor\Plugin;
use PHPCensor\ZeroConfigPluginInterface;
use PHPCensor\Common\Exception\RuntimeException;
/**
* SensioLabs Security Checker Plugin
*
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class SecurityChecker extends Plugin implements ZeroConfigPluginInterface
{
/**
* @var int
*/
protected $allowedWarnings = 0;
/**
* @var string
*/
protected $binaryType = 'symfony';
/**
* @var string[]
*/
protected $allowedBinaryTypes = [
'symfony',
'local-php-security-checker',
];
/**
* @return string
*/
public static function pluginName()
{
return 'security_checker';
}
/**
* {@inheritDoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
if (isset($options['zero_config']) && $options['zero_config']) {
$this->allowedWarnings = -1;
}
if (\array_key_exists('allowed_warnings', $options)) {
$this->allowedWarnings = (int)$options['allowed_warnings'];
}
if (
\array_key_exists('binary_type', $options) &&
\in_array((string)$options['binary_type'], $this->allowedBinaryTypes, true)
) {
$this->binaryType = (string)$options['binary_type'];
}
}
/**
* {@inheritDoc}
*/
public static function canExecuteOnStage($stage, Build $build)
{
$path = $build->getBuildPath() . 'composer.lock';
if (\file_exists($path) && $stage === Build::STAGE_TEST) {
return true;
}
return false;
}
public function execute()
{
$composerLockFile = $this->builder->buildPath . 'composer.lock';
if (!\is_file($composerLockFile)) {
throw new RuntimeException('Lock file does not exist.');
}
if ('symfony' === $this->binaryType) {
$cmd = '%s check:security --format=json --dir=%s';
$executable = $this->findBinary('symfony');
} else {
$cmd = '%s --format=json --path="%s"';
$executable = $this->findBinary('local-php-security-checker');
}
$builder = $this->getBuilder();
if (!$this->getBuild()->isDebug()) {
$builder->logExecOutput(false);
}
// works with dir, composer.lock, composer.json
$builder->executeCommand($cmd, $executable, $composerLockFile);
$builder->logExecOutput(true);
$success = true;
$result = $builder->getLastOutput();
$warnings = \json_decode($result, true);
if ($warnings) {
foreach ($warnings as $library => $warning) {
foreach ($warning['advisories'] as $data) {
$this->build->reportError(
$this->builder,
self::pluginName(),
$library . ' (' . $warning['version'] . ")\n" . $data['cve'] . ': ' . $data['title'] . "\n" . $data['link'],
BuildError::SEVERITY_CRITICAL
);
}
}
if ($this->allowedWarnings !== -1 && (\count($warnings) > $this->allowedWarnings)) {
$success = false;
}
} elseif (null === $warnings && $result) {
throw new RuntimeException('invalid json: '.$result);
}
return $success;
}
}