Skip to content

Commit

Permalink
Hide username and client ip in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Aug 21, 2023
1 parent 9a0f178 commit be0e24c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
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

Check failure on line 34 in src/Symfony/Component/Security/Http/RateLimiter/DefaultLoginRateLimiter.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidParamDefault

src/Symfony/Component/Security/Http/RateLimiter/DefaultLoginRateLimiter.php:34:15: InvalidParamDefault: Default value type '' for argument 3 of method Symfony\Component\Security\Http\RateLimiter\DefaultLoginRateLimiter::__construct does not match the given type non-empty-string (see https://psalm.dev/062)

Check failure on line 34 in src/Symfony/Component/Security/Http/RateLimiter/DefaultLoginRateLimiter.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidParamDefault

src/Symfony/Component/Security/Http/RateLimiter/DefaultLoginRateLimiter.php:34:15: InvalidParamDefault: Default value type '' for argument 3 of method Symfony\Component\Security\Http\RateLimiter\DefaultLoginRateLimiter::__construct does not match the given type non-empty-string (see https://psalm.dev/062)
*/
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 \InvalidArgumentException('A non-empty secret is required.');
}
$this->globalFactory = $globalFactory;
$this->localFactory = $localFactory;
$this->secret = $secret;
}

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), '/+', '._');
}
}
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 comments on commit be0e24c

Please sign in to comment.