Skip to content

Commit fb4722f

Browse files
committed
reject inline notations followed by invalid content
1 parent 4eae3a6 commit fb4722f

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

Parser.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,17 +1167,17 @@ private function lexUnquotedString(int &$cursor): string
11671167
return substr($this->currentLine, $offset, $cursor - $offset);
11681168
}
11691169

1170-
private function lexInlineMapping(int &$cursor = 0): string
1170+
private function lexInlineMapping(int &$cursor = 0, bool $consumeUntilEol = true): string
11711171
{
1172-
return $this->lexInlineStructure($cursor, '}');
1172+
return $this->lexInlineStructure($cursor, '}', $consumeUntilEol);
11731173
}
11741174

1175-
private function lexInlineSequence(int &$cursor = 0): string
1175+
private function lexInlineSequence(int &$cursor = 0, bool $consumeUntilEol = true): string
11761176
{
1177-
return $this->lexInlineStructure($cursor, ']');
1177+
return $this->lexInlineStructure($cursor, ']', $consumeUntilEol);
11781178
}
11791179

1180-
private function lexInlineStructure(int &$cursor, string $closingTag): string
1180+
private function lexInlineStructure(int &$cursor, string $closingTag, bool $consumeUntilEol = true): string
11811181
{
11821182
$value = $this->currentLine[$cursor];
11831183
++$cursor;
@@ -1197,15 +1197,19 @@ private function lexInlineStructure(int &$cursor, string $closingTag): string
11971197
++$cursor;
11981198
break;
11991199
case '{':
1200-
$value .= $this->lexInlineMapping($cursor);
1200+
$value .= $this->lexInlineMapping($cursor, false);
12011201
break;
12021202
case '[':
1203-
$value .= $this->lexInlineSequence($cursor);
1203+
$value .= $this->lexInlineSequence($cursor, false);
12041204
break;
12051205
case $closingTag:
12061206
$value .= $this->currentLine[$cursor];
12071207
++$cursor;
12081208

1209+
if ($consumeUntilEol && isset($this->currentLine[$cursor]) && (strspn($this->currentLine, ' ', $cursor) + $cursor) < strlen($this->currentLine)) {
1210+
throw new ParseException(sprintf('Unexpected token "%s".', trim(substr($this->currentLine, $cursor))));
1211+
}
1212+
12091213
return $value;
12101214
case '#':
12111215
break 2;

Tests/ParserTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1718,7 +1718,8 @@ public function testWrappedUnquotedStringWithMultipleSpacesInValue(string $yaml,
17181718
$this->assertSame($expected, $this->parser->parse($yaml));
17191719
}
17201720

1721-
public static function wrappedUnquotedStringsProvider() {
1721+
public static function wrappedUnquotedStringsProvider()
1722+
{
17221723
return [
17231724
'mapping' => [
17241725
'{ foo: bar bar, fiz: cat cat }',
@@ -2252,6 +2253,30 @@ public function testRootLevelInlineMappingFollowedByMoreContentIsInvalid()
22522253
$this->parser->parse($yaml);
22532254
}
22542255

2256+
public function testInlineMappingFollowedByMoreContentIsInvalid()
2257+
{
2258+
$this->expectException(ParseException::class);
2259+
$this->expectExceptionMessage('Unexpected token "baz" at line 1 (near "{ foo: bar } baz").');
2260+
2261+
$yaml = <<<YAML
2262+
{ foo: bar } baz
2263+
YAML;
2264+
2265+
$this->parser->parse($yaml);
2266+
}
2267+
2268+
public function testInlineSequenceFollowedByMoreContentIsInvalid()
2269+
{
2270+
$this->expectException(ParseException::class);
2271+
$this->expectExceptionMessage('Unexpected token ",bar," at line 1 (near "[\'foo\'],bar,").');
2272+
2273+
$yaml = <<<YAML
2274+
['foo'],bar,
2275+
YAML;
2276+
2277+
$this->parser->parse($yaml);
2278+
}
2279+
22552280
public function testTaggedInlineMapping()
22562281
{
22572282
$this->assertSameData(new TaggedValue('foo', ['foo' => 'bar']), $this->parser->parse('!foo {foo: bar}', Yaml::PARSE_CUSTOM_TAGS));

0 commit comments

Comments
 (0)