8000 [DependencyInjection] XmlFileLoader: enforce tags to have a name by xabbuh · Pull Request #16956 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] XmlFileLoader: enforce tags to have a name #16956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ private function parseDefinition($id, $service, $file)
$parameters[$name] = SimpleXMLElement::phpize($value);
}

if ('' === (string) $tag['name']) {
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
}

$definition->addTag((string) $tag['name'], $parameters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ private function parseDefinition($id, $service, $file)
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
}

if (!is_string($tag['name']) || '' === $tag['name']) {
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
}

$name = $tag['name'];
unset($tag['name']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
</xsd:complexType>

<xsd:complexType name="tag">
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:anyAttribute namespace="##any" processContents="lax" />
</xsd:complexType>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="foo" class="BarClass">
<tag name="" foo="bar" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="foo" class="BarClass">
<tag foo="bar" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
foo_service:
class: FooClass
tags:
# tag name is an empty string
- { name: '', foo: bar }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
foo_service:
class: FooClass
tags:
# tag name is not a string
- { name: [], foo: bar }
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
Expand Down Expand Up @@ -207,6 +208,27 @@ public function testParsesTags()
}
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
*/
public function testParseTagsWithoutNameThrowsException()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('tag_without_name.xml');
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /The tag name for service ".+" in .* must be a non-empty string/
*/
public function testParseTagWithEmptyNameThrowsException()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('tag_with_empty_name.xml');
}

public function testConvertDomElementToArray()
{
$doc = new \DOMDocument('1.0');
Expand Down
< 7156 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,24 @@ public function testTagWithAttributeArrayThrowsException()
$this->assertStringStartsWith('A "tags" attribute must be of a scalar-type for service ', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag-attribute is not a scalar');
}
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /The tag name for service ".+" in .+ must be a non-empty string/
*/
public function testTagWithEmptyNameThrowsException()
{
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('tag_name_empty_string.yml');
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessageREgExp /The tag name for service "\.+" must be a non-empty string/
*/
public function testTagWithNonStringNameThrowsException()
{
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('tag_name_no_string.yml');
}
}
0