I have an (intranet) web page (ASP.NET C#) which allows users to create a table on a specific db in SQL-Server, table name and column name/s are free text fields and the act
This is what QUOTENAME() was created to solve. You pass in your column and table names as parameters in to QUOTENAME()
and then you use the output of it to represent objecs in your database in a dynamic sql query.
//The evil name tries to expliot code like:
// set @sql = N'CREATE TABLE [' + @tablename + N'] (Foo int)'
var evilName = "someName] (Foo int); Drop table students --";
var query = @"
declare @sql as nvarchar(max)
set @sql = N'CREATE TABLE ' + QUOTENAME(@tablename) + N' (Foo int)'
exec sp_executesql @sql
";
using(var connection = new SqlConnection(ConnectionString))
using(var command = new SqlCommand(query, connection))
{
command.Parameters.Add("@tablename", SqlDbType.NVarChar, 128).Value = evilName ;
connection.Open();
command.ExecuteNonQuery();
}
The query that will be executed on the server will be
CREATE TABLE [someName]] (Foo int); Drop table students --] (Foo int)
which creates a table with a valid table name and does not drop my other table.