请问如下所述创建任何安全问题或SQL注入类似的声明。而且,如果是,如何降低风险?
下面的脚本将在存储过程中被执行;什么是使用like
查询推荐的方法吗?
此代码段由参数化存储过程复制。
declare @args varchar(100) ='SID%' -- start with
-- set @args ='SID01' -- exact match
-- set @args ='%%' -- all
-- set @args = '%VV%' -- contains
-- set @args ='SID%' -- start with
-- set @args ='%JUN' -- end with
select * from Employee e where e.LastName like @args
如果您使用类型变量在你的SQL参数你是从SQL注入攻击安全。 SQL注入的工作原理,当攻击者能够访问动态SQL。
这是不安全的:
DECLARE @sql nvarchar(50)=N'select * from Employee e where e.LastName like '''+@args+''''
EXEC sys.sp_executesql @sql
这是安全的:
select * from Employee e where e.LastName like @args