# VNX-460: Improper Cleanup on Thrown Exception

Reports Java code that releases a lock (unlock()) or closes a file, socket or JDBC resource (close()) only on the normal path.

| Field | Value |
|---|---|
| Rule ID | `VNX-460` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | Java |
| CWE | [CWE-460](https://cwe.mitre.org/data/definitions/460.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 | `improper-cleanup` `cwe-460` |
| Source | [`internal/sast/rules/vnx-460.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-460.rego) |

## Overview

VNX-460 maps to [CWE-460: Improper Cleanup on Thrown Exception](https://cwe.mitre.org/data/definitions/460.html). It reports a `x.lock()` (including `readLock()`/`writeLock()`) followed later in the same block by `x.unlock()` with no `finally` in between, and a closeable resource (FileInputStream, FileWriter, Buffered*, RandomAccessFile, Socket, ZipFile, Object*Stream, Scanner, `getConnection()`, `prepareStatement()`, `Files.newInputStream()`...) assigned to a variable and later closed with `v.close()` in the same block with no `finally` in between. The lock/try/finally idiom, try-with-resources, release inside a finally block, tryLock branches, a lock released in another method and streams over in-memory buffers are not reported.

## Why this matters

If anything between acquisition and release throws, the release never runs. A lock stays held and every later caller blocks; file descriptors and database connections leak until the pool or process runs out, which is a reliable denial of service.

## What gets flagged

```java
// FLAGGED: unlock() is skipped if validate() throws
lock.lock();
validate(delta);
value += delta;
lock.unlock();

// FLAGGED: the stream leaks if load() throws
FileInputStream in = new FileInputStream(file);
props.load(in);
in.close();

// NOT FLAGGED
lock.lock();
try {
    value += delta;
} finally {
    lock.unlock();
}
```

## Remediation

1. Call `unlock()` in a `finally` block that immediately follows `lock()`.
2. Open closeable resources in try-with-resources (`try (var in = new FileInputStream(f)) { ... }`).
3. For confirmed false positives, add a `// vulnetix-ignore: VNX-460` comment on the line.

## References

- [CWE-460: Improper Cleanup on Thrown Exception](https://cwe.mitre.org/data/definitions/460.html)
- [OWASP Top 10](https://owasp.org/www-project-top-ten/)

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

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