Repository navigation
Conversation
|
| try { | ||
| TokenGenerator tokenGenerator = tokenGeneratorFactory.createGenerator(scheme); | ||
| return tokenGenerator.generate(token); | ||
| } catch (AuthenticationException | ResponseStatusException e) { | ||
| failedAuthRateLimiter.recordFailedAttempt(ip, username); | ||
| throw e; | ||
| } | ||
| } |
There was a problem hiding this comment.
so here we return a auth token and in case of an error - go into the catch. The catch then internally calls failedAuthRateLimiter.recordFailedAttempt and following the methods in it, it uses the consumeToken method which can throw a OperationLimitExceeded exception and if that happens, we would directly exit the catch and the final line "throw e" - will not be reached at all. THis way we lose the original exception and just get a simple rate limit one. Maybe try to come up with a way to preserve the whole original exception or at least its message or code etc.
| private static final Duration REFILL_PERIOD = Duration.ofHours(1); | ||
| private static final long TOKENS_PER_ATTEMPT = 1; |
There was a problem hiding this comment.
maybe extract these constants so both AnonymousrateLimiter and the FailedAuthRateLimiter can share them.
| * {@link OperationRateLimitExceededException} is raised. Throttling is a no-op unless explicitly enabled. | ||
| */ | ||
| @Named | ||
| public class FailedAuthRateLimiter { |
There was a problem hiding this comment.
Right now the OperationRateLimiter and AnonymousRateLimiter and FailedAuthRateLimiter all use the same table to store their entries the operation_rate_limit_bucket table - how is the table cleaned since the CleanUpJob goes through the list of cleaners and the OperationRateLimitBucketCleaner does not implement the Cleaner interface.
I see that it is now set to go every hour, but at the begining we have a guard for the OPERATION_RATE_LIMITING_ENABLED boolean and it is by default set to false and if it is false for the deploy-service, then this cleaner does not execute at all.
Maybe add checks for this particular cleaner to be started if we have FAILED_AUTH_RATE_LIMITING_ENABLED enabled and also it is not part of your code change but I think It would be better to make the cleaner go with all the other cleaners executed by the clean up job - by making it implement the Cleaner interface as well.
| public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
| if (!shouldRateLimit(request)) { | ||
| return true; | ||
| } | ||
| String ipAddress = resolveClientIp(request); | ||
| AtomicRateLimiter rateLimiter = rateLimiterProvider.getRateLimiter(ipAddress); | ||
|
|
||
| boolean hasAcquiredPermission = rateLimiter.acquirePermission(); | ||
| AtomicRateLimiterMetrics metrics = rateLimiter.getDetailedMetrics(); | ||
| RateLimiterConfig config = rateLimiter.getRateLimiterConfig(); | ||
| String ipAddress = clientIpResolver.resolve(request); | ||
| ConsumptionProbe probe = anonymousRateLimiter.tryConsume(ipAddress); |
There was a problem hiding this comment.
tryConsume runs on every request now so authenticated calls also use up the per ip limit 5000 per hour. Is this intended or should authenticated requests be skipped?
| consumeIpToken(ip); | ||
| if (username != null) { | ||
| consumeUserToken(username); | ||
| } |
There was a problem hiding this comment.
ip and user tokens are not consumed together, so if the ip bucket throws exception, the user token is never consumed and also if ip passes but user is exhausted, the ip token is already spent
| // value sent by the client. If that guarantee does not hold, a caller could spoof this header to | ||
| // evade per-IP rate limiting. Do not remove the getRemoteAddr() fallback, which is the only | ||
| // non-spoofable source when the trusted header is absent. | ||
| String trueClientIp = request.getHeader(Constants.X_CF_TRUE_CLIENT_IP_HEADER); |
There was a problem hiding this comment.
is it confirmed the router injects this and clients cant set it X-CF-True-Client-IP? If its spoofable, maybe there would be failed auth attempts with rotating fake IPs and so per-IP limit will be bypassed


No description provided.