IQueryable LINQ 提供程序和 SQL 注入?

发布于 2024-12-05 11:12:50 字数 390 浏览 1 评论 0原文

我正在开发一个 LINQ 提供程序,它使用 IQ Toolkit 将 LINQ 查询转换为 SQL 查询。 IQ Toolkit 提供的类是否可以免受 SQL 注入攻击?如果没有,假设我正在使用 IQ Toolkit 并实现我自己的 LINQ 提供程序,我必须采取哪些措施来防止 SQL 注入攻击。我阅读了 LINQ to SQL 使用 SqlParameter, 但我仍然不清楚需要使用 SqlParameter 做什么来防止 SQL 注入。

I'm working on a LINQ provider that uses the IQ Toolkit to tranlate LINQ queries to SQL queries. Are the classes provided by the IQ Toolkit safe from SQL injection attacks? If not, what I have to do to protect against SQL injection attacks, supposing that I'm using the IQ Toolkit and implementing my own LINQ provider. I read the LINQ to SQL uses SqlParameter,
but it's still not clear to me what needs to be done with SqlParameter to protect against SQL injection.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人哭 2024-12-12 11:12:50

来自博客 post 看起来 IQ 工具包(或工具包的初始版本)无法免受 SQL 注入攻击。但您可以自己验证 - 执行查询,捕获生成的 SQL 并查看是否使用了参数。

From the blog post it looks like IQ toolkit (or the initial version of the toolkit) is not safe from SQL injection attacks. But you can verify it by yourself - execute a query, capture the generated SQL and see if there are parameters used.

青朷 2024-12-12 11:12:50

如果你想建立自己的提供商,你必须知道这并不是那么容易。考虑诸如嵌套选择、嵌套位置等。有很棒的 关于此主题的博客文章

但您有兴趣保护您的数据库免受 SQL 注入。因此,如果您查看此 pageVisitConstant 方法,这是您遇到值类型常量(字符串、整数等)或I可查询。

防止 SQL 注入并不复杂,您只需创建新的 SQLParameter 或调用 此处。在遍历表达式树时,您将需要一些集合来存储参数。因此,修改后的代码将如下所示:

protected override Expression VisitConstant(ConstantExpression c) {
    IQueryable q = c.Value as IQueryable;
    if (q != null) {
        // assume constant nodes w/ IQueryables are table references
        sb.Append("SELECT * FROM ");
        sb.Append(q.ElementType.Name);
    }
    else if (c.Value == null) {
        sb.Append("NULL");
    }
    else {
        switch (Type.GetTypeCode(c.Value.GetType())) {
            case TypeCode.Boolean:
                param = dbProvider.CreateParameter();
                param.Name = "@param" + paramsList.Count;
                param.Value = (((bool)c.Value) ? 1 : 0;
                paramsList.Add(param);
                sb.Append(param.Name);
                break;
            case TypeCode.String:
                param = dbProvider.CreateParameter();
                param.Name = "@param" + paramsList.Count;
                param.Value = c.Value; // you don't have to care about escaping or formatting
                paramsList.Add(param);
                sb.Append(param.Name);
                break;
            ...
            case TypeCode.Object:
                throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", c.Value));
            default:
                sb.Append(c.Value);
                break;
        }
    }
    return c;
}

因此,当您遍历表达式树时,您正在构建 SQL 字符串并收集 SQL 参数。

If you want to build your own provider, you must know that it is not that easy. Consider things like nested select, nested where, etc. There are great blog posts on this topic.

But you are interested in protecting your database against SQL injection. So if you look at the sample code on this page and the VisitConstant method, that's the place where you run into constants of value type (string, int, etc.) or IQueryable.

Protection against SQL injections is not complicated, you just create new SQLParameter or you call method DbProviderFactory.CreateParameter described here. You will need some collection to store your parameters while you are traversing the expression tree. So the modified code will look like this:

protected override Expression VisitConstant(ConstantExpression c) {
    IQueryable q = c.Value as IQueryable;
    if (q != null) {
        // assume constant nodes w/ IQueryables are table references
        sb.Append("SELECT * FROM ");
        sb.Append(q.ElementType.Name);
    }
    else if (c.Value == null) {
        sb.Append("NULL");
    }
    else {
        switch (Type.GetTypeCode(c.Value.GetType())) {
            case TypeCode.Boolean:
                param = dbProvider.CreateParameter();
                param.Name = "@param" + paramsList.Count;
                param.Value = (((bool)c.Value) ? 1 : 0;
                paramsList.Add(param);
                sb.Append(param.Name);
                break;
            case TypeCode.String:
                param = dbProvider.CreateParameter();
                param.Name = "@param" + paramsList.Count;
                param.Value = c.Value; // you don't have to care about escaping or formatting
                paramsList.Add(param);
                sb.Append(param.Name);
                break;
            ...
            case TypeCode.Object:
                throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", c.Value));
            default:
                sb.Append(c.Value);
                break;
        }
    }
    return c;
}

So while you are travesing the expression tree, you are building the SQL string and collecting the SQL parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文