避免Azure DocumentDB存储过程中的SQL注入

时间:2014-11-23 13:08:22

标签: sql-injection azure-cosmosdb

如何避免sql注入Azure DocumentDB存储过程?

除了清理输入(列入白名单的字符)之外,这里的最佳做法是什么?

例如,以下从MSDN示例改编的存储过程:

function simple_sp(s1) {
   var context = getContext();
   var collection = context.getCollection();
   var response = context.getResponse();

   collection.queryDocuments(collection.getSelfLink(), 
      'SELECT * FROM Families f where f.id  = "' + s1 + '"', {}, 
      function(res){}
   );
}

s1参数是将sql注入查询的标准示例。到目前为止,我还没有找到一种参数化查询的方法。

2 个答案:

答案 0 :(得分:10)

<强>更新

很高兴地说,截至2015年1月14日 - DocumentDB 支持SQL参数化。已经在.NET,Java,Node.js和Python SDK以及REST API中添加了支持。享受=)

以下是使用.NET SDK的示例:

IQueryable<Book> queryable = client.CreateDocumentQuery<Book>(collectionSelfLink, new SqlQuerySpec { 
                    QueryText = "SELECT * FROM books b WHERE (b.Author.Name = @name)", 
                    Parameters = new SqlParameterCollection()  { 
                          new SqlParameter("@name", "Herman Melville") 
                     } 
});

原始答案

DocumentDB不支持SQL参数化尚未 ...因此您需要清理输入以避免无意中暴露读取数据(例如,对于多租户应用程序)。

话虽如此...... DocumentDB SQL注入攻击表面区域相当有限 - 因为DocumentDB SQL仅支持只读查询。换句话说,您不必担心DocumentDB和SQL Injection的上下文中的无意写入/更新/删除。

答案 1 :(得分:1)

要回答适用于存储过程JavaScript文件的问题,

function simple_sp(s1) {
   var context = getContext();
   var collection = context.getCollection();
   var response = context.getResponse();

   var query = { query: "select * from Families f where f.id = @id", parameters: [{ name: "@id", value: id }] };

   collection.queryDocuments(collection.getSelfLink(), 
      query, {}, 
      function(res){}
   );
}