代码之家  ›  专栏  ›  技术社区  ›  Tim unnamed eng

为什么参数化查询允许将用户数据移出要解释的字符串?

  •  -2
  • Tim unnamed eng  · 技术社区  · 7 年前

    https://en.wikipedia.org/wiki/Code_injection#Preventing_problems

    为了防止代码注入问题,使用安全的输入和输出处理,例如:

    • 使用API,如果使用得当,对所有输入字符都是安全的。 参数化查询(也称为“编译查询”、“准备语句”、“绑定变量”)允许将用户数据从要解释的字符串中移出。 另外,标准api[7]和类似的api远离了要创建和解释的命令字符串的概念。

    我想知道“参数化查询(也称为“编译查询”、“准备好的语句”、“绑定变量”)如何以及为什么允许将用户数据从字符串中移出进行解释,并防止或缓解代码注入问题?

    你也能提供一些解释的例子吗?

    谢谢。

    1 回复  |  直到 7 年前
        1
  •  3
  •   The Impaler    7 年前

    select * from applicant where name = ?
    
    select * from applicant where name = :name
    

    Mary x'; delete from applicant; --

    select * from applicant where name = 'x; delete from applicant; --'
    

    String sql = "select * from applicant where name = '" + param1 + "'";
    

    select * from applicant where name = 'x'; delete from applicant; --