10000 Hide username and client ip in logs · symfony/symfony@da1e536 · GitHub
[go: up one dir, main page]

Skip to content

Commit da1e536

Browse files
committed
Hide username and client ip in logs
1 parent 9a0f178 commit da1e536

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function createAuthenticator(ContainerBuilder $container, string $firewal
7676
$container->register($config['limiter'] = 'security.login_throttling.'.$firewallName.'.limiter', DefaultLoginRateLimiter::class)
7777
->addArgument(new Reference('limiter.'.$globalId))
7878
->addArgument(new Reference('limiter.'.$localId))
79+
->addArgument('%kernel.secret%')
7980
;
8081
}
8182

src/Symfony/Component/Security/Http/RateLimiter/DefaultLoginRateLimiter.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,28 @@ final class DefaultLoginRateLimiter extends AbstractRequestRateLimiter
2828
{
2929
private RateLimiterFactory $globalFactory;
3030
private RateLimiterFactory $localFactory;
31+
private ?string $secret;
3132

32-
public function __construct(RateLimiterFactory $globalFactory, RateLimiterFactory $localFactory)
33+
public function __construct(RateLimiterFactory $globalFactory, RateLimiterFactory $localFactory, #[\SensitiveParameter] string $secret = null)
3334
{
35+
if (!$secret) {
36+
trigger_deprecation('symfony/security-http', '6.4', 'Calling "%s()" with an empty secret is deprecated, pass a non-empty string instead.', __METHOD__);
37+
}
3438
$this->globalFactory = $globalFactory;
3539
$this->localFactory = $localFactory;
40+
$this->secret = $secret;
3641
}
3742

3843
protected function getLimiters(Request $request): array
3944
{
4045
$username = $request->attributes->get(SecurityRequestAttributes::LAST_USERNAME, '');
4146
$username = preg_match('//u', $username) ? mb_strtolower($username, 'UTF-8') : strtolower($username);
47+
$ip = $this->secret ? hash_hmac('sha256', $request->getClientIp(), $this->secret) : $request->getClientIp();
48+
$usernameAndIp = $this->secret ? hash_hmac('sha256', $username.'-'.$request->getClientIp(), $this->secret) : $username.'-'.$request->getClientIp();
4249

4350
return [
44-
$this->globalFactory->create($request->getClientIp()),
45-
$this->localFactory->create($username.'-'.$request->getClientIp()),
51+
$this->globalFactory->create($ip),
52+
$this->localFactory->create($usernameAndIp),
4653
];
4754
}
4855
}

src/Symfony/Component/Security/Http/Tests/EventListener/LoginThrottlingListenerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected function setUp(): void
4747
'limit' => 6,
4848
'interval' => '1 minute',
4949
], new InMemoryStorage());
50-
$limiter = new DefaultLoginRateLimiter($globalLimiter, $localLimiter);
50+
$limiter = new DefaultLoginRateLimiter($globalLimiter, $localLimiter, '$3cre7');
5151

5252
$this->listener = new LoginThrottlingListener($this->requestStack, $limiter);
5353
}

0 commit comments

Comments
 (0)
0