Allowing a user to pass table name and column name whilst preventing SQL injection

后端 未结 1 1316
梦毁少年i
梦毁少年i 2020-12-20 08:48

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

相关标签:
1条回答
  • 2020-12-20 09:15

    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.

    0 讨论(0)
提交回复
热议问题