-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1122 from joestewart/feature/security-checker-com…
…poseraudit Add security-checker using Composer Audit
- Loading branch information
Showing
6 changed files
with
297 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Composer Audit Security Checker | ||
|
||
The Security Checker will check your `composer.lock` file for known security vulnerabilities. | ||
|
||
***Config*** | ||
|
||
The task lives under the `securitychecker_composeraudit` namespace and has the following configurable parameters: | ||
|
||
```yaml | ||
# grumphp.yml | ||
grumphp: | ||
tasks: | ||
securitychecker_composeraudit: | ||
format: null | ||
locked: true | ||
no_dev: false | ||
run_always: false | ||
working_dir: null | ||
``` | ||
**format** | ||
*Default: null* | ||
You can choose the format of the output. The available options are `table`, `plain`, `json` and `summary`. By default, grumphp will use the format `table`. | ||
|
||
**locked** | ||
|
||
*Default: true* | ||
|
||
Audit packages from the lock file, regardless of what is currently in vendor dir. | ||
|
||
**no_dev** | ||
|
||
*Default: false* | ||
|
||
When this option is set to `true`, the task will skip packages under `require-dev`. | ||
|
||
**run_always** | ||
|
||
*Default: false* | ||
|
||
When this option is set to `false`, the task will only run when the `composer.lock` file has changed. If it is set to `true`, the `composer.lock` file will be checked on every commit. | ||
|
||
**working_dir** | ||
|
||
*Default: null | ||
|
||
If your `composer.lock` file is located in an exotic location, you can specify the location with this option. By default, the task will try to load a `composer.lock` file in the current directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GrumPHP\Task; | ||
|
||
use GrumPHP\Formatter\ProcessFormatterInterface; | ||
use GrumPHP\Runner\TaskResult; | ||
use GrumPHP\Runner\TaskResultInterface; | ||
use GrumPHP\Task\Config\ConfigOptionsResolver; | ||
use GrumPHP\Task\Context\ContextInterface; | ||
use GrumPHP\Task\Context\GitPreCommitContext; | ||
use GrumPHP\Task\Context\RunContext; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* @extends AbstractExternalTask<ProcessFormatterInterface> | ||
*/ | ||
class SecurityCheckerComposeraudit extends AbstractExternalTask | ||
{ | ||
public static function getConfigurableOptions(): ConfigOptionsResolver | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$resolver->setDefaults([ | ||
'format' => null, | ||
'locked' => true, | ||
'no_dev' => false, | ||
'run_always' => false, | ||
'working_dir' => null, | ||
]); | ||
|
||
$resolver->addAllowedTypes('format', ['null', 'string']); | ||
$resolver->addAllowedTypes('locked', ['bool']); | ||
$resolver->addAllowedTypes('no_dev', ['bool']); | ||
$resolver->addAllowedTypes('run_always', ['bool']); | ||
$resolver->addAllowedTypes('working_dir', ['null', 'string']); | ||
|
||
return ConfigOptionsResolver::fromOptionsResolver($resolver); | ||
} | ||
|
||
public function canRunInContext(ContextInterface $context): bool | ||
{ | ||
return $context instanceof GitPreCommitContext || $context instanceof RunContext; | ||
} | ||
|
||
public function run(ContextInterface $context): TaskResultInterface | ||
{ | ||
$config = $this->getConfig()->getOptions(); | ||
$files = $context->getFiles() | ||
->path(pathinfo("composer.lock", PATHINFO_DIRNAME)) | ||
->name(pathinfo("composer.lock", PATHINFO_BASENAME)); | ||
if (0 === \count($files) && !$config['run_always']) { | ||
return TaskResult::createSkipped($this, $context); | ||
} | ||
|
||
$arguments = $this->processBuilder->createArgumentsForCommand('composer'); | ||
$arguments->add('audit'); | ||
$arguments->addOptionalArgument('--format=%s', $config['format']); | ||
$arguments->addOptionalArgument('--locked', $config['locked']); | ||
$arguments->addOptionalArgument('--no-dev', $config['no_dev']); | ||
$arguments->addOptionalArgument('--working-dir=%s', $config['working_dir']); | ||
|
||
$process = $this->processBuilder->buildProcess($arguments); | ||
$process->run(); | ||
|
||
if (!$process->isSuccessful()) { | ||
return TaskResult::createFailed($this, $context, $this->formatter->format($process)); | ||
} | ||
|
||
return TaskResult::createPassed($this, $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GrumPHPTest\Unit\Task; | ||
|
||
use GrumPHP\Task\Context\GitPreCommitContext; | ||
use GrumPHP\Task\Context\RunContext; | ||
use GrumPHP\Task\SecurityCheckerComposeraudit; | ||
use GrumPHP\Task\TaskInterface; | ||
use GrumPHP\Test\Task\AbstractExternalTaskTestCase; | ||
|
||
class SecurityCheckerComposerauditTest extends AbstractExternalTaskTestCase | ||
{ | ||
protected function provideTask(): TaskInterface | ||
{ | ||
return new SecurityCheckerComposeraudit( | ||
$this->processBuilder->reveal(), | ||
$this->formatter->reveal() | ||
); | ||
} | ||
|
||
public function provideConfigurableOptions(): iterable | ||
{ | ||
yield 'defaults' => [ | ||
[], | ||
[ | ||
'format' => null, | ||
'locked' => true, | ||
'no_dev' => false, | ||
'run_always' => false, | ||
'working_dir' => null, | ||
] | ||
]; | ||
} | ||
|
||
public function provideRunContexts(): iterable | ||
{ | ||
yield 'run-context' => [ | ||
true, | ||
$this->mockContext(RunContext::class) | ||
]; | ||
|
||
yield 'pre-commit-context' => [ | ||
true, | ||
$this->mockContext(GitPreCommitContext::class) | ||
]; | ||
|
||
yield 'other' => [ | ||
false, | ||
$this->mockContext() | ||
]; | ||
} | ||
|
||
public function provideFailsOnStuff(): iterable | ||
{ | ||
yield 'exitCode1' => [ | ||
[], | ||
$this->mockContext(RunContext::class, ['composer.lock']), | ||
function () { | ||
$this->mockProcessBuilder('composer', $process = $this->mockProcess(1)); | ||
$this->formatter->format($process)->willReturn('nope'); | ||
}, | ||
'nope' | ||
]; | ||
} | ||
|
||
public function providePassesOnStuff(): iterable | ||
{ | ||
yield 'exitCode0' => [ | ||
[], | ||
$this->mockContext(RunContext::class, ['composer.lock']), | ||
function () { | ||
$this->mockProcessBuilder('composer', $this->mockProcess(0)); | ||
} | ||
]; | ||
yield 'exitCode0WhenRunAlways' => [ | ||
[ | ||
'run_always' => true | ||
], | ||
$this->mockContext(RunContext::class, ['notrelated.php']), | ||
function () { | ||
$this->mockProcessBuilder('composer', $this->mockProcess(0)); | ||
} | ||
]; | ||
} | ||
|
||
public function provideSkipsOnStuff(): iterable | ||
{ | ||
yield 'no-files' => [ | ||
[], | ||
$this->mockContext(RunContext::class), | ||
function () {} | ||
]; | ||
yield 'no-composer-file' => [ | ||
[], | ||
$this->mockContext(RunContext::class, ['thisisnotacomposerfile.lock']), | ||
function () {} | ||
]; | ||
} | ||
|
||
public function provideExternalTaskRuns(): iterable | ||
{ | ||
yield 'defaults' => [ | ||
[], | ||
$this->mockContext(RunContext::class, ['composer.lock']), | ||
'composer', | ||
[ | ||
'audit', | ||
'--locked', | ||
] | ||
]; | ||
|
||
yield 'format' => [ | ||
[ | ||
'format' => 'json', | ||
], | ||
$this->mockContext(RunContext::class, ['composer.lock']), | ||
'composer', | ||
[ | ||
'audit', | ||
'--format=json', | ||
'--locked', | ||
] | ||
]; | ||
|
||
yield 'locked' => [ | ||
[ | ||
'locked' => false, | ||
], | ||
$this->mockContext(RunContext::class, ['composer.lock']), | ||
'composer', | ||
[ | ||
'audit', | ||
] | ||
]; | ||
|
||
yield 'no-dev' => [ | ||
[ | ||
'no_dev' => true, | ||
], | ||
$this->mockContext(RunContext::class, ['composer.lock']), | ||
'composer', | ||
[ | ||
'audit', | ||
'--locked', | ||
'--no-dev', | ||
] | ||
]; | ||
|
||
yield 'working-dir' => [ | ||
[ | ||
'working_dir' => 'dir', | ||
], | ||
$this->mockContext(RunContext::class, ['composer.lock']), | ||
'composer', | ||
[ | ||
'audit', | ||
'--locked', | ||
'--working-dir=dir', | ||
] | ||
]; | ||
} | ||
} |