# VNX-459: Incomplete Cleanup

Reports Python code that creates a temporary directory or file with tempfile.mkdtemp().

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

## Overview

VNX-459 maps to [CWE-459: Incomplete Cleanup](https://cwe.mitre.org/data/definitions/459.html). It reports `tempfile.mkdtemp()`, `tempfile.mkstemp()` and `tempfile.NamedTemporaryFile(..., delete=False)` whose result is held in a name, attribute or `with ... as` target that no `shutil.rmtree`, `os.remove`, `os.unlink`, `os.replace`, `Path(...).unlink`, `.cleanup()`, `atexit.register` or `addCleanup` call in the same file receives, or that is created inline in a file with no removal at all. `TemporaryDirectory()`, `NamedTemporaryFile()` with the default `delete=True`, results returned or yielded to the caller, and test files are not reported.

## Why this matters

Temporary files and directories that are never removed accumulate on disk with whatever they held: extracted archives, rendered documents, uploaded data or credentials. On a shared host they outlive the process and can be read by anyone who can list the temporary directory.

## What gets flagged

```python
# FLAGGED: the build directory is never removed
workdir = tempfile.mkdtemp(prefix="build-")
subprocess.run(["make", "-C", workdir], check=True)

# NOT FLAGGED: removed in a finally block
workdir = tempfile.mkdtemp(prefix="build-")
try:
    subprocess.run(["make", "-C", workdir], check=True)
finally:
    shutil.rmtree(workdir, ignore_errors=True)
```

## Remediation

1. Prefer `with tempfile.TemporaryDirectory() as d:` and `with tempfile.NamedTemporaryFile() as f:`.
2. When the object must outlive a block, remove it in a `finally` block or register the cleanup with `atexit` or `addCleanup`.
3. For confirmed false positives, add a `# vulnetix-ignore: VNX-459` comment on the line.

## References

- [CWE-459: Incomplete Cleanup](https://cwe.mitre.org/data/definitions/459.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-459
// nosec vnx-459
```

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