2
function escape_sqli(source)
    to_replace = {"'", '"'}
    replace_with = {"\'", '\"'}
    output = source
    for i = 1, table.getn(to_replace) do
        output = string.gsub(output, to_replace[i], replace_with[i])
    end
    return output
end

我尝试使用上面的代码来转义 SQLis,但是当我尝试编译它时出现以下错误:

Unfinished String near '"}'
4

1 回答 1

4

就目前而言,代码中没有语法错误。


一个建议;从string.gsub文档:

string.gsub (s, 模式, repl [, n])

[...]

如果repl是一个表,则使用第一个捕获作为键来查询每个匹配项的表。

您可以简单地重新创建替换表,如下所示:

local replacements = { ['"'] = '\\"', ["'"] = "\\'" }

并在一次gsub调用中使用它:

function escape_sqli(source)
    local replacements = { ['"'] = '\\"', ["'"] = "\\'" }
    return source:gsub( "['\"]", replacements ) -- or string.gsub( source, "['\"]", replacements )
end
于 2015-03-01T18:39:13.160 回答