SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Protection from SQL Injection with dynamic SQL using SQL 2012 ?


Protection from SQL Injection with dynamic SQL using SQL 2012 ?

Author
Message
isuckatsql
isuckatsql
SSC Eights!
SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)

Group: General Forum Members
Points: 875 Visits: 1110
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87487 Visits: 45272
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


isuckatsql
isuckatsql
SSC Eights!
SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)

Group: General Forum Members
Points: 875 Visits: 1110
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87487 Visits: 45272
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


isuckatsql
isuckatsql
SSC Eights!
SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)SSC Eights! (875 reputation)

Group: General Forum Members
Points: 875 Visits: 1110
When i use static SQL it is 800% slower, that is why i am using dynamic SQL.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87487 Visits: 45272
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


Perry Whittle
Perry Whittle
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20004 Visits: 17243
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" ;-)
Andrew G
Andrew G
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2487 Visits: 2254
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search