Skip to content

Commit f146c8d

Browse files
committed
tag for dumped PHP objects must be a local one
1 parent a729462 commit f146c8d

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
105105
return 'null';
106106
case is_object($value):
107107
if ($objectSupport) {
108-
return '!!php/object:'.serialize($value);
108+
return '!php/object:'.serialize($value);
109109
}
110110

111111
if ($exceptionOnInvalidType) {
@@ -476,6 +476,16 @@ private static function evaluateScalar($scalar, $references = array())
476476
return (string) substr($scalar, 5);
477477
case 0 === strpos($scalar, '! '):
478478
return (int) self::parseScalar(substr($scalar, 2));
479+
case 0 === strpos($scalar, '!php/object:'):
480+
if (self::$objectSupport) {
481+
return unserialize(substr($scalar, 12));
482+
}
483+
484+
if (self::$exceptionOnInvalidType) {
485+
throw new ParseException('Object support when parsing a YAML file has been disabled.');
486+
}
487+
488+
return;
479489
case 0 === strpos($scalar, '!!php/object:'):
480490
if (self::$objectSupport) {
481491
return unserialize(substr($scalar, 13));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function testObjectSupportEnabled()
180180
{
181181
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
182182

183-
$this->assertEquals('{ foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $dump, '->dump() is able to dump objects');
183+
$this->assertEquals('{ foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $dump, '->dump() is able to dump objects');
184184
}
185185

186186
public function testObjectSupportDisabledButNoExceptions()

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,15 +426,19 @@ public function testObjectSupportEnabled()
426426
bar: 1
427427
EOF;
428428
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
429-
}
430429

431-
public function testObjectSupportDisabledButNoExceptions()
432-
{
433430
$input = <<<EOF
434-
foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
431+
foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
435432
bar: 1
436433
EOF;
434+
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
435+
}
437436

437+
/**
438+
* @dataProvider invalidDumpedObjectProvider
439+
*/
440+
public function testObjectSupportDisabledButNoExceptions($input)
441+
{
438442
$this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
439443
}
440444

@@ -470,11 +474,29 @@ public function testObjectForMapIsAppliedAfterParsing()
470474
}
471475

472476
/**
477+
* @dataProvider invalidDumpedObjectProvider
473478
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
474479
*/
475-
public function testObjectsSupportDisabledWithExceptions()
480+
public function testObjectsSupportDisabledWithExceptions($yaml)
481+
{
482+
$this->parser->parse($yaml, true, false);
483+
}
484+
485+
public function invalidDumpedObjectProvider()
476486
{
477-
$this->parser->parse('foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}', true, false);
487+
$yamlTag = <<<EOF
488+
foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
489+
bar: 1
490+
EOF;
491+
$localTag = <<<EOF
492+
foo: !php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
493+
bar: 1
494+
EOF;
495+
496+
return array(
497+
'yaml-tag' => array($yamlTag),
498+
'local-tag' => array($localTag),
499+
);
478500
}
479501

480502
/**

0 commit comments

Comments
 (0)