Shaun Mccran

My digital playground

09
J
U
L
2009

Basic fusebox fuseaction to handle security references

I am a big fan of fusebox, I like the way it handles inheritance, and I love the fact that it instinctively lends itself to a modular approach.

Part of the strength in using fusebox is in knowing exactly when each of the framework fuse actions run, and just what sort of functionality you can embed in them. In this case I'm using the "Pre fuse Action" to perform a basic security validation on any fuseactions in that circuit.

view plain print about
1<cffunction name="prefuseaction">
2        <cfargument name="myFusebox" />
3        <cfargument name="event" />
4
5
6    </cffunction>

Above is a blank prefuseaction, insert any code you want to perform on any of the other fuseactions in that circuit here. Note that it runs before the circuit action.

A basic session validation script could be something like:

view plain print about
1<!--- check that user is logged in --->
2        <cfif NOT isdefined('session.loggedIn')>
3            <cfset session.logoutMsg = "Your session has timed out, please login again">
4            <cflocation url="index.cfm">
5            
6            <cfif NOT isdefined('session.superadmin')>
7                <cfset session.logoutMsg = "You do not have sufficient rights to view Super admin functions">
8                <cflocation url="index.cfm">
9            </cfif>
10
11        </cfif>

In the code above I am checking for a valid session variables, and if it is not there sets an error message and redirects to the homepage.

This is a pretty basic "catch all - are you logged in?" type query, but if you have an administration circuit then it provides good basic fuseaction protection. I've extended it out one step further by creating a cfc call to this code which just returns true/false. Something like this:

view plain print about
1<cfif application.security.check()>true<cfelse>false</cfif>

I am currently extending this further with more robust security, and user roles and groups.

03
J
U
L
2009

Function for getting the last modified date of a template

Whilst looking at creating dynamic Sitemaps for Google bot spidering I found that I needed to populate an XML node with the last modified date of the templates. I figured there must be a programmatic way of doing this, so after some searching around this is what I ended out with:

view plain print about
1<cfset mod_time = createObject("java", "java.util.Date").init(createObject("java", "java.io.File").init(getcurrenttemplatepath()).lastModified())>

This creates a java object, using the date and file utilities we can use 'getcurrenttemplatepath()' to provide the path data, finally referencing the lastModified property.

A handy and relatively quick way of getting the last modified date.

It is a bit of a mouthful though, so might be easier to create it as a handy referencable object:

view plain print about
1<cffunction name="modStamp" access="public" returntype="date" output="no" hint="Gets the last mod date of a file, returns a ts">
2    <cfargument name="file" type="string" required="yes" hint="File to get mod date">
3        <cfset var mod_time = now()>
4        
5        <cfif fileexists(arguments.file)>
6            <cfset mod_time = createObject("java", "java.util.Date").init(createObject("java", "java.io.File").init(arguments.file).lastModified())>
7        </cfif>
8        
9    <cfreturn mod_time>
10</cffunction>

Then you can just do:

view plain print about
1<cfset variables.fileMod = modStamp()>

02
J
U
L
2009

Coldfusion dropping session ID in fusebox application

I recently rolled out beta version of a new application I've been writing, only to discover that there was a bizarre session problem that didn't exist in dev, but does in live.

I've worked it out, but I thought I'd explore it some more. It is a fusebox 5.5 non xml application. The error I had was that as soon as I made a call through a "new" circuit, IE one I hadn't called before ColdFusion would generate a new session ID, and thus invalidate my current active session.

Looking through my application CFC I had this line of code present.

view plain print about
1<cfset this.SetClientCookies = false />

Setting this to true fixed the issue. This is because ColdFusion relies on the CFID and CFTOKEN to maintain the session state. You can either pass these two variables through the URL on every page request, which is a bit messy, or you can use a cookie. It is the variable above that lets the application use cookies on the user's session.

The problem with setClientCookies is that it is persistent, IE it is built for that session, and left on the user's pc, even after the session has expired, or they have left the application. Also some users will accept per-session cookies, but not persistent session cookies.

They are a lot more secure as per-session cookies, as they cannot be duplicated and hacked to spoof a previous user's session, and if you pass the token through the URL it is easy changed.

You could put something like this in your onRequestend function in application.cfc

view plain print about
1<cfif IsDefined("Cookie.CFID") AND
2IsDefined("Cookie.CFTOKEN")>

3<cfset cfid_local = Cookie.CFID>
4<cfset cftoken_local = Cookie.CFTOKEN>
5<cfcookie name="CFID" value="#cfid_local#">
6<cfcookie name="CFTOKEN" value="#cftoken_local#">
7</cfif>

This will make them per-session. I originally thought that it was something to do with the Fusebox framework, but I had overlooked the simple fact that it was still a new page request, so would be lost. Although this doesn't explain why I wasn't getting this error in my development environment but did in live.

01
J
U
L
2009

Gmail Stripping styles out of html elements in emails

A recently 'feature' that has been added to Google is the way they are handling un styled elements.

In the last few weeks Gmail has started stripping the paragraph margins from its HTML emails. Basically is destroys the spacing from P tags, so that instead of being new lines, they are more like line breaks.

Checking the element in firebug shows that:

view plain print about
1.X0uMP p { margin:0;}

If you disable the style, then the html reverts to back to what you probably thought it should look like.

I'm guessing that as Gmail hasn't found an associated style with the element (P) it is applying its own.

You could change all the inline P's:

view plain print about
1<p style="margin-bottom: 8px;">

But that's hassle and not best practice really. I'll apply a CSS fix to it, and see if Gmail accepts that.

view plain print about
1p{margin-bottom:8px;}

_UNKNOWNTRANSLATION_ /