Protection from SQL Injection with dynamic SQL using SQL 2012 ?

  • Instead of this: --exec (@Query);

    I am using this:

    DECLARE @QueryCondition nVARCHAR(4000)

    DECLARE @ParmDefinition1 nVARCHAR(4000);

    SET @ParmDefinition1 = N'@fromtoRec nvarchar';

    DECLARE @ParmDefinition2 nVARCHAR(4000);

    SET @ParmDefinition2 = N'@toRec nvarchar';

    EXECUTE sp_executesql

    @Query,

    @ParmDefinition1,

    @ParmDefinition2;

    Is that enough to protect the dynamic sql, or do i need to do more?

    Thanks

  • Not enough information. Need to see what and how you set @Query.

    Also, the parameter definitions would be one variable, not two, and you're not setting the parameters to anything.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Here is the whole SP.

    Thanks

    USE [JobPortalIAN]

    GO

    /****** Object: StoredProcedure [dbo].[GetAllResumesSearchedDynamicQuery] Script Date: 10/2/2012 4:40:05 PM ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    -- =============================================

    --

    -- =============================================

    ALTER PROCEDURE [dbo].[GetAllResumesSearchedDynamicQuery]

    -- Add the parameters for the stored procedure here

    @fromRec int,

    @toRec int

    AS

    Declare @Date int

    set @Date = -365;

    Declare @Salarytype varchar(10)

    set @Salarytype = 'year';

    DECLARE @TotalRecord INT

    declare @Query as nvarchar(4000)

    declare @QueryForCount as varchar(8000)

    DECLARE @QueryCondition nVARCHAR(4000)

    DECLARE @ParmDefinition1 nVARCHAR(4000);

    SET @ParmDefinition1 = N'@fromtoRec nvarchar';

    DECLARE @ParmDefinition2 nVARCHAR(4000);

    SET @ParmDefinition2 = N'@toRec nvarchar';

    SET @QueryCondition =''

    SET @QueryForCount = ''

    set @Query = '

    WITH CTE ( Id,rwagefreq,recentjobtitle,Position,recentwage,regionid,cityid,countryid,Industry1id,Industry2id,Industry3id,Industry4id,Industry5id,Industry1,Industry2,Industry3,Industry4,Industry5,

    Desiredempid1,Desiredempid2,Desiredempid3,Desiredempid4,Desiredempid5,Jobtype1,Jobtype2,Jobtype3,Jobtype4,Jobtype5,TotalYears,Militaryid,

    Securityid,militaryname,securityname,Degree1id,Degree2id,Degree3id,degree,InputDate,

    experience,HomePhone,CellPhone,WorkPhone,PrimaryEmailAddress,SecondaryEmailAddress,City,State,newdate,UserName,RowNumber)

    AS

    (

    select

    p.idnew as Id,

    p.rwagefreq,

    p.recentjobtitle,

    p.recentjobtitle as Position,

    p.recentwage,

    p.regionid,

    p.cityid,

    p.countryid,

    p.Industry1id,

    p.Industry2id,

    p.Industry3id,

    p.Industry4id,

    p.Industry5id,

    (select i.Name from Industries i where i.Id = p.Industry1id) as Industry1,

    (select i.Name from Industries i where i.Id = p.Industry2id) as Industry2,

    (select i.Name from Industries i where i.Id = p.Industry3id) as Industry3,

    (select i.Name from Industries i where i.Id = p.Industry4id) as Industry4,

    (select i.Name from Industries i where i.Id = p.Industry5id) as Industry5,

    p.Desiredempid1,

    p.Desiredempid2,

    p.Desiredempid3,

    p.Desiredempid4,

    p.Desiredempid5,

    (select n.Name from NewJobTypes n where n.Id = p.Desiredempid1) as Jobtype1,

    (select n.Name from NewJobTypes n where n.Id = p.Desiredempid2) as Jobtype2,

    (select n.Name from NewJobTypes n where n.Id = p.Desiredempid3) as Jobtype3,

    (select n.Name from NewJobTypes n where n.Id = p.Desiredempid4) as Jobtype4,

    (select n.Name from NewJobTypes n where n.Id = p.Desiredempid5) as Jobtype5,

    p.TotalYears,

    p.Militaryid,

    p.Securityid,

    (select m.Name from MilitaryExperienceTypes m where m.Id = p.Militaryid) as militaryname,

    (select s.Name from SecurityClearances s where s.Id = p.Securityid) as securityname,

    p.Degree1id,

    p.Degree2id,

    p.Degree3id,

    case

    when degree3id > degree2id then (select d.Name from AcademicExperienceTypes d where p.Degree3id=d.Id)

    when degree3id < degree2id then (select d.Name from AcademicExperienceTypes d where p.Degree2id=d.Id)

    when degree2id > degree1id then (select d.Name from AcademicExperienceTypes d where p.Degree2id=d.Id)

    else (select d.name from AcademicExperienceTypes d where p.Degree1id=d.id)

    end

    as degree,

    p.InputDate,

    p.TotalYrsExp as experience,

    p.HomePhone,

    p.CellPhone,

    p.WorkPhone,

    p.PrimaryEmailAddress,

    p.SecondaryEmailAddress,

    (select c.name from Cities c where p.cityid = c.Id) as City,

    (select r.abbreviatedname from regions r where p.regionid = r.Id) as State,

    substring(cast(InputDate as varchar(20)),1,12) as newdate,

    isnull(p.FirstName,'''') + '' '' + isnull(p.MiddleName + '' '','''') + isnull(p.LastName,'''') as UserName,

    ROW_NUMBER() OVER (ORDER BY (select 1)) AS RowNumber

    from profiles p With (nolock)

    where p.allowrecruiters=1 '

    if (@Date <> 0)

    Begin

    set @Query = @Query + ' and p.InputDate >''' + cast(((select dateadd(d,@Date,GETDATE()))) as varchar(20)) + ''''

    end

    if (@Salarytype <> '')

    Begin

    set @Query = @Query + ' and p.rwagefreq = ''' + cast(@Salarytype as varchar(10)) + ''''

    end

    SET @Query = @Query + @QueryCondition +' )

    SELECT top 4000 (SELECT Max(RowNumber) FROM CTE ) AS TotalCount ,*, isnull(City,'''')

    + '', '' + isnull(State,'''') as Location FROM CTE WHERE RowNumber BETWEEN '

    + Convert(varchar(100),@fromRec)+ ' AND ' + Convert(varchar(100),@toRec) + ' OpTION( Maxdop 2) '

    --exec (@Query);

    EXECUTE sp_executesql @Query,

    @ParmDefinition1,

    @ParmDefinition2;

    SET STATISTICS TIME off

  • Why are you using dynamic SQL at all? Looks like that could just be two normal SQL statements, no dynamic necessary.

    Also, you don't have the sp_executeSQL call correct. There are three sections to the parameters for that sp, the query, the parameter definitions and the passing of values to the parameters. You're missing the third section.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • When i use static SQL it is 800% slower, that is why i am using dynamic SQL.

  • There is nothing about dynamic SQL that is intrinsically faster, so rather figure out why the normal version is slow than add complexity, additional difficulty in writing/changing and potential security problems.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • To help combat sql injection ensure you assign only the required privileges to your accounts that access the database.

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • If dynamic SQL fixes the slowness issue, then it's due to paramater sniffing - most likely due to having default values specified for the stored proc parameters.

    To fix, declare new parameters within the stored procedure, then assign the stored proc parameters to the newly declared inner parameters.

    The optimizer will then realize that those defaults aren't constant and should fix the bad plan.

    Read this: http://www.sommarskog.se/query-plan-mysteries.html#compileps

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply