Skip to content

Commit 6bd9907

Browse files
authored
Don't trigger ColdBox's invalid event looping protection
This is the same fix that @elpete implemented in cbGuard for an issue I reported where introducing cbSecurity or cbGuard to an app would break the `invalidEventHandler` Coldbox setting, See here for details: coldbox-modules/cbguard#15
1 parent 7a6fd64 commit 6bd9907

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

interceptors/Security.cfc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ component accessors="true" extends="coldbox.system.Interceptor" {
1212
property name="rulesLoader" inject="rulesLoader@cbSecurity";
1313
property name="handlerService" inject="coldbox:handlerService";
1414
property name="cbSecurity" inject="@cbSecurity";
15+
property name="invalidEventHandler" inject="coldbox:setting:invalidEventHandler";
1516

1617
/**
1718
* The reference to the security validator for this interceptor
@@ -27,6 +28,11 @@ component accessors="true" extends="coldbox.system.Interceptor" {
2728
* Configure the security firewall
2829
*/
2930
function configure(){
31+
variables.onInvalidEventHandlerBean = javacast( "null", "" );
32+
if ( len( variables.invalidEventHandler ) ) {
33+
variables.onInvalidEventHandlerBean = handlerService.getHandlerBean( variables.invalidEventHandler );
34+
}
35+
3036
// init the security modules dictionary
3137
variables.securityModules = {};
3238

@@ -222,6 +228,19 @@ component accessors="true" extends="coldbox.system.Interceptor" {
222228
){
223229
// Get handler bean for the current event
224230
var handlerBean = variables.handlerService.getHandlerBean( arguments.event.getCurrentEvent() );
231+
232+
if ( isInvalidEventHandlerBean( handlerBean ) ) {
233+
// ColdBox tries to detect invalid event handler loops by keeping
234+
// track of the last invalid event to fire. If that invalid event
235+
// fires twice, it throws a hard exception to prevent infinite loops.
236+
// Unfortunately for us, just attempting to get a handler bean
237+
// starts the invalid event handling. Here, if we got the invalid
238+
// event handler bean back, we reset the `_lastInvalidEvent` so
239+
// ColdBox can handle the invalid event properly.
240+
request._lastInvalidEvent = variables.invalidEventHandler;
241+
return;
242+
}
243+
225244
if ( handlerBean.getHandler() == "" ) {
226245
return;
227246
}
@@ -702,5 +721,18 @@ component accessors="true" extends="coldbox.system.Interceptor" {
702721

703722
return len( CGI.REMOTE_ADDR ) ? CGI.REMOTE_ADDR : "127.0.0.1";
704723
}
724+
725+
private boolean function isInvalidEventHandlerBean( required handlerBean ) {
726+
if ( isNull( variables.onInvalidEventHandlerBean ) ) {
727+
return false;
728+
}
729+
730+
return (
731+
variables.onInvalidEventHandlerBean.getInvocationPath() == arguments.handlerBean.getInvocationPath() &&
732+
variables.onInvalidEventHandlerBean.getHandler() == arguments.handlerBean.getHandler() &&
733+
variables.onInvalidEventHandlerBean.getMethod() == arguments.handlerBean.getMethod() &&
734+
variables.onInvalidEventHandlerBean.getModule() == arguments.handlerBean.getModule()
735+
);
736+
}
705737

706738
}

0 commit comments

Comments
 (0)