Skip to content

Commit

Permalink
Improve twig validation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwindell authored May 14, 2019
1 parent 8ba9c7d commit ca58162
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/Edge/Twig/Validator/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Twig_Error;
use Twig_Error_Syntax;
use Twig_Loader_Array;
use Twig_Sandbox_SecurityError;
use Twig_Source;

class Twig extends AbstractValidator
{
Expand Down Expand Up @@ -50,19 +50,15 @@ public function getTwig()
public function isValid($value)
{
try {
$template = $this->twig->createTemplate($value);
$template->render([]);
$this->twig->parse($this->twig->tokenize(new Twig_Source($value, 'template')));
} catch (Twig_Error_Syntax $e) {
$this->error(self::SYNTAX_ERROR, $e->getRawMessage());
return false;
} catch (Twig_Sandbox_SecurityError $e) {
$this->error(self::SECURITY_ERROR, $e->getRawMessage());
return false;
} catch (Twig_Error $e) {
$this->error(self::UNKNOWN_PARSE_ERROR);
return false;
}

return true;
}
}
}

0 comments on commit ca58162

Please sign in to comment.