---
title: "CWE-495: Private Data Structure Returned From A Public Method"
url: https://radicalnotion.ai/cwe/cwe-495
type: cwe
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."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-495: Private Data Structure Returned From A Public Method

- **Abstraction:** Variant
- **Structure:** Simple
- **Status:** Draft
- **CVEs with this as primary weakness:** 1
- **Of those, in CISA KEV:** 0
- **New in 2026:** 0
- **CWE catalog version:** 4.21

## 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.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | - |

## Applicable platforms

**Languages:** Object-Oriented (Undetermined), C (Undetermined), C++ (Undetermined), Java (Undetermined), C# (Undetermined)

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Integrity | Modify Application Data | The contents of the data structure can be modified from outside the intended scope. |

## Potential mitigations

### Implementation

Declare the method private.

### Implementation

Clone the member data and keep an unmodified version of the data private to the object.

### Implementation

Use public setter methods that govern how a private member can be modified.

## Detection methods

### Automated Static Analysis

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 searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

Effectiveness: High

## Demonstrative examples

### Example 1

Here, a public method in a Java class returns a reference to a private array. Given that arrays in Java are mutable, any modifications made to the returned reference would be reflected in the original private array.

Bad code:

```java
private String[] colors;
public String[] getColors() {
    return colors;
}
```

### Example 2

In this example, the Color class defines functions that return non-const references to private members (an array type and an integer type), which are then arbitrarily altered from outside the control of the class.

Bad code:

```c++
class Color
{
    private:
        int[2] colorArray;
        int colorValue;
    public:
        Color () : colorArray { 1, 2 }, colorValue (3) { };
        int[2] & fa () { return colorArray; } // return reference to private array
        int & fv () { return colorValue; } // return reference to private integer
};

int main ()
{
    Color c;

    c.fa () [1] = 42; // modifies private array element
    c.fv () = 42; // modifies private int

    return 0;
}
```

## Real CVEs with this weakness

| CVE | Title | Severity | CVSS | EPSS | CISA KEV | Published |
| --- | --- | --- | --- | --- | --- | --- |
| CVE-2025-29868 | Apache Answer: Using externally referenced images can leak user privacy. | low | 1.1 | - | No | 2025-04-01 |

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-668](https://radicalnotion.ai/cwe/cwe-668) | Exposure of Resource to Wrong Sphere |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| 7 Pernicious Kingdoms | Private Array-Typed Field Returned From A Public Method | - | - |
| Software Fault Patterns | Exposed Data | SFP23 | - |

## Frequently asked questions

### What is CWE-495?

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.

### What CVEs are caused by CWE-495?

1 recorded CVE is attributed to CWE-495: CVE-2025-29868.

### How do you prevent CWE-495?

Declare the method private.

### How is CWE-495 detected?

Automated Static Analysis: 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 searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

### What are the consequences of CWE-495?

Exploiting CWE-495 can lead to: Modify Application Data.

### Is CWE-495 actively exploited?

1 recorded CVE is caused by CWE-495; it is not currently in CISA's KEV catalog of actively exploited flaws.

Primary source: [MITRE CWE](https://cwe.mitre.org/data/definitions/495.html)

---

Source: [RadicalNotion.AI](https://radicalnotion.ai/cwe/cwe-495). Licensed CC BY-NC 4.0; attribute RadicalNotion.AI and link the source page when you reuse this data.
