Skip to content

Commit

Permalink
Log file validator (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmleroux authored Mar 13, 2017
2 parents 407c5ae + 41c6b0f commit 1fa54ad
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Controller/LogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public function fileAction($id, $type)
$cronManager = $this->get('foa.cron_bundle.cron_manager');
$cron = $cronManager->getById($id);
$filepath = ($type == 'log') ? $cron->getLogFile() : $cron->getErrorFile();
$content = file_get_contents($filepath);
// TODO: re-activate when secure
// $content = file_get_contents($filepath);
$content = 'File content not displayable.';

return $this->render('FOACronBundle:Dashboard:log.html.twig', [
'filepath' => $filepath,
Expand Down
2 changes: 2 additions & 0 deletions Manager/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Cron

/**
* @var string
* @CronAsserts\LogFile()
*/
protected $logFile = null;

Expand All @@ -66,6 +67,7 @@ class Cron

/**
* @var string
* @CronAsserts\LogFile()
*/
protected $errorFile = null;

Expand Down
14 changes: 14 additions & 0 deletions Validator/Constraints/LogFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace FOA\CronBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
* @author JM Leroux <jmleroux.pro@gmail.com>
*/
class LogFile extends Constraint
{
public $message = 'Invalid log path "%string%".';
}
37 changes: 37 additions & 0 deletions Validator/Constraints/LogFileValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace FOA\CronBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
* @author JM Leroux <jmleroux.pro@gmail.com>
*/
class LogFileValidator extends ConstraintValidator
{
/** @var string */
private $appDir;

/**
* @param string $appDir
*/
public function __construct($appDir)
{
$this->appDir = $appDir;
}

/**
* @param string $value
* @param Constraint $constraint
*/
public function validate($value, Constraint $constraint)
{
$allowedPattern = '#(app/logs?)|(^/tmp)#';
if (!preg_match($allowedPattern, $value)) {
$this->context->buildViolation($constraint->message)
->setParameter('%string%', $value)
->addViolation();
}
}
}

0 comments on commit 1fa54ad

Please sign in to comment.