【问题标题】:SQLInjection against CosmosDB in an Azure functionAzure 函数中针对 CosmosDB 的 SQL 注入
【发布时间】:2018-06-30 21:54:41
【问题描述】:

我已经实现了一个由 HttpRequest 触发的 Azure 函数。名为name 的参数作为HttpRequest 的一部分传递。在 Integration 部分,我使用以下查询从 CosmosDB 检索数据(作为输入):

SELECT * FROM c.my_collection pm 
WHERE
Contains(pm.first_name,{name}) 

如您所见,我发送的“名称”没有sanitizing。这里有SQLInjection 的问题吗?

我搜索并注意到parameterization 可用,但这不是我可以在这里做的任何事情。

【问题讨论】:

    标签: azure sql-injection azure-cosmosdb azure-functions


    【解决方案1】:

    当绑定发生时 (the data from the HTTP Trigger gets sent to the Cosmos DB Input bind),它会通过一个 SQLParameterCollection 来处理清理。

    请查看this article:

    参数化 SQL 提供对用户输入的稳健处理和转义,防止通过“SQL 注入”意外暴露数据

    这将涵盖通过name 属性注入 SQL 的任何尝试。

    【讨论】:

    【解决方案2】:

    如果您使用的是 Microsoft.Azure.Cosmos 而不是 Microsoft.Azure.Documents:

    public class MyContainerDbService : IMyContainerDbService
    {
        private Container _container;
    
        public MyContainerDbService(CosmosClient dbClient)
        {
            this._container = dbClient.GetContainer("MyDatabaseId", "MyContainerId");
        }
    
        public async Task<IEnumerable<MyEntry>> GetMyEntriesAsync(string queryString, Dictionary<string, object> parameters)
        {
            if ((parameters?.Count ?? 0) < 1)
            {
                throw new ArgumentException("Parameters are required to prevent SQL injection.");
            }
            var queryDef = new QueryDefinition(queryString);
            foreach(var parm in parameters)
            {
                queryDef.WithParameter(parm.Key, parm.Value);
            }
            var query = this._container.GetItemQueryIterator<MyEntry>(queryDef);
            List<MyEntry> results = new List<MyEntry>();
            while (query.HasMoreResults)
            {
                var response = await query.ReadNextAsync();
                results.AddRange(response.ToList());
            }
    
            return results;
        }
    }
    

    【讨论】:

    • 谢谢。很难找到有关 QueryDefinition 参数的信息。
    猜你喜欢
    • 2012-03-22
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多