Copy link to clipboard
Copied
Hello,
Here is my query
<cfquery name="myQuery" datasource="myDataSource">
SELECT *
FROM Table
WHERE myDate BETWEEN ThisDate AND ThatDate
</cfQuery>
I wonder if I need to use <CFQUERYPARAM> at WHERE to avoid SQL Injection please?
Thanks for advice
Hung Pham
...hello,
I am not sure it would work because just like myDate: thisDate and thatDate are column name of myTable. They are not input data.
The code below gives me an error
<cfquery name="myQuery" datasource="myDataSource"> SELECT * FROM Table WHERE sysDate BETWEEN <cfqueryparam value ="thisDate" cfsqltype="CF_SQL_DATE"> AND <cfqueryparam value ="thatDate" cfsqltype="CF_SQL_DATE"> <cfquery> OR <cfquery name="myQuery" datasource="myDataSource"> SELECT * FROM Table WHERE sysDate BETWEEN <cfquery
Copy link to clipboard
Copied
It's Oracle as back-end
Thank you
Copy link to clipboard
Copied
Hello,
Here is my query
<cfquery name="myQuery" datasource="myDataSource">
SELECT *
FROM Table
WHERE myDate BETWEEN ThisDate AND ThatDate
</cfQuery>
I wonder if I need to use <CFQUERYPARAM> at WHERE to avoid SQL Injection please?
Hung Pham
By @pham_mn
Yes, definitely. You would then be doing something like
<cfset thisDate=createdate(2021,1,1)>
<cfset thatDate=createdate(2021,12,31)>
<cfquery name="myQuery" datasource="myDSN">
SELECT *
FROM myTable
WHERE myDate BETWEEN <CFQUERYPARAM VALUE="#thisDate#" CFSQLType="CF_SQL_Date">
AND <CFQUERYPARAM VALUE="#thatDate#" CFSQLType="CF_SQL_Date">
</cfQuery>
<!---
<cfdump var="#myQuery#" >
--->
Especially if you don't know what the input is or where it originates from. Anyway, it is best to make a habit of always using cfqueryparam in this way. Remember that even data from your own database might contain injection code. Some other innocuous process might have saved it beforehand.
Not only is cfqueryparam safer, it is more efficient too. It uses data-binding, improving performance.
Copy link to clipboard
Copied
Thank you very much for your codes. And I have another question that relates to SQL Injections. Well... I am new hired and assigned to add <CFQUERYPARAM> to about 1500 lines of code (from diff cfm and cfc files) that missing<CFQUERYPARAM>. This could take weeks or even months to add and could cause some problems like typo or could be anything.
I think I should test one file at a time, but I am new hire and don't know what page I should test
I wonder if you guys have any method to approach the issue
Thank you very much
Copy link to clipboard
Copied
I would not try to automate this. Correcting 1500 lines of code is a small, at most medium-sized, project. So, for maximum accuracy, I would add the cfqueryparams the old-fashioned way: manually. That is because you have to know the CF_SQL of each input. Also, it follows from this that I suggest that you test one page or function call at a time.
Copy link to clipboard
Copied
Thank you so very much for your time and advice.
Copy link to clipboard
Copied
hello,
I am not sure it would work because just like myDate: thisDate and thatDate are column name of myTable. They are not input data.
The code below gives me an error
<cfquery name="myQuery" datasource="myDataSource">
SELECT *
FROM Table
WHERE sysDate BETWEEN <cfqueryparam value ="thisDate" cfsqltype="CF_SQL_DATE"> AND
<cfqueryparam value ="thatDate" cfsqltype="CF_SQL_DATE">
<cfquery>
OR
<cfquery name="myQuery" datasource="myDataSource">
SELECT *
FROM Table
WHERE sysDate BETWEEN <cfqueryparam value ="#thisDate#" cfsqltype="CF_SQL_DATE"> AND
<cfqueryparam value ="#thatDate#" cfsqltype="CF_SQL_DATE">
<cfquery>
Copy link to clipboard
Copied
It should be SysDate instead of myDate
Thank you
Copy link to clipboard
Copied
This is orginal codes, and it works fine
<cfquery name="qryFacAreaList" datasource="#application.dsn#">
SELECT *
FROM AREA_DIM
WHERE 1 = <cfqueryparam value="1" cfsqltype="CF_SQL_NUMERIC">
AND SYSDATE BETWEEN ACTIVE_DATE AND INACTIVE_DATE
ORDER BY area_name
</cfquery>
Copy link to clipboard
Copied
hello,
I am not sure it would work because just like myDate: thisDate and thatDate are column name of myTable. They are not input data.
The code below gives me an error
<cfquery name="myQuery" datasource="myDataSource"> SELECT * FROM Table WHERE sysDate BETWEEN <cfqueryparam value ="thisDate" cfsqltype="CF_SQL_DATE"> AND <cfqueryparam value ="thatDate" cfsqltype="CF_SQL_DATE"> <cfquery> OR <cfquery name="myQuery" datasource="myDataSource"> SELECT * FROM Table WHERE sysDate BETWEEN <cfqueryparam value ="#thisDate#" cfsqltype="CF_SQL_DATE"> AND <cfqueryparam value ="#thatDate#" cfsqltype="CF_SQL_DATE"> <cfquery>
By @pham_mn
cfqueryparam is for input data. If thisDate and thatDate are column names representing date values, and you trust the values, then you require
<cfquery name="myQuery" datasource="myDataSource">
SELECT *
FROM Table
WHERE sysDate BETWEEN thisDate AND thatDate
<cfquery>
Copy link to clipboard
Copied
cfqueryparam is for input data is the key word. Thank you so much
Copy link to clipboard
Copied
Hung Pham, you asked about automating this task (of finding and fixing potential sql injection coding issues). BKBK argued against it, saying yours was a small enough task to do it manually, which he even encouraged.
But I think it's appropriate to mention that there are at least TWO tools that can help with this very process of finding and fixing such CFML security coding issues like sql injection:
Hope that's helpful.
Copy link to clipboard
Copied
I do encourage the use of tools to assist in a task. I take that for granted. Tools help speed up our work, for sure. For example, you can use the tools that Charlie mentions or the search facilities in a text editor or IDE to locate the places where cfqueryparam may be required.
However, as far as I know, these tools are not yet fully machine-intelligent. For example, a tool might not be able to tell a decimal from a float, a numeric from a double or a varchar from an nvarchar.
My suggestion remains. If your project is relatively small, you should - even if you use a tool - manually check each cfqueryparam that you add.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more