# VNX-584: Return Inside Finally Block

Reports a return statement inside a finally block in Java, JavaScript/TypeScript and Python.

| Field | Value |
|---|---|
| Rule ID | `VNX-584` |
| Severity | Low |
| SARIF level | `note` |
| Kind | `sast` |
| Languages | Java, JavaScript / TypeScript, Python |
| CWE | [CWE-584](https://cwe.mitre.org/data/definitions/584.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:L/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:L/DI:H/EX:L/EC:N/P:L` |
| Tags | `return-in-finally` `cwe-584` |
| Source | [`internal/sast/rules/vnx-584.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-584.rego) |

## Overview

VNX-584 maps to [CWE-584: Return Inside Finally Block](https://cwe.mitre.org/data/definitions/584.html). It reports a `return` inside a `finally` block at any depth (inside an `if`, a nested `try`), including the one-line form `finally { return x; }`. The block is bounded by brace depth, or indentation for Python, within 80 lines.

## Why this matters

A `return` in `finally` completes the whole `try` statement normally. Any exception thrown by the `try` or `catch` block is silently dropped, and the `try` block's own return value is replaced. Failures such as a failed write, an authorisation error or a parse error then look like success to the caller.

## What gets flagged

```java
} finally {
    stream.close();
    return -1;
}
```

```ts
} finally { return undefined; }
```

```python
finally:
    if cache is not None:
        return cache
```

Not flagged: a return after the `finally` block closes, a return in the `try` or `catch` block, a return inside a lambda, arrow function, anonymous class or nested `def` opened in the `finally` block, comments and strings.

## Remediation

1. Move the `return` after the `try`/`finally` statement, or into the `try` block.
2. Keep `finally` for cleanup only; if cleanup can fail, record that failure without replacing the original exception (for example with `addSuppressed`).

## References

- [CWE-584: Return Inside Finally Block](https://cwe.mitre.org/data/definitions/584.html)
- [SEI CERT ERR04-J: Do not complete abruptly from a finally block](https://wiki.sei.cmu.edu/confluence/display/java/ERR04-J.+Do+not+complete+abruptly+from+a+finally+block)
- [PEP 765: Disallow return/break/continue that exit a finally block](https://peps.python.org/pep-0765/)

## 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-584
// nosec vnx-584
```

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