8000 feature #51128 [SecurityBundle] Allow an array of `pattern` in firewa… · symfony/symfony@60e7a5d · GitHub
[go: up one dir, main page]

Skip to content

Commit 60e7a5d

Browse files
committed
feature #51128 [SecurityBundle] Allow an array of pattern in firewall configuration (lyrixx, chalasr)
This PR was merged into the 6.4 branch. Discussion ---------- [SecurityBundle] Allow an array of `pattern` in firewall configuration | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#18617 allow this : ```diff security: firewalls: no_security: - pattern: "^/(register|documentation)$" + pattern: + - "^/register$" + - "^/documentation$" ``` --- ```php /** * `@Revs`(100) * `@Iterations`(100) */ class RegexBench { public function benchOneBigString() { preg_match("{^/(register|documentation)$}", "/register"); preg_match("{^/(register|documentation)$}", "/foo"); } public function benchArrayConcat() { preg_match("{(?:^/register$|^/documentation$)}", "/register"); preg_match("{(?:^/register$|^/documentation$)}", "/foo"); } } ``` => ``` PHPBench (dev-master) running benchmarks... #standwithukraine with configuration file: /home/gregoire/dev/github.com/lyrixx/php-bench/phpbench.json with PHP version 8.2.8, xdebug ❌, opcache ✔ \RegexBench benchOneBigString.......................I99 - Mo0.670μs (±17.85%) benchArrayConcat........................I99 - Mo0.664μs (±12.49%) Subjects: 2, Assertions: 0, Failures: 0, Errors: 0 +-------------------+---------+-----------+ | subject | mean | mem_peak | +-------------------+---------+-----------+ | benchOneBigString | 0.716μs | 995.920kb | | benchArrayConcat | 0.707μs | 995.920kb | +-------------------+---------+-----------+ ``` Commits ------- 46344de [SecurityBundle] Allow an array of `pattern` in firewall configuration
2 parents 510e51f + 46344de commit 60e7a5d

File tree

6 files changed

+39
-1
lines changed

6 files changed

+39
-1
lines changed

src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Deprecate `Security::ACCESS_DENIED_ERROR`, `AUTHENTICATION_ERROR` and `LAST_USERNAME` constants, use the ones on `SecurityRequestAttributes` instead
8+
* Allow an array of `pattern` in firewall configuration
89

910
6.3
1011
---

src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,12 @@ private function addFirewallsSection(ArrayNodeDefinition $rootNode, array $facto
191191
;
192192

193193
$firewallNodeBuilder
194-
->scalarNode('pattern')->end()
194+
->scalarNode('pattern')
195+
->beforeNormalization()
196+
->ifArray()
197+
->then(fn ($v) => sprintf('(?:%s)', implode('|', $v)))
198+
->end()
199+
->end()
195200
->scalarNode('host')->end()
196201
->arrayNode('methods')
197202
->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end()

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,15 @@ public function testFirewallLogoutClearSiteData()
716716
$this->assertSame(['cookies', 'executionContexts'], $ClearSiteDataConfig);
717717
}
718718

719+
public function testFirewallPatterns()
720+
{
721+
$container = $this->getContainer('firewall_patterns');
722+
$chainRequestMatcherId = (string) $container->getDefinition('security.firewall.map')->getArgument(1)->getValues()['security.firewall.map.context.no_security'];
723+
$requestMatcherId = (string) $container->getDefinition($chainRequestMatcherId)->getArgument(0)[0];
724+
725+
$this->assertSame('(?:^/register$|^/documentation$)', $container->getDefinition($requestMatcherId)->getArgument(0));
726+
}
727+
719728
protected function getContainer($file)
720729
{
721730
$file .= '.'.$this->getFileExtension();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
$container->loadFromExtension('security', [
4+
'firewalls' => [
5+
'no_security' => [
6+
'pattern' => [
7+
'^/register$',
8+
'^/documentation$',
9+
],
10+
],
11+
],
12+
]);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
security:
2+
firewalls:
3+
no_security:
4+
pattern:
5+
- "^/register$"
6+
- "^/documentation$"

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/XmlCompleteConfigurationTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
class XmlCompleteConfigurationTest extends CompleteConfigurationTestCase
1919
{
20+
public function testFirewallPatterns()
21+
{
22+
$this->markTestSkipped('This features is not supported in XML.');
23+
}
24+
2025
protected function getLoader(ContainerBuilder $container)
2126
{
2227
return new XmlFileLoader($container, new FileLocator(__DIR__.'/Fixtures/xml'));

0 commit comments

Comments
 (0)
0