Insufficient Encapsulation

The product does not sufficiently hide the internal representation and implementation details of data or methods, which might allow external components or modules to modify data unexpectedly, invoke unexpected functionality, or introduce dependencies that the programmer did not intend.


Description

This issue makes it more difficult to maintain the product, which indirectly affects security by making it more difficult or time-consuming to find and/or fix vulnerabilities. It also might make it easier to introduce vulnerabilities.

Demonstrations

The following examples help to illustrate the nature of this weakness and describe methods or techniques which can be used to mitigate the risk.

Note that the examples here are by no means exhaustive and any given weakness may have many subtle varieties, each of which may require different detection methods or runtime controls.

Example One

The following example shows a basic user account class that includes member variables for the username and password as well as a public constructor for the class and a public method to authorize access to the user account.

#define MAX_PASSWORD_LENGTH 15
#define MAX_USERNAME_LENGTH 15

class UserAccount
{

  public:

    UserAccount(char *username, char *password)
    {
      if ((strlen(username) > MAX_USERNAME_LENGTH) ||
      (strlen(password) > MAX_PASSWORD_LENGTH)) {
        ExitError("Invalid username or password");
      }
      strcpy(this->username, username);
      strcpy(this->password, password);
    }



  int authorizeAccess(char *username, char *password)
  {

    if ((strlen(username) > MAX_USERNAME_LENGTH) ||
    (strlen(password) > MAX_PASSWORD_LENGTH)) {
      ExitError("Invalid username or password");
    }
    // if the username and password in the input parameters are equal to

    // the username and password of this account class then authorize access
    if (strcmp(this->username, username) ||
    strcmp(this->password, password))
      return 0;

    // otherwise do not authorize access
    else
      return 1;


  }

  char username[MAX_USERNAME_LENGTH+1];
  char password[MAX_PASSWORD_LENGTH+1];

};

However, the member variables username and password are declared public and therefore will allow access and changes to the member variables to anyone with access to the object. These member variables should be declared private as shown below to prevent unauthorized access and changes.

class UserAccount
{
public:
  ...


private:
  char username[MAX_USERNAME_LENGTH+1];
  char password[MAX_PASSWORD_LENGTH+1];
};

See Also

Comprehensive Categorization: Poor Coding Practices

Weaknesses in this category are related to poor coding practices.

Comprehensive CWE Dictionary

This view (slice) covers all the elements in CWE.

Quality Weaknesses with Indirect Security Impacts

CWE identifiers in this view (slice) are quality issues that only indirectly make it easier to introduce a vulnerability and/or make the vulnerability more difficult t...


Common Weakness Enumeration content on this website is copyright of The MITRE Corporation unless otherwise specified. Use of the Common Weakness Enumeration and the associated references on this website are subject to the Terms of Use as specified by The MITRE Corporation.