CWE-766

Critical Data Element Declared Public. [Permission-Issues, Improper-Access-Control, Improper-Control-Of-A-Resource-Through-Its-Lifetime]

Required inputs: IR

The product declares a critical variable, field, or member to be public when intended security policy requires it to be private.

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.

Demonstrative Examples
Example 1

The following example declares a critical variable public, making it accessible to anyone with access to the object in which it is contained.

Example Language:C++
    public: char* password;

Instead, the critical data should be declared private.

Example Language:C++
    private: char* password;

Even though this example declares the password to be private, there are other possible issues with this implementation, such as the possibility of recovering the password from process memory (CWE-257).

Example 2

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.

Example Language:C++
    #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.

Example Language:C++
    class UserAccount
    {
    public:
        ...


    private:
        char username[MAX_USERNAME_LENGTH+1];
        char password[MAX_PASSWORD_LENGTH+1];
    };
Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See section 9.4. "3rd-Party Licenses" in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

public_critical_field

Critical fields must be private.

None

False

Options

critical_field_patterns

critical_field_patterns : set[bauhaus.analysis.config.SearchPattern] = {'pass_?[word|key]', 'user_?name'}

List of regex patterns used to find critical fields.