调用控制台命令安全性:从控制器操作进行检查会产生未找到锁定文件的响应


Calling console command security:check from controller action produces Lock file not found response

(Symfony3(

我正在考虑设置一些简单的cron任务,为我们的项目经理生成安全报告,这样他们就可以为开发人员安排升级时间(而我忘记了手动运行它们(。

作为一项非常基本的检查,我将简单地运行。。。

php bin/console security:check

看看composer对漏洞有什么看法。最终,我想在运行cron时将此输出滚动到电子邮件中,或将其发布到空闲通道或大本营作业中。

问题

当我通过终端运行命令时,它工作得很好。在控制器内运行命令总是返回响应锁定文件不存在。我在引用项目根目录下的composer.lock文件时假设了这一点。我可以确认这个文件确实存在。

以下是我目前使用的控制器,它是根据以下内容改编的:

http://symfony.com/doc/current/console/command_in_controller.html

<?php
namespace Treetop1500'SecurityReportBundle'Controller;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
use Symfony'Bundle'FrameworkBundle'Console'Application;
use Symfony'Component'Console'Input'ArrayInput;
use Symfony'Component'Console'Output'BufferedOutput;
use Symfony'Component'HttpFoundation'Response;
use Symfony'Component'HttpKernel'Exception'UnauthorizedHttpException;
class DefaultController extends Controller
{
    public function indexAction($key)
    {
        if ($key != $this->getParameter('easy_cron_key')) {
            throw new UnauthorizedHttpException("You are not authorized to access this page.");
        }
        $kernel = $this->get('kernel');
        $application = new Application($kernel);
        $application->setAutoExit(false);
        $input = new ArrayInput(array(
          'command' => 'security:check'
        ));
        // You can use NullOutput() if you don't need the output
        $output = new BufferedOutput();
        $application->run($input, $output);
        // return the output, don't use if you used NullOutput()
        $content = $output->fetch();
        // return new Response(""), if you used NullOutput()
        return new Response($content);
    }
}

$content总是具有值"锁定文件不存在">

我意识到可能有更好的工具和方法可以做到这一点,但我真的很想理解为什么这是控制器动作中生成的响应。谢谢你看!

将绝对路径传递到composer.lock文件,如下所示:

php bin/console security:check /path/to/another/composer.lock

所以在你的例子中,这将是:

$input = new ArrayInput([
    'command'  => 'security:check',
    'lockfile' => '/path/to/another/composer.lock'
]);

阅读更多:SensioLabs的SecurityCheckerCommand。可选参数是lockfile,由SecurityChecker进行检查。在第46行,他们正在查找composer.lock文件(默认参数(,并在未找到时抛出异常。

p.S.早些时候,我在数组中键入了错误的参数。我查看了Symfony文档(如何调用其他命令(并修复了答案。

解决方案是将lockfile参数传递给ArrayInput对象,如下所示:

$lockfile = $this->get('kernel')->getRootDir()."/../composer.lock";
$input = new ArrayInput(array('command'=>'security:check','lockfile'=>$lockfile));