8000 [Yaml] fixed ignored text when parsing an inlined mapping or sequence… · unframework/symfony@53ccc2c · GitHub
[go: up one dir, main page]

Skip to content

Commit 53ccc2c

Browse files
committed
[Yaml] fixed ignored text when parsing an inlined mapping or sequence (closes symfony#6786)
1 parent 0391ac7 commit 53ccc2c

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,23 @@ public static function parse($value, $exceptionOnInvalidType = false, $objectSup
5050
mb_internal_encoding('ASCII');
5151
}
5252

53+
$i = 0;
5354
switch ($value[0]) {
5455
case '[':
55-
$result = self::parseSequence($value);
56+
$result = self::parseSequence($value, $i);
57+
++$i;
5658
break;
5759
case '{':
58-
$result = self::parseMapping($value);
60+
$result = self::parseMapping($value, $i);
61+
++$i;
5962
break;
6063
default:
61-
$result = self::parseScalar($value);
64+
$result = self::parseScalar($value, null, array('"', "'"), $i);
65+
}
66+
67+
// some comments are allowed at the end
68+
if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) {
69+
throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)));
6270
}
6371

6472
if (isset($mbEncoding)) {
@@ -185,6 +193,13 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter
185193
if (in_array($scalar[$i], $stringDelimiters)) {
186194
// quoted scalar
187195
$output = self::parseQuotedScalar($scalar, $i);
196+
197+
if (null !== $delimiters) {
198+
$tmp = ltrim(substr($scalar, $i), ' ');
199+
if (!in_array($tmp[0], $delimiters)) {
200+
throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)));
201+
}
202+
}
188203
} else {
189204
// "normal" string
190205
if (!$delimiters) {
@@ -220,6 +235,11 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter
220235
*/
221236
private static function parseQuotedScalar($scalar, &$i)
222237
{
238+
// Only check the current item we're dealing with (for sequences)
239+
$subject = substr($scalar, $i);
240+
$items = preg_split('/[\'"]\s*(?:[,:]|[}\]]\s*,)/', $subject);
241+
$subject = substr($subject, 0, strlen($items[0]) + 1);
242+
223243
if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
224244
throw new ParseException(sprintf('Malformed inline YAML string (%s).', substr($scalar, $i)));
225245
}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,57 @@ public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedTo
6565
$this->assertSame($value, Inline::parse(Inline::dump($value)));
6666
}
6767

68+
/**
69+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
70+
*/
71+
public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException()
72+
{
73+
$value = "'don't do somthin' like that'";
74+
Inline::parse($value);
75+
}
76+
77+
/**
78+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
79+
*/
80+
public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException()
81+
{
82+
$value = '"don"t do somthin" like that"';
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);
93+
}
94+
95+
/**
96+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
97+
*/
98+
public function testParseInvalidMappingShouldThrowException()
99+
{
100+
Inline::parse('[foo] bar');
101+
}
102+
103+
/**
104+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
105+
*/
106+
public function testParseInvalidSequenceShouldThrowException()
107+
{
108+
Inline::parse('{ foo: bar } bar');
109+
}
110+
111+
public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
112+
{
113+
$value = "'don''t do somthin'' like that'";
114+
$expect = "don't do somthin' like that";
115+
116+
$this->assertSame($expect, Inline::parseScalar($value));
117+
}
118+
68119
protected function getTestsForParse()
69120
{
70121
return array(

0 commit comments

Comments
 (0)
0