1091
Example Language: PHP (Bad)
if (authenticate($username,$password) && setAdmin($username)){
$isAdmin = true;
}
/.../
if ($isAdmin){
deleteUser($userToDelete);
}
The $isAdmin variable is set to true if the user is an admin, but is uninitialized otherwise. If PHP's
register_globals feature is enabled, an attacker can set uninitialized variables like $isAdmin to
arbitrary values, in this case gaining administrator privileges by setting $isAdmin to true.
Example 4:
In the following Java code the BankManager class uses the user variable of the class User to
allow authorized users to perform bank manager tasks. The user variable is initialized within the
method setUser that retrieves the User from the User database. The user is then authenticated as
unauthorized user through the method authenticateUser.
Example Language: Java (Bad)
public class BankManager {
// user allowed to perform bank manager tasks
private User user = null;
private boolean isUserAuthentic = false;
// constructor for BankManager class
public BankManager() {
...
}
// retrieve user from database of users
public User getUserFromUserDatabase(String username){
...
}
// set user variable using username
public void setUser(String username) {
this.user = getUserFromUserDatabase(username);
}
// authenticate user
public boolean authenticateUser(String username, String password) {
if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
isUserAuthentic = true;
}
return isUserAuthentic;
}
// methods for performing bank manager tasks
...
}
However, if the method setUser is not called before authenticateUser then the user variable will not
have been initialized and will result in a NullPointerException. The code should verify that the user
variable has been initialized before it is used, as in the following code.
Example Language: Java (Good)
public class BankManager {
// user allowed to perform bank manager tasks
private User user = null;
private boolean isUserAuthentic = false;
// constructor for BankManager class
public BankManager(String username) {
user = getUserFromUserDatabase(username);
}
// retrieve user from database of users
public User getUserFromUserDatabase(String username) {...}
CWE Version 4.14
CWE-456: Missing Initialization of a Variable
CWE-456: Missing Initialization of a Variable