Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any logical reason to use CFQUERYPARAM in Query of Queries?

I primarily use CFQUERYPARAM to prevent SQL injection. Since Query-of-Queries (QoQ) does not touch the database, is there any logical reason to use CFQUERYPARAM in them? I know that values that do not match the cfsqltype and maxlength will throw an exception, but, these values should already be validated before that and display friendly messages (from a UX viewpoint).

like image 515
Eric Belair Avatar asked Feb 27 '14 16:02

Eric Belair


People also ask

Why use cfqueryparam?

Adobe recommends that you use the cfqueryparam tag within every cfquery tag, to help secure your databases from unauthorized users.

What is Cfqueryparam?

Description. Verifies the data type of a query parameter and, for DBMSs that support bind variables, enables ColdFusion to use bind variables in the SQL statement. Bind variable usage enhances performance when executing a cfquery statement multiple times.

What is query of queries in ColdFusion?

Last updated on 17 Jan 2022 | Also Applies to ColdFusion More. After you have created a recordset with a tag or function, you can retrieve data from the recordset in one or more dependent queries. A query that retrieves data from a recordset is called a Query of Queries.


2 Answers

Since Query-of-Queries (QoQ) does not touch the database, is there any logical reason to use CFQUERYPARAM in them? Actually, it does touch the database, the database that you currently have stored in memory. The data in that database could still theoretically be tampered with via some sort of injection from the user. Does that affect your physical database - no. Does that affect the use of the data within your application - yes.

You did not give any specific details but I would err on the side of caution. If ANY of the data you are using to build your query comes from the client then use cfqueryparam in them. If you can guarantee that none of the elements in your query comes from the client then I think it would be okay to not use the cfqueryparam.

As an aside, using cfqueryparam also helps optimize the query for the database although I'm not sure if that is true for query of queries. It also escapes characters for you like apostrophes.

like image 139
Miguel-F Avatar answered Oct 12 '22 16:10

Miguel-F


Here is a situation where it's simpler, in my opinion.

<cfquery name="NoVisit" dbtype="query">
select chart_no, patient_name, treatment_date, pr, BillingCompareField
from BillingData
where BillingCompareField not in 
(<cfqueryparam cfsqltype="cf_sql_varchar" 
value="#ValueList(FinalData.FinalCompareField)#" list="yes">)
</cfquery>

The alternative would be to use QuotedValueList. However, if anything in that value list contained an apostrophe, cfqueryparam will escape it. Otherwise I would have to.

Edit starts here

Here is another example where not using query parameters causes an error.

QueryAddRow(x,2);
QuerySetCell(x,"dt",CreateDate(2001,1,1),1);
QuerySetCell(x,"dt",CreateDate(2001,1,11),2);
</cfscript>

<cfquery name="y" dbtype="query">
select * from x
<!--- 
where dt in (<cfqueryparam cfsqltype="cf_sql_date" value="#ValueList(x.dt)#" list="yes">) 
--->
where dt in (#ValueList(x.dt)#)
</cfquery>

The code as written throws this error:

Query Of Queries runtime error.  
Comparison exception while executing IN.
Unsupported Type Comparison Exception: 
The IN operator does not support comparison between the following types: 
Left hand side expression type = "DATE".
Right hand side expression type = "LONG".

With the query parameter, commented out above, the code executes successfully.

like image 22
Dan Bracuk Avatar answered Oct 12 '22 17:10

Dan Bracuk