From e8412fe1e6f6074561229b8cbb0a9596fddf7e6d Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sat, 7 Oct 2017 12:36:15 +0200 Subject: [PATCH 1/3] Blade: Add tests for compiling json directive --- tests/View/Blade/BladeJsonTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/View/Blade/BladeJsonTest.php diff --git a/tests/View/Blade/BladeJsonTest.php b/tests/View/Blade/BladeJsonTest.php new file mode 100644 index 000000000000..495c8a3d6ddc --- /dev/null +++ b/tests/View/Blade/BladeJsonTest.php @@ -0,0 +1,22 @@ +;'; + + $this->assertEquals($expected, $this->compiler->compileString($string)); + } + + public function testEncodingOptionsCanBeOverwritten() + { + $string = 'var foo = @json($var, JSON_HEX_TAG);'; + $expected = 'var foo = ;'; + + $this->assertEquals($expected, $this->compiler->compileString($string)); + } +} From 8bd4f01c922c3789ec1c3161a2d6095757aa47a8 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sat, 7 Oct 2017 12:37:20 +0200 Subject: [PATCH 2/3] Blade: Prettify output with arguments --- src/Illuminate/View/Compilers/Concerns/CompilesJson.php | 5 ++--- tests/View/Blade/BladeJsonTest.php | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesJson.php b/src/Illuminate/View/Compilers/Concerns/CompilesJson.php index cb1ed23298c2..bbcc93eb7e4a 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesJson.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesJson.php @@ -14,9 +14,8 @@ protected function compileJson($expression) { $parts = explode(',', $this->stripParentheses($expression)); - $options = $parts[1] ?? 0; - - $depth = $parts[2] ?? 512; + $options = trim($parts[1] ?? 0); + $depth = trim($parts[2] ?? 512); return ""; } diff --git a/tests/View/Blade/BladeJsonTest.php b/tests/View/Blade/BladeJsonTest.php index 495c8a3d6ddc..dac5819604cc 100644 --- a/tests/View/Blade/BladeJsonTest.php +++ b/tests/View/Blade/BladeJsonTest.php @@ -15,7 +15,7 @@ public function testStatementIsCompiledWithDefaultEncodingOptions() public function testEncodingOptionsCanBeOverwritten() { $string = 'var foo = @json($var, JSON_HEX_TAG);'; - $expected = 'var foo = ;'; + $expected = 'var foo = ;'; $this->assertEquals($expected, $this->compiler->compileString($string)); } From a82af39414e4d1bcc50e88b8e4cec328a2940a5d Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sat, 7 Oct 2017 12:44:31 +0200 Subject: [PATCH 3/3] Blade: Use safe encoding options by default for json directive --- .../View/Compilers/Concerns/CompilesJson.php | 12 +++++++++++- tests/View/Blade/BladeJsonTest.php | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesJson.php b/src/Illuminate/View/Compilers/Concerns/CompilesJson.php index bbcc93eb7e4a..6215e37fbca3 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesJson.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesJson.php @@ -4,6 +4,16 @@ trait CompilesJson { + /** + * Default encoding options. + * + * To make JSON safe for embedding into HTML, <, >, ', &, and " characters + * should be escaped. + * + * @var int + */ + private $safeEncodingOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT; + /** * Compile the JSON statement into valid PHP. * @@ -14,7 +24,7 @@ protected function compileJson($expression) { $parts = explode(',', $this->stripParentheses($expression)); - $options = trim($parts[1] ?? 0); + $options = trim($parts[1] ?? $this->safeEncodingOptions); $depth = trim($parts[2] ?? 512); return ""; diff --git a/tests/View/Blade/BladeJsonTest.php b/tests/View/Blade/BladeJsonTest.php index dac5819604cc..bdfab95ef375 100644 --- a/tests/View/Blade/BladeJsonTest.php +++ b/tests/View/Blade/BladeJsonTest.php @@ -4,10 +4,10 @@ class BladeJsonTest extends AbstractBladeTestCase { - public function testStatementIsCompiledWithDefaultEncodingOptions() + public function testStatementIsCompiledWithSafeDefaultEncodingOptions() { $string = 'var foo = @json($var);'; - $expected = 'var foo = ;'; + $expected = 'var foo = ;'; $this->assertEquals($expected, $this->compiler->compileString($string)); }