# Cwe 78 Os Command Injection

> Use this skill when you need to remediate CWE-78 (Improper Neutralization of OS Command) vulnerabilities in Java code. Triggers on SAST findings, security reviews, or when fixing improper neutralization of os command issues.

- Skill: `developerscoffee/cwe-78-os-command-injection` (Agent Skill)
- Install (CLI): `npx skillmds@latest add developerscoffee/cwe-78-os-command-injection`
- Raw SKILL.md: https://api.skillmd.com/api/skills/developerscoffee/cwe-78-os-command-injection/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Security
- License: MIT
- Author: DevelopersCoffee (https://skillmd.com/u/developerscoffee)
- Updated: 2026-09-10
- Page: https://skillmd.com/skills/developerscoffee/cwe-78-os-command-injection

---


# CWE-78 Improper Neutralization of OS Command

## Description

Improper Neutralization of OS Command

Reference:
https://cwe.mitre.org/data/definitions/78.html


**OWASP Category**: A03:2021 – Injection


---

## Vulnerable Pattern


### ❌ Example 1: Vulnerable Pattern

```java
// VULNERABLE: Shell metacharacters not neutralized
String filename = request.getParameter("file");
Runtime.getRuntime().exec("cat " + filename);
```


**Why it's vulnerable:** This pattern is vulnerable to Improper Neutralization of OS Command




---

## Deterministic Fix


### ✅ Secure Implementation: Secure Implementation

```java
// SECURE: Validate and use ProcessBuilder
String filename = request.getParameter("file");
if (!filename.matches("^[a-zA-Z0-9_.-]+$")) {
    throw new SecurityException("Invalid filename");
}
ProcessBuilder pb = new ProcessBuilder("cat", filename);
Process p = pb.start();
```


**Why it's secure:** Implements proper protection against Improper Neutralization of OS Command




---

## Detection Pattern

Look for these patterns in your codebase:


```bash
# Find exec with user input
grep -rn "exec.*getParameter\\|exec.*request" --include="*.java"
```



---

## Remediation Steps


1. Validate input against whitelist of allowed characters

2. Use parameterized command execution (ProcessBuilder)

3. Escape shell metacharacters if shell execution is unavoidable


---

## Key Imports

```java

import java.lang.ProcessBuilder;

```

---

## Verification

After remediation:


- Run SAST scanner to confirm vulnerability is resolved

- Review all instances of the vulnerable pattern

- Add unit tests that verify the secure implementation

- Check for similar patterns in related code


---

## Trigger Examples

```
Fix CWE-78 vulnerability
Resolve Improper Neutralization of OS Command issue
Secure this Java code against improper neutralization of os command
SAST reports CWE-78
```

---

## Common Vulnerable Locations

| Layer | Files | Patterns |
|-------|-------|----------|

| Controller | `*Controller.java` | User input handling |

| Service | `*Service.java` | Business logic |

| Repository | `*Repository.java` | Data access |


---

## References


- [CWE-78: OS Command Injection](https://cwe.mitre.org/data/definitions/78.html)


---

**Source**: Generated by [Java CWE Security Skills Generator](https://github.com/DevelopersCoffee/java-cwe-security-skills)
**Last Updated**: 2026-03-07

