8000 [Security] Improve BC-layer to deprecate eraseCredentials methods · symfony/symfony@40b3696 · GitHub
[go: up one dir, main page]

Skip to content

Commit 40b3696

Browse files
[Security] Improve BC-layer to deprecate eraseCredentials methods
1 parent e556606 commit 40b3696

34 files changed

+153
-206
lines changed

UPGRADE-7.3.md

+24-6
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,37 @@ If you're upgrading from a version below 7.2, follow the [7.2 upgrade guide](UPG
1111
Ldap
1212
----
1313

14-
* Deprecate `LdapUser::eraseCredentials()`, use `LdapUser::setPassword(null)` instead
14+
* Deprecate `LdapUser::eraseCredentials()` in favor of `__serialize()`
1515

1616
Security
1717
--------
1818

1919
* Deprecate `UserInterface::eraseCredentials()` and `TokenInterface::eraseCredentials()`,
20-
use a dedicated DTO or erase credentials on your own e.g. upon `AuthenticationTokenCreatedEvent` instead
20+
erase credentials in e.g. `__serialize()` instead
2121

22-
SecurityBundle
23-
--------------
22+
*Before*
23+
```php
24+
public function eraseCredentials(): void
25+
{
26+
}
27+
```
2428

25-
* Deprecate the `erase_credentials` config option, erase credentials on your own e.g. upon `AuthenticationTokenCreatedEvent` instead
29+
*After*
30+
```php
31+
#[\Deprecated]
32+
public function eraseCredentials(): void
33+
{
34+
}
35+
36+
// If your eraseCredentials() method was used to empty a "password" property:
37+
public function __serialize(): array
38+
{
39+
$data = (array) $this;
40+
unset($data["\0".self::class."\0password"]);
41+
42+
return $data;
43+
}
44+
```
2645

2746
Console
2847
-------
@@ -131,4 +150,3 @@ VarDumper
131150

132151
* Deprecate `ResourceCaster::castCurl()`, `ResourceCaster::castGd()` and `ResourceCaster::castOpensslX509()`
133152
* Mark all casters as `@internal`
134-
* Deprecate the `CompiledClassMetadataFactory` and `CompiledClassMetadataCacheWarmer` classes

src/Symfony/Bridge/Doctrine/Tests/Fixtures/User.php

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function getUserIdentifier(): string
4545
return $this->name;
4646
}
4747

48+
#[\Deprecated]
4849
public function eraseCredentials(): void
4950
{
5051
}

src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public static function handleError($type, $msg, $file, $line, $context = [])
336336

