简体   繁体   English

Oracle的DBMS_ASSERT的Sql Server等效项是什么?

[英]What is the Sql Server equivalent for Oracle's DBMS_ASSERT?

DBMS_ASSERT is one of the keys to prevent SQL injection attacks in Oracle. DBMS_ASSERT是防止Oracle中的SQL注入攻击的关键之一。 I tried a cursory search...is there any SQL Server 2005/2008 equivalent for this functionality? 我尝试过粗略的搜索...此功能是否有等效的SQL Server 2005/2008?

I am looking for a specific implementation that has a counterpart of all the respective Oracle package members of DBMS_ASSERT. 我正在寻找一个与DBMS_ASSERT的所有相应Oracle软件包成员相对应的特定实现。

  • NOOP NOOP
  • SIMPLE_SQL_NAME SIMPLE_SQL_NAME
  • QUALIFIED_SQL_NAME QUALIFIED_SQL_NAME
  • SCHEMA_NAME SCHEMA_NAME

I know the best-practices of preventing injection...bind variables...being one of them. 我知道防止注入的最佳实践...绑定变量...是其中之一。
But,in this question I am specifically looking for a good way to sanitize input...in scenarios where bind-variables were not used. 但是,在这个问题上,我特别希望在不使用绑定变量的情况下,对输入进行清理的好方法。

Do you have any specific implemetations? 您有什么具体实现吗?
Is there a library that actually is a SQL Server Port of the Oracle package? 是否有一个实际上是Oracle软件包的SQL Server端口的库?

Don't do dynamic queries by building strings and the EXECuting them. 不要通过构建字符串并执行它们来执行动态查询。

Use sp_executesql and pass parameters as parameters. 使用sp_executesql并将参数作为参数传递。

You'll find that sql injection is no more. 您会发现不再需要sql注入。

EDIT : sorry, I was in a hurry and wrote the wrong command. 编辑 :对不起,我急着写了错误的命令。 it's not sp_execute, it's sp_executesql; 它不是sp_execute,它是sp_executesql; it takes a string and a set of parameters: all the encoding and escaping of the parameters is done by SQL Server. 它需要一个字符串和一组参数:参数的所有编码和转义均由SQL Server完成。

EDIT2 : sp_executesql statement explaination EDIT2sp_executesql语句说明

The closest thing I have is TSQLAssert for TSQLMacro but it only supports TSQL Stored Procedures. 我拥有的最接近的东西是TSQLMacro的TSQLAssert,但它仅支持TSQL存储过程。 It's free. 免费。

TSQLAssert is an assertion framework built on top of TSQLMacro. TSQLAssert是在TSQLMacro之上构建的断言框架。 It is intended to provide debug-time assertion failures similar to assertions in languages like C++ -- with an additional logging component not found in those languages. 它旨在提供类似于C ++这样的语言中的断言的调试时断言失败-以及在这些语言中找不到的其他日志记录组件。 TSQLAssert can be used only within stored procedures and triggers -- unfortunately, user-defined functions and views do not support many of the keywords that allow it to work. TSQLAssert只能在存储过程和触发器中使用-不幸的是,用户定义的函数和视图不支持许多允许它工作的关键字。

There is no magic "prevent injection" command. 没有神奇的“预防注射”命令。 The methodology is a combination of: 该方法是以下各项的组合:

  1. Using parameterized queries to ensure type safety 使用参数化查询以确保类型安全
  2. Sanitize inputs before passing them to the database layer 在将输入传递到数据库层之前对其进行消毒
  3. When you can't do 1. and 2., replace ' with '' in all input, and other sanitize methods before blindly executing dynamic SQL. 当您无法执行1.和2.时,请在盲目执行动态SQL之前,在所有输入中以及其他清理方法中将'替换为'。

As such there is no equivalent of DBMS_ASSERT in SQL SERVER. 因此,SQL SERVER中没有等效的DBMS_ASSERT。

However enhancing the answer of Aaron Bertrand by this link SQL Injection 但是通过此链接SQL Injection增强了Aaron Bertrand的答案

The only likely option you have is QUOTENAME which is used to escape object names (and thus may be an equivalent for SIMPLE_SQL_NAME or ENQUOTE_NAME and possibly others. So table names (providing they are not qualified with owner or database) and column names can be escaped. 您唯一可能的选择是QUOTENAME ,用于转义对象名称(因此可能与SIMPLE_SQL_NAMEENQUOTE_NAME等价,并且可能与其他名称等效。因此,可以对表名( ENQUOTE_NAME它们不具有所有者或数据库限定)和列名进行转义。

There isn't a mechanism for fully qualifying an object (eg, turning table 'bob' into 'database.owner.bob'), so you'd have to put this together manually, optionally using QUOTENAME to escape the values, eg: 没有完全限定对象的机制(例如,将表'bob'转换为'database.owner.bob'),因此您必须手动将其放在一起,可以选择使用QUOTENAME来转义值,例如:

QUOTENAME(@database) + '.' + QUOTENAME(@owner) + '.' + QUOTENAME(@tableName)

If the object is in the existing database, then you could use DB_NAME(), and assume that the owner's going to be passed in as a variable: 如果对象在现有数据库中,则可以使用DB_NAME(),并假定所有者将作为变量传递:

DB_NAME() + '.' + QUOTENAME(@owner) + '.' + QUOTENAME(@tablename)

In a really convoluted way, you can get owner out as well: 以一种令人费解的方式,您还可以让所有者离开:

USER_NAME(OBJECTPROPERTY(OBJECT_ID(@tablename), 'ownerid')))

Yes I realise all of these may be considered workarounds, but they are options. 是的,我意识到所有这些都可以视为解决方法,但是它们是可选项。

However for escaping values you really are on your own: there is no built-in SQL Server equivalent, so would be all manual string manipulation. 但是,对于转义值,您实际上是自己一个人:没有内置的SQL Server等效项,因此所有手动字符串操作也将如此。 You might be able to create a UDF to sit in place to do this, although if you're going to that effort, it's probably also worth looking at rewriting the sproc using SQL Server sp_ExecuteSQL semantics. 您也许可以创建一个UDF来执行此操作,尽管如果要这样做,可能还值得一看的是使用SQL Server sp_ExecuteSQL语义重写存储过程。

I also was once looking for something similar to DBMS_ASSERT for Sql Server, but to no avail. 我也曾经在寻找类似于DBMS_ASSERT的Sql Server,但无济于事。 So I ended up writing a collection of PROCs that we needed. 因此,我最终编写了我们需要的一系列PROC。

Microsoft should really ship something similar, but till then, you're on your own. 微软确实应该发布类似的东西,但是在那之前,您是一个人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM