Description
The product has a method that is declared public, but returns a reference to a private data structure, which could then be modified in unexpected ways.
Potential Impact
Integrity
Modify Application Data
Demonstrative Examples
private String[] colors;public String[] getColors() {return colors;}class Color{private:int[2] colorArray;int colorValue;public:Color () : colorArray { 1, 2 }, colorValue (3) { };int[2] & fa () { return colorArray; } // return reference to private arrayint & fv () { return colorValue; } // return reference to private integer};int main (){Color c;c.fa () [1] = 42; // modifies private array elementc.fv () = 42; // modifies private intreturn 0;}Mitigations & Prevention
Declare the method private.
Clone the member data and keep an unmodified version of the data private to the object.
Use public setter methods that govern how a private member can be modified.
Detection Methods
- Automated Static Analysis High — Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then sea
Related Weaknesses
Taxonomy Mappings
- 7 Pernicious Kingdoms: — Private Array-Typed Field Returned From A Public Method
- Software Fault Patterns: SFP23 — Exposed Data
Frequently Asked Questions
What is CWE-495?
CWE-495 (Private Data Structure Returned From A Public Method) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Variant-level weakness. The product has a method that is declared public, but returns a reference to a private data structure, which could then be modified in unexpected ways.
How can CWE-495 be exploited?
Attackers can exploit CWE-495 (Private Data Structure Returned From A Public Method) to modify application data. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-495?
Key mitigations include: Declare the method private.
What is the severity of CWE-495?
CWE-495 is classified as a Variant-level weakness (Low-Medium abstraction). Its actual severity depends on the specific context and how the weakness manifests in your application.