diff --git a/src/Latte/Compiler/Parser.php b/src/Latte/Compiler/Parser.php index a57f1c9f6..3f541f2fc 100644 --- a/src/Latte/Compiler/Parser.php +++ b/src/Latte/Compiler/Parser.php @@ -94,6 +94,11 @@ public function parse(string $input): array $input = substr($input, 3); } + if (preg_match('#[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]#', $input, $m, PREG_OFFSET_CAPTURE)) { + trigger_error('Template contains control character \x' . dechex(ord($m[0][0])) . ' on line ' . (substr_count($input, "\n", 0, $m[0][1]) + 1) . '.', E_USER_WARNING); + $input = preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]#', '', $input); + } + $this->input = $input = str_replace("\r\n", "\n", $input); $this->offset = 0; $this->line = 1; diff --git a/tests/Latte/Parser.errors.phpt b/tests/Latte/Parser.errors.phpt index 5b87b1f15..6b5246b2f 100644 --- a/tests/Latte/Parser.errors.phpt +++ b/tests/Latte/Parser.errors.phpt @@ -60,3 +60,10 @@ Assert::exception(function () use (&$parser) { $parser->parse("\n{"); }, Latte\CompileException::class, 'Malformed tag.'); Assert::same(2, $parser->getLine()); + + +Assert::error(function () use (&$res) { + $parser = new Parser; + $res = $parser->parse("a\x00\x1F\x7Fb"); +}, E_USER_WARNING, 'Template contains control character \x0 on line 1.'); +Assert::same('ab', $res[0]->text);