Security Best Practices
A practical checklist for hardening ColdBox applications, from secure by default and protected secrets to defended forms and APIs.
Use this as a checklist. Each item links to the part of ColdBox or cbsecurity that implements it.
Secure by Default
Deny everything, then allow what is public. Rules are evaluated in order and a rule's whitelist only skips that rule, so put your catch-all rule last. Patterns are comma-delimited regular expressions by default.
rules : [
// Public pages are whitelisted from this rule, everything else needs a login
{ secureList : ".*", whitelist : "^main\.(login|doLogin)$,^public\." }
]Prefer permissions over roles when you check access. Roles change with your organization, permissions describe what the code does.
Authenticate With cbauth, Hash With bcrypt
Let
cbauthown login state, usingauthenticate(),isLoggedIn()andgetUser(). Do not keep your own flags in the session.Store only password hashes, created with the
bcryptmodule (box install bcrypt). Never use plain hashing functions for passwords.
Keep Secrets Out of Source
Read secrets from the environment, never from committed files.
jwt : { secretKey : getSystemSetting( "JWT_SECRET", "" ) }Protect Forms With CSRF Tokens
Every request that changes state needs a token, which cbcsrf provides through the csrf() and csrfVerify() helpers.
Use JWT for APIs
Stateless APIs should not depend on server sessions. Secure them with the JWT validator and set token expiration and refresh tokens in the jwt settings.
Return a block action for APIs instead of redirecting to a login page: set defaultAuthenticationAction : "block" for API modules.
Turn On the Security Headers
cbsecurity sends these by default: X-Content-Type-Options, X-Frame-Options, Strict-Transport-Security, Referrer-Policy and X-XSS-Protection. These are off by default, so enable them when they fit your app:
securityHeaders.secureSSLRedirects.enabled
Always in production, to redirect non-SSL requests.
securityHeaders.contentSecurityPolicy
Once you have written a policy for your assets.
securityHeaders.hostHeaderValidation
To reject unexpected Host headers, with an allowedHosts list.
securityHeaders.trustUpstream
Only when your proxies are trusted, so x-forwarded-* headers can be inspected first.
Treat All Input as Hostile
Validate everything in
rcwith cbvalidation, and keep server-side state inprc.Restrict what each action accepts with HTTP method security.
Harden Production
Set a
reinitPasswordand never expose?fwreinit=1publicly.Leave the cbsecurity visualizer disabled (its default), or turn on
visualizer.securedand give it asecurityRule.
Defend Against Abuse
Route middleware from cbsecurity covers the common abuse cases without a rules file:
Brute force and scraping:
Throttle@cbsecurity, with a strict named limiter on login and password reset routes.Machine to machine endpoints:
ApiKey@cbsecurity, orJwtAuth@cbsecuritywhen you need to know who is calling.Admin areas:
AllowedIPs@cbsecuritynext to your authentication middleware.Public forms:
Honeypot@cbsecurityplusThrottle@cbsecurity.Cleartext traffic:
EnsureHttps@cbsecurity.Links that must not be guessed or edited:
Signed@cbsecuritywithsignedRoute()for email verification, password resets and temporary downloads. Keep the signing secret in an environment variable.
See the middleware reference for every middleware and its settings.
Observe Denials
cbsecurity announces cbSecurity_onInvalidAuthentication and cbSecurity_onInvalidAuthorization for every denied request, whether it came from a rule, an annotation or route middleware. Listen to them to log, alert or rate limit.
Test Your Security
Integration tests can assert denials end to end. With ColdBox 8.3+, execute(), get() and the other helpers also run route middleware.
See Also
Last updated
Was this helpful?