Java – 4 Security Vulnerabilities Related Coding Practices to Avoid---reference
- Executing a dynamically generated SQL statement
- Directly writing an Http Parameter to Servlet output
- Creating an SQL PreparedStatement from dynamic string
- Array is stored directly
Executing a Dynamically Generated SQL Statement
This is most common of all. One can find mention of this vulenrability at several places. As a matter of fact, many developers are also aware of this vulnerability, although this is a different thing they end up making mistakes once in a while. In several DAO classes, the instances such as following code were found which could lead to SQL injection attacks.
StringBuilder query = new StringBuilder(); |
Instead of above query, one could as well make use of prepared statement such as that demonstrated in the code below. It not only makes code less vulnerable to SQL injection attacks but also makes it more efficient.
StringBuilder query = new StringBuilder(); |
Directly writing an Http Parameter to Servlet Output
In Servlet classes, I found instances where the Http request parameter was written as it is, to the output stream, without any validation checks. Following code demonstrate the same:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
Note that above code does not persist anything. Code like above may lead to what is called reflected (or non-persistent) cross site scripting (XSS) vulnerability. Reflected XSS occur when an attacker injects browser executable code within a single HTTP response. As it goes by definition (being non-persistent), the injected attack does not get stored within the application; it manifests only users who open a maliciously crafted link or third-party web page. The attack string is included as part of the crafted URI or HTTP parameters, improperly processed by the application, and returned to the victim. You could read greater details on following OWASP page on reflect XSS
Creating an SQL PreparedStatement from Dynamic Query String
What it essentially means is the fact that although PreparedStatement was used, but the query was generated as a string buffer and not in the way recommended for prepared statement (parametrized). If unchecked, tainted data from a user would create a String where SQL injection could make it behave in unexpected and undesirable manner. One should rather make the query statement parametrized and, use the PreparedStatement appropriately. Take a look at following code to identify the vulenarble code.
StringBuilder query = new StringBuilder(); |
Array is Stored Directly
Instances of this vulnerability, Array is stored directly, could help the attacker change the objects stored in array outside of program, and the program behave in inconsistent manner as the reference to the array passed to method is held by the caller/invoker. The solution is to make a copy within the object when it gets passed. In this manner, a subsequent modification of the collection won’t affect the array stored within the object. You could read the details on followingstackoverflow page. Following code represents the vulnerability:
// Note that values is a String array in the code below. |
reference from:http://vitalflux.com/java-4-security-vulnerabilities-related-coding-practices-avoid/
appendix:
There is a Sonar Violation:
Sonar Violation: Security - Array is stored directly
public void setMyArray(String[] myArray) {
this.myArray = myArray;
}
Solution:
public void setMyArray(String[] newMyArray) {
if(newMyArray == null) {
this.myArray = new String[0];
} else {
this.myArray = Arrays.copyOf(newMyArray, newMyArray.length);
}
}
It's complaining that the array you're storing is the same array that is held by the caller. That is, if the caller subsequently modifies this array, the array stored in the object (and hence the object itself) will change.
The solution is to make a copy within the object when it gets passed. This is called defensive copying. A subsequent modification of the collection won't affect the array stored within the object.
It's also good practice to normally do this when returning a collection (e.g. in a corresponding getMyArray() call). Otherwise the receiver could perform a modification and affect the stored instance.
Note that this obviously applies to all mutable collections (and in fact all mutable objects) - not just arrays. Note also that this has a performance impact which needs to be assessed alongside other concerns.
reference from:http://stackoverflow.com/questions/11580948/sonar-violation-security-array-is-stored-directly
Java – 4 Security Vulnerabilities Related Coding Practices to Avoid---reference的更多相关文章
- Java – Top 5 Exception Handling Coding Practices to Avoid
This article represents top 5 coding practices related with Java exception handling that you may wan ...
- Java Tips and Best practices to avoid NullPointerException
A NullPointerException in Java application is best way to solve it and that is also key to write rob ...
- Types of Security Vulnerabilities
1)内存空间安全.2)参量级别数据安全:3)通信级别数据安全:4)数据访问控制:5)通信对象身份确认. https://developer.apple.com/library/content/docu ...
- Java Basic&Security Tools
JDK Tools and Utilities Basic Tools These tools are the foundation of the JDK. They are the tools yo ...
- We found potential security vulnerabilities in your dependencies. Only the owner of this reposito...
删除package-lock.json并同步到git 定义的依赖项./package-lock.json具有已知的安全漏洞 找到一个叫做.gitignore,把package-lock.json贴在这 ...
- How Will Java Technology Change My Life?
How Will Java Technology Change My Life? We can't promise you fame, fortune, or even a job if you le ...
- BlackArch-Tools
BlackArch-Tools 简介 安装在ArchLinux之上添加存储库从blackarch存储库安装工具替代安装方法BlackArch Linux Complete Tools List 简介 ...
- Find out when memory leaks are a concern and how to prevent them
Handling memory leaks in Java programs Find out when memory leaks are a concern and how to prevent t ...
- malloc(50) 内存泄露 内存溢出 memory leak会最终会导致out of memory
https://en.wikipedia.org/wiki/Memory_leak In computer science, a memory leak is a type of resource l ...
随机推荐
- sencha app build 到 Capturing theme image不执行
解决sencha app build 到 Capturing theme image不执行 今天电脑重装系统,重新安装了sencha cmd,但是在打包时,到了 Capturing theme ima ...
- table-css
- Flowplayer-一款免费的WEB视频播放器 转 - helloweba.com
Flowplayer支持播放flv.swf等流媒体以及图片文件,能够非常流畅的播放视频文件,支持自定义配置和扩展. 1.加载flowplayer.js 在要播放视频的页面的head之间加入flowpl ...
- mongoDB之用户及权限设置
之前用MongoDB没有设置用户和权限,一直都是本机应用程序连接MongoDB.在服务器上部署后对外没有开数据库连接端口,本机应用程序连接再开放应用程序端口供外部访问. 我部署的环境是ubuntu 1 ...
- JS小技巧大本事(持续更新)
1. 复制N个字符 String.prototype.repeat = function(num){ return (new Array(++num)).join(this); } var a = ' ...
- c语言Winpcap编程构造并接收解析arp包
/* 程序功能: 1.构造arp包,并发送.程序参数顺序:源IP.目的IP.mac地址.flag 2.获取网络中的ARP数据包,解析数据包的内容.程序参数:日志文件名 winpacp中文技术文档(基本 ...
- 转:如何创建.htaccess文件
方法1.开始-运行-键入cmd,打开cmd窗口 此时的cmd窗口路径是C:\Documents and Settings\username> 键入以下(不包括括号内信息): copy con . ...
- Ansible好像很好玩的样子哟
这个比SALTSTACK轻量,,不知道速度如何..... 参考文档: http://linux.cn/article-4215-1.html wget http://download.fedorapr ...
- SCALA中的函数式编程
演示了值函数,匿名函数,闭包... 其它具体的应用,还得在生产当中吧.. 这个告一段落..其它SAM,CURRY,高阶函数,集合,泛型,隐式类..这些,还是找专门的书去深入了解啦... C:\User ...
- 教你如何用Qt做透明的窗体,setMask, Opacity
// In this function, we can get the height and width of the current widgetvoid Widget::resizeEvent(Q ...