Skip to content

Commit e85ecdb

Browse files
authored
👌 IMPROVE: Check firewall logging is enabled before creating DB Table (#40)
* 👌 IMPROVE: Check firewall logging is enabled before creating DB Table * 🐛 FIX: Don't try to proceed with an empty datasource string `this.datasource` may be an empty string - not NULL - on Lucee. Co-authored-by: michaelborn <[email protected]>
1 parent a258066 commit e85ecdb

File tree

11 files changed

+91
-65
lines changed

11 files changed

+91
-65
lines changed

interceptors/Security.cfc

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ component accessors="true" extends="coldbox.system.Interceptor" {
6464
variables.rulesLoader
6565
.getRuleTemplate()
6666
.append( variables.properties.visualizer.securityRule )
67-
.append( { secureList : "^cbsecurity:Visualizer.*", action : "block" } )
67+
.append( {
68+
secureList : "^cbsecurity:Visualizer.*",
69+
action : "block"
70+
} )
6871
);
6972
}
7073

@@ -187,19 +190,21 @@ component accessors="true" extends="coldbox.system.Interceptor" {
187190
}
188191

189192
// Log it
190-
log.info( "+ Registered module (#arguments.module#) with cbSecurity using #arrayLen( arguments.settings.firewall.rules.inline )# rules." );
193+
log.info(
194+
"+ Registered module (#arguments.module#) with cbSecurity using #arrayLen( arguments.settings.firewall.rules.inline )# rules."
195+
);
191196

192197
return this;
193198
}
194199

195200
/**
196201
* Listen to module loadings, so we can do module rule registrations
197202
*
198-
* @event
203+
* @event
199204
* @interceptData
200-
* @rc
201-
* @prc
202-
* @buffer
205+
* @rc
206+
* @prc
207+
* @buffer
203208
*/
204209
function postModuleLoad( event, interceptData, rc, prc, buffer ){
205210
// Is this a cbSecurity Module & not registered
@@ -218,11 +223,11 @@ component accessors="true" extends="coldbox.system.Interceptor" {
218223
/**
219224
* Listen to module unloadings, so we can do module rule cleanups
220225
*
221-
* @event
226+
* @event
222227
* @interceptData
223-
* @rc
224-
* @prc
225-
* @buffer
228+
* @rc
229+
* @prc
230+
* @buffer
226231
*/
227232
function postModuleUnload( event, interceptData, rc, prc, buffer ){
228233
// Is the module registered?
@@ -241,11 +246,11 @@ component accessors="true" extends="coldbox.system.Interceptor" {
241246
/**
242247
* Our firewall kicks in at preProcess
243248
*
244-
* @event
249+
* @event
245250
* @interceptData
246-
* @rc
247-
* @prc
248-
* @buffer
251+
* @rc
252+
* @prc
253+
* @buffer
249254
*/
250255
function preProcess( event, interceptData, rc, prc, buffer ){
251256
// Add SecureView() into the requestcontext
@@ -285,9 +290,9 @@ component accessors="true" extends="coldbox.system.Interceptor" {
285290
/**
286291
* Process handler annotation based security rules.
287292
*
288-
* @event
293+
* @event
289294
* @interceptData
290-
* @currentEvent
295+
* @currentEvent
291296
*/
292297
function processAnnotationRules(
293298
required event,

interceptors/SecurityHeaders.cfc

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ component extends="coldbox.system.Interceptor" {
104104
log.debug( "Non-SSL URI detected (#event.getFullUrl()#), redirecting in ssl" );
105105
}
106106
variables.dbLogger.log(
107-
action : "redirect",
108-
blockType : "NON-SSL",
109-
ip : variables.cbSecurity.getRealIp(),
110-
host : variables.cbSecurity.getRealHost(),
111-
userId : variables.cbSecurity.isLoggedIn() ? variables.cbSecurity.getUser().getId() : ""
107+
action : "redirect",
108+
blockType: "NON-SSL",
109+
ip : variables.cbSecurity.getRealIp(),
110+
host : variables.cbSecurity.getRealHost(),
111+
userId : variables.cbSecurity.isLoggedIn() ? variables.cbSecurity.getUser().getId() : ""
112112
);
113113
relocate( url: arguments.event.getFullUrl(), ssl: true );
114114
return;
@@ -139,11 +139,11 @@ component extends="coldbox.system.Interceptor" {
139139
}
140140

141141
variables.dbLogger.log(
142-
action : "block",
143-
blockType : "INVALID-HOST",
144-
ip : variables.cbSecurity.getRealIp(),
145-
host : variables.cbSecurity.getRealHost(),
146-
userId : variables.cbSecurity.isLoggedIn() ? variables.cbSecurity.getUser().getId() : ""
142+
action : "block",
143+
blockType: "INVALID-HOST",
144+
ip : variables.cbSecurity.getRealIp(),
145+
host : variables.cbSecurity.getRealHost(),
146+
userId : variables.cbSecurity.isLoggedIn() ? variables.cbSecurity.getUser().getId() : ""
147147
);
148148

149149
// Announce
@@ -187,11 +187,11 @@ component extends="coldbox.system.Interceptor" {
187187
}
188188

189189
variables.dbLogger.log(
190-
action : "block",
191-
blockType : "INVALID-IP",
192-
ip : variables.cbSecurity.getRealIp(),
193-
host : variables.cbSecurity.getRealHost(),
194-
userId : variables.cbSecurity.isLoggedIn() ? variables.cbSecurity.getUser().getId() : ""
190+
action : "block",
191+
blockType: "INVALID-IP",
192+
ip : variables.cbSecurity.getRealIp(),
193+
host : variables.cbSecurity.getRealHost(),
194+
userId : variables.cbSecurity.isLoggedIn() ? variables.cbSecurity.getUser().getId() : ""
195195
);
196196

197197
// Announce

models/CBSecurity.cfc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ component threadsafe singleton accessors="true" {
149149
)
150150
) {
151151
variables.settings.authentication.userService = variables.moduleSettings.cbauth.settings.userServiceClass;
152-
log.info( "+ cbAuth detected and no UserService detected -> User Service set to cbAuth's UserServiceClass" );
152+
log.info(
153+
"+ cbAuth detected and no UserService detected -> User Service set to cbAuth's UserServiceClass"
154+
);
153155
}
154156

155157
// User service default if basic auth is selected

models/auth/BasicAuthUserService.cfc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ component accessors="true" singleton {
1717

1818
variables.DEFAULT_SETTINGS = {
1919
// Hashing algorithm to use
20-
"hashAlgorithm" : "SHA-512",
20+
"hashAlgorithm" : "SHA-512",
2121
// Iterates the number of times the hash is computed to create a more computationally intensive hash.
2222
"hashIterations" : 5,
2323
// User storage
24-
"users" : {}
24+
"users" : {}
2525
};
2626

2727
/**
@@ -33,7 +33,10 @@ component accessors="true" singleton {
3333

3434
function onDIComplete(){
3535
// Normalize settings
36-
variables.settings.basicAuth = duplicate( variables.DEFAULT_SETTINGS ).append( variables.settings.basicAuth, true );
36+
variables.settings.basicAuth = duplicate( variables.DEFAULT_SETTINGS ).append(
37+
variables.settings.basicAuth,
38+
true
39+
);
3740
// Normalize User Storage + password encryption
3841
settings.basicAuth.users = settings.basicAuth.users.map( ( key, value ) => {
3942
var user = getNewUserTemplate().append( arguments.value, true );
@@ -45,10 +48,16 @@ component accessors="true" singleton {
4548

4649
/**
4750
* Hash the incoming target according to our hashing algorithm and settings
51+
*
4852
* @target The string target to hash
4953
*/
5054
string function hashSecurely( required string target ){
51-
return hash( arguments.target, variables.settings.basicAuth.hashAlgorithm, "UTF-8", variables.settings.basicAuth.hashIterations );
55+
return hash(
56+
arguments.target,
57+
variables.settings.basicAuth.hashAlgorithm,
58+
"UTF-8",
59+
variables.settings.basicAuth.hashIterations
60+
);
5261
}
5362

5463
/**

models/jwt/JwtService.cfc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ component accessors="true" singleton threadsafe {
163163
/**
164164
* Logout a user and invalidate their access token
165165
*
166-
* @user
166+
* @user
167167
* @customClaims
168168
*/
169169
function logout(){
@@ -245,10 +245,7 @@ component accessors="true" singleton threadsafe {
245245
// Verify it
246246
if ( isNull( oUser ) || !len( oUser.getId() ) ) {
247247
// Announce the invalid user
248-
variables.interceptorService.announce(
249-
"cbSecurity_onJWTInvalidUser",
250-
{ payload : arguments.payload }
251-
);
248+
variables.interceptorService.announce( "cbSecurity_onJWTInvalidUser", { payload : arguments.payload } );
252249
throw( message = "The user was not found by the user service", type = "InvalidTokenUser" );
253250
}
254251

models/jwt/storages/CacheTokenStorage.cfc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
component accessors="true" singleton threadsafe {
88

99
// DI
10-
property name="wirebox" inject="wirebox";
11-
property name="cachebox" inject="cachebox";
12-
property name="settings" inject="coldbox:moduleSettings:cbSecurity";
10+
property name="wirebox" inject="wirebox";
11+
property name="cachebox" inject="cachebox";
12+
property name="settings" inject="coldbox:moduleSettings:cbSecurity";
1313
property name="jwtService" inject="JwtService@cbSecurity";
1414

1515
/**
@@ -53,10 +53,10 @@ component accessors="true" singleton threadsafe {
5353
/**
5454
* Set a token in the storage
5555
*
56-
* @key The cache key
57-
* @token The token to store
56+
* @key The cache key
57+
* @token The token to store
5858
* @expiration The token expiration
59-
* @payload The payload
59+
* @payload The payload
6060
*
6161
* @return JWTStorage
6262
*/
@@ -92,7 +92,7 @@ component accessors="true" singleton threadsafe {
9292
/**
9393
* Retrieve the token via the cache key, if the key doesn't exist a TokenNotFoundException will be thrown
9494
*
95-
* @key The cache key
95+
* @key The cache key
9696
* @defaultValue If not found, return a default value
9797
*
9898
* @throws TokenNotFoundException

models/util/DBLogger.cfc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ component accessors="true" singleton threadsafe {
6363
*/
6464
function configure(){
6565
// Log settings check
66+
if ( !variables.settings.firewall.logs.enabled ) {
67+
return;
68+
}
6669
if ( !len( variables.settings.firewall.logs.table ) ) {
6770
throw(
6871
message = "No 'table' property defined for the firewall logs: firewall.logs.table",
@@ -210,10 +213,7 @@ component accessors="true" singleton threadsafe {
210213
{},
211214
{ datasource : variables.settings.firewall.logs.dsn }
212215
).reduce( ( results, row ) => {
213-
results[ row.action ] = {
214-
"total" : row.total,
215-
"percentage" : row.percentage
216-
};
216+
results[ row.action ] = { "total" : row.total, "percentage" : row.percentage };
217217
return results;
218218
}, {} );
219219
}
@@ -348,7 +348,7 @@ component accessors="true" singleton threadsafe {
348348
}
349349

350350
// else default to app datasource
351-
if ( !isNull( settings.datasource ) ) {
351+
if ( !isNull( settings.datasource ) && len( settings.datasource ) ) {
352352
return settings.datasource;
353353
}
354354

models/validators/AuthValidator.cfc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ component singleton threadsafe {
7474
// Validate new interface if not, just warn
7575
// TODO: Change to just use the hasRole() by vNext : Compat for now.
7676
if ( !structKeyExists( oUser, "hasRole" ) ) {
77-
variables.log.warn( "CBSecurity User object does not implement the `hasRole()` method. Please add it." );
77+
variables.log.warn(
78+
"CBSecurity User object does not implement the `hasRole()` method. Please add it."
79+
);
7880
}
7981

8082
// Check roles

models/validators/BasicAuthValidator.cfc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ component singleton threadsafe {
101101
// Validate new interface if not, just warn
102102
// TODO: Change to just use the hasRole() by vNext : Compat for now.
103103
if ( !structKeyExists( oUser, "hasRole" ) ) {
104-
variables.log.warn( "CBSecurity User object does not implement the `hasRole()` method. Please add it." );
104+
variables.log.warn(
105+
"CBSecurity User object does not implement the `hasRole()` method. Please add it."
106+
);
105107
}
106108

107109
// Check roles
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
component singleton threadsafe extends="AuthValidator"{
1+
component singleton threadsafe extends="AuthValidator" {
22

33
function onDIComplete(){
4-
variables.log.warn( "The CBAuthValidator has been deprecated, please change your references to just `AuthValidator@cbsecurity` " );
4+
variables.log.warn(
5+
"The CBAuthValidator has been deprecated, please change your references to just `AuthValidator@cbsecurity` "
6+
);
57
}
68

79
}

0 commit comments

Comments
 (0)