337337
return $h ? $h($type, $msg, $file, $line, $context) : false;
338338
}
339-
// If the message is serialized we need to extract the message. This occurs when the error is triggered by
339+
// If the message is serialized we need to extract the message. This occurs when the error is triggered
340340
// by the isolated test path in \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait::endTest().
341341
$parsedMsg = @unserialize($msg);
342342
if (\is_array($parsedMsg)) {

src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ CHANGELOG
88
* Add encryption support to `OidcTokenHandler` (JWE)
99
* Add `expose_security_errors` config option to display `AccountStatusException`
1010
* Deprecate the `security.hide_user_not_found` config option in favor of `security.expose_security_errors`
11-
* Deprecate the `erase_credentials` config option, erase credentials on your own e.g. upon `AuthenticationTokenCreatedEvent` instead
1211

1312
7.2
1413
---

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/LdapFactoryTrait.php

-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\DependencyInjection\Definition;
1717
use Symfony\Component\DependencyInjection\Reference;
1818
use Symfony\Component\Ldap\Security\CheckLdapCredentialsListener;
19-
use Symfony\Component\Ldap\Security\EraseLdapUserCredentialsListener;
2019
use Symfony\Component\Ldap\Security\LdapAuthenticator;
2120

2221
/**
@@ -43,12 +42,6 @@ public function createAuthenticator(ContainerBuilder $container, string $firewal
4342
->addArgument(new Reference('security.ldap_locator'))
4443
;
4544

46-
if (class_exists(EraseLdapUserCredentialsListener::class && !$container->getParameter('security.authentication.manager.erase_credentials'))) {
47-
$container->setDefinition('security.listener.'.$key.'.'.$firewallName.'erase_ldap_credentials', new Definition(EraseLdapUserCredentialsListener::class))
48-
->addTag('kernel.event_subscriber', ['dispatcher' => 'security.event_dispatcher.'.$firewallName])
49-
;
50-
}
51-
5245
$ldapAuthenticatorId = 'security.authenticator.'.$key.'.'.$firewallName;
5346
$definition = $container->setDefinition($ldapAuthenticatorId, new Definition(LdapAuthenticator::class))
5447
->setArguments([

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

-3
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ public function load(array $configs, ContainerBuilder $container): void
136136

137137
// set some global scalars
138138
$container->setParameter('security.access.denied_url', $config['access_denied_url']);
139-
if (true === $config['erase_credentials']) {
140-
trigger_deprecation('symfony/security-bundle', '7.3', 'Setting the "security.erase_credentials" config option to true is deprecated and won\'t have any effect in 8.0, set it to false instead and use your own erasing logic if needed.');
141-
}
142139
$container->setParameter('security.authentication.manager.erase_credentials', $config['erase_credentials']);
143140
$container->setParameter('security.authentication.session_strategy.strategy', $config['session_fixation_strategy']);
144141

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\HttpFoundation\Response;
2020
use Symfony\Component\HttpKernel\Event\RequestEvent;
2121
use Symfony\Component\HttpKernel\HttpKernelInterface;
22+
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
2223
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2324
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2425
use Symfony\Component\Security\Http\Authentication\AuthenticatorManager;
@@ -89,7 +90,7 @@ public function testOnKernelRequestRecordsAuthenticatorsInfo()
8990
$supportingAuthenticator
9091
->expects($this->once())
9192
->method('createToken')
92-
->willReturn($this->createMock(TokenInterface::class));
93+
->willReturn(new class extends AbstractToken {});
9394

9495
$notSupportingAuthenticator = $this->createMock(DummyAuthenticator::class);
9596
$notSupportingAuthenticator
@@ -103,9 +104,7 @@ public function testOnKernelRequestRecordsAuthenticatorsInfo()
103104
[new TraceableAuthenticator($notSupportingAuthenticator), new TraceableAuthenticator($supportingAuthenticator)],
104105
$tokenStorage,
105106
$dispatcher,
106-
'main',
107-
null,
108-
false
107+
'main'
109108
);
110109

111110
$listener = new TraceableAuthenticatorManagerListener(new AuthenticatorManagerListener($authenticatorManager));

src/Symfony/Bundle/SecurityBundle/composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"symfony/clock": "^6.4|^7.0",
2323
"symfony/config": "^6.4|^7.0",
2424
"symfony/dependency-injection": "^6.4.11|^7.1.4",
25-
"symfony/deprecation-contracts": "^2.5|^3",
2625
"symfony/event-dispatcher": "^6.4|^7.0",
2726
"symfony/http-kernel": "^6.4|^7.0",
2827
"symfony/http-foundation": "^6.4|^7.0",

src/Symfony/Component/Ldap/CHANGELOG.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ CHANGELOG
44
7.3
55
---
66

7-
* Deprecate `LdapUser::eraseCredentials()`, use `LdapUser::setPassword(null)` instead
8-
* Add `EraseLdapUserCredentialsListener`
7+
* Deprecate `LdapUser::eraseCredentials()` in favor of `__serialize()`
98

109
7.2
1110
---

src/Symfony/Component/Ldap/Security/EraseLdapUserCredentialsListener.php

-48
This file was deleted.

src/Symfony/Component/Ldap/Security/LdapUser.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,15 @@ public function getUserIdentifier(): string
6060
return $this->identifier;
6161
}
6262

63+
/**
64+
* @deprecated since Symfony 7.3
65+
*/
66+
#[\Deprecated(since: 'symfony/ldap 7.3')]
6367
public function eraseCredentials(): void
6468
{
65-
trigger_deprecation('symfony/security-core', '7.3', sprintf('The "%s()" method is deprecated and will be removed in 8.0, call "setPassword(null)" instead.', __METHOD__));
69+
if (\PHP_VERSION_ID < 80400) {
70+
@trigger_error(\sprintf('Method %s::eraseCredentials() is deprecated since symfony/ldap 7.3', self::class), \E_USER_DEPRECATED);
71+
}
6672

6773
$this->password = null;
6874
}
@@ -100,11 +106,9 @@ public function isEqualTo(UserInterface $user): bool
100106

101107
public function __serialize(): array
102108
{
103-
return [$this->entry, $this->identifier, null, $this->roles, $this->extraFields];
104-
}
109+
$data = (array) $this;
110+
unset($data[\sprintf("\0%s\0password", self::class)]);
105111

106-
public function __unserialize(array $data): void
107-
{
108-
[$this->entry, $this->identifier, $this->password, $this->roles, $this->extraFields] = $data;
112+
return $data;
109113
}
110114
}

src/Symfony/Component/Ldap/Tests/Security/EraseLdapUserCredentialsListenerTest.php

-53
This file was deleted.

