Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The following are properties used when the source of the rules is xml
Property
Type
Required
Default
Description
rulesfile
string
true if rulesSource = xml
---
The location of the security rules xml file
The basics of the security validation is that you define a set of rules, much like how you define a firewall. Each rule is composed of several elements: securelist, whitelist, roles, permissions, match, and redirect. However, each rule can be expanded by the developer as needed with custom elements, etc. Each rule will be evaluated in the order that it is declared and the follow validation via our flow diagram below.
An incoming request or internal event reaches the first rule and the type of matching is determined: event or URI matching
The incoming event or URI is matched against the whitelist element
If matched, then the event is whitelisted so it continues to the next rule
Else, continue
The incoming event or URI is matched against the securelist element
If not matched, then continue to next rule
Else, continue validation
Do we have a custom validator or not?
Yes, validate against the custom validator
No, validate against ColdFusion's logged in user roles or logged in credentials
If validation fails, redirect the user via the redirect element
This module will provide your application with a security rule engine.
Lucee 4.5+
ColdFusion 9+
Just drop into your modules folder or use CommandBox to install
box install cbsecurity
You will then need to configure the interceptor via the cbsecurity
settings in your main ColdBox.cfc
or you can also declare the interceptor manually by leveraging the class: cbsecurity.interceptors.Security
. If you define the cbsecurity
settings, then the module will load the interceptor automatically for you with those settings.
You can find all the documentation here: https://github.com/ColdBox/cbox-security/wiki
Security rules can come from xml, json, query, memory or custom locations. You will find some examples in this module's config
folder.
Below are the security settings you can use for this module. Remember you must create the cbsecurity
struct in your ColdBox.cfc
:
Here is a sample declaration you can use in your ColdBox.cfc
:
The topic of security is always an interesting and essential one. However, most MVC frameworks offer very loose security when it comes down to an event-oriented architecture. ColdBox Interceptors change this behavior as we have the ability to intercept requests in any point in time during our application executions. With this feature we introduce our Security Module that implements what we call ColdBox Security. With the ColdBox security module you will be able to secure all your public and private ColdBox events from execution and incoming URL patterns. However, as we all know, every application has different requirements and since we are keen on extensibility, the module can be configured to work with whatever security authentications and permissions you might have.
The module wraps itself around the preProcess
execution of your request and also (if configured) on any `runEvent()`` methods that get executed internally. The module is based on a custom rules engine that will validate a request against a set of rules that you define and then if a rule is matched, it will try to see if the user is authenticated and in a specific criteria (as specified by you). The two available authentication algorithms are the following:
(default) By using cflogin, cfloginuser, cflogout
Security Validation Object that you create and implement.
The default security is based on what ColdFusion gives you for basic security using their security tags. You basically use cfloginuser
to log in a user and set their appropriate roles in the system. The module can then match to these roles via the security rules you have created.
The second method of authentication is based on your custom security logic. You will be able to register a validation object with the module. Once a rule is matched, the module will call your validation object, send in the rule and ask if the user can access it or not. It will be up to your logic to determine if the rule is satisfied or not.
Secures incoming events and URIs
If enabled, secure internal event executions via preEvent()
interceptions
Security rules can exist in:
XML File
JSON File
Database
Model/WireBox Object
IoC Module
ColdBox Cache (Placing a query of rules into the cache)
The rules can be configured to use regular expressions or simple snippets
Can use ColdFusion authentication security
Can Use your own Security Validation by creating a security validation object.
The following are properties used when the source of the rules is db or coming from the database.
The following are properties used when the source of the rules is json
In order to enable ColdBox security you must register the Security interceptor in your parent or other module configuration's interceptors
section:
IMPORTANT If you are using SES or URL mappings in your ColdBox 4 application, make sure that you declare the security interceptor after the SES interceptor. Interceptors require order, so security needs for the URL to be translated first. In coldbox 5 SES is handled by the Routing service, so you don't need this SES interceptor.
The following are properties used when the source of the rules is ioc or coming from an IoC module
Property
Type
Required
Default
Description
rulesDSN
string
true if rulesSource = db
---
The dsn to use if the rules are coming from a database
rulesTable
string
true if rulesSource = db
---
The table where the rules are
rulesSQL
string
false
select* from rulesTable
The custom SQL statement to use to retrieve the rules according to the rulesTable property. If not set, the default of select* from rulesTable will be used.
rulesOrderBy
string
false
---
The column to order the rules by. If not chosen, the interceptor will not order the query, just select it.
Property
Type
Required
Default
Description
rulesfile
string
true if rulesSource = JSON
---
The location of the security rules json file
Property
Type
Required
Default
Description
useRegex
boolean
false
true
By default all secure and white lists are matched using regular expressions. You can disable it if you like and use plain old string matching.
queryChecks
boolean
false
false
Flag that tells the interceptor to validate the columns in the security rules. This makes sure all columns have the same columns. By default it is in relaxed mode so all columns are used.
preEventSecurity
boolean
false
false
This turns on the preEvent
execution point that will make sure that before any event is fired internally, that its verified against the security rules. Only use this if you really want to secure all internal events, else this can hinder performance.
ruleSource
string
true
---
Where to look for the rules as described above, this value has to be a choice from the following list xml,json,db,model,ioc or ocm
.
validator
string
false
---
The class path of the validator object to use. The interceptor will create the object for you and cache it internally. If the object has an init()
method, the interceptor will call it for you.
validatorModel
string
false
---
The model name of the security validator to use for custom validations. The interceptor will call getModel()
with the name of this property to be retrieved via WireBox
validatorIOC
string
false
---
The bean name of the security validator to use for custom validations. The interceptor will ask the IoC module for the bean according to this property
Property | Type | Required | Default | Description |
| string | true if rulesSource = ioc | --- | The bean name to ask the IoC module for that has the rules |
| string | true if rulesSource = ioc | --- | The method in the bean to call that will return a query of rules |
| string | false | --- | A comma-delimited list of arguments to send into the method. This is an optional argument and if not set, the method will be called with no arguments |
This module will try to use ColdFusion's cflogin + cfloginuser
authentication by default. However, if you are using your own authentication mechanisms you can still use this module by implementing a Security Validator Object (See next section). Below we can see a sample on how to use the cflogin
tag:
Example:
For more information about cflogin, cfloginuser and cflogout
, please visit the docs http://cfdocs.org/security-functions
IMPORTANT Please remember to white list your main events (implicit), login and logout events if you will be securing the entire application.
As you can see from the sample, the first rule has the following elements
So it will match the incoming event.
This means that the following events will not be verified for security: user.login, user.logout and any event that starts with main will be let through, if they match the secure list pattern.
This means that any event that starts with the word user will be secured and anything that starts with the word admin will also be secured, unless the incoming event matches a pattern in the whitelist element.
This means that only a user with admin role will be allowed to visit the securelist events.
This probably means that I am doing my own security validation and apart from having the user have a role of admin, he/she must also have the read and write permissions. My own validator will validate this logic.
Then if it does not validate it will use this redirect element to relocate via setNextEvent()
The second rule has the following elements:
So it will match the incoming event.
No white listed events are defined.
This means that any event that starts with the word moderator will be secured and validated against the user's credentials.
This means that users with roles of admin and moderator can execute events that are in the securelist.
This probably means that I am doing my own security validation and apart from having the user have a role of admin or moderator, he/she must also have the read permission. My own validator will validate this logic.
Then if it does not validate it will use this redirect element to relocate via setNextEvent()
The third rule has the following elements:
So it will match the incoming URL pattern after the domain name and application location.
No white listed events are defined.
It will secure any incoming URI that starts with /secured
This means that users with roles of admin and paid_subscriber can visit URLs with /secured in them
No permissions
Then if it does not validate it will use this redirect element to relocate via setNextEvent()
The following are properties used when the source of the rules is ocm
or coming from the CacheBox
Now that we have seen all the properties of this module, how to configure it and declare it, let's look at how the rules work. All the security rules follows the following format, however, you can append as many columns as you like for your own custom validations and the interceptor will register all the columns you pass to it:
You might be asking, why is the element permissions here, if the default ColdFusion security doesn't work with permissions but with roles. Well, in anticipation to customizations and permissions based systems, the permissions element exists. It's there already if you need to use it. However, the default interceptor security does not use it, just the roles element. A few guidelines:
Test your regular expressions (http://gskinner.com/blog/archives/2008/03/regexr_free_onl.html)
Determine if you want to secure the incoming URL or event
Order of declaration of the rules is important as they are fired in order
Test your whitelist
events, if not you might be producing endless loops of security redirections
Make sure implicit events are whitelisted if you are securing the entire application events
As best practice, create your own validator object for more granular control
IMPORTANT The basic rules table is provided. However, it is very denormalized and for simple applications. If your applications will deal with many permissions and roles, I suggest you build your DB according to your business rules and then just create a method that can join the rules into the above format. You can easily do this in SQL or create a view for your security rules. If you are doing this, then your security implementations are advanced and you know what you are doing (Hopefully). So please note that the way you store the rules in the DB is your priority and consideration.
A security validator object is a simple CFC that implements the following function:
This function must return a boolean
variable and it must validate a user according to the rule that just ran by testing the fields that get sent in as a rule. Where this method exists is up to you. It will also receive a reference to the current ColdBox controller. You can use the controller to call other plugins, persist keys, or anything you like (please see the controller API). The important note here is that the rule structure contains all the elements/columns you defined in your xml or query. Below is a real life example:
Or, in cfscript
:
Property
Type
Required
Default
Description
rulesOCMKey
string
true
---
The cache key to use to retrieve the rules from the ColdBox default cache provider
Property
Type
Description
match
event or URI
Determines if it needs to match the incoming URI or the incoming event. By default it matches the incoming event.
whitelist
varchar
A comma delimited list of events or patterns to whitelist or to bypass security on
securelist
varchar
A comma delimited list of events or patterns to secure
roles
varchar
A comma delimited list of roles that can access these secure events
permissions
varchar
A comma delimited list of permissions that can access these secure events
redirect
varchar
An event or route to redirect if the user is not in a role or permission
When the security module is about to relocate redirect element, it will save the incoming URL that was requested in a flash RAM variable called: _securedURL
. This key will be persisted in the flash memory of the framework and when the user get's relocated to the redirect
element, this key will exist in the request collection. You can then use this to store where they came from and do proper redirections. So always remember to use this key if you want to provide a seamless login experience to your users. You can easily place it in the login form: