From 447398c29f6ed8cef8b567a3e508e3a6f1a162db Mon Sep 17 00:00:00 2001 From: Gunnar Date: Wed, 26 Jun 2024 15:31:16 +0200 Subject: [PATCH] Update RateLimiter.cfc use cache.getOrSet, a little bit of formatting --- .../models/security/RateLimiter.cfc | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/modules/contentbox/models/security/RateLimiter.cfc b/modules/contentbox/models/security/RateLimiter.cfc index da886728b..48c005d00 100755 --- a/modules/contentbox/models/security/RateLimiter.cfc +++ b/modules/contentbox/models/security/RateLimiter.cfc @@ -8,12 +8,13 @@ component extends="coldbox.system.Interceptor" { // DI - property name="settingService" inject="id:settingService@contentbox"; - property name="securityService" inject="id:securityService@contentbox"; + property name = "settingService" inject = "id:settingService@contentbox"; + property name = "securityService" inject = "id:securityService@contentbox"; property name = "cachebox" inject = "Cachebox"; /** - * Limiter + * onRequestCapture + * fires before any event caching or processing */ function onRequestCapture( event, data, buffer ){ var allSettings = variables.settingService.getAllSettings(); @@ -50,11 +51,15 @@ component extends="coldbox.system.Interceptor" { var cache = cachebox.getDefaultCache(); var cacheKey = 'limiter'&realIP; - // If first time visit, create record. - var targetData = cache.get( cacheKey ); - if( isNull( targetData ) ){ - cache.set( cacheKey, { attempts = 1, lastAttempt = now() }); - return this; + var targetData = cache.getOrSet( cacheKey, function(){ + return { attempts = 0, lastAttempt = now() } + } ); + + // on first visit no further processing + if( targetData.attempts == 0 ){ + targetData.attempts++; + cache.set( cacheKey, targetData ); + return this; } log.debug( "Limit data", targetData );