Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5bf5e0e

Browse files
committed
[Yaml] Throw on unquoted exclamation mark
1 parent 477e843 commit 5bf5e0e

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,10 @@ private static function parseTag(string $value, int &$i, int $flags): ?string
669669
$nextOffset = $i + $tagLength + 1;
670670
$nextOffset += strspn($value, ' ', $nextOffset);
671671

672+
if ('' === $tag && (!isset($value[$nextOffset]) || \in_array($value[$nextOffset], [']', '}', ','], true))) {
673+
throw new ParseException(sprintf('Using the unquoted scalar value "!" is not supported. You must quote it at line 1 (near "%s")', $value), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
674+
}
675+
672676
// Is followed by a scalar and is a built-in tag
673677
if ('' !== $tag && (!isset($value[$nextOffset]) || !\in_array($value[$nextOffset], ['[', '{'], true)) && ('!' === $tag[0] || 'str' === $tag || 'php/const' === $tag || 'php/object' === $tag)) {
674678
// Manage in {@link self::evaluateScalar()}

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,4 +736,69 @@ public function getTestsForOctalNumbers()
736736
'negative octal number' => [-28, '-034'],
737737
];
738738
}
739+
740+
/**
741+
* @dataProvider unquotedExclamationMarkThrowsProvider
742+
*/
743+
public function testUnquotedExclamationMarkThrows($value)
744+
{
745+
$this->expectException(ParseException::class);
746+
$this->expectExceptionMessageRegExp('/^Using the unquoted scalar value "!" is not supported\. You must quote it at line 1/');
747+
748+
Inline::parse($value);
749+
}
750+
751+
public function unquotedExclamationMarkThrowsProvider()
752+
{
753+
return [
754+
['!'],
755+
['! '],
756+
['! '],
757+
[' ! '],
758+
['[!]'],
759+
['[! ]'],
760+
['[! ]'],
761+
['[!, "foo"]'],
762+
['["foo", !, "ccc"]'],
763+
['{foo: !}'],
764+
['{foo: !}'],
765+
['{foo: !, bar: "ccc"}'],
766+
['{bar: "ccc", foo: ! }'],
767+
['!]]]'],
768+
['!}'],
769+
['!,}foo,]'],
770+
['! [!]'],
771+
];
772+
}
773+
774+
/**
775+
* @dataProvider quotedExclamationMarkProvider
776+
*/
777+
public function testQuotedExclamationMark($expected, $value)
778+
{
779+
$this->assertSame($expected, Inline::parse($value));
780+
}
781+
782+
// This provider should ideally stay consistent with unquotedExclamationMarkThrowsProvider
783+
public function quotedExclamationMarkProvider()
784+
{
785+
return [
786+
['!', '"!"'],
787+
['! ', '"! "'],
788+
[' !', '" !"'],
789+
[' ! ', '" ! "'],
790+
[['!'], '["!"]'],
791+
[['! '], '["! "]'],
792+
[['!', 'foo'], '["!", "foo"]'],
793+
[['foo', '!', 'ccc'], '["foo", "!", "ccc"]'],
794+
[['foo' => '!'], '{foo: "!"}'],
795+
[['foo' => ' !'], '{foo: " !"}'],
796+
[['foo' => '!', 'bar' => 'ccc'], '{foo: "!", bar: "ccc"}'],
797+
[['bar' => 'ccc', 'foo' => '! '], '{bar: "ccc", foo: "! "}'],
798+
['!]]]', '"!]]]"'],
799+
['!}', '"!}"'],
800+
['!,}foo,]', '"!,}foo,]"'],
801+
[['!'], '! ["!"]'],
802+
];
803+
}
739804
}

0 commit comments

Comments
 (0)