8000 [Security] [Throttling] Hide username and client ip in logs by Spomky · Pull Request #51434 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] [Throttling] Hide username and client ip in logs #51434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Hide username and client ip in logs
  • Loading branch information
Spomky committed Aug 22, 2023
commit ff8a8ab8ad8fbbbf60f616ba5b934bd6932b8106
8000
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function createAuthenticator(ContainerBuilder $container, string $firewal
$container->register($config['limiter'] = 'security.login_throttling.'.$firewallName.'.limiter', DefaultLoginRateLimiter::class)
->addArgument(new Reference('limiter.'.$globalId))
->addArgument(new Reference('limiter.'.$localId))
->addArgument('%kernel.secret%')
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ final class DefaultLoginRateLimiter extends AbstractRequestRateLimiter
{
private RateLimiterFactory $globalFactory;
private RateLimiterFactory $localFactory;
private string $secret;

public function __construct(RateLimiterFactory $globalFactory, RateLimiterFactory $localFactory)
/**
* @param non-empty-string $secret A secret to use for hashing the IP address and username
*/
public function __construct(RateLimiterFactory $globalFactory, RateLimiterFactory $localFactory, #[\SensitiveParameter] string $secret = '')
{
if ('' === $secret) {
trigger_deprecation('symfony/security-http', '6.4', 'Calling "%s()" with an empty secret is deprecated. A non-empty secret will be mandatory in version 7.0.', __METHOD__);
// throw new \Symfony\Component\Security\Core\Exception\InvalidArgumentException('A non-empty secret is required.');
}
$this->globalFactory = $globalFactory;
$this->localFactory = $localFactory;
$this->secret = $secret;
8000
}

protected function getLimiters(Request $request): array
Expand All @@ -41,8 +50,13 @@ protected function getLimiters(Request $request): array
$username = preg_match('//u', $username) ? mb_strtolower($username, 'UTF-8') : strtolower($username);

return [
$this->globalFactory->create($request->getClientIp()),
$this->localFactory->create($username.'-'.$request->getClientIp()),
$this->globalFactory->create($this->hash($request->getClientIp())),
$this->localFactory->create($this->hash($username.'-'.$request->getClientIp())),
];
}

private function hash(string $data): string
{
return strtr(substr(base64_encode(hash_hmac('sha256', $data, $this->secret, true)), 0, 8), '/+', '._');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came here to check as I was worried I'd have to complain about a massive cache size increase, but I'm happy to see that it was taken into account and this will actually probably even reduce the cache storage requirements. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I fell into the trap 😅🤦‍♂️. But there are people here who are very quick-witted and who paid attention to that 😊. #bestcommunityever 👌

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function setUp(): void
'limit' => 6,
'interval' => '1 minute',
], new InMemoryStorage());
$limiter = new DefaultLoginRateLimiter($globalLimiter, $localLimiter);
$limiter = new DefaultLoginRateLimiter($globalLimiter, $localLimiter, '$3cre7');

$this->listener = new LoginThrottlingListener($this->requestStack, $limiter);
}
Expand Down
0