This article represents top 4 security vulnerabilities related coding practice to avoid while you are programming withJava language. Recently, I came across few Java projects where these instances were found. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.
 
Following are the key points described later in this article:

  • 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();
query.append( "select * from user u where u.name in (" + namesString + ")" );
try {
Connection connection = getConnection();
Statement statement = connection.createStatement();
resultSet = statement.executeQuery(query.toString());
}

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();
query.append( "select * from user u where u.name in (?)" );
try {
Connection connection = getConnection();
PreparedStatement statement = connection.prepareCall(query.toString());
statement.setString( 1, namesString );
resultSet = statement.execute();
}
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 {
String content = request.getParameter("some_param");
//
// .... some code goes here
//
response.getWriter().print(content);
}

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();
query.append( "select * from user u where u.name in (" + namesString + ")" );
try {
Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement(query.toString());
resultSet = statement.executeQuery();
}
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.
//
public void setValues(String[] somevalues) {
this.values = somevalues;
}

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Types of Security Vulnerabilities

    1)内存空间安全.2)参量级别数据安全:3)通信级别数据安全:4)数据访问控制:5)通信对象身份确认. https://developer.apple.com/library/content/docu ...

  4. Java Basic&Security Tools

    JDK Tools and Utilities Basic Tools These tools are the foundation of the JDK. They are the tools yo ...

  5. 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贴在这 ...

  6. 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 ...

  7. BlackArch-Tools

    BlackArch-Tools 简介 安装在ArchLinux之上添加存储库从blackarch存储库安装工具替代安装方法BlackArch Linux Complete Tools List 简介 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. C#设置IP地址,启用禁用适配器

    界面效果图如下: 报表界面 说下关键代码 需要开启 Windows Management Instrumentation服务(默认已经开启),在程序中需要增加 Management引用. 主要有Net ...

  2. hibernate_validator_05

    校验约束 一,认识Validator---Validation中最主要的接口 1.怎么获取一个Validator--Validation.buildDefaultValidatorFactory() ...

  3. cocos2d-x 音乐播放猜想

    "SimpleAudioEngine.cpp": void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePa ...

  4. 78 Subsets(求子集Medium)

    题目意思:求解一个数组的所有子集,子集内的元素增序排列eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]思路:这是一个递推的过程 [] ...

  5. codevs 2188 最长上升子序列

    题目描述 Description LIS问题是最经典的动态规划基础问题之一.如果要求一个满足一定条件的最长上升子序列,你还能解决吗? 给出一个长度为N整数序列,请求出它的包含第K个元素的最长上升子序列 ...

  6. webkit的基本应用

    新建Qt Widgets Application->Browser01 修改.pro文件内容: #------------------------------------------------ ...

  7. 玩死人不偿命的CLOUDSTACK

    玩过CLOUDSTACK(CS)的人,一定不会陌生下面的LOG: 2013-12-27 18:26:43,861 DEBUG [allocator.impl.FirstFitAllocator] (J ...

  8. filter高级应用

    Filter高级应用: Decorator模式  1)包装类需要和被包装对象 实现相同接口,或者继承相同父类 2)包装类需要持有 被包装对象的引用   在包装类中定义成员变量,通过包装类构造方法,传入 ...

  9. -_-#【事件】keyCode

  10. yui--datatable 更新table数据

    使用render可以重新渲染datatable,之前添加的样式等信息也想相应会初始化,另外行定位等也会失效 使用updateRows方法不会删除样式等信息 更新datasource中_oData数据 ...