<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Forgettable Mister Ruthsarian &#187; ColdFusion</title>
	<atom:link href="http://weblog.bridgew.edu/ruthsarian/category/coldfusion/feed/" rel="self" type="application/rss+xml" />
	<link>http://weblog.bridgew.edu/ruthsarian</link>
	<description>All manner of web development issues discussed, but with a heavy focus on CSS.</description>
	<lastBuildDate>Mon, 26 Oct 2009 20:13:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Washing Client Certs in ColdFusion with SOAP &#8211; Part 2</title>
		<link>http://weblog.bridgew.edu/ruthsarian/2009/10/26/washing-client-certs-in-coldfusion-with-soap-part-2/</link>
		<comments>http://weblog.bridgew.edu/ruthsarian/2009/10/26/washing-client-certs-in-coldfusion-with-soap-part-2/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 18:40:42 +0000</pubDate>
		<dc:creator>Ruthsarian</dc:creator>
				<category><![CDATA[ColdFusion]]></category>

		<guid isPermaLink="false">http://weblog.bridgew.edu/ruthsarian/?p=262</guid>
		<description><![CDATA[In part 1 I introduced you to basic SOAP consumption in ColdFusion. Let&#8217;s see where things go from there.
The task at hand was to integrate our system with a third-party site. I authenticate users locally, request a token from the third-party site that allows the user to access said site, and then redirect the user [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://weblog.bridgew.edu/ruthsarian/2009/10/26/washing-client-certs-in-coldfusion-with-soap-part-1/">part 1</a> I introduced you to basic SOAP consumption in ColdFusion. Let&#8217;s see where things go from there.</p>
<p>The task at hand was to integrate our system with a third-party site. I authenticate users locally, request a token from the third-party site that allows the user to access said site, and then redirect the user to that site with the token passed on the URL.</p>
<p>But how does the third-party site know to trust my requests and not the requests from other people? After all you don&#8217;t want people forging requests to access the third-party site as some other user. The solution for this third-party site is <a href="http://wiki.cacert.org/ClientCerts">client certificates</a>. A client certificate is either issued by the third-party site or you provide your public certificate to the third-party site and the third-party&#8217;s site is configured to trust the certificate. SOAP requests are then made over SSL using this certificate to confirm your identity and to encrypt the channel.</p>
<p>Starting with ColdFusion 8 <strong>CFHTTP</strong> accepts two parameters to afford the use of client certs, <strong>clientCert</strong> and <strong>clientCertPassword</strong>. The <strong>clientCert</strong> parameter points to a <em>PKCS12</em> formatted file containing your public and private keys and, possibly, the certificate chain from the root certificate authority (such as VeriSign or Thawte) down to whoever issued your client certificate. The <strong>clientCertPassword</strong> parameter contains the password with which the PKCS12 file is encrypted.</p>
<p>A note to ColdFusion developers:<br />
T<strong>he PKCS12 must be encrypted with a password</strong>! For whatever reason (I believe a limitation in the underlying <em>java.security.KeyStore</em> object) your cert must have a password. This is never explicitly stated in the ColdFusion 8 documentation.</p>
<p>I obtained my client certificate and set out to start writing code to talk with this third-party.</p>
<p>The first problem I encountered was I would not be able to use the <strong>CreateObject()</strong> method covered in part 1. The reason being that there was no way to provide my client certificate to the object. So it&#8217;s back to the <strong>CFHTTP</strong> method.</p>
<p>The second problem I encountered had nothing to do with ColdFusion and everything to do with the documentation provided by the third-party. Turns out the header for the SOAP request changed considerably during development. I had been given some early development documents that did not reflect the current header structure. Once I realized the problem and obtained the current documentation I was able to correctly construct my SOAP request&#8217;s envelope header and&#8230;</p>
<p>I got another error.</p>
<p>But this time it wasn&#8217;t a normal SOAP request error. The <strong>CFHTTP</strong> object&#8217;s <em>filecontent</em> value contained nothing more than &#8220;Connection failure&#8221;. But the HTTP status code was 200, which indicates a successful request. Previous SOAP request errors would return a 403 status code. This was odd.</p>
<p>In searching the Adobe forums and the internet in general I found sparse comments about possible problems with <strong>CFHTTP</strong> handling SSLv3 sessions, although there wasn&#8217;t any sort of official comment or response to the few reports of this problem. I loaded up the developer edition of ColdFusion 9 on my own computer to see if perhaps this problem had been resolved with the latest copy of ColdFusion. It had not.</p>
<p>To confirm this as a problem with ColdFusion and not my client cert, or my SOAP request, I installed Apache and PHP locally and ran the equivalent PHP code. The PHP code worked perfectly. I started trying to do as much comparison between the two platforms as I could. I event went so far as to run a packet capture on the PHP and CF requests (pointing them at a dummy, local page that wasn&#8217;t encrypted so I could see the requests) and compared them to make sure everything was the same, which they were.</p>
<p>Eventually I found a post online that mentioned <strong>CFHTTP</strong> wasn&#8217;t up to the job, but a third-party custom tag written in C++ did work just fine. That custom tag is <a href="http://www.cftagstore.com/tags/cfxhttp5.cfm">CFX_HTTP5</a>. I downloaded a demo copy and installed it locally. How <strong>CFX_HTTP5</strong> handles client certs is different from <strong>CFHTTP</strong>. Rather than simply pointing the tag at the client cert, I had to install the client cert into the Windows certificate store and then point the tag at the store. There is a bit of work involved with it, although nothing too difficult and it&#8217;s all covered in the CFX_HTTP5 documentation.</p>
<p>Once I had the tag and the certificate imported into the local Windows certificate store, I rewrote the <strong>CFHTTP</strong> call using <strong>CFX_HTTP5</strong> and it worked! The SOAP envelope was the same, the headers were the same, the only difference between the two was the logic underlying the tags.</p>
<p>Something is broken with CFHTTP and it can&#8217;t be used to do some operations using client certificates. But at least there is an alternative.</p>
<p>However I didn&#8217;t like the alternative.</p>
<p>First, it&#8217;s a Windows-only solution. We&#8217;re out of luck if we&#8217;re running ColdFusion on a Linux machine.</p>
<p>Secondly, the client cert must be stored in a place that the ColdFusion process has permissions to access, and it is accessed without needing to know the client cert password. The result is that any person with permission to create CFM scripts that are executed under this process could authenticate against this third-party web site. In a shared hosting environment this can create a serious security issue. The only solution is to separate the process, but that probably means a separate server and a new OS license and hardware costs. If that option isn&#8217;t available you have some problems. You might register the CFX with a name that contains random characters and hope none of the users in the shared environment know how to enumerate registered custom tags. I&#8217;m not sure if that is possible, but I&#8217;m willing to bet it is.</p>
<p>Long story short, I don&#8217;t like the <strong>CFX_HTTP5</strong> solution.</p>
<p>So I went back to the drawing board.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.bridgew.edu/ruthsarian/2009/10/26/washing-client-certs-in-coldfusion-with-soap-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Washing Client Certs in ColdFusion with SOAP &#8211; Part 1</title>
		<link>http://weblog.bridgew.edu/ruthsarian/2009/10/26/washing-client-certs-in-coldfusion-with-soap-part-1/</link>
		<comments>http://weblog.bridgew.edu/ruthsarian/2009/10/26/washing-client-certs-in-coldfusion-with-soap-part-1/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 17:22:43 +0000</pubDate>
		<dc:creator>Ruthsarian</dc:creator>
				<category><![CDATA[ColdFusion]]></category>

		<guid isPermaLink="false">http://weblog.bridgew.edu/ruthsarian/?p=257</guid>
		<description><![CDATA[Recently I was asked to look into integrating our systems with an external application via the third-party&#8217;s single sign-on system. The way it works is simple enough. We would have an application that authenticates the user through our system and then sends a request to the third-party asking for a token to sign the user [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was asked to look into integrating our systems with an external application via the third-party&#8217;s single sign-on system. The way it works is simple enough. We would have an application that authenticates the user through our system and then sends a request to the third-party asking for a token to sign the user into their system. The third-party would return the token that I then give to the end-user and redirect them to the third-party web site. This token is how the third-party would authenticate the user into their system. (The token is nothing more than a long string of characters that is passed on the URL of the redirect.)</p>
<p>The method of obtaining the token is also fairly simple. The application submits a SOAP request over an SSL session to the third-party&#8217;s authentication server and that server would respond with the token. <a href="http://en.wikipedia.org/wiki/SOAP">SOAP</a>, for all the technical specs and other crap, is very straightforward. It&#8217;s a simple XML document consisting of a root element called ENVELOPE which contains two children called HEADER and BODY. The header isn&#8217;t always required and the body typically contains elements with the names of various fields the SOAP function your calling requires with each element containing the value of that parameter. Very straightforward.</p>
<p>A SOAP request in ColdFusion couldn&#8217;t be simpler, especially with the <strong>CFSAVECONTENT</strong> tag. Simply construct your envelope inside a <strong>CFSAVECONTENT</strong> tag and then use <strong>CFHTTP</strong> to submit the request. It looks a little something like this:</p>
<pre>&lt;cfsavecontent variable="variables.soap"&gt;
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  &lt;soap:Body&gt;
    &lt;GetCurrentTime xmlns="http://ws.historicaloptiondata.com/" /&gt;
  &lt;/soap:Body&gt;
&lt;/soap:Envelope&gt;
&lt;/cfsavecontent&gt;

&lt;cfhttp
  url="http://ws.historicaloptiondata.com/Service.asmx"
  method="POST"
&gt;
  &lt;cfhttpparam type="header" name="SOAPAction" value="""http://ws.historicaloptiondata.com/GetCurrentTime""" /&gt;
  &lt;cfhttpparam type="header" name="Content-Length" value="#Len( Trim( variables.soap ))#" /&gt;
  &lt;cfhttpparam type="xml" value="#Trim( variables.soap )#" /&gt;
&lt;/cfhttp&gt;

&lt;cfdump var="#xmlParse( cfhttp.filecontent )#" /&gt;</pre>
<p>You should be able to plug this code into a CFM file and run it without having to touch a thing and you should see the <strong>CFDUMP</strong> of an XML object. (I say &#8220;should&#8221; because <strong>xmlParse()</strong> seems to first try and open a file with the name of the content of the passed variable and, when that fails, treat the passed value as an XML document itself. This can trigger errors and make it unusable if you employ any sort of file operation restrictions on your server. In which case modify the code to remove the <strong>xmlParse()</strong> call and just dump the <strong>cfhttp.filecontent</strong>.)</p>
<p>A few  notes about this code SOAP in general.</p>
<ul>
<li>This is an example of a <em>SOAP 1.1</em> request. There is another, slightly different format known as <em>SOAP 1.2</em>. The major differences between the two are that the <strong>content-type</strong> for 1.1 is <em>text/xml</em>, but for 1.2 it is <em>application/soap+xml</em>. Also the <strong>SOAPAction</strong> HTTP header is no longer needed in 1.2.</li>
<li>The <strong>SOAPAction</strong> HTTP header&#8217;s value must be wrapped in double-quotation marks. And sometimes the first character after the open quotes will need to be a pound (#) symbol. This means ColdFusion programmers will need to be certain they escape these special characters in their values.</li>
<li>If the <strong>CFHTTPPARAM</strong> type &#8220;xml&#8221; is present, <em>CFHTTP</em> automatically sets the content-type to <em>text/xml</em>. I am not sure if it&#8217;s possible to override this, but I believe not, therefore you&#8217;re almost always going to have to stick with <em>SOAP 1.1</em> if you&#8217;re using <strong>CFHTTP</strong> for your SOAP requests.</li>
<li>You must <strong>Trim()</strong> the <em>variables.soap</em> variable! The newline at the beginning of the value, which exists because there is a newline immediately after after <strong>CFSAVEDCONTENT</strong> tag (for visual formatting purposes) will make the XML document you&#8217;re sending an invalid XML document and result in errors.</li>
</ul>
<p>Now comes the <a href="http://en.wikipedia.org/wiki/Web_Services_Description_Language">WSDL</a> file. A WSDL file is an XML documents that describes the functions and parameters of said functions available through a SOAP service. The <strong>CreateObject()</strong> function has a &#8220;<em>webservice</em>&#8221; object type which will consume a WSDL file and create an object with all the available functions offered by the SOAP service. All the stuff with XML and ENVELOPES and CFHTTP becomes transparent and, as it turns out, SOAP can be simpler than my previous example. The above code can be reduced to the following using <strong>CreateObject()</strong>:</p>
<pre>&lt;cfset ws = CreateObject( "webservice", "http://ws.historicaloptiondata.com/Service.asmx?WSDL" ) /&gt;
&lt;cfset ws.getCurrentTime() /&gt;
&lt;cfdump var="#GetSOAPResponse( ws )#" /&gt;</pre>
<p>This makes this life much simpler for ColdFusion programmers. No need to worry about what version of SOAP you&#8217;re using or what URLs you need to submit your request to, the formatting of your SOAP envelope, the <strong>SOAPAction</strong> HTTP header variable, etc. It&#8217;s all taken care of for you by ColdFusion.</p>
<p>So this integration I was asked to do should be a piece of cake, right?</p>
<p>Wrong.</p>
<p>Simple SOAP is simple. Complex SOAP&#8230; well, you&#8217;ll see in Part 2.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.bridgew.edu/ruthsarian/2009/10/26/washing-client-certs-in-coldfusion-with-soap-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>cf_buffer</title>
		<link>http://weblog.bridgew.edu/ruthsarian/2007/10/24/cf_buffer/</link>
		<comments>http://weblog.bridgew.edu/ruthsarian/2007/10/24/cf_buffer/#comments</comments>
		<pubDate>Wed, 24 Oct 2007 18:24:00 +0000</pubDate>
		<dc:creator>Ruthsarian</dc:creator>
				<category><![CDATA[ColdFusion]]></category>

		<guid isPermaLink="false">http://weblog.bridgew.edu/ruthsarian/2007/10/24/cf_buffer/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p>My knowledge of ColdFusion 8 (or hell, even 7 for that matter) isn&#8217;t terribly broad. So if what I&#8217;m about to cover can be accomplished with an existing CF function or tag please let me know.</p>
<p>Output Buffering.</p>
<p>It&#8217;s something I first got use to <a href="http://us2.php.net/ob_start">with PHP</a>. What it does is allow you to set a start and end point in your program and any output generated between those two points gets put into a variable which you can then manipulate further before outputting the content.</p>
<p>Recently I was asked if this was possible to do with ColdFusion. I couldn&#8217;t think of any existing feature that does this so I put together a (surprisingly) short custom tag that would do just that.</p>
<p>Here it is:<br />
<code><br />
&lt;cfif CompareNoCase( thistag.ExecutionMode, "start" ) EQ 0&gt;<br />
&nbsp;&nbsp;&lt;cfparam name="attributes.buffer" default="variables.buffer"&gt;<br />
&lt;cfelse&gt;<br />
&nbsp;&nbsp;&lt;cfset "caller.#attributes.buffer#" = thisTag.GeneratedContent&gt;<br />
&nbsp;&nbsp;&lt;cfset thisTag.GeneratedContent = ""&gt;<br />
&lt;/cfif&gt;<br />
</code></p>
<p>I put this into a file named buffer.cfm. Now anything that occurs between an opening and closing CF_BUFFER tag will be stored in a variable which can then be further manipulated however you want.</p>
<p>I did find <a href="http://www.overset.com/2006/09/18/true-coldfusion-whitespace-removal/">something similar</a> using <a href="http://livedocs.adobe.com/coldfusion/7/htmldocs/00000490.htm">getPageContext</a>().getOut().getString(), but nothing simpler.</p>
<p>You might argue that this feature shouldn&#8217;t be needed in the first place. That whatever the need is it can be solved by redesigning the application. That might be true. But it&#8217;s a handy feature to play around with.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.bridgew.edu/ruthsarian/2007/10/24/cf_buffer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ColdFusion: CFQUERY and Evaluate()</title>
		<link>http://weblog.bridgew.edu/ruthsarian/2006/10/03/coldfusion-cfquery-and-evaluate/</link>
		<comments>http://weblog.bridgew.edu/ruthsarian/2006/10/03/coldfusion-cfquery-and-evaluate/#comments</comments>
		<pubDate>Tue, 03 Oct 2006 20:58:01 +0000</pubDate>
		<dc:creator>Ruthsarian</dc:creator>
				<category><![CDATA[ColdFusion]]></category>

		<guid isPermaLink="false">http://weblog.bridgew.edu/ruthsarian/2006/10/03/coldfusion-cfquery-and-evaluate/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p>
Been going back and forth with a friend who does CF development for some company. Anyways, he rings me up to talk about some SQL injection attacks that have been reported on an application he maintains. I take a look at the code and, sure enough, there are. Problem is they&#8217;re buried a bit and not easily found. Furthermore, one of the two exploits I&#8217;m about to cover is something not many CF developers will ever notice, even if they are aware of the dangers of SQL injection. This is because ColdFusion, on some levels, sucks.
</p>
<h3>Case 1: Evaluate</h3>
<p>Here&#8217;s the code:</p>
<p style="font-family:courier new, monospace">
&lt;cfset session.isadmin = false&gt;</p>
<p>&lt;cfparam name=&quot;url.x&quot; default=&quot;1&quot;&gt;</p>
<p>&lt;cfset variables.msg1 = &quot;Welcome To Our Site&quot;&gt;<br />
&lt;cfset variables.msg2 = &quot;Site Administration&quot;&gt;</p>
<p>&lt;h1&gt;&lt;cfoutput&gt;#Evaluate( &quot;variables.msg#x#&quot; )#&lt;/cfoutput&gt;&lt;/h1&gt;</p>
<p>&lt;cfif session.isadmin is true&gt;<br />
&nbsp;&nbsp;&lt;p&gt;You may now administer the site as you see fit.&lt;/p&gt;<br />
&lt;cfelse&gt;<br />
&nbsp;&nbsp;&lt;p&gt;Buy our product.&lt;/p&gt;<br />
&lt;/cfif&gt;
</p>
<p>
This is now how you write CF, this is just a very simplified example to show you the problem that was discovered; in this case, it&#8217;s the <code>Evaluate()</code> line. Variable <code>X</code> is being passed on the URL in this example for simplification. In reality, there are any number of ways user-provided data could find it&#8217;s way into an <code>Evaluate()</code> statement. I&#8217;m just making it easy and obvious here.
</p>
<p>
<code>Evaluate()</code> will evaluate (and execute) the contents of the string that is passed to the function and return the result. The intention of the example above is to return the relevant message associated with the user&#8217;s level of access. Since <code>X</code> is supplied by the user it is possible to inject something into that <code>Evaluate()</code> line. But what?
</p>
<p>
Long story short, calling this script with the following URL will alter the <code>isadmin</code> session variable to change the user&#8217;s access level.<br />
<code>vulnerable.cfm?x=2+eq+'a'+or+SetVariable(session.isadmin,true)</code>
</p>
<p>
This creates the following <code>Evaluate()</code> statement:<br />
<code>Evaluate( "variables.msg2 eq 'a' or SetVariable(session.isadmin,true)" )</code>
</p>
<p>
The string now represents a boolean statement. Instead of returning the value of the string <code>variables.msg2</code> it now will return TRUE or FALSE. And before it does that it will execute every expression in the statement, including a call to the function <code>SetVariable()</code> which alters the <code>isadmin</code> session variable. With that, the user is now an admin and will have full control of the application. What&#8217;s worse, more function calls could could be passed to the application which essentially give the user the same level of access to the machine as whatever user ColdFusion is running under. Especially under ColdFusion MX where CreateObject() can be used to create JAVA objects that provide system-level access.
</p>
<h3>Case 2: Evaluate in a CFQUERY</h3>
<p><code>&lt;cfsetting showdebugoutput=&quot;Yes&quot;&gt;<br />
&lt;cfset variables.hack  = &quot;' OR 1=1 OR Name='&quot;&gt;<br />
&lt;cfquery datasource=&quot;misc&quot; name=&quot;test&quot;&gt;<br />
&nbsp;&nbsp;SELECT *<br />
&nbsp;&nbsp;FROM Test<br />
&nbsp;&nbsp;WHERE Name = '#Evaluate( &quot;variables.hack&quot; )#'<br />
&lt;/cfquery&gt;</code></p>
<p>
Again, I&#8217;m oversiplifying this a bit, but I&#8217;ll show you where this would happen in the real world once you understand what&#8217;s going on here.
</p>
<p>
variables.hack represents the point of injection. We assume this variable has a legit purpose but, at this point in the code execution, it&#8217;s value has been altered by a malicious user to what it&#8217;s set as in the code. The relevant portion of the SQL injection is <code>OR 1=1 OR</code> which will trigger the database to return all records instead of just the one record intended. There are many other things you can do when you&#8217;ve got a SQL injection point like this, but this isn&#8217;t about SQL injection it&#8217;s about how <code>Evaluate()</code> creates problems.
</p>
<p>
In ColdFusion, any variable in a CFQUERY block that is wrapped in single quotes (<code>'#variables.string#'</code>) will be automatically escaped. In other languages like PHP you have to do this maually, but CF does it automatically. Escaping these strings prevents malicious users from injecting SQL commands into the query. It makes the database treat the string as just a string and not as a series of commands that&#8217;s part of the SQL statement.
</p>
<p>
If you were to run this script on a CF server you would discover that the string in <code>variables.hack</code> is <em>NOT</em> escaped. This allows SQL to be injected into the query. Why is this? The <code>Evaluate()</code> statement is wrapped in single quotes, so what gives?
</p>
<p>
Well.. actually, ColdFusion is working as intended. There are several passes of the SQL statement made by ColdFusion. The first pass handles the escaping of variables. The second pass handles executing functions and after that the SQL is passed on to the database. If you had something like:</p>
<p><code><br />
&lt;cfset variables.msg=&quot;hack'd&quot;&gt;<br />
&lt;cfquery datasource=&quot;misc&quot; name=&quot;test&quot;&gt;<br />
&nbsp;&nbsp;SELECT *<br />
&nbsp;&nbsp;FROM Test<br />
&nbsp;&nbsp;WHERE Name = '#Evaluate( &quot;variables.#msg#&quot; )#'<br />
&lt;/cfquery&gt;<br />
</code><br />
</code></p>
<p>You would find the <code>Evaluate()</code> statement becomes:<br />
<code>Evaluate( &quot;variables.hack''d&quot; )</code></p>
<p>Two single-quotes is the escape sequence for a single quot e in SQL. The contents of the <code>msg</code> variable are escaped. After this escape, <code>Evaluate()</code> is executed. In this case it'd return an error since variable names can't contain single-quotes.
</p>
<p>
So you see, the escaping of variables occurs before <code>Evaluate()</code> is processed. This means the contents of whatever <code>Evaluate()</code> will not be escaped and provides a point of injection for an attacker.
</p>
<p>
The solution is to set the results of the <code>Evaluate()</code> call to a temp variable and use that temp variable inside the CFQUERY.</p>
<p><code><br />
&lt;cfset variables.test = Evaluate( &quot;variables.hack&quot; )&gt;<br />
&lt;cfquery datasource=&quot;misc&quot; name=&quot;test&quot;&gt;<br />
&nbsp;&nbsp;SELECT *<br />
&nbsp;&nbsp;FROM Test<br />
&nbsp;&nbsp;WHERE Name = '#variables.test#'<br />
&lt;/cfquery&gt;<br />
</code>
</p>
<p>
Where is this used in the real world? Arrays. Something where you loop through a number set, say 1 to 20, where you insert the Nth value in an array into the database. Something like <code>SET data = '#Evaluate( &quot;userdata[#X#]&quot; )#'</code> where userdata is an array that stores data provided by the user (form data, data pulled from a database that was set through another application, etc.)
</p>
<p>
<STRONG>This problem exists for other functions used in a CFQUERY</STRONG>.</p>
<p>That is, any function that performs an evaluation of a string will have the same problem. <code>IIF()</code> is one such function. I'm not sure what others there are off the top of my head, but it's definately something to think about.
</p>
<p>
So the point to remember is to never pass any data that, at any time could be set by the user, through <code>Evaluate()</code> and <code>IIF()</code>.
</p>
<p>
And there you go. A couple exploit vectors (as the security guys say) in ColdFusion that you need to be aware of if you're a CF developer.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.bridgew.edu/ruthsarian/2006/10/03/coldfusion-cfquery-and-evaluate/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>CF: cflock</title>
		<link>http://weblog.bridgew.edu/ruthsarian/2005/10/03/cf-cflock/</link>
		<comments>http://weblog.bridgew.edu/ruthsarian/2005/10/03/cf-cflock/#comments</comments>
		<pubDate>Mon, 03 Oct 2005 18:08:39 +0000</pubDate>
		<dc:creator>Ruthsarian</dc:creator>
				<category><![CDATA[ColdFusion]]></category>

		<guid isPermaLink="false">http://weblog.bridgew.edu/ruthsarian/2005/10/03/cf-cflock/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p>CFLOCK is used whenever there&#8217;s a chance that more than one process will try to manipulate a given resource at the same exact time. You might use cflock with a database so that if a read operation occurs during a write, the read operation will have to wait for the write to finish. That way the read operation will have the most up-to-date information possible. Normally you don&#8217;t need to worry about stuff like this. I use this in a room selection application (where students can pick the room they live in for the following year). I cflock an entire block of logic that first checks to see if the room being selected is available and, if it is, record the room selection. This way I don&#8217;t risk someone else selecting the room between the read to check for availability and the write to record the selection. If I didn&#8217;t do that, there&#8217;s a chance rooms could become double-booked.</p>
<p>When working with a file that is used in an application, you almost always use cflock. Now I don&#8217;t mean you cflock whenever someone is uploading a file or you&#8217;re going to read from some random htm file (like a template system) because your application isn&#8217;t going to be altering the file&#8217;s contents (more than once, in the case of writing an uploaded file). But if you&#8217;ve got a file your application is going to read and write to throughout the application&#8217;s life, you need to protect against the chance two writes will occur at the same time. For example, in a user account claim application I have to write usernames to two separate files. One gets picked up by a process that creates e-mail accounts, another that creates LMS accounts. There&#8217;s a chance two people will try to claim an account at the same time. So I&#8217;ll cflock the file during the write, so no other CF process tries to write to the file at the same time. If that happened, you can wind up with a corrupt or empty file. Guestbooks, blog comments, etc are examples of applications where you&#8217;d cflock file access (assuming you&#8217;re using files and not a database).</p>
<p>Now why CFLOCK session variables?</p>
<p>Because you can&#8217;t assume a user will only make one connection at a time. When a user requests a page, the web browser begins to make several simulatneous connections to download the images, CSS files, javascript, etc.. on top of the HTML for the page. If you&#8217;ve got one or more of those files setup as a CFM (a dynamic image, dynamic stylesheet, who knows what) you&#8217;re application.cfm will run as well. If you&#8217;ve got logic in your application.cfm that manipulates session variables, you run the risk of having your session variables being changed in mid-process, creating either corrupted data (less likely) or incorrect data being acted upon (more likely).</p>
<p>For example, let&#8217;s say you&#8217;ve got a voting system. The voting system uses a session variable to set whether or not you&#8217;ve voted. A person logs into this system, makes their vote selection, and double-clicks the &#8220;vote&#8221; button. Your CF server now has 2 separate vote processes from the same person that it will process. The application logic is:</p>
<p>1. Check if allowed to vote<br />
2. Record vote<br />
3. Flag user as having voted</p>
<p>Step 2 is a database operation. In computer time, that step is going to take forever to process. While process 1 is working on step 2, process 2 comes along and passes the check in step 1, and gets in line for step 2. Process 1 moves on to step three and records the user has voted, but only after process 2 has started recording the vote for a second time.</p>
<p>Your user has now voted twice because of a race condidtion with session variables.</p>
<p>To fix this process you can do a couple things. You could put all three steps inside a single cflock block. You could swap steps 2 and 3 and then put steps 1 &amp; 2 (check/record allowed to vote flag) then do your database options. The latter option frees the lock sooner to help keep resources to a minimum and is a potential speed increase but you might lose votes. You could wrap the database operation in a cftry/catch block and reset the flag if needed, but now you&#8217;re getting overly complicated in a system where just wrapping the 3 steps in a cfblock works fine.</p>
<p>So why not wrap every page in a cflock?</p>
<p>Because you will have pages that take a few seconds to process. If a user double-clicks a button, like in the example above, they will have to wait twice as long for the results to be displayed. If they get bored/angry at the wait, they might press that button 10 or 20 more times thinking it&#8217;ll go quicker, when in reality it&#8217;s only slowing things down. At that point, you&#8217;ve got 20+ processes waiting for that lock to open up. CFMX recommends your number of simultaneous processes allowed in CF be 3 or 4 times the number of CPUs in the machine. I&#8217;ll tell you that we have ours set to 12. (3 * 4 (2 P4s, each of which act like 2 separate processors)). So at 20+ processes, each waiting for that lock, each on the stack of running processes, you&#8217;re entire site (or your CF applications at least) grind to a halt. Now every user (not just the one) starts clicking on that button to speed things up. You eventually wind up with a really nasty situation where you&#8217;ve got hundreds (even thousands) of processes in the queue waiting to be processed by CF. Your site becomes unusable for minutes, maybe even hours.</p>
<p>That&#8217;s why you need to be very very efficient in your use of cflock. They can be a source of severe bottlenecking. I have a cf_sleep custom tag that gets CF to hang for a few seconds. I don&#8217;t use it much (if ever) but the way it works is by nesting cflocks on the same resource. Create a page with a 20 second sleep, reload it 20 times, and you&#8217;ll shut down the site for 20+ seconds. Very nasty.</p>
<p>If you can, set yourself up with a performance monitor (this is a Windows thing. start-&gt;run-&gt;perfmon) set on your CF server and you can see this in action yourself. (Assuming you&#8217;re setup, monitoring all the CF related monitors.) Turn on highlighting in your performance monitor (CTRL+H or click the lightbulb icon in the top toolbar). Then select the &#8220;running requests&#8221; monitor from the list in the bottom section of the performance monitor window. The highlighted (white) line you see shows you how many current requests are being processed.</p>
<p>Create a script with a 20 second sleep in it. Load the page then check your performance monitor and you&#8217;ll see that there is 1 process running. Now hold down CTRL+R in your browser for a few seconds. You&#8217;ll get maybe a couple hundred of these processes going. Now check out the performance monitor. The running requests will max out at 12 (which is what I set it to as mentioned earlier). Now check out the &#8220;queued requests&#8221; monitor and see how that spikes up.</p>
<p>The site is essentially useless while you wait for those processes to finish. All this because of 1 user holding down CTRL+R in a browser for a few seconds. That&#8217;s the downside of cflock (and any slow ColdFusion page). Try changing the page so the sleep is only a second and do it again. The server takes a little longer to go unresponsive, and it recovers more quickly. But you start to see how CF can be exploited.</p>
<p>There&#8217;s a configuration option to kill any process that runs over X seconds long in the administrator interface. That offers some protection from prolonged denial-of-service attacks, but not much. (I typically set it to 30 seconds, but 5-10 seconds might be better for most people.)</p>
<p>Increase the number of simultaneous requests? That only prolongs the inevitable and when you hit that max, it takes much longer to recover because your server is doing a lot more than it can handle.</p>
<p>So be careful of bottlenecks in your CF code, CFLOCK being potentially one of the biggest in your application.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.bridgew.edu/ruthsarian/2005/10/03/cf-cflock/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CF: Session Hijacking</title>
		<link>http://weblog.bridgew.edu/ruthsarian/2005/10/03/cf-session-hijacking/</link>
		<comments>http://weblog.bridgew.edu/ruthsarian/2005/10/03/cf-session-hijacking/#comments</comments>
		<pubDate>Mon, 03 Oct 2005 17:55:56 +0000</pubDate>
		<dc:creator>Ruthsarian</dc:creator>
				<category><![CDATA[ColdFusion]]></category>

		<guid isPermaLink="false">http://weblog.bridgew.edu/ruthsarian/2005/10/03/cf-session-hijacking/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p>ColdFusion uses two unique values to keep track of user session information. These values are CFID and CFTOKEN. They are stored as cookies but can also be passed along the URL and inside POST data.</p>
<p>Session variables are a place to store information specific to the user and to the current session (such as whether or not a user is logged in).</p>
<p>It is possible to hijack a user&#8217;s session by supplying the correct CFID and CFTOKEN values to the server, either on the URL, or wherever else you want.</p>
<p>The two numbers combined represent a space of 10^15 numbers. Average brute force will take half that amount, so 10^15/2. Figure 100 attempts per second, and the average time it would take to brute force is in the neighborhood of 150,000 years.</p>
<p>There&#8217;s the 1 in a jillion chance someone might guess a correct CFID and CFTOKEN, but that doesn&#8217;t really worry me much.</p>
<p>Your more likely to see someone hack your application by doing a little packet sniffing (or looking over someone&#8217;s shoulder) and capturing the CFID and CFTOKEN that way.</p>
<p>Packet sniffing you can curb by going over SSL with your application. Over-the-shoulder attacks can be stopped by not passing the CFID and CFTOKEN values on the URL (which CF does with cflocation tag by default&#8230; go figure).</p>
<p>If the user has a virus on their machine passing out their cookies, well that user has a bigger problem than having their session hijacked.</p>
<p>So how do you protect against session hijacking? You store the IP address as a session variable. Compare the IP in the session variable to the user&#8217;s IP (stored in cgi.remote_addr) and if they don&#8217;t match, you&#8217;ve got a hijacking attempt.</p>
<p>&#8230; But there&#8217;s a catch.</p>
<p>AOL, for example, uses a proxy server for their packaged browser. This means everyone comes from the same IP address. Not cool. Now AOL users can simply go into their browser settings and kill the proxy config and they&#8217;ll surf using their own IP, but can you really ask users to do that for every little application we have using session variables?</p>
<p>Also AOL users won&#8217;t be the only ones behind a proxy server.</p>
<p>And if you have session timeouts set to days, dial-up users and any other user on a network with shared IP addresses will eventually get an IP address of a former user. And they might be able to get into the application that way.</p>
<p>So what can you do? Not much. You won&#8217;t ever be 100% certain in your security. It&#8217;s all about managing risk. In this case, you&#8217;re at a fairly low risk with hijacking if you&#8217;re comparing IP addresses.</p>
<p>But here&#8217;s what I do to take it 1 step further.</p>
<p>Combine the user&#8217;s IP address and browser string (cgi.http_user_agent) into a single string. Then MD5 hash the thing. Store that hash as a session variable. Recalculate and compare hashes as the first step in any user request (in other words: put this logic in your application.cfm file, and put it up at the top). And that should protect you well enough. The browser string provides a little extra security in the event of proxy users hitting the site.</p>
<p>Also keep your session timeouts to a low value (30minutes.. 2 hours MAX, unless security isn&#8217;t a big issue for your application).</p>
<p>When you detect a hijack attempt, you might not want to kill the session because the legit user also gets locked out. You can reset CFID and CFTOKEN on the user with the CFCOOKIE tag then redirect the user to the enterance page.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.bridgew.edu/ruthsarian/2005/10/03/cf-session-hijacking/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ColdFusion</title>
		<link>http://weblog.bridgew.edu/ruthsarian/2005/10/03/coldfusion/</link>
		<comments>http://weblog.bridgew.edu/ruthsarian/2005/10/03/coldfusion/#comments</comments>
		<pubDate>Mon, 03 Oct 2005 17:46:39 +0000</pubDate>
		<dc:creator>Ruthsarian</dc:creator>
				<category><![CDATA[ColdFusion]]></category>

		<guid isPermaLink="false">http://weblog.bridgew.edu/ruthsarian/2005/10/03/coldfusion/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written some long-winded e-mails about CF development recently and, rather than see them go to waste, I figured I would put them here and have my blog serve not only CSS matters, but CF as well. I&#8217;ll put these entries into their own category for those who care to follow.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.bridgew.edu/ruthsarian/2005/10/03/coldfusion/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>cf_lensflare</title>
		<link>http://weblog.bridgew.edu/ruthsarian/2005/04/29/cf_lensflare/</link>
		<comments>http://weblog.bridgew.edu/ruthsarian/2005/04/29/cf_lensflare/#comments</comments>
		<pubDate>Fri, 29 Apr 2005 22:32:08 +0000</pubDate>
		<dc:creator>Ruthsarian</dc:creator>
				<category><![CDATA[ColdFusion]]></category>

		<guid isPermaLink="false">http://weblog.bridgew.edu/ruthsarian/2005/04/29/cf_lensflare/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p>A stupid little store to share.</p>
<p>I do a lot of work with Cold Fusion. The other day I hear that Macromedia (who owns ColdFusion) has been bought out by Adobe.</p>
<p>So a friend of mine makes a comment about how one of ColdFusion&#8217;s new features is a lens flare tag that throws up a lens flare on a web page. Lens flare being one of the most (ab)used filters in Adobe Photoshop.</p>
<p>Well I think for a minute, and decide it&#8217;s absolutely doable, and set to work. I created a lens flare on a black background in Photoshop. I then used the color selection tool to select and remove the black color. This left me with a nice <a href="http://webhost.bridgew.edu/etribou/lensflare.png">transparent png flare</a>. Good enough for me, afterall this is just a quick goof, nothing I want to really waste time on.</p>
<p>I then create a quick custom tag in ColdFusion which creates a <code>DIV</code> and absolutely positions it on top of the page in the upper left-hand area via some inline CSS.</p>
<p>The PNG is placed inside the div block. I even threw in the filter line needed to get IE 6 working with transparent PNGs.</p>
<p>Silly stuff.</p>
<p>&lt;!&#8211;<br />
<a href="http://www.bridgew.edu/index.cfm?lensflare=ruth">Here is the result</a>. A fun little easter egg.<br />
&#8211;&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.bridgew.edu/ruthsarian/2005/04/29/cf_lensflare/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
