I’ve been researching a security report that was generated for an ASP.NET WebForms application. Some areas in our code have been noted to be potential SQL injection vulnerabilities. I have been looking into these areas in our codebase and they all appear to be where we are importing excel workbooks. The offending code seems to be when we are trying to create an OleDbCommand based on the name of the first sheet in the workbook. We are making a connection to the database (excel sheet) using an OleDbConnection object, which reads the database schema to find the name of the first sheet. We then dynamically construct a SQL command using that name. Example code might look like this:
String connectionString = String.Format(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" +
"Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1\"", sFullPath);
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
DataTable dbSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string firstSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();
using (OleDbCommand command = new OleDbCommand("SELECT * FROM [" + firstSheetName+ "]", conn)) // Offending line of code
In an effort to remediate this SQL Injection vulnerability I have been looking into alternatives to the above code. Does anyone have any ideas how I might perform this without being flagged as a SQL Injection vulnerability? I do not want to use the ActiveX office objects to perform this operation.