# VNX-627: Dynamic Variable Evaluation

Detects a program variable whose name comes from the request.

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

## Overview

VNX-627 maps to [CWE-627: Dynamic Variable Evaluation](https://cwe.mitre.org/data/definitions/627.html). When the client chooses the *name* of the variable a program reads or writes, it can overwrite `$is_admin`, a configuration global or the instance variable that holds the current user, or read a secret the page was never meant to show.

Reported:

- **Python:** `globals()[k]`, `locals()[k]`, `vars()[k]` (or `globals().get(k)`) where `k` is a request value (`request.args`, `form`, `values`, `json`, `GET`, `POST`, `data`, `cookies`, `headers`, `query_params`), and `globals().update(...)`, `vars(x).update(...)`, `x.__dict__.update(...)` with request data.
- **Ruby:** `instance_variable_set`/`get`, `class_variable_set`/`get` and `binding.local_variable_set`/`get` whose name interpolates `params`, `cookies` or request parameters.
- **PHP:** `$$name`, `${$name}` and `${$_GET[...]}` where the name is a superglobal or a variable assigned from one.
- **JavaScript/TypeScript:** `global[...]`, `globalThis[...]`, `window[...]` indexed by `req.query`, `body`, `params`, `cookies` or `headers`.

The request value may be inline or reach the sink through a variable assigned from it, a destructuring, a `for` loop or an `each` block in the 10 lines above.

## What gets flagged

```python
for key, value in request.POST.items():
    globals()[key] = value          # FLAGGED
```

```ruby
instance_variable_set("@#{params[:field]}", params[:value])   # FLAGGED
```

```php
$name = $_GET['var'];
$$name = $_GET['value'];            // FLAGGED
```

```javascript
res.send(String(global[req.query.name]));   // FLAGGED
```

## What is not flagged

- Constant names (`globals()["LAST_LOOKUP"] = name`, `instance_variable_set(:@theme, ...)`).
- Names from function parameters or configuration: there is no cross-function dataflow.
- Ordinary dictionary or object lookups (`HANDLERS.get(name)`, `handlers[req.query.name]`).
- PHP variable variables inside a `foreach` over a superglobal and `extract()`: [VNX-PHP-007](/sast/rules/vnx-php-007.md) reports those.
- The sinks inside comments or string literals.

## Remediation

Never let input name a variable. Map the allowed names to values explicitly:

```python
ALLOWED = {"theme", "locale"}
settings = {k: v for k, v in request.form.items() if k in ALLOWED}
```

## References

- [CWE-627: Dynamic Variable Evaluation](https://cwe.mitre.org/data/definitions/627.html)
- [OWASP Mass Assignment Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Mass_Assignment_Cheat_Sheet.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-627
// nosec vnx-627
```

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