How to use sp_executesql to avoid SQL Injection

前端 未结 2 1381
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-22 03:17

In the below sample code, Table Name is an input parameter. In this case, how can I avoid SQL injection using sp_executesql. Below is the sample code, I am trying t

2条回答
  •  不要未来只要你来
    2021-01-22 04:06

    You could first check if the parameter value is indeed a table name:

    ALTER PROC Test @param1  NVARCHAR(50), 
                 @param2  INT, 
                 @tblname NVARCHAR(100) 
    AS 
    BEGIN 
      DECLARE @sql NVARCHAR(1000) 
    
      IF EXISTS(SELECT 1 FROM sys.objects WHERE type = 'u' AND name = @tblname)
      BEGIN
          SET @sql= N'  select * from ' + @tblname 
                    + ' where name= @param1 and id= @param2'; 
    
          PRINT @sql 
    
          EXEC Sp_executesql 
            @sql, 
            N'@param1 nvarchar(50), @param2 int', 
            @param1, 
            @param2; 
      END
    END 
    

    If the passed value is not a table name your procedure won't do anything; or you could change it to throw an error. This way you're safe if the parameter contains a query.

提交回复
热议问题