8000 [Yaml] fixed string parsing (closes #4561) · symfony/symfony@3ab9a6e · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ab9a6e

Browse files
committed
[Yaml] fixed string parsing (closes #4561)
1 parent c55ddb9 commit 3ab9a6e

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
class Inline
2222
{
2323
const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\']*(?:\'\'[^\']*)*)\')';
24-
const REGEX_SINGLE_QUOTED_STRING = '(?:\'([^\']*(?:\'\'[^\']*)*)\')(?!.*\')';
25-
const REGEX_DOUBLE_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)")(?!.*")';
2624

2725
/**
2826
* Converts a YAML string to a PHP array.
@@ -52,7 +50,13 @@ static public function parse($value)
5250
$result = self::parseMapping($value);
5351
break;
5452
default:
55-
$result = self::parseScalar($value);
53+
$i = 0;
54+
$result = self::parseScalar($value, null, array('"', "'"), $i);
55+
56+
// some comment can end the scalar
57+
if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) {
58+
throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)));
59+
}
5660
}
5761

5862
if (isset($mbEncoding)) {
@@ -163,6 +167,13 @@ static public function parseScalar($scalar, $delimiters = null, $stringDelimiter
163167
if (in_array($scalar[$i], $stringDelimiters)) {
164168
// quoted scalar
165169
$output = self::parseQuotedScalar($scalar, $i);
170+
171+
if (null !== $delimiters) {
172+
$tmp = ltrim(substr($scalar, $i), ' ');
173+
if (!in_array($tmp[0], $delimiters)) {
174+
throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)));
175+
}
176+
}
166177
} else {
167178
// "normal" string
168179
if (!$delimiters) {
@@ -203,11 +214,7 @@ static private function parseQuotedScalar($scalar, &$i)
203214
$items = preg_split('/[\'"]\s*(?:[,:]|[}\]]\s*,)/', $subject);
204215
$subject = substr($subject, 0, strlen($items[0]) + 1);
205216

206-
if (($scalar[$i] == "'"
207-
&& !preg_match('/'.self::REGEX_SINGLE_QUOTED_STRING.'/Au', $subject, $match))
208-
|| ($scalar[$i] == '"'
209-
&& !preg_match('/'.self::REGEX_DOUBLE_QUOTED_STRING.'/Au', $subject, $match))
210-
) {
217+
if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
211218
throw new ParseException(sprintf('Malformed inline YAML string (%s).', substr($scalar, $i)));
212219
}
213220

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,30 @@ public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedTo
6666
}
6767

6868
/**
69-
*
7069
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
7170
*/
7271
public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException()
7372
{
7473
$value = "'don't do somthin' like that'";
75-
Inline::parseScalar($value);
74+
Inline::parse($value);
7675
}
7776

7877
/**
79-
*
8078
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
8179
*/
8280
public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException()
8381
{
8482
$value = '"don"t do somthin" like that"';
85-
Inline::parseScalar($value);
83+
Inline::parse($value);
84+
}
85+
86+
/**
87+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
88+
*/
89+
public function testParseInvalidMappingKeyShouldThrowException()
90+
{
91+
$value = '{ "foo " bar": "bar" }';
92+
Inline::parse($value);
8693
}
8794

8895
public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()

0 commit comments

Comments
 (0)
0