Skip to content

Commit 76546cb

Browse files
committed
Merge branch '4.4' into 5.2
* 4.4: [Security/Core] fix checking for bcrypt [HttpFoundation] Fixes for PHP 8.1 deprecations [Mime] DataPart: remove confusing fix-me comment expose references detected in inline notation structures
2 parents 6796885 + 1c2fd24 commit 76546cb

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

Inline.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static function initialize(int $flags, int $parsedLineNumber = null, stri
5858
*
5959
* @throws ParseException
6060
*/
61-
public static function parse(string $value = null, int $flags = 0, array $references = [])
61+
public static function parse(string $value = null, int $flags = 0, array &$references = [])
6262
{
6363
self::initialize($flags);
6464

@@ -267,7 +267,7 @@ private static function dumpNull(int $flags): string
267267
*
268268
* @throws ParseException When malformed inline YAML string is parsed
269269
*/
270-
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array $references = [])
270+
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array &$references = [])
271271
{
272272
if (\in_array($scalar[$i], ['"', "'"], true)) {
273273
// quoted scalar
@@ -343,7 +343,7 @@ private static function parseQuotedScalar(string $scalar, int &$i): string
343343
*
344344
* @throws ParseException When malformed inline YAML string is parsed
345345
*/
346-
private static function parseSequence(string $sequence, int $flags, int &$i = 0, array $references = []): array
346+
private static function parseSequence(string $sequence, int $flags, int &$i = 0, array &$references = []): array
347347
{
348348
$output = [];
349349
$len = \strlen($sequence);
@@ -385,6 +385,11 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
385385
}
386386
}
387387

388+
if (\is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) {
389+
$references[$matches['ref']] = $matches['value'];
390+
$value = $matches['value'];
391+
}
392+
388393
--$i;
389394
}
390395

@@ -407,7 +412,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
407412
*
408413
* @throws ParseException When malformed inline YAML string is parsed
409414
*/
410-
private static function parseMapping(string $mapping, int $flags, int &$i = 0, array $references = [])
415+
private static function parseMapping(string $mapping, int $flags, int &$i = 0, array &$references = [])
411416
{
412417
$output = [];
413418
$len = \strlen($mapping);
@@ -433,14 +438,14 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
433438
// key
434439
$offsetBeforeKeyParsing = $i;
435440
$isKeyQuoted = \in_array($mapping[$i], ['"', "'"], true);
436-
$key = self::parseScalar($mapping, $flags, [':', ' '], $i, false, []);
441+
$key = self::parseScalar($mapping, $flags, [':', ' '], $i, false);
437442

438443
if ($offsetBeforeKeyParsing === $i) {
439444
throw new ParseException('Missing mapping key.', self::$parsedLineNumber + 1, $mapping);
440445
}
441446

442447
if ('!php/const' === $key) {
443-
$key .= ' '.self::parseScalar($mapping, $flags, [':'], $i, false, []);
448+
$key .= ' '.self::parseScalar($mapping, $flags, [':'], $i, false);
444449
$key = self::evaluateScalar($key, $flags);
445450
}
446451

@@ -522,6 +527,11 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
522527
if ('<<' === $key) {
523528
$output += $value;
524529
} elseif ($allowOverwrite || !isset($output[$key])) {
530+
if (\is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) {
531+
$references[$matches['ref']] = $matches['value'];
532+
$value = $matches['value'];
533+
}
534+
525535
if (null !== $tag) {
526536
$output[$key] = new TaggedValue($tag, $value);
527537
} else {
@@ -548,7 +558,7 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
548558
*
549559
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
550560
*/
551-
private static function evaluateScalar(string $scalar, int $flags, array $references = [])
561+
private static function evaluateScalar(string $scalar, int $flags, array &$references = [])
552562
{
553563
$scalar = trim($scalar);
554564

Parser.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Parser
2525
{
2626
public const TAG_PATTERN = '(?P<tag>![\w!.\/:-]+)';
2727
public const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
28+
public const REFERENCE_PATTERN = '#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u';
2829

2930
private $filename;
3031
private $offset = 0;
@@ -161,7 +162,7 @@ private function doParse(string $value, int $flags)
161162
}
162163
$context = 'sequence';
163164

164-
if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
165+
if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) {
165166
$isRef = $matches['ref'];
166167
$this->refsBeingParsed[] = $isRef;
167168
$values['value'] = $matches['value'];
@@ -299,7 +300,7 @@ private function doParse(string $value, int $flags)
299300
$data += $parsed; // array union
300301
}
301302
}
302-
} elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u', $values['value'], $matches)) {
303+
} elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) {
303304
$isRef = $matches['ref'];
304305
$this->refsBeingParsed[] = $isRef;
305306
$values['value'] = $matches['value'];

Tests/InlineTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
190190
*/
191191
public function testParseReferences($yaml, $expected)
192192
{
193-
$this->assertSame($expected, Inline::parse($yaml, 0, ['var' => 'var-value']));
193+
$references = ['var' => 'var-value'];
194+
$this->assertSame($expected, Inline::parse($yaml, 0, $references));
194195
}
195196

196197
public function getDataForParseReferences()
@@ -214,7 +215,8 @@ public function testParseMapReferenceInSequence()
214215
'b' => 'Clark',
215216
'c' => 'Brian',
216217
];
217-
$this->assertSame([$foo], Inline::parse('[*foo]', 0, ['foo' => $foo]));
218+
$references = ['foo' => $foo];
219+
$this->assertSame([$foo], Inline::parse('[*foo]', 0, $references));
218220
}
219221

220222
public function testParseUnquotedAsterisk()

Tests/ParserTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,10 @@ public function testReferenceResolvingInInlineStrings()
10721072
'map' => ['key' => 'var-value'],
10731073
'list_in_map' => ['key' => ['var-value']],
10741074
'map_in_map' => ['foo' => ['bar' => 'var-value']],
1075+
'foo' => ['bar' => 'baz'],
1076+
'bar' => ['foo' => 'baz'],
1077+
'baz' => ['foo'],
1078+
'foobar' => ['foo'],
10751079
], Yaml::parse(<<<'EOF'
10761080
var: &var var-value
10771081
scalar: *var
@@ -1082,6 +1086,10 @@ public function testReferenceResolvingInInlineStrings()
10821086
map: { key: *var }
10831087
list_in_map: { key: [*var] }
10841088
map_in_map: { foo: { bar: *var } }
1089+
foo: { bar: &baz baz }
1090+
bar: { foo: *baz }
1091+
baz: [ &foo foo ]
1092+
foobar: [ *foo ]
10851093
EOF
10861094
));
10871095
}

0 commit comments

Comments
 (0)