gpt4 book ai didi

java - "status"在外部提供给该方法,在使用前未经过清理。为什么?

转载 作者:行者123 更新时间:2023-12-05 00:39:16 26 4
gpt4 key购买 nike

我用的是Intellij,但不知道为什么总是报如下错误:

"status" is provided externally to the method and not sanitized before use.

我的方法:

...
public List getActionIdByTypeAndCodeAndStatus(String type, String code, String status) throws Exception {
String sql = "select action_id from action where type = '" + type + "' and code = '" + code + "' and status = '" + status + "' ";
Query checkWriteLog = entityManager.createNativeQuery(sql);
return checkWriteLog.getResultList();
}

抛出错误的行是

 Query checkWriteLog = entityManager.createNativeQuery(sql);

问题:你知道原因吗?我该如何解决?

最佳答案

问题


您正在连接字符串以形成您的 sql 查询。这很容易SQL injection attacks .

给予

 String sql = "select action_id from action where type = '" + type + "' and code = '" + code + "' and status = '" + status + "' "

我们可以传递以下状态字符串来破坏您的数据库:

'; DROP TABLE action; --

为什么?这 ';将完成您的查询并运行它,然后我们提供另一个查询(;关闭第一个),即“DROP TABLE 操作;”最后我们添加两个破折号来忽略后面的所有内容

这会导致表操作的删除表,并且可能是灾难性的。在 wiki page 上阅读更多相关信息.

解决方案


像这样使用准备好的语句:

Query query = JPA.em().createNativeQuery("select action_id from action where type = ':type' and code = ':code' and status = :status ");
query.setParameter("type", type);
query.setParameter("code", code);
query.setParameter("status", status);

这将以一种易于理解的方式基本上将查询发送到数据库并告诉它“运行它,但我会给你稍后添加的值”,然后将值发送给它。这意味着您发送的任何内容都将放在“”之间,并且不会被视为查询。 **

** 这不是实际发生的事情,而是一种理解其工作原理的方式。如果您需要实际解释,请阅读 wiki 页面。

关于java - "status"在外部提供给该方法,在使用前未经过清理。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41762902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com