Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit 3ca3e98

Browse files
Merge branch '4.3' into 4.4
* 4.3: [OptionsResolve] Revert change in tests for a not-merged change in code [HttpClient] fix handling of 3xx with no Location header - ignore Content-Length when no body is expected [Workflow] Made the configuration more robust for the 'property' key [Security/Core] make NativePasswordEncoder use sodium to validate passwords when possible #30432 fix an error message fix paths to detect code owners [HttpClient] ignore the body of responses to HEAD requests [Validator] Ensure numeric subpaths do not cause errors on PHP 7.4 [SecurityBundle] Fix wrong assertion Remove unused local variables in tests [Yaml][Parser] Remove the getLastLineNumberBeforeDeprecation() internal unused method Make sure to collect child forms created on *_SET_DATA events [WebProfilerBundle] Improve display in Email panel for dark theme do not render errors for checkboxes twice
2 parents 5d55b92 + 9938711 commit 3ca3e98

9 files changed

+28
-25
lines changed

Core/Encoder/NativePasswordEncoder.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct(int $opsLimit = null, int $memLimit = null, int $cos
4848
throw new \InvalidArgumentException('$cost must be in the range of 4-31.');
4949
}
5050

51-
$this->algo = $algo ?? (\defined('PASSWORD_ARGON2I') ? max(PASSWORD_DEFAULT, \defined('PASSWORD_ARGON2ID') ? PASSWORD_ARGON2ID : PASSWORD_ARGON2I) : PASSWORD_DEFAULT);
51+
$this->algo = (string) ($algo ?? \defined('PASSWORD_ARGON2ID') ? PASSWORD_ARGON2ID : (\defined('PASSWORD_ARGON2I') ? PASSWORD_ARGON2I : PASSWORD_BCRYPT));
5252
$this->options = [
5353
'cost' => $cost,
5454
'time_cost' => $opsLimit,
@@ -62,33 +62,38 @@ public function __construct(int $opsLimit = null, int $memLimit = null, int $cos
6262
*/
6363
public function encodePassword($raw, $salt): string
6464
{
65-
if (\strlen($raw) > self::MAX_PASSWORD_LENGTH) {
65+
if (\strlen($raw) > self::MAX_PASSWORD_LENGTH || ((string) PASSWORD_BCRYPT === $this->algo && 72 < \strlen($raw))) {
6666
throw new BadCredentialsException('Invalid password.');
6767
}
6868

6969
// Ignore $salt, the auto-generated one is always the best
7070

71-
$encoded = password_hash($raw, $this->algo, $this->options);
72-
73-
if (72 < \strlen($raw) && 0 === strpos($encoded, '$2')) {
74-
// BCrypt encodes only the first 72 chars
75-
throw new BadCredentialsException('Invalid password.');
76-
}
77-
78-
return $encoded;
71+
return password_hash($raw, $this->algo, $this->options);
7972
}
8073

8174
/**
8275
* {@inheritdoc}
8376
*/
8477
public function isPasswordValid($encoded, $raw, $salt): bool
8578
{
86-
if (72 < \strlen($raw) && 0 === strpos($encoded, '$2')) {
87-
// BCrypt encodes only the first 72 chars
79+
if (\strlen($raw) > self::MAX_PASSWORD_LENGTH) {
8880
return false;
8981
}
9082

91-
return \strlen($raw) <= self::MAX_PASSWORD_LENGTH && password_verify($raw, $encoded);
83+
if (0 === strpos($encoded, '$2')) {
84+
// BCrypt encodes only the first 72 chars
85+
return 72 >= \strlen($raw) && password_verify($raw, $encoded);
86+
}
87+
88+
if (\extension_loaded('sodium') && version_compare(\SODIUM_LIBRARY_VERSION, '1.0.14', '>=')) {
89+
return sodium_crypto_pwhash_str_verify($encoded, $raw);
90+
}
91+
92+
if (\extension_loaded('libsodium') && version_compare(phpversion('libsodium'), '1.0.14', '>=')) {
93+
return \Sodium\crypto_pwhash_str_verify($encoded, $raw);
94+
}
95+
96+
return password_verify($raw, $encoded);
9297
}
9398

9499
/**

Core/Encoder/SodiumPasswordEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function isPasswordValid($encoded, $raw, $salt): bool
9393
return \Sodium\crypto_pwhash_str_verify($encoded, $raw);
9494
}
9595

96-
throw new LogicException('Libsodium is not available. You should either install the sodium extension, upgrade to PHP 7.2+ or use a different encoder.');
96+
return false;
9797
}
9898

9999
/**

Core/Tests/Encoder/EncoderFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function testGetInvalidNamedEncoderForEncoderAware()
117117

118118
$user = new EncAwareUser('user', 'pass');
119119
$user->encoderName = 'invalid_encoder_name';
120-
$encoder = $factory->getEncoder($user);
120+
$factory->getEncoder($user);
121121
}
122122

123123
public function testGetEncoderForEncoderAwareWithClassName()

Guard/Tests/Provider/GuardAuthenticationProviderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function testGuardWithNoLongerAuthenticatedTriggersLogout()
166166
$token->setAuthenticated(false);
167167

168168
$provider = new GuardAuthenticationProvider([], $this->userProvider, $providerKey, $this->userChecker);
169-
$actualToken = $provider->authenticate($token);
169+
$provider->authenticate($token);
170170
}
171171

172172
public function testSupportsChecksGuardAuthenticatorsTokenOrigin()

Http/Tests/Firewall/LogoutListenerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class LogoutListenerTest extends TestCase
2121
{
2222
public function testHandleUnmatchedPath()
2323
{
24-
list($listener, $tokenStorage, $httpUtils, $options) = $this->getListener();
24+
list($listener, , $httpUtils, $options) = $this->getListener();
2525

2626
list($event, $request) = $this->getGetResponseEvent();
2727

@@ -131,7 +131,7 @@ public function testSuccessHandlerReturnsNonResponse()
131131
$this->expectException('RuntimeException');
132132
$successHandler = $this->getSuccessHandler();
133133

134-
list($listener, $tokenStorage, $httpUtils, $options) = $this->getListener($successHandler);
134+
list($listener, , $httpUtils, $options) = $this->getListener($successHandler);
135135

136136
list($event, $request) = $this->getGetResponseEvent();
137137

@@ -153,7 +153,7 @@ public function testCsrfValidationFails()
153153
$this->expectException('Symfony\Component\Security\Core\Exception\LogoutException');
154154
$tokenManager = $this->getTokenManager();
155155

156-
list($listener, $tokenStorage, $httpUtils, $options) = $this->getListener(null, $tokenManager);
156+
list($listener, , $httpUtils, $options) = $this->getListener(null, $tokenManager);
157157

158158
list($event, $request) = $this->getGetResponseEvent();
159159

Http/Tests/Firewall/RememberMeListenerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function testOnCoreSecurity()
224224

225225
public function testSessionStrategy()
226226
{
227-
list($listener, $tokenStorage, $service, $manager, , $dispatcher, $sessionStrategy) = $this->getListener(false, true, true);
227+
list($listener, $tokenStorage, $service, $manager, , , $sessionStrategy) = $this->getListener(false, true, true);
228228

229229
$tokenStorage
230230
->expects($this->once())
@@ -289,7 +289,7 @@ public function testSessionStrategy()
289289

290290
public function testSessionIsMigratedByDefault()
291291
{
292-
list($listener, $tokenStorage, $service, $manager, , $dispatcher, $sessionStrategy) = $this->getListener(false, true, false);
292+
list($listener, $tokenStorage, $service, $manager) = $this->getListener(false, true, false);
293293

294294
$tokenStorage
295295
->expects($this->once())

Http/Tests/Firewall/RemoteUserAuthenticationListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testGetPreAuthenticatedDataNoUser()
6060
$method = new \ReflectionMethod($listener, 'getPreAuthenticatedData');
6161
$method->setAccessible(true);
6262

63-
$result = $method->invokeArgs($listener, [$request]);
63+
$method->invokeArgs($listener, [$request]);
6464
}
6565

6666
public function testGetPreAuthenticatedDataWithDifferentKeys()

Http/Tests/Firewall/X509AuthenticationListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function testGetPreAuthenticatedDataNoData()
9898
$method = new \ReflectionMethod($listener, 'getPreAuthenticatedData');
9999
$method->setAccessible(true);
100100

101-
$result = $method->invokeArgs($listener, [$request]);
101+
$method->invokeArgs($listener, [$request]);
102102
}
103103

104104
public function testGetPreAuthenticatedDataWithDifferentKeys()

Http/Tests/RememberMe/ResponseListenerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ public function testRememberMeCookieIsNotSendWithResponse()
6666

6767
public function testItSubscribesToTheOnKernelResponseEvent()
6868
{
69-
$listener = new ResponseListener();
70-
7169
$this->assertSame([KernelEvents::RESPONSE => 'onKernelResponse'], ResponseListener::getSubscribedEvents());
7270
}
7371

0 commit comments

Comments
 (0)