Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Protection from SQL Injection with dynamic SQL using SQL 2012 ? Expand / Collapse
Author
Message
Posted Monday, October 1, 2012 7:16 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Saturday, July 19, 2014 9:15 PM
Points: 537, Visits: 1,110
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
Post #1366558
Posted Monday, October 1, 2012 8:18 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 11:31 AM
Points: 42,844, Visits: 35,973
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 2008, MVP
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

Post #1366596
Posted Tuesday, October 2, 2012 2:50 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Saturday, July 19, 2014 9:15 PM
Points: 537, Visits: 1,110
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
Post #1367296
Posted Tuesday, October 2, 2012 3:44 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 11:31 AM
Points: 42,844, Visits: 35,973
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 2008, MVP
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

Post #1367311
Posted Tuesday, October 2, 2012 4:13 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Saturday, July 19, 2014 9:15 PM
Points: 537, Visits: 1,110
When i use static SQL it is 800% slower, that is why i am using dynamic SQL.
Post #1367321
Posted Wednesday, October 3, 2012 1:04 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 11:31 AM
Points: 42,844, Visits: 35,973
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 2008, MVP
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

Post #1367421
Posted Wednesday, October 3, 2012 1:11 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:53 AM
Points: 6,318, Visits: 13,622
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"
Post #1367425
Posted Wednesday, October 3, 2012 1:26 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 1:04 AM
Points: 1,202, Visits: 1,671
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


Post #1367435
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse