简体   繁体   English

MyBatis保护字符串替换参数免受SQL注入

[英]MyBatis protect String Substitution parameter from sql injection

I need to protect (automatically) requests using String substitution in MyBatis ${myValue) . 我需要在MyBatis ${myValue)使用字符串替换来保护(自动)请求。

From what I've seen, the substitution is done automatically in the GenericTokenParser class using the TokenHandler.handleToken method. 从我所看到的来看,替换是使用TokenHandler.handleToken方法在GenericTokenParser类中自动完成的。

I can't find anything about getting a grab on those values. 关于这些价值,我找不到任何东西。 I can access PreparedStatement parameters using 我可以使用以下方法访问PreparedStatement参数

sqlSession.getConfiguration().getMappedStatement(statement).getBoundSql(parameter).getParameterMappings()

But I really need to retrieve ${} parameters before sending the request to be able to modify the value if needed to protect from SQL Injection. 但是我确实需要在发送请求之前检索${}参数,以便能够在需要时修改值,以防止受到SQL Injection的攻击。

I though about using AspectJ to intercept the TokenHandler.handleToken method. 我虽然要使用AspectJ来拦截TokenHandler.handleToken方法。 But I would really prefer to be able to inject my own handler or token parser. 但是我真的更希望能够注入自己的处理程序或令牌解析器。

Has someone a clue ? 有线索吗?


I'm working on a "framework" for future development, the aim is to have something automatic. 我正在为将来的开发制定“框架”,目的是要自动化。 Going through the whole parameter object isn't an option (could be heavy), we only want to protect those used in the request before executing it. 遍历整个参数对象不是一个选择(可能很繁重),我们只想在执行请求之前保护那些在请求中使用的对象。

MyBatis has 2 substitution methods. MyBatis有2种替代方法。 The ${} method does direct string replacement (ie property substitution) so it's vulnerable to SQL injection. ${}方法直接进行字符串替换(即属性替换),因此容易受到SQL注入的攻击。 The #{} method does parameter substitution on a PreparedStatement so it is not vulnerable. #{}方法在PreparedStatement上进行参数替换,因此它容易受到攻击。

Are you sure you want to be using ${} ? 您确定要使用${}吗? Unless you are dynamically building SQL statements, you probably want to use #{} . 除非您动态构建SQL语句,否则可能要使用#{} If you are building dynamic SQL, then you should validate the parameters before loading the MyBatis configuration. 如果要构建动态SQL,则应在加载MyBatis配置之前验证参数。

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

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