Skip to content

Commit

Permalink
Merge pull request #28160 from brendt/escape-json-path
Browse files Browse the repository at this point in the history
[5.8] Correctly escape single quotes in json paths
  • Loading branch information
taylorotwell authored Apr 10, 2019
2 parents e62dff8 + 93f59c4 commit a056cd8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,8 @@ protected function wrapJsonFieldAndPath($column)
*/
protected function wrapJsonPath($value, $delimiter = '->')
{
$value = preg_replace("/([\\\\]+)?\\'/", "\\'", $value);

return '\'$."'.str_replace($delimiter, '"."', $value).'"\'';
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,29 @@ public function testMySqlWrappingJsonWithBooleanAndIntegerThatLooksLikeOne()
$this->assertEquals('select * from `users` where json_extract(`items`, \'$."available"\') = true and json_extract(`items`, \'$."active"\') = false and json_unquote(json_extract(`items`, \'$."number_available"\')) = ?', $builder->toSql());
}

public function testJsonPathEscaping()
{
$expectedWithJsonEscaped = <<<SQL
select json_unquote(json_extract(`json`, '$."\'))#"'))
SQL;

$builder = $this->getMySqlBuilder();
$builder->select("json->'))#");
$this->assertEquals($expectedWithJsonEscaped, $builder->toSql());

$builder = $this->getMySqlBuilder();
$builder->select("json->\'))#");
$this->assertEquals($expectedWithJsonEscaped, $builder->toSql());

$builder = $this->getMySqlBuilder();
$builder->select("json->\\'))#");
$this->assertEquals($expectedWithJsonEscaped, $builder->toSql());

$builder = $this->getMySqlBuilder();
$builder->select("json->\\\'))#");
$this->assertEquals($expectedWithJsonEscaped, $builder->toSql());
}

public function testMySqlWrappingJson()
{
$builder = $this->getMySqlBuilder();
Expand Down

0 comments on commit a056cd8

Please sign in to comment.