【问题标题】:Correct variable binding and avoiding SQL injection with SQLite.swift queries使用 SQLite.swift 查询更正变量绑定并避免 SQL 注入
【发布时间】:2016-04-29 02:10:55
【问题描述】:

SQLite.swift documentation for filtered queries 给出了这个例子:

users.filter(email.like("%@mac.com"))
// SELECT * FROM "users" WHERE ("email" LIKE '%@mac.com')

由于我想根据用户输入搜索数据库,我想我可以执行以下操作:

let stringPrefix = userInput + "%"
users.filter(email.like(stringPrefix))
// SELECT * FROM "users" WHERE ("email" LIKE 'johndoe%')

我这样做是否正确?在过去的其他 SQLite 环境中,我使用variable binding? 来避免SQL injection。这是使用 SQLite.swift 在幕后完成的吗?除了Executing Arbitrary SQL 部分中的一些关于绑定的信息外,我没有在文档中看到任何信息。

【问题讨论】:

    标签: ios swift sqlite.swift


    【解决方案1】:

    取自this源文件:

    @warn_unused_result public func like(pattern: String, escape character: Character? = nil) -> Expression<Bool> {
        guard let character = character else {
            return "LIKE".infix(self, pattern)
        }
        return Expression("(\(template) LIKE ? ESCAPE ?)", bindings + [pattern, String(character)])
    }
    

    这只是 like 函数的重载之一。另一个重载看起来很相似,并且确实也使用了参数绑定。自己浏览一下源代码来验证这一点。

    但是,我希望您进行内部测试,以验证 SQLite 注入是不可能的。

    【讨论】:

    • 也许这是一个单独的问题,但如果参数绑定已经处理好,我不确定我应该做哪些其他内部测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    相关资源
    最近更新 更多