Fixing SQL injection in ASP and Oracle

Note: This post is part of our series on “How to Fix SQL Injection Vulnerabilities“. The series contains examples on how to fix SQL Injection Vulnerabilities in various programming languages.

An SQL Injection attack is a code injection attack when input from an attacker reaches one of your databases without any filteration or validation. As a result, a malicious user can execute Read / Write / Delete / Update query in your database. In addition to this he can also run system level commands. The following example shows how to prevent a malicious input in ASP by filtering user input before it is passed to Oracle.

Parameterized query

Dim cmd, rs

Response.Write "Return employees for department " & dept & ".<br />"
Set cmd = Server.CreateObject ("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = "SELECT * FROM emp WHERE dept = ? ORDER BY empno"
cmd.CommandType = adCmdText

'name, type, direction, size, value
cmd.Parameters.Append cmd.CreateParameter ("dept", adInteger, adParamInput, , CInt(dept))

Set rs = cmd.Execute