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

A public Java method that returns an array type returns a private array field itself (return field.

| Field | Value |
|---|---|
| Rule ID | `VNX-495` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | Java |
| CWE | [CWE-495](https://cwe.mitre.org/data/definitions/495.html) |
| CAPEC | [CAPEC-66](https://capec.mitre.org/data/definitions/66.html) |
| ATT&CK | [T1190](https://attack.mitre.org/techniques/T1190/) |
| CWSS vector | `CWSS:1.0/TI:M/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:M/DI:H/EX:M/EC:N/P:C` |
| Tags | `private-array` `defensive-copy` `encapsulation` `cwe-495` |
| Source | [`internal/sast/rules/vnx-495.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-495.rego) |

## Overview

VNX-495 maps to [CWE-495: Private Data Structure Returned From A Public Method](https://cwe.mitre.org/data/definitions/495.html). It reports public methods that hand out a private array field instead of a copy. VNX-375, the general mutable-return rule, leaves these lines to this rule and reports collections, dates and the other mutable fields.

## Why this matters

Arrays are mutable and returned by reference. A getter that returns `roles` lets the caller write `getRoles()[0] = "admin"` and change the object's own state, bypassing every check the class performs on its setters. Marking the field `final` does not prevent this.

## What gets flagged

```java
public class KeyHolder {
    private byte[] secretKey;
    private final String[] roles;

    public byte[] getSecretKey() {
        return secretKey;                        // FLAGGED
    }

    public String[] getRoles() { return roles; } // FLAGGED
}
```

Not flagged: `return secretKey.clone();`, `return Arrays.copyOf(roles, roles.length);`, returning a `private static final String[] EMPTY = new String[0];`, and non-public methods.

## Remediation

1. Return a copy: `return secretKey.clone();` or `Arrays.copyOf(...)`.
2. Return an unmodifiable view such as `List.of(roles)` or `Collections.unmodifiableList(...)`.
3. For secrets, avoid returning them at all; expose the operation (sign, verify) instead.

## References

- [CWE-495: Private Data Structure Returned From A Public Method](https://cwe.mitre.org/data/definitions/495.html)

## Suppressing this rule

Put a `nosec` comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file's language (`#`, `//`, `--` or `;`):

```text
# nosec vnx-495
// nosec vnx-495
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-495 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
