Safeguard
Application Security•

CWE-496 Public Data Assigned to a Private Array-Typed Field

Storing a caller's array in a private field keeps the caller's handle on it. Copy the array on the way in so the private field stays private.

Safeguard Security Team
2 min read

CWE-496, Public Data Assigned to Private Array-Typed Field, says that assigning public data to a private array is equivalent to giving public access to the array. MITRE lists object oriented code in C, C++, Java and C#.

It is the input side of CWE-495, which covers the same problem on the way out.

How it happens

A constructor or setter takes an array argument and stores it directly. The caller still holds a reference to the same array.

// Vulnerable: stores the caller's array
public class Keyring {
    private final byte[] key;
    public Keyring(byte[] key) {
        this.key = key;
    }
}

byte[] k = loadKey();
Keyring r = new Keyring(k);
k[0] = 0;          // silently changes the key inside r

What an attacker gains

MITRE lists one consequence: the array contents can be modified from outside the intended scope, affecting integrity. The record has no observed CVEs.

How to find it

Static analysis can find some instances. In review, look for constructors and setters that assign an array parameter straight to a field.

How to fix it

MITRE says not to allow objects to modify private members of a class. For arrays, that means copying on the way in.

public Keyring(byte[] key) {
    this.key = key.clone();       // private copy
}

Pair this with copying on the way out, as in CWE-495, so neither side shares the array.

Quick check

Why is a private field not enough?

Private limits who can name the field. The caller already holds a reference to the same array object, so it can change the contents at will.

Sources

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.