SQL injection is a common attack performed against websites that allows the attacker to execute commands on your SQL database. By injecting malicious SQL code into queries, attackers can gain unauthorized access to sensitive information, manipulate data, or even delete it entirely. Protecting your website from SQL injection is crucial for maintaining the integrity and security of your data.
Step-by-step guides in this article:
Enforce Expected Data Type
Every variable used within a <cfquery>
tag needs to be validated to ensure it meets the expected data type. For example, if a column expects an integer, you should validate that the data type matches an integer and nothing else. If only some of the variables in the <cfquery>
are protected, then the query will still be open to SQL injection. So make sure every variable makes use of the <cfqueryparam>
.
Below is an example of a query that is open to SQL injection:
<cfquery name="getUser" datasource="myDSN"> SELECT * FROM users WHERE username = "#username#"> </cfquery>
To secure this query, we can wrap the username variable in <cfqueryparam>
like in the example below:
<cfquery name="getUser" datasource="myDSN"> SELECT * FROM users WHERE username = <cfqueryparam value="#username#" cfsqltype="varchar"> </cfquery>
This ensures that the username
variable matches the expected data type of varchar and does not contain anything that can be used for malicious intent.
See ColdFusion's documentation for more information on this tag, or if you are using Lucee, see the Lucee documentation instead.
Extra Security via FuseGuard
FuseGuard is a Web Application Firewall (WAF) that can protect your website from SQL injection attacks, as well as many other types of attacks. This is a very popular product due to not only the huge list of attack vectors it protects against, but also because of the fact it is built in CFML and fully customizable.
You can view and obtain FuseGuard from our website, or through Foundeo's website, which develops and maintains this product.
It is important to note that while FuseGuard will help protect from SQL injections, securing your code using the <cfqueryparam>
method mentioned above is still recommended.
Extra Security via Cloudflare
While Cloudflare is not ColdFusion-specific like FuseGuard, it still provides a Web Application Firewall (WAF) that can help protect your website from SQL injection and other attacks. You can also add additional rules to Cloudflare's WAF for certain keywords or patterns in request body that are commonly performed in SQL injection attacks. In addition to the security features that Cloudflare offers, you can also take advantage of their Content Delivery Network (CDN), which can help speed up your website by caching assets like CSS, JavaScript, and other static assets across a wide range of servers around the world, speeding up the delivery of those assets.
It's important to note that while Cloudflare will help protect from SQL injections, securing your code using the <cfqueryparam>
method mentioned above is still recommended.
Cloudflare can be used at the same time as FuseGuard if you are interested in using both to secure your website.
If you're encountering any issues that you would like our team to help investigate, we'd be happy to assist you via our SysOps Support. Let us know how we can help!