Java leading security practice

OWASP Code Review Guide Table of Contents

Introduction
This section covers the main Java-centric areas which are prescribed as leading security practices when developing Java applications and code. So when we are performing a code review on Java code we should look at the following areas of concern. Getting developers to adopt leading practice techniques gives the inherent basic security features all code should have, "Self Defending Code".

Class Access

 * 1) Methods
 * 2) Fields
 * 3) Mutable Objects

Put simply, don't have public fields or methods in a class unless required. Every method, field, or class that is not private is a potential avenue of attack. Provide accessors to them so you can limit their accessibility.

Initialisation
Allocation of objects without calling a constructor is possible. One does not neet to call a constructor to instantiate an object, so dont rely on initialization as there are many ways to allocate uninitialized objects.

Add a boolean that is set to "TRUE" when initialized, make this private. This can be checked when required by all non-constructor methods.
 * 1) Get the class to verify that it has been initialized prior to it performing any function.
 * 1) Make all variables private and use setters/getters.
 * 2) Make static variables private, this prevents access to uninitialized variables.

Finality
Non-Final classes let an attacker extend a class in a malicious manner. An application may have a USER object which by design would never be extended, so implementing this class as Final would prevent malicious code extending the user class. Non-final classes should be such for a good reason. Extensibility of classes should be enabled if it is required not simply for the sake of being extensible.

Scope
Package scope is really used so there are no naming conflicts for an application especially when reusing classes from another framework. Packages are by default open, not sealed which means a rogue class can be added to your package. If such a rogue class was added to a package the scope of protected fields would not yield any security. By default all fields and methods not declared public or private are protected and can only be accessed within the same package, don’t rely on this for security.

Inner Classes
Simply put, when translated into bytecode, inner classes are "rebuilt" as external classes in the same package. This means any class in the package can access this inner class. The owner/enclosing/father classes’ private fields are morphed into protected fields as they are accessible by the now external inner class.

Hard Coding
Don't hard code any passwords, user ID's, etc in your code. Silly and bad design. Can be decompiled. Place them in a protected directory in the deployment tree.