8000 XmlFileLoader: enforce tags to have a name · symfony/symfony@9a7167a · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a7167a

Browse files
committed
XmlFileLoader: enforce tags to have a name
1 parent 0c2f1d9 commit 9a7167a

File tree

9 files changed

+93
-1
lines changed

9 files changed

+93
-1
lines changed

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ private function parseDefinition($id, $service, $file)
185185
$parameters[$name] = SimpleXMLElement::phpize($value);
186186
}
187187

188+
if ('' === trim($tag['name'])) {
189+
throw new \InvalidArgumentException(sprintf('The tag name for service "%s" must not be an empty string in %s.', $id, $file));
190+
}
191+
188192
$definition->addTag((string) $tag['name'], $parameters);
189193
}
190194

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ private function parseDefinition($id, $service, $file)
244244
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
245245
}
246246

247+
if (!is_string($tag['name']) || '' === trim($tag['name'])) {
248+
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
249+
}
250+
247251
$name = $tag['name'];
248252
unset($tag['name']);
249253

src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
</xsd:complexType>
9898

9999
<xsd:complexType name="tag">
100-
<xsd:attribute name="name" type="xsd:string" />
100+
<xsd:attribute name="name" type="xsd:string" use="required" />
101101
<xsd:anyAttribute namespace="##any" processContents="lax" />
102102
</xsd:complexType>
103103

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="foo" class="BarClass">
8+
<tag foo="bar" />
9+
</service>
10+
</services>
11+
</container>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="foo" class="BarClass">
8+
<tag name="" foo="bar" />
9+
</service>
10+
</services>
11+
</container>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
foo_service:
3+
class: FooClass
4+
tags:
5+
# tag name is an empty string
6+
- { name: '', foo: bar }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
foo_service:
3+
class: FooClass
4+
tags:
5+
# tag name is an empty string
6+
- { name: [], foo: bar }

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1617
use Symfony\Component\DependencyInjection\Reference;
1718
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1819
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
@@ -207,6 +208,35 @@ public function testParsesTags()
207208
}
208209
}
209210

211+
/**
212+
* @expectedException \InvalidArgumentException
213+
*/
214+
public function testParseTagsWithoutNameThrowsException()
215+
{
216+
try {
217+
$container = new ContainerBuilder();
218+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
219+
$loader->load('badtag2.xml');
220+
} catch (InvalidArgumentException $e) {
221+
throw $e->getPrevious();
222+
}
223+
}
224+
225+
/**
226+
* @expectedException \InvalidArgumentException
227+
* @expectedExceptionMessageRegExp /The tag name for service ".+" must not be an empty string/
228+
*/
229+
public function testParseTagWithEmptyNameThrowsException()
230+
{
231+
try {
232+
$container = new ContainerBuilder();
233+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
234+
$loader->load('badtag5.xml');
235+
} catch (InvalidArgumentException $e) {
236+
throw $e->getPrevious();
237+
}
238+
}
239+
210240
public function testConvertDomElementToArray()
211241
{
212242
$doc = new \DOMDocument('1.0');

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,24 @@ public function testTagWithAttributeArrayThrowsException()
233233
$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');
234234
}
235235
}
236+
237+
/**
238+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
239+
* @expectedExceptionMessageRegExp /The tag name for service ".+" in .+ must be a non-empty string/
240+
*/
241+
public function testTagWithEmptyNameThrowsException()
242+
{
243+
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
244+
$loader->load('badtag5.yml');
245+
}
246+
247+
/**
248+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
249+
* @expectedExceptionMessageREgExp /The tag name for service "\.+" must be a non-empty string/
250+
*/
251+
public function testTagWithNonStringNameThrowsException()
252+
{
253+
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
254+
$loader->load('badtag6.yml');
255+
}
236256
}

0 commit comments

Comments
 (0)
0