src/Symfony/Component/Ldap/composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"require": {
1919
"php": ">=8.2",
2020
"ext-ldap": "*",
21-
"symfony/deprecation-contracts": "^2.5|^3",
2221
"symfony/options-resolver": "^6.4|^7.0"
2322
},
2423
"require-dev": {

src/Symfony/Component/PasswordHasher/Tests/Fixtures/TestLegacyPasswordAuthenticatedUser.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ public function getRoles(): array
3535
return $this->roles;
3636
}
3737

38+
#[\Deprecated]
3839
public function eraseCredentials(): void
3940
{
40-
// Do nothing
41-
return;
4241
}
4342

4443
public function getUserIdentifier(): string

src/Symfony/Component/PasswordHasher/Tests/Hasher/PasswordHasherFactoryTest.php

-16
Original file line numberDiff line numberDiff line change
@@ -238,25 +238,9 @@ public function testMigrateFromWithCustomInstance()
238238

239239
class SomeUser implements PasswordAuthenticatedUserInterface
240240
{
241-
public function getRoles(): array
242-
{
243-
}
244-
245241
public function getPassword(): ?string
246242
{
247243
}
248-
249-
public function getSalt(): ?string
250-
{
251-
}
252-
253-
public function getUserIdentifier(): string
254-
{
255-
}
256-
257-
public function eraseCredentials()
258-
{
259-
}
260244
}
261245

262246
class SomeChildUser extends SomeUser

src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ public function setUser(UserInterface $user): void
6262
/**
6363
* Removes sensitive information from the token.
6464
*
65-
* @deprecated since Symfony 7.3
65+
* @deprecated since Symfony 7.3, erase credentials using the "__serialize()" method instead
6666
*/
6767
public function eraseCredentials(): void
6868
{
69-
trigger_deprecation('symfony/security-core', '7.3', sprintf('The "%s()" method is deprecated and will be removed in 8.0, use a DTO instead or implement your own erasing logic if needed.', __METHOD__));
69+
trigger_deprecation('symfony/security-core', '7.3', \sprintf('The "%s::eraseCredentials()" method is deprecated and will be removed in 8.0, erase credentials using the "__serialize()" method instead.', TokenInterface::class));
7070

7171
if ($this->getUser() instanceof UserInterface) {
7272
$this->getUser()->eraseCredentials();

src/Symfony/Component/Security/Core/Authentication/Token/NullToken.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ public function getUserIdentifier(): string
4444
}
4545

4646
/**
47-
* Removes sensitive information from the token.
48-
*
4947
* @deprecated since Symfony 7.3
5048
*/
49+
#[\Deprecated(since: 'symfony/security-core 7.3')]
5150
public function eraseCredentials(): void
5251
{
52+
if (\PHP_VERSION_ID < 80400) {
53+
@trigger_error(\sprintf('Method %s::eraseCredentials() is deprecated since symfony/security-core 7.3', self::class), \E_USER_DEPRECATED);
54+
}
5355
}
5456

5557
public function getAttributes(): array

src/Symfony/Component/Security/Core/Authentication/Token/TokenInterface.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
/**
1717
* TokenInterface is the interface for the user authentication information.
1818
*
19+
* The __serialize/__unserialize() magic methods can be implemented on the token
20+
* class to prevent sensitive credentials from being put in the session storage.
21+
*
1922
* @author Fabien Potencier <fabien@symfony.com>
2023
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
2124
*/
@@ -57,8 +60,7 @@ public function setUser(UserInterface $user): void;
5760
/**
5861
* Removes sensitive information from the token.
5962
*
60-
* @deprecated since Symfony 7.3, use a dedicated DTO instead or implement your
61-
* own erasing logic instead
63+
* @deprecated since Symfony 7.3; erase credentials using the "__serialize()" method instead
6264
*/
6365
public function eraseCredentials(): void;
6466

src/Symfony/Component/Security/Core/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CHANGELOG
88
For example, users not currently logged in, or while processing a message from a message queue.
99
* Add `OfflineTokenInterface` to mark tokens that do not represent the currently logged-in user
1010
* Deprecate `UserInterface::eraseCredentials()` and `TokenInterface::eraseCredentials()`,
11-
use a dedicated DTO or erase credentials on your own e.g. upon `AuthenticationTokenCreatedEvent` instead
11+
erase credentials using the `__serialize()` method instead
1212

1313
7.2
1414
---

src/Symfony/Component/Security/Core/Tests/Authentication/AuthenticationTrustResolverTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public function getUserIdentifier(): string
119119
{
120120
}
121121

122+
#[\Deprecated]
122123
public function eraseCredentials(): void
123124
{
124125
}

0 commit comments

Comments
 (0)
0