我们正在尝试更新我们的经典asp搜索引擎,以防止SQL注入。我们有一个VB 6函数,它通过基于各种搜索参数连接查询来动态构建查询。我们已经将这个转换为一个存储过程,对于除关键字以外的所有参数都使用动态sql。动态SQL - 搜索查询 - 变量关键字数
关键字的问题在于,用户提供了可变数字词,我们希望为每个关键字搜索多个列。由于我们无法为每个关键字创建单独的参数,因此我们如何才能构建安全的查询?
例子:
@CustomerId AS INT
@Keywords AS NVARCHAR(MAX)
@sql = 'SELECT event_name FROM calendar WHERE customer_id = @CustomerId '
--(loop through each keyword passed in and concatenate)
@sql = @sql + 'AND (event_name LIKE ''%' + @Keywords + '%'' OR event_details LIKE ''%' + @Keywords + '%'')'
EXEC sp_executesql @sql N'@CustomerId INT, @CustomerId = @CustomerId
什么是从SQL注入对此进行处理并维持保护的最佳方式?
+1这种做法将避免逃避问题完全 – 2008-09-30 22:36:10