E573 Merge branch '5.3' into 5.4 · symfony/symfony@f1353d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit f1353d5

Browse files
committed
Merge branch '5.3' into 5.4
* 5.3: [Security] Fix str_contains type mismatch in ChannelListener remove 5.2 branch from PR template [PasswordHasher] Fix usage of PasswordHasherAdapter in PasswordHasherFactory
2 parents 62d4f44 + e39ee06 commit f1353d5

File tree

5 files changed

+67
-12
lines changed

5 files changed

+67
-12
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
| Q | A
22
| ------------- | ---
3-
| Branch? | 5.4 for features / 4.4, 5.2 or 5.3 for bug fixes <!-- see below -->
3+
| Branch? | 5.4 for features / 4.4 or 5.3 for bug fixes <!-- see below -->
44
| Bug fix? | yes/no
55
| New feature? | yes/no <!-- please update src/**/CHANGELOG.md files -->
66
| Deprecations? | yes/no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->

src/Symfony/Component/PasswordHasher/Hasher/PasswordHasherFactory.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,7 @@ public function getPasswordHasher($user): PasswordHasherInterface
6161
throw new \RuntimeException(sprintf('No password hasher has been configured for account "%s".', \is_object($user) ? get_debug_type($user) : $user));
6262
}
6363

64-
if (!$this->passwordHashers[$hasherKey] instanceof PasswordHasherInterface) {
65-
$this->passwordHashers[$hasherKey] = $this->passwordHashers[$hasherKey] instanceof PasswordEncoderInterface
66-
? new PasswordHasherAdapter($this->passwordHashers[$hasherKey])
67-
: $this->createHasher($this->passwordHashers[$hasherKey])
68-
;
69-
}
70-
71-
return $this->passwordHashers[$hasherKey];
64+
return $this->createHasherUsingAdapter($hasherKey);
7265
}
7366

7467
/**
@@ -111,6 +104,18 @@ private function createHasher(array $config, bool $isExtra = false): PasswordHas
111104
return new MigratingPasswordHasher($hasher, ...$extrapasswordHashers);
112105
}
113106

107+
private function createHasherUsingAdapter(string $hasherKey): PasswordHasherInterface
108+
{
109+
if (!$this->passwordHashers[$hasherKey] instanceof PasswordHasherInterface) {
110+
$this->passwordHashers[$hasherKey] = $this->passwordHashers[$hasherKey] instanceof PasswordEncoderInterface
111+
? new PasswordHasherAdapter($this->passwordHashers[$hasherKey])
112+
: $this->createHasher($this->passwordHashers[$hasherKey])
113+
;
114+
}
115+
116+
return $this->passwordHashers[$hasherKey];
117+
}
118+
114119
private function getHasherConfigFromAlgorithm(array $config): array
115120
{
116121
if ('auto' === $config['algorithm']) {
@@ -142,8 +147,8 @@ private function getHasherConfigFromAlgorithm(array $config): array
142147
$hasherChain = [$this->createHasher($config, true)];
143148

144149
foreach ($frompasswordHashers as $name) {
145-
if ($hasher = $this->passwordHashers[$name] ?? false) {
146-
$hasher = $hasher instanceof PasswordHasherInterface ? $hasher : $this->createHasher($hasher, true);
150+
if (isset($this->passwordHashers[$name])) {
151+
$hasher = $this->createHasherUsingAdapter($name);
147152
} else {
148153
$hasher = $this->createHasher(['algorithm' => $name], true);
149154
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,29 @@ public function testMigrateFrom()
163163
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $hasher->hash('foo', null));
164164
}
165165

166+
/**
167+
* @group legacy
168+
*/
169+
public function testMigrateFromLegacy()
170+
{
171+
if (!SodiumPasswordHasher::isSupported()) {
172+
$this->markTestSkipped('Sodium is not available');
173+
}
174+
175+
$factory = new PasswordHasherFactory([
176+
'plaintext_encoder' => $plaintext = new PlaintextPasswordEncoder(),
177+
SomeUser::class => ['algorithm' => 'sodium', 'migrate_from' => ['bcrypt', 'plaintext_encoder']],
178+
]);
179+
180+
$hasher = $factory->getPasswordHasher(SomeUser::class);
181+
$this->assertInstanceOf(MigratingPasswordHasher::class, $hasher);
182+
183+
$this->assertTrue($hasher->verify((new SodiumPasswordHasher())->hash('foo', null), 'foo', null));
184+
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, null, \PASSWORD_BCRYPT))->hash('foo', null), 'foo', null));
185+
$this->assertTrue($hasher->verify($plaintext->encodePassword('foo', null), 'foo', null));
186+
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $ 3ADC hasher->hash('foo', null));
187+
}
188+
166189
public function testDefaultMigratingHashers()
167190
{
168191
$this->assertInstanceOf(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function supports(Request $request): ?bool
4949
if (null !== $this->logger) {
5050
if ('https' === $request->headers->get('X-Forwarded-Proto')) {
5151
$this->logger->info('Redirecting to HTTPS. ("X-Forwarded-Proto" header is set to "https" - did you set "trusted_proxies" correctly?)');
52-
} elseif (str_contains($request->headers->get('Forwarded'), 'proto=https')) {
52+
} elseif (str_contains($request->headers->get('Forwarded', ''), 'proto=https')) {
5353
$this->logger->info('Redirecting to HTTPS. ("Forwarded" header is set to "proto=https" - did you set "trusted_proxies" correctly?)');
5454
} else {
5555
$this->logger->info('Redirecting to HTTPS.');

src/Symfony/Component/Security/Http/Tests/Firewall/ChannelListenerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Security\Http\Tests\Firewall;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\NullLogger;
16+
use Symfony\Component\HttpFoundation\HeaderBag;
1517
use Symfony\Component\HttpFoundation\Request;
1618
use Symfony\Component\HttpFoundation\Response;
1719
use Symfony\Component\HttpKernel\Event\RequestEvent;
@@ -153,4 +155,29 @@ public function testHandleWithSecuredRequestAndHttpChannel()
153155

154156
$this->assertSame($response, $event->getResponse());
155157
}
158+
159+
public function testSupportsWithoutHeaders()
160+
{
161+
$request = $this->createMock(Request::class);
162+
$request
163+
->expects($this->any())
164+
->method('isSecure')
165+
->willReturn(false)
166+
;
167+
$request->headers = new HeaderBag();
168+
169+
$accessMap = $this->createMock(AccessMapInterface::class);
170+
$accessMap
171+
->expects($this->any())
172+
->method('getPatterns')
173+
->with($this->equalTo($request))
174+
->willReturn([[], 'https'])
175+
;
176+
177+
$entryPoint = $this->createMock(AuthenticationEntryPointInterface::class);
178+
179+
$listener = new ChannelListener($accessMap, $entryPoint, new NullLogger());
180+
181+
$this->assertTrue($listener->supports($request));
182+
}
156183
}

0 commit comments

Comments
 (0)
0