简体   繁体   English

EJB3 SQL注入

[英]EJB3 SQL Injection

I would like to know how safe is EJB3 to prevent SQL Injection. 我想知道EJB3防止SQL注入的安全性。
I've read that using prepared statements is quite safe, but for instance with a function like this 我已经读过,使用预处理语句非常安全,但是例如使用这样的函数

@Override
public Collection<Project> searchProjectsPerProfessorLastname(String lastname) {
    Query q = manager.createQuery("SELECT DISTINCT OBJECT(p) FROM Project p JOIN p.professors prof WHERE lower(prof.lastName) = ?1");
    q.setParameter(1, lastname.toLowerCase());
    @SuppressWarnings("unchecked")
    Collection<Project> c = q.getResultList();
    if(c.size()==0)
       return null;
    return c;
}

it is possible to perform SQL Injection? 有可能执行SQL注入?

No. 没有。

Simply put, as long as you're not building the SQL dynamically (and binding is not building SQL), there's no risk of SQL injection. 简而言之,只要您不动态构建SQL(绑定也不构建SQL),就不会有SQL注入的风险。 Barring a buggy SQL driver. 除非存在错误的SQL驱动程序。

Binding is the technique of assigning parameters to SQL statements via the driver rather than through simply building up SQL text yourself. 绑定是通过驱动程序而不是仅通过自己构建SQL文本为SQL语句分配参数的技术。

Different drivers do different things, but the reason that SQL injection happens is that people creating the SQL text do not take the proper precautions to prevent SQL injection (notably escaping special characters alike quotes and such). 不同的驱动程序会做不同的事情,但是发生SQL注入的原因是,创建SQL文本的人没有采取适当的预防措施来防止SQL注入(特别是转义引号等特殊字符)。

Ideally, the driver will take care to build the SQL properly. 理想情况下,驱动程序会注意正确构建SQL。 But if the driver is buggy in some way, there's still some risk. 但是,如果驱动程序在某些方面存在问题,则仍然存在一定的风险。 I, personally, have not encountered a driver had a bug that affected this. 我个人没有遇到驱动程序有错误影响此错误。 So, while it's possible, it's really, really remote. 因此,尽管有可能,但它确实非常遥远。

Finally, for you example, you're not even using SQL, you're using EQL, which is the JPA query language. 最后,例如,您甚至没有使用SQL,而是使用EQL,这是JPA查询语言。 This has to be translated yet again from EQL to SQL, which gives more opportunity for the intervening software (JPA and the JDBC driver) opportunity to prevent a SQL injection from happening. 必须再次将其从EQL转换为SQL,这为中间软件(JPA和JDBC驱动程序)提供了更多机会来防止发生SQL注入。

All the parametized query forms of JPA calls are considered safe against SQL injection. 所有JPA调用的参数化查询形式都被认为可以防止SQL注入。 You can however, create a query string with concatenation, which would be unsafe. 但是,您可以使用串联创建查询字符串,这是不安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM