Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7dd75b6

Browse files
committed
[Security] Deprecate callable firewall listeners
1 parent1ecb87c commit7dd75b6

File tree

10 files changed

+132
-28
lines changed

10 files changed

+132
-28
lines changed

‎UPGRADE-7.4.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ HttpClient
2222
----------
2323

2424
* Deprecate using amphp/http-client < 5
25+
26+
Security
27+
--------
28+
29+
* Deprecate callable firewall listeners, extend`AbstractListener` or implement`FirewallListenerInterface` instead

‎src/Symfony/Bundle/SecurityBundle/Debug/TraceableFirewallListener.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
useSymfony\Bundle\SecurityBundle\Security\LazyFirewallContext;
1717
useSymfony\Component\HttpKernel\Event\RequestEvent;
1818
useSymfony\Component\Security\Http\Authenticator\Debug\TraceableAuthenticatorManagerListener;
19+
useSymfony\Component\Security\Http\Firewall\AbstractListener;
1920
useSymfony\Component\Security\Http\Firewall\FirewallListenerInterface;
2021
useSymfony\Contracts\Service\ResetInterface;
2122

@@ -88,7 +89,11 @@ protected function callListeners(RequestEvent $event, iterable $listeners): void
8889
}
8990

9091
foreach ($requestListenersas$listener) {
91-
$listener($event);
92+
if (!$listenerinstanceof FirewallListenerInterface) {
93+
$listener($event);
94+
}elseif (false !==$listener->supports($event->getRequest())) {
95+
$listener->authenticate($event);
96+
}
9297

9398
if ($event->hasResponse()) {
9499
break;

‎src/Symfony/Bundle/SecurityBundle/Security/FirewallContext.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Bundle\SecurityBundle\Security;
1313

1414
useSymfony\Component\Security\Http\Firewall\ExceptionListener;
15+
useSymfony\Component\Security\Http\Firewall\FirewallListenerInterface;
1516
useSymfony\Component\Security\Http\Firewall\LogoutListener;
1617

1718
/**
@@ -39,7 +40,7 @@ public function getConfig(): ?FirewallConfig
3940
}
4041

4142
/**
42-
* @return iterable<mixed, callable>
43+
* @return iterable<mixed,FirewallListenerInterface|callable>
4344
*/
4445
publicfunctiongetListeners():iterable
4546
{

‎src/Symfony/Bundle/SecurityBundle/Security/LazyFirewallContext.php‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespaceSymfony\Bundle\SecurityBundle\Security;
1313

14+
useSymfony\Component\HttpFoundation\Request;
1415
useSymfony\Component\HttpKernel\Event\RequestEvent;
1516
useSymfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
1617
useSymfony\Component\Security\Http\Event\LazyResponseEvent;
18+
useSymfony\Component\Security\Http\Firewall\AbstractListener;
1719
useSymfony\Component\Security\Http\Firewall\ExceptionListener;
1820
useSymfony\Component\Security\Http\Firewall\FirewallListenerInterface;
1921
useSymfony\Component\Security\Http\Firewall\LogoutListener;
@@ -23,7 +25,7 @@
2325
*
2426
* @author Nicolas Grekas <p@tchwork.com>
2527
*/
26-
class LazyFirewallContextextends FirewallContext
28+
class LazyFirewallContextextends FirewallContextimplements FirewallListenerInterface
2729
{
2830
publicfunction__construct(
2931
iterable$listeners,
@@ -40,19 +42,26 @@ public function getListeners(): iterable
4042
return [$this];
4143
}
4244

43-
publicfunction__invoke(RequestEvent$event):void
45+
publicfunctionsupports(Request$request): ?bool
46+
{
47+
returntrue;
48+
}
49+
50+
publicfunctionauthenticate(RequestEvent$event):void
4451
{
4552
$listeners = [];
4653
$request =$event->getRequest();
4754
$lazy =$request->isMethodCacheable();
4855

4956
foreach (parent::getListeners()as$listener) {
50-
if (!$lazy || !$listenerinstanceof FirewallListenerInterface) {
57+
if (!$listenerinstanceof FirewallListenerInterface) {
58+
trigger_deprecation('symfony/security-http','7.4','Using a callable as firewall listener is deprecated, extend "%s" or implement "%s" instead.', AbstractListener::class, FirewallListenerInterface::class);
59+
5160
$listeners[] =$listener;
52-
$lazy =$lazy &&$listenerinstanceof FirewallListenerInterface;
61+
$lazy =false;
5362
}elseif (false !==$supports =$listener->supports($request)) {
5463
$listeners[] = [$listener,'authenticate'];
55-
$lazy =null ===$supports;
64+
$lazy =$lazy &&null ===$supports;
5665
}
5766
}
5867

@@ -75,4 +84,9 @@ public function __invoke(RequestEvent $event): void
7584
}
7685
});
7786
}
87+
88+
publicstaticfunctiongetPriority():int
89+
{
90+
return0;
91+
}
7892
}

‎src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
useSymfony\Component\Security\Core\Authorization\Voter\VoterInterface;
3333
useSymfony\Component\Security\Core\Role\RoleHierarchy;
3434
useSymfony\Component\Security\Core\User\InMemoryUser;
35+
useSymfony\Component\Security\Http\Firewall\FirewallListenerInterface;
3536
useSymfony\Component\Security\Http\FirewallMapInterface;
3637
useSymfony\Component\Security\Http\Logout\LogoutUrlGenerator;
3738
useSymfony\Component\VarDumper\Caster\ClassStub;
@@ -193,8 +194,24 @@ public function testGetListeners()
193194
$request =newRequest();
194195
$event =newRequestEvent($this->createMock(HttpKernelInterface::class),$request, HttpKernelInterface::MAIN_REQUEST);
195196
$event->setResponse($response =newResponse());
196-
$listener =function ($e)use ($event, &$listenerCalled) {
197-
$listenerCalled +=$e ===$event;
197+
$listener =newclassimplements FirewallListenerInterface
198+
{
199+
publicint$callCount =0;
200+
201+
publicfunctionsupports(Request$request): ?bool
202+
{
203+
returntrue;
204+
}
205+
206+
publicfunctionauthenticate(RequestEvent$event):void
207+
{
208+
++$this->callCount;
209+
}
210+
211+
publicstaticfunctiongetPriority():int
212+
{
213+
return0;
214+
}
198215
};
199216
$firewallMap =$this
200217
->getMockBuilder(FirewallMap::class)
@@ -217,9 +234,9 @@ public function testGetListeners()
217234
$collector =newSecurityDataCollector(null,null,null,null,$firewallMap,$firewall,true);
218235
$collector->collect($request,$response);
219236

220-
$this->assertNotEmpty($collected =$collector->getListeners()[0]);
237+
$this->assertCount(1,$collector->getListeners());
221238
$collector->lateCollect();
222-
$this->assertSame(1,$listenerCalled);
239+
$this->assertSame(1,$listener->callCount);
223240
}
224241

225242
publicfunctiontestCollectCollectsDecisionLogWhenStrategyIsAffirmative()

‎src/Symfony/Bundle/SecurityBundle/Tests/Debug/TraceableFirewallListenerTest.php‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
useSymfony\Component\Security\Http\Authenticator\Passport\Passport;
3131
useSymfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
3232
useSymfony\Component\Security\Http\Firewall\AuthenticatorManagerListener;
33+
useSymfony\Component\Security\Http\Firewall\FirewallListenerInterface;
3334
useSymfony\Component\Security\Http\Logout\LogoutUrlGenerator;
3435

3536
/**
@@ -41,9 +42,25 @@ public function testOnKernelRequestRecordsListeners()
4142
{
4243
$request =newRequest();
4344
$event =newRequestEvent($this->createMock(HttpKernelInterface::class),$request, HttpKernelInterface::MAIN_REQUEST);
44-
$event->setResponse($response =newResponse());
45-
$listener =function ($e)use ($event, &$listenerCalled) {
46-
$listenerCalled +=$e ===$event;
45+
$event->setResponse(newResponse());
46+
$listener =newclassimplements FirewallListenerInterface
47+
{
48+
publicint$callCount =0;
49+
50+
publicfunctionsupports(Request$request): ?bool
51+
{
52+
returntrue;
53+
}
54+
55+
publicfunctionauthenticate(RequestEvent$event):void
56+
{
57+
++$this->callCount;
58+
}
59+
60+
publicstaticfunctiongetPriority():int
61+
{
62+
return0;
63+
}
4764
};
4865
$firewallMap =$this->createMock(FirewallMap::class);
4966
$firewallMap

‎src/Symfony/Bundle/SecurityBundle/composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"symfony/clock":"^6.4|^7.0|^8.0",
2323
"symfony/config":"^7.3|^8.0",
2424
"symfony/dependency-injection":"^6.4.11|^7.1.4|^8.0",
25+
"symfony/deprecation-contracts":"^2.5|^3",
2526
"symfony/event-dispatcher":"^6.4|^7.0|^8.0",
2627
"symfony/http-kernel":"^6.4|^7.0|^8.0",
2728
"symfony/http-foundation":"^6.4|^7.0|^8.0",

‎src/Symfony/Component/Security/Http/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Deprecate callable firewall listeners, extend`AbstractListener` or implement`FirewallListenerInterface` instead
8+
49
7.3
510
---
611

‎src/Symfony/Component/Security/Http/Firewall.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
useSymfony\Component\HttpKernel\Event\FinishRequestEvent;
1717
useSymfony\Component\HttpKernel\Event\RequestEvent;
1818
useSymfony\Component\HttpKernel\KernelEvents;
19+
useSymfony\Component\Security\Http\Firewall\AbstractListener;
1920
useSymfony\Component\Security\Http\Firewall\ExceptionListener;
2021
useSymfony\Component\Security\Http\Firewall\FirewallListenerInterface;
2122
useSymfony\Contracts\EventDispatcher\EventDispatcherInterface;
@@ -123,6 +124,8 @@ protected function callListeners(RequestEvent $event, iterable $listeners)
123124
{
124125
foreach ($listenersas$listener) {
125126
if (!$listenerinstanceof FirewallListenerInterface) {
127+
trigger_deprecation('symfony/security-http','7.4','Using a callable as firewall listener is deprecated, extend "%s" or implement "%s" instead.', AbstractListener::class, FirewallListenerInterface::class);
128+
126129
$listener($event);
127130
}elseif (false !==$listener->supports($event->getRequest())) {
128131
$listener->authenticate($event);
@@ -134,8 +137,8 @@ protected function callListeners(RequestEvent $event, iterable $listeners)
134137
}
135138
}
136139

137-
privatefunctiongetListenerPriority(object$logoutListener):int
140+
privatefunctiongetListenerPriority(object$listener):int
138141
{
139-
return$logoutListenerinstanceof FirewallListenerInterface ?$logoutListener->getPriority() :0;
142+
return$listenerinstanceof FirewallListenerInterface ?$listener->getPriority() :0;
140143
}
141144
}

‎src/Symfony/Component/Security/Http/Tests/FirewallTest.php‎

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\Security\Http\Tests;
1313

1414
usePHPUnit\Framework\TestCase;
15+
useSymfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
1516
useSymfony\Component\EventDispatcher\EventDispatcherInterface;
1617
useSymfony\Component\HttpFoundation\Request;
1718
useSymfony\Component\HttpFoundation\Response;
@@ -25,6 +26,8 @@
2526

2627
class FirewallTestextends TestCase
2728
{
29+
use ExpectUserDeprecationMessageTrait;
30+
2831
publicfunctiontestOnKernelRequestRegistersExceptionListener()
2932
{
3033
$dispatcher =$this->createMock(EventDispatcherInterface::class);
@@ -54,21 +57,26 @@ public function testOnKernelRequestRegistersExceptionListener()
5457

5558
publicfunctiontestOnKernelRequestStopsWhenThereIsAResponse()
5659
{
57-
$called = [];
60+
$listener =newclassextends AbstractListener
61+
{
62+
publicint$callCount =0;
5863

59-
$first =function ()use (&$called) {
60-
$called[] =1;
61-
};
64+
publicfunctionsupports(Request$request): ?bool
65+
{
66+
returntrue;
67+
}
6268

63-
$second =function ()use (&$called) {
64-
$called[] =2;
69+
publicfunctionauthenticate(RequestEvent$event):void
70+
{
71+
++$this->callCount;
72+
}
6573
};
6674

6775
$map =$this->createMock(FirewallMapInterface::class);
6876
$map
6977
->expects($this->once())
7078
->method('getListeners')
71-
->willReturn([[$first,$second],null,null])
79+
->willReturn([[$listener,$listener],null,null])
7280
;
7381

7482
$event =newRequestEvent($this->createMock(HttpKernelInterface::class),newRequest(), HttpKernelInterface::MAIN_REQUEST);
@@ -77,7 +85,7 @@ public function testOnKernelRequestStopsWhenThereIsAResponse()
7785
$firewall =newFirewall($map,$this->createMock(EventDispatcherInterface::class));
7886
$firewall->onKernelRequest($event);
7987

80-
$this->assertSame([1],$called);
88+
$this->assertSame(1,$listener->callCount);
8189
}
8290

8391
publicfunctiontestOnKernelRequestWithSubRequest()
@@ -100,11 +108,10 @@ public function testOnKernelRequestWithSubRequest()
100108
$this->assertFalse($event->hasResponse());
101109
}
102110

103-
publicfunctiontestListenersAreCalled()
111+
publicfunctiontestFirewallListenersAreCalled()
104112
{
105113
$calledListeners = [];
106114

107-
$callableListener =staticfunction()use(&$calledListeners) {$calledListeners[] ='callableListener'; };
108115
$firewallListener =newclass($calledListeners)implements FirewallListenerInterface {
109116
publicfunction__construct(privatearray &$calledListeners) {}
110117

@@ -144,14 +151,43 @@ public function authenticate(RequestEvent $event): void
144151
->expects($this->once())
145152
->method('getListeners')
146153
->with($this->equalTo($request))
147-
->willReturn([[$callableListener,$firewallListener,$callableFirewallListener],null,null])
154+
->willReturn([[$firewallListener,$callableFirewallListener],null,null])
155+
;
156+
157+
$event =newRequestEvent($this->createMock(HttpKernelInterface::class),$request, HttpKernelInterface::MAIN_REQUEST);
158+
159+
$firewall =newFirewall($map,$this->createMock(EventDispatcherInterface::class));
160+
$firewall->onKernelRequest($event);
161+
162+
$this->assertSame(['firewallListener','callableFirewallListener'],$calledListeners);
163+
}
164+
165+
/**
166+
* @group legacy
167+
*/
168+
publicfunctiontestCallableListenersAreCalled()
169+
{
170+
$this->expectUserDeprecationMessage('Since symfony/security-http 7.4: Using a callable as firewall listener is deprecated, extend "Symfony\Component\Security\Http\Firewall\AbstractListener" or implement "Symfony\Component\Security\Http\Firewall\FirewallListenerInterface" instead.');
171+
172+
$calledListeners = [];
173+
174+
$callableListener =staticfunction()use(&$calledListeners) {$calledListeners[] ='callableListener'; };
175+
176+
$request =$this->createMock(Request::class);
177+
178+
$map =$this->createMock(FirewallMapInterface::class);
179+
$map
180+
->expects($this->once())
181+
->method('getListeners')
182+
->with($this->equalTo($request))
183+
->willReturn([[$callableListener],null,null])
148184
;
149185

150186
$event =newRequestEvent($this->createMock(HttpKernelInterface::class),$request, HttpKernelInterface::MAIN_REQUEST);
151187

152188
$firewall =newFirewall($map,$this->createMock(EventDispatcherInterface::class));
153189
$firewall->onKernelRequest($event);
154190

155-
$this->assertSame(['callableListener','firewallListener','callableFirewallListener'],$calledListeners);
191+
$this->assertSame(['callableListener'],$calledListeners);
156192
}
157193
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp