Target ColdFusion Template Gets Compiled Even If Not Executed

Posted October 2, 2007 at 8:28 AM

Tags: ColdFusion

A while back, I had suggested a hack to get ColdFusion-based security on non-ColdFusion files by adding a ".cfm" file extension. Then, in the Application.cfc's OnRequest() event method, intercepting those file requests, checking security permissions, and streaming the requested files (using CFContent). Since the requested file was never being executed (only streamed), it never occurred to me that it would matter what type of file it was.

As Regs pointed out to me, this was an incorrect assumption. Apparently, the requested template gets compiled even if it is never executed. This can cause problems, as he points out, if the file contains ColdFusion-tag-like data that starts with "<cf". To test this, I set up a tiny application that never includes the requested file, but rather a default file. Here is the ColdFusion Application.cfc file:

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

  • <cfcomponent
  • output="false"
  • hint="Handles the application level events and application settings.">
  •  
  •  
  • <!--- Define application settings. --->
  • <cfset THIS.Name = "SecurityTest2" />
  • <cfset THIS.ApplicationTimeout = CreateTimeSpan( 0, 0, 10, 0 ) />
  • <cfset THIS.SessionManagement = false />
  • <cfset THIS.SetClientCookies = false />
  •  
  • <!--- Define page settings. --->
  • <cfsetting
  • requesttimeout="20"
  • showdebugoutput="false"
  • />
  •  
  •  
  • <cffunction
  • name="OnRequest"
  • access="public"
  • returntype="void"
  • output="true"
  • hint="Fires after pre page processing is complete - when determining which template to ultimately execute.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="TargetPage"
  • type="string"
  • required="true"
  • />
  •  
  • <!---
  • Include the default page regardless of what page
  • was actually requested.
  • --->
  • <cfinclude template="default.cfm" />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  • </cfcomponent>

Notice that default.cfm is included no matter what happens. This is just a simple HTML file. But, I am gonna put in a bunk index.cfm in that directory:

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

  • asdlfj alksdf lkajdf lajlfj ald fladsfs
  • adfj aldjflajsdf ajsdf asdf alsdflasdlfl
  • adf adsfasdf asf<cfset adkfjasdlf asdf />
  • ljlaj dlfjals dfljasd flajsdl fasjdlfj al

Notice the poorly formeed ColdFusion CFSet tag mixed in with the random characters. This is meant to simulate the random text that might show up in a secure file.

Ok, so when the index.cfm ColdFusion template is requested, what I assumed would happen is that the default.cfm template would be executed and everything would go smoothly. But, no; just at Regs pointed out, ColdFusion throws an error:

Invalid CFML construct found on line 3 at column 35. ColdFusion was looking at the following text: asdf The CFML compiler was processing: * a cfset tag beginning on line 3, column 18.

I don't like this behavior. It doesn't feel right. The whole point of a conditional CFInclude tag is that the file only gets executed if it actually gets included. This target page compilation goes against this idea, not to mention it makes my security hack totally unusable.

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

Oct 2, 2007 at 9:36 AM // reply »
177 Comments

Yeah, the hack is unusable. Unfortunately the best way to secure files is to move them out of webroot and then use:
<CFHTMLHEAD text="<title>Download #finalName#</title>" />
<CFHEADER NAME="content-disposition" VALUE="#theAction#; filename=#finalName#">
<CFCONTENT TYPE="#mimetype#" FILE="#downloadFile.file_name#" DELETEFILE="No">

It sucks, but it's not that bad. You could code up a system rather quickly and you'll never have to think about it again.


Oct 2, 2007 at 9:48 AM // reply »
27 Comments

At CFUnited this year someone (New Atlanta, Microsoft?) showed how IIS8 can use any ISAPI application to handle IIS requests. I don't recall the exact details but I think one of the examples was for security of any file.


Oct 2, 2007 at 4:22 PM // reply »
6,516 Comments

@Todd,

Yeah, agreed. Oh well.

@Dan,

I remember that, I think. Was that the presentation about tapping into the "request pipeline" at any point so that CF(BD) could reach in before IIS processes files or something. I vaguely remember it, but much of the presentation went way over my head as I am not very familiar with IIS at all.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
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 »