The Same CFID-CFTOKEN Values Are Used Across ColdFusion Session Timeouts

Posted March 23, 2009 at 9:06 AM

Tags: ColdFusion

In my other post on handling ColdFusion session management with asynchronous page requests, Lee brought up the point that a user will use the same CFID and CFTOKEN values even after their session expires. Meaning, that if the user makes a page request after their session has expired, ColdFusion will open a new session for them and use the same CFID and CFTOKEN values from their previous, expired session.

I didn't doubt this, but at the same time, I had never thought about it and had never seen this fact for myself. As such, I figured I would run a quick demo to see if this actually happens. All I needed to do was set up a small, simple Application.cfc that has a short session timeout and logs the start and end of each session:

 Launch code in new window » Download code as text file »

  • <cfcomponent
  • output="false"
  • hint="I set up the application settings and event handlers.">
  •  
  • <!--- Application settings. --->
  • <cfset THIS.Name = "TimeoutCookieTest" />
  • <cfset THIS.ApplicationTimeout = CreateTimeSpan( 0, 0, 5, 0 ) />
  • <cfset THIS.SessionManagement = true />
  • <cfset THIS.SessionTimeout = CreateTimeSpan( 0, 0, 0, 10 ) />
  •  
  •  
  • <cffunction
  • name="OnSessionStart"
  • access="public"
  • returntype="void"
  • output="false"
  • hint="I fire when a session needs to be initialized.">
  •  
  • <!--- Log session stsart. --->
  • <cffile
  • action="append"
  • file="#ExpandPath( './log.txt' )#"
  • output="Started: #SESSION.CFID#-#SESSION.CFTOKEN#"
  • addnewline="true"
  • />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="OnSessionEnd"
  • access="public"
  • returntype="void"
  • output="false"
  • hint="I fire when a session needs to be ended.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Session"
  • type="struct"
  • required="true"
  • hint="I am the expired session scope."
  • />
  •  
  • <!--- Log session stsart. --->
  • <cffile
  • action="append"
  • file="#ExpandPath( './log.txt' )#"
  • output="Ended: #ARGUMENTS.Session.CFID#-#ARGUMENTS.Session.CFTOKEN#"
  • addnewline="true"
  • />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  • </cfcomponent>

With this Application.cfc in place, I ran the page, waited for the session to timeout, and then ran the page again. After a few bouts of this, my log file looked like this:

Started: 7501-57161699
Ended: 7501-57161699
Started: 7501-57161699
Ended: 7501-57161699
Started: 7501-57161699
Ended: 7501-57161699

As you can see, even as the one session ends and the next begins, ColdFusion uses the same CFID and CFTOKEN values stored in the user's cookies.

Seeing this, I wondered what would happen if I manually changed my CFID / CFTOKEN value and made another page request? So, using my Web Developer Toolbar, I opened my page cookies and changed my CFID value to:

1234

Then, I refreshed the same page, waited for timeout, and checked the log:

Ended: 7501-57161699
Started: 1234-57161699
Ended: 1234-57161699

As you can see, ColdFusion picked up my manually edited CFID value and started using that in the new SESSION management.

Anyway, this isn't really stuff that needs to be thought about, but it's nice to know how the cookies are being handled under the covers.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Mar 23, 2009 at 11:44 AM // reply »
25 Comments

It's always interesting to know what happens under the covers ;).


Mar 23, 2009 at 11:54 AM // reply »
6,516 Comments

@Francois,

Ha ha :)


Mar 23, 2009 at 1:04 PM // reply »
27 Comments

This sounds very unlikely.. but what if you edit the cookie values to match that of someone else's session, are you able to hijack a session this way?

(Again I can't imagine that anyone would be able to exploit this, but just curious.)


Mar 23, 2009 at 1:08 PM // reply »
6,516 Comments

@Tim,

Unfortunately, I think that is exactly what happens. This is why you hear about crazy problems where a search engine accidentally spiders a site that has URL-based CFID/CFTOKEN values. Remember, sessions can still work without cookies as long as the user puts the CFID/CFTOKEN values in the URLs (hence the AddToken boolean in CFLocation).

This is also why security audits won't pass if you use the built-in CFID/CFTOKEN values as unique identifiers.


Mar 23, 2009 at 1:31 PM // reply »
5 Comments

Nice investigation Ben. If you think about it this behaviour makes sense but is unintuitive; the client tokens identify the client, not the session. A session simply represents a period of time in which a particular client is active. Just don't use client tokens for information which is login/session sensitive.

I have kind of learnt this the hard way.


Mar 23, 2009 at 1:36 PM // reply »
6,516 Comments

@Darren,

Right, exactly. It makes sense, but it's the kind of thing you never really have to think about, so you don't really know if it's intuitive or not.


Mar 23, 2009 at 1:37 PM // reply »
27 Comments

@Ben

Duh.. I knew that. Well, I knew that adding it in the URL passes the session information along, I should have figured the cookies would do the same thing.


Mar 23, 2009 at 6:33 PM // reply »
8 Comments

@ Tim (and @Ben), yes it is exploitable.
Note: There is a tick option in ColdFusion Administrator 'Use UUID for cftoken'.
This makes the cftoken unique and harder to guess. I haven't found a reason not to use this setting ever. Has anyone?


Mar 23, 2009 at 7:11 PM // reply »
2 Comments

For reasons described here, we decided 5 or 6 years ago to abandon the CF session management. The fatal flaw in the system was clueless users copying and pasting urls from their browser address bar and emailing them to others. Result was hijacked sessions with users doing things they shouldn't have been allowed to.


Mar 23, 2009 at 7:39 PM // reply »
27 Comments

@Jeff

Sounds to me a bit like throwing the baby out with the bath water.. Why not just make sure to set addToken to no in all cflocations? Wouldn't that keep it our of the URL? (And be alot easier then reproducing that functionality.)

Though I am curious, how did you brew your session management?


Mar 23, 2009 at 7:44 PM // reply »
6,516 Comments

@Tim,

Agreed. You still need to leverage ColdFusion as the base session management as it creates your memory spaces on the server. However, I think we can take steps to make ColdFusion session management much more secure (via setting our own cookies and a host of other tweaks).


Mar 23, 2009 at 9:50 PM // reply »
2 Comments

@Tim

That's where we started, but since we were government, we had very strict rules about usage of cookies. The other piece that tipped the scales was that CF was apparently reusing the token id's which was causing big problems, for obvious reasons. I talked to some Macromedia guys about it and they suggested I use the UUID's, but, alas, we were still on CF5 and it wasn't an option, so we reluctantly rolled our own.

We did it by building a few custom tags which were called in application.cfm and onRequestEnd.cfm and kept everything in the db. I'm not sure we would go that route again knowing what we know now. The good side is that we totally know what's going on under the covers.


Mar 24, 2009 at 7:32 AM // reply »
27 Comments

...Ahh CF 5.

Makes me nostalgic about switching from Allaire ColdFusion Studio 4.5.2. To Macromedia HomeSite 5..

Anyway I see your point Jeff. CF's come a ways since then it seems.


Lee
Mar 24, 2009 at 9:06 AM // reply »
9 Comments

You can also use cfcookie to manually set the cookies to expire immediately. That way when the user closes the browser the cookies are gone.


Mar 24, 2009 at 9:18 AM // reply »
6,516 Comments

@Lee,

I believe for "Browser Session cookies", you need to set the cookie to have no expiration date (meaning, you exclude it from the CFCookie tag).


Mar 25, 2009 at 4:08 PM // reply »
11 Comments

Couldn't you also use a database to store client data? That would be much more secure than a cookie. From what I remember CF5 does let you use the db to store client dat.. Another idea would be to use browser session-only cookies which would be deleted as soon as the browser is closed.


Mar 25, 2009 at 4:29 PM // reply »
6,516 Comments

@Larry,

I haven't dealt with CLIENT variables in a long time, but yeah I believe you can store them in the database. But, I think the browser still needs a way to associate itself with the session on the server; I think that aspect still needs cookies (but I am not nearly sure)


Mar 26, 2009 at 12:29 AM // reply »
9 Comments

@Ben,

Yes, that is correct.


Mar 26, 2009 at 10:09 PM // reply »
28 Comments

I deal a lot with these kinds of issues since I sell 3rd party software and have to handle all kinds of setups that people will use it in...like supporting shared SSL (which requires passing the session ID in order to not lose the session). I use a variety of other little tricks that at least help reduce accidental sharing of links with the session IDs on them (and I hide them from search engines). I also use a setting so that for sites that use a dedicated SSL that improves the security, and also check for a CFID or CFTOKEN being passed in a URL or Form, and automatically reset them manually. Also set a secure random cookie each time the user logs in and make sure it exists before giving them access to anything. So there's a lot of things you can do if you want to beef up security even if you use the built-in session management.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »