Secure Coding Practices in Java
These secure coding practices were picked from the list maintained by Carnegie Mellon University Software Engineering Institute:
-
DCL00-J. Prevent class initialization cycles
-
DCL01-J. Do not reuse public identifiers from the Java Standard Library
-
DCL02-J. Do not modify the collection's elements during an enhanced for statement
-
DCL51-J. Do not shadow or obscure identifiers in subscopes
-
DCL52-J. Do not declare more than one variable per declaration
-
DCL53-J. Minimize the scope of variables
-
DCL54-J. Use meaningful symbolic constants to represent literal values in program logic
-
DCL55-J. Properly encode relationships in constant definitions
-
DCL56-J. Do not attach significance to the ordinal associated with an enum
-
EXP00-J. Do not ignore values returned by methods
-
EXP01-J. Do not use a null in a case where an object is required
-
EXP02-J. Do not use the Object.equals() method to compare two arrays
-
EXP03-J. Do not use the equality operators when comparing values of boxed primitives
-
EXP06-J. Expressions used in assertions must not produce side effects
-
EXP50-J. Do not confuse abstract object equality with reference equality
-
EXP51-J. Do not perform assignments in conditional expressions
-
EXP52-J. Use braces for the body of an if, for, or while statement
-
EXP53-J. Use parentheses for precedence of operation
-
EXP55-J. Use the same type for the second and third operands in conditional expressions
-
NUM00-J. Detect or prevent integer overflow
-
NUM01-J. Do not perform bitwise and arithmetic operations on the same data
-
NUM02-J. Ensure that division and remainder operations do not result in divide-by-zero errors
-
NUM03-J. Use integer types that can fully represent the possible range of unsigned data
-
NUM07-J. Do not attempt comparisons with NaN
-
NUM08-J. Check floating-point inputs for exceptional values
-
NUM09-J. Do not use floating-point variables as loop counters
-
NUM10-J. Do not construct BigDecimal objects from floating-point literals
-
NUM11-J. Do not compare or inspect the string representation of floating-point values
-
NUM12-J. Ensure conversions of numeric types to narrower types do not result in lost or misinterpreted data
-
NUM13-J. Avoid loss of precision when converting primitive integers to floating-point
-
NUM14-J. Use shift operators correctly
-
NUM50-J. Convert integers to floating point for floating-point operations
-
STR01-J. Do not assume that a Java char fully represents a Unicode code point
-
STR02-J. Specify an appropriate locale when comparing locale-dependent data
-
OBJ01-J. Limit accessibility of fields
-
OBJ04-J. Provide mutable classes with copy functionality to safely allow passing instances to untrusted code
-
OBJ05-J. Do not return references to private mutable class members
-
OBJ06-J. Defensively copy mutable inputs and mutable internal components
-
OBJ07-J. Sensitive classes must not let themselves be copied
-
OBJ09-J. Compare classes and not class names
-
OBJ10-J. Do not use public static nonfinal fields
-
OBJ11-J. Be wary of letting constructors throw exceptions
-
OBJ13-J. Ensure that references to mutable objects are not exposed
-
OBJ51-J. Minimize the accessibility of classes and their members
-
MET00-J. Validate method arguments
-
MET01-J. Never use assertions to validate method arguments
-
MET03-J. Methods that perform a security check must be declared private or final
-
MET04-J. Do not increase the accessibility of overridden or hidden methods
-
MET05-J. Ensure that constructors do not call overridable methods
-
MET06-J. Do not invoke overridable methods in clone()
-
MET07-J. Never declare a class method that hides a method declared in a superclass or superinterface
-
MET08-J. Preserve the equality contract when overriding the equals() method
-
MET09-J. Classes that define an equals() method must also define a hashCode() method
-
MET50-J. Avoid ambiguous or confusing uses of overloading
-
MET51-J. Do not use overloaded methods to differentiate between runtime types
-
MET53-J. Ensure that the clone() method calls super.clone()
-
MET54-J. Always provide feedback about the resulting value of a method
-
MET55-J. Return an empty array or collection instead of a null value for methods that return an array or collection
-
ERR00-J. Do not suppress or ignore checked exceptions
-
ERR01-J. Do not allow exceptions to expose sensitive information
-
ERR03-J. Restore prior object state on method failure
-
ERR04-J. Do not complete abruptly from a finally block
-
ERR05-J. Do not let checked exceptions escape from a finally block
-
ERR07-J. Do not throw RuntimeException, Exception, or Throwable
-
ERR08-J. Do not catch NullPointerException or any of its ancestors
-
ERR50-J. Use exceptions only for exceptional conditions
-
ERR51-J. Prefer user-defined exceptions over more general exception types
-
ERR52-J. Avoid in-band error indicators
-
MSC02-J. Generate strong random numbers
-
MSC03-J. Never hard code sensitive information
-
MSC04-J. Do not leak memory
-
MSC05-J. Do not exhaust heap space
-
MSC06-J. Do not modify the underlying collection when an iteration is in progress
-
MSC50-J. Minimize the scope of the @SuppressWarnings annotation
-
MSC51-J. Do not place a semicolon immediately following an if, for, or while condition
-
MSC54-J. Avoid inadvertent wrapping of loop counters
-
MSC56-J. Detect and remove superfluous code and values
-
MSC57-J. Strive for logical completeness
-
MSC58-J. Prefer using iterators over enumerations
-
MSC60-J. Do not use assertions to verify the absence of runtime errors
-
MSC63-J. Ensure that SecureRandom is properly seeded