Skip to content

Commit 33d2fac

Browse files
committed
Refresh PHPStan baseline, more session provider hardening
1 parent f3448ee commit 33d2fac

4 files changed

Lines changed: 43 additions & 21 deletions

File tree

phpstan-baseline.neon

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: '#^Parameter \#1 \$data of function unserialize expects string, mixed given\.$#'
5-
identifier: argument.type
6-
count: 1
7-
path: src/Authentication/Provider/SessionAuthenticationProvider.php
8-
93
-
104
message: '#^Cannot cast mixed to string\.$#'
115
identifier: cast.string
@@ -144,12 +138,6 @@ parameters:
144138
count: 1
145139
path: src/DependencyInjection/Compiler/PingDBALConnectionsCompilerPass.php
146140

147-
-
148-
message: '#^Method BabDev\\WebSocketBundle\\DependencyInjection\\Factory\\Authentication\\AuthenticationProviderFactory\:\:addConfiguration\(\) has parameter \$builder with generic class Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition but does not specify its types\: TParent$#'
149-
identifier: missingType.generics
150-
count: 1
151-
path: src/DependencyInjection/Factory/Authentication/AuthenticationProviderFactory.php
152-
153141
-
154142
message: '#^Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition\:\:children\(\)\.$#'
155143
identifier: method.notFound
@@ -198,12 +186,6 @@ parameters:
198186
count: 1
199187
path: src/DependencyInjection/Factory/Authentication/SessionAuthenticationProviderFactory.php
200188

201-
-
202-
message: '#^Method BabDev\\WebSocketBundle\\DependencyInjection\\Factory\\Authentication\\SessionAuthenticationProviderFactory\:\:addConfiguration\(\) has parameter \$builder with generic class Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition but does not specify its types\: TParent$#'
203-
identifier: missingType.generics
204-
count: 1
205-
path: src/DependencyInjection/Factory/Authentication/SessionAuthenticationProviderFactory.php
206-
207189
-
208190
message: '#^Parameter \#1 \$class of function class_exists expects string, mixed given\.$#'
209191
identifier: argument.type
@@ -235,7 +217,7 @@ parameters:
235217
path: src/Routing/Loader/AttributeLoader.php
236218

237219
-
238-
message: '#^Cannot use array destructuring on \(list\<\(BabDev\\WebSocket\\Server\\WAMP\\WAMPConnection&PHPUnit\\Framework\\MockObject\\MockObject\)\|string\>\)\|null\.$#'
220+
message: '#^Cannot use array destructuring on \(list\<\(BabDev\\WebSocket\\Server\\WAMP\\WAMPConnection&PHPUnit\\Framework\\MockObject\\Stub\)\|string\>\)\|null\.$#'
239221
identifier: offsetAccess.nonArray
240222
count: 5
241223
path: tests/Authentication/StorageBackedConnectionRepositoryTest.php

src/Authentication/Provider/SessionAuthenticationProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ private function getToken(Connection $connection): TokenInterface
8383

8484
foreach ($this->firewalls as $firewall) {
8585
if (false !== $serializedToken = $session->get($sessionKey = '_security_'.$firewall, false)) {
86+
if (!is_string($serializedToken)) {
87+
$this->logger?->debug('Session has a non-string serialized token.', [
88+
'key' => $sessionKey,
89+
'type' => get_debug_type($serializedToken),
90+
]);
91+
92+
break;
93+
}
94+
8695
$token = $this->safelyUnserialize($serializedToken, $sessionKey);
8796

8897
$this->logger?->debug('Read existing security token from the session.', [

tests/Authentication/Provider/SessionAuthenticationProviderTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,37 @@ public function testANullTokenUsedWhenANonSecurityTokenIsExtractedFromTheSession
161161
self::assertInstanceOf(NullToken::class, $this->provider->authenticate($connection));
162162
}
163163

164+
public function testANullTokenUsedWhenANonStringIsExtractedFromTheSession(): void
165+
{
166+
/** @var MockObject&SessionInterface $session */
167+
$session = $this->createMock(SessionInterface::class);
168+
$session->expects(self::once())
169+
->method('get')
170+
->with('_security_main')
171+
->willReturn(new \stdClass());
172+
173+
$attributeStore = new ArrayAttributeStore();
174+
$attributeStore->set('session', $session);
175+
$attributeStore->set('resource_id', 'resource');
176+
177+
/** @var MockObject&Connection $connection */
178+
$connection = $this->createMock(Connection::class);
179+
$connection->method('getAttributeStore')
180+
->willReturn($attributeStore);
181+
182+
$storageIdentifier = '42';
183+
184+
$this->tokenStorage->expects(self::once())
185+
->method('generateStorageId')
186+
->willReturn($storageIdentifier);
187+
188+
$this->tokenStorage->expects(self::once())
189+
->method('addToken')
190+
->with($storageIdentifier, self::isInstanceOf(TokenInterface::class));
191+
192+
self::assertInstanceOf(NullToken::class, $this->provider->authenticate($connection));
193+
}
194+
164195
public function testANullTokenUsedWhenUnserializingTheTokenRaisesAnError(): void
165196
{
166197
/** @var MockObject&SessionInterface $session */

tests/Authentication/StorageBackedConnectionRepositoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,13 @@ public function testFetchingAllConnectionsByDefaultOnlyReturnsAuthenticatedUsers
177177
$storageId1 = 42;
178178
$storageId2 = 84;
179179

180-
/** @var Stub&TokenInterface $authenticatedToken */
180+
/** @var MockObject&TokenInterface $authenticatedToken */
181181
$authenticatedToken = $this->createMock(TokenInterface::class);
182182
$authenticatedToken->expects(self::once())
183183
->method('getUser')
184184
->willReturn(self::createStub(UserInterface::class));
185185

186-
/** @var Stub&TokenInterface $guestToken */
186+
/** @var MockObject&TokenInterface $guestToken */
187187
$guestToken = $this->createMock(TokenInterface::class);
188188
$guestToken->expects(self::once())
189189
->method('getUser')

0 commit comments

Comments
 (0)