Useful checks to test for XSS attacks on your ColdFusion site

If you have a ColdFusion page that contains a form with text inputs or uses URL params, make sure you are not vulnerable to a XSS attack. I'm quite novice at this myself, but learning more about it recently.

Here are some inputs to try in your forms or URL param values, if they echo the value back to the user after the page submits/reloads. This is often done on forms with server side validation when 1 or more errors are found, you preserve the fields already typed by the user and give them an error message to try again.

FORM INPUTS
"><blink>XSS</blink>
"
><script>alert("XSS")</script><

FORM TEXTAREA
</textarea><script>alert("XSS vulnerability")</script><textarea

URL PARAM VALUES
">
<script>alert("
XSS")<%2Fscript><
"
><img+src%3Dhttp%3A%2F%2Fintercodes.files.wordpress.com%2F2007%2F10%2Fhacked.jpg><"
"
+onmouseover=alert("XSS")+
click%20here%22%20onmouseover=%22javasript:alert(%27XSS%27)%22

The solution is to wrap any value that echos back on the page in HtmlEditFormat(). For example:

#HtmlEditFormat(URL.firstname)#
or
#HtmlEditFormat(Form.company)#

Even Ray Camden's blog.cfc is vulnerable. To see what I mean, follow these steps:

1) Click here to the contact page

2) Enter the following in the Name field:

"><script>alert("XSS")</script><

3) Click the Send Your Comments button

Comments
Jason Dean's Gravatar You're absolutely right about using HTMLEditFormat() to prevent XSS attacks on your site. It is a very good method.

One thing that I always try to remind people of is to make sure you use the HTMLEditFormat() either before you put it into the database or after you retrieve it from the database, but not both. I personally prefer to use it after I retrieve info form the DB, I don;t like messing with my users' input.

Also, in defense of blogCFC, it is not really "vulnerable" to an XSS attack in the contact form, since the result of the contact form are not displayed on the site. Even if you view it in gmail after it is mailed to you, the script is escaped and does nto execute.
# Posted by Jason Dean | 6/25/08 9:42 PM
Amish's Gravatar nice job punking out Ray but too bad your didn't fix it on your own form...it's called open source, check into it.

@Jason

You are right about it not echoing back but still an Adobe Guru should make sure that his stuff is airtight, or he will get bullied out of his milk money at the AdobeMax conference.

I don't mess with #HtmlEditFormat# though your intentions are good. I prefer to pull things out exactly via something along these lines:

<cfloop list="#FORM.FieldNames#" index="i">
<cfscript >
FORM[i] = replace(FORM[i],'<','<','ALL');
FORM[i] = replace(FORM[i],'>','>','ALL');
FORM[i] = replace(FORM[i],'"','"','ALL');
FORM[i] = replace(FORM[i],'''',' ','ALL');
</cfscript >
</cfloop>

no tags = no scripting
# Posted by Amish | 9/9/08 2:06 PM
Einar's Gravatar <!--- Prevent XSS --->
   <cfif IsDefined('FORM.fieldnames')>
      <cfloop list="#FORM.FieldNames#" index="i">
         <cfscript>
         FORM[i] = replace(FORM[i],"<","","ALL");
         FORM[i] = replace(FORM[i],">","","ALL");
         FORM[i] = replace(FORM[i],'"',"","ALL");
         FORM[i] = replace(FORM[i],"'","","ALL");
         </cfscript>
      </cfloop>
   </cfif>
# Posted by Einar | 12/15/09 2:03 AM
Jeremy's Gravatar I use these two functions.

<cfscript>
function NumbersOnly(str) {
      return reReplace(str,"[^[:digit:]]","","all");
   }
      
   function StripXSS(str) {
      str = replace(str,'<','<','ALL');
      str = replace(str,'>','>','ALL');
      str = replace(str,'(','&##x28','ALL');
      str = replace(str,')','&##x29','ALL');
      str = replace(str,'"','"','ALL');
      return replace(str,"'",''','ALL');
   }

someID=NumbersOnly(someID);
someString=StripXSS(someString);
</cfscript>
# Posted by Jeremy | 1/28/10 9:07 AM
Gary's Gravatar You are right about it not echoing back but still an Adobe Guru should make sure that his stuff is airtight, or he will get bullied out of his milk money at the AdobeMax conference. http://www.rapidsloth.com
# Posted by Gary | 3/8/10 4:38 PM
Lunia money's Gravatar <a href="http://www.mmodo.com/product/Lunia_gold.html"... gold</a> in the game you may or may not notice. I know vitality is very tempting in <a href="http://www.mmodo.com/product/Lunia_gold.html"... money</a>.
# Posted by Lunia money | 3/11/10 2:48 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner