Skip to main content
Inspiring
August 13, 2006
Question

how to stop a hacker putting SQL in to your query strings

  • August 13, 2006
  • 3 replies
  • 386 views
Hi there

i am looking for a way to "validate" data that arrives as part of the query string

for example if i have this..

<cfquery name="blah" datasource="blahblah">
select *
from tbl_blah
where something = #url.var#
</cfquery>

all's well and good until some bright spark edits the url and changes var to be something naughty (yesterday someone passed "drop database" to one of my pages in this way!)

is there a best practice for checking data that arrives as a url variable is of the correct type

thank you very much indeed.

PS currently if the passed data is text i tend to run a load of cfif's in the query

eg
<cfquery name="blah" datasource="blahblah">
select *
from tbl_blah
<cfif isdefined("url.var") and url.sex EQ 'blue'> WHERE var='blue'</cfif>
<cfif isdefined("url.var") and url.varEQ 'red'>WHERE var='red'</cfif>
</cfquery>

which is a bit of a pain - but what if the var is a number from 0 to 1000?! you can't have 1000 cfif statements in every query can you?!

thanks guys
This topic has been closed for replies.

3 replies

Inspiring
August 14, 2006
I tested this to a certain extent and the only way I could successfully inject sql was with a mssql numeric field. Char/varchar fields treated the sql as simple text.

If you are worried about numeric fields and cfqueryparam is inappropriate for your situation, you can always muliply your variable by 1.
Inspiring
August 13, 2006
CFQUERYPARAM is a technique that you should always use. Also always close your expressión between parenthesis, so SQL statements inserted with a ";" will not work.
August 13, 2006
happysailingdude,

Take a look at cfqueryparam. It should do exactly what you want. Googling it will give you plenty of information, but the primary page is below:

http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-b20.htm

An example of usage (for a varchar field) is below.

You could also use #Val()# around your variable to just get out a numeric value if you are dealing with numbers, but I would still recommendusing cfqueryparam. It does real data binding and is a good habit to get into.