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

Skip to content

Commit 1e78f53

Browse files
committed
XmlFileLoader: enforce tags to have a name
1 parent 0005c56 commit 1e78f53

File tree

9 files changed

+85
-1
lines changed

9 files changed

+85
-1
lines changed

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

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

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

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

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

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

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 name="" 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 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 not a string
6+
- { name: [], foo: bar }

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

Lines changed: 22 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,27 @@ public function testParsesTags()
207208
}
208209
}
209210

211+
/**
212+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
213+
*/
214+
public function testParseTagsWithoutNameThrowsException()
215+
{
216+
$container = new ContainerBuilder();
217+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
218+
$loader->load('tag_without_name.xml');
219+
}
220+
221+
/**
222+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
223+
* @expectedExceptionMessage F438 RegExp /The tag name for service ".+" in .* must be a non-empty string/
224+
*/
225+
public function testParseTagWithEmptyNameThrowsException()
226+
{
227+
$container = new ContainerBuilder();
228+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
229+
$loader->load('tag_with_empty_name.xml');
230+
}
231+
210232
public function testConvertDomElementToArray()
211233
{
212234
$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('tag_name_empty_string.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('tag_name_no_string.yml');
255+
}
236256
}

0 commit comments

Comments
 (0)
0