java-os-command-injection-mining
Mine OS command sinks (Runtime.exec, ProcessBuilder) built from dynamic input that may enable command injection (javasecurity:S2076); read-only metrics for security review
# java-os-command-injection-mining
Read-only **mining** codemod for SonarSource rule [`javasecurity:S2076`](https://rules.sonarsource.com/java/) — _OS commands should not be vulnerable to command injection attacks_.
It scans Java source and emits **metrics only**; it never modifies code (`return null`).
Findings are high-signal review candidates surfaced by AST pattern matching — they are
**not** proven vulnerabilities. A static mining pass cannot prove an end-to-end taint flow
from an untrusted source to the sink, so confirm data flow before remediating.
| Field | Value |
| ----------- | ----------------------------- |
| Rule key | `javasecurity:S2076` |
| Type | Vulnerability |
| Severity | Blocker |
| Primary CWE | CWE-78 (OS Command Injection) |
| Tags | `cwe` |
## What it detects
OS command-execution sinks whose arguments are built from dynamic data.
**Sinks:**
- `Runtime.getRuntime().exec(...)`
- `new ProcessBuilder(...)`
- `ProcessBuilder.command(...)`
**Emission rule:** Only emits when an argument is dynamic. Calls using only constant literals are skipped.
## Metric: `java-os-command-injection-mining`
| dimension | meaning |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `rule` | SonarSource rule key |
| `cwe` | primary CWE identifier |
| `severity` | always `blocker` |
| `sink` | the detected sink (method/constructor) |
| `package_name` | enclosing Java package |
| `class_name` | enclosing class/interface/enum/record |
| `method_name` | enclosing method/constructor (or `<none>`) |
| `file` | file path (relative to scan target) |
| `line` | 1-based line of the finding |
| `arg_kind` | classification of the relevant argument (`literal`, `concatenation`, `variable`, `method_call`, `field`, `object_creation`, `expression`, `none`) |
## Security standards
- CWE-20 (Improper Input Validation)
- CWE-78 (OS Command Injection)
- OWASP ASVS 4.0: 5.1.3, 5.1.4, 5.3.8, 12.3.5
- OWASP Mobile Top 10 2024: M4
- OWASP Top 10 2021: A3
- OWASP Top 10: A1
- PCI DSS 3.2: 6.5.1
- PCI DSS 4.0: 6.2.4
- STIG ASD V5R3: V-222604, V-222609
## Run locally
```bash
cd codemods/java-security-blocker-mining/java-os-command-injection-mining
npm test
# Scan a target project (read-only; emits metrics, leaves source unchanged)
npx codemod@latest workflow run \
-w codemods/java-security-blocker-mining/java-os-command-injection-mining/workflow.yaml \
--target /path/to/java/project
```
> Part of the **java-security-blocker-mining** suite covering 19 blocker-severity
> Java vulnerability rules. Each codemod is read-only and emits a single named metric.