Security: Fix CWE-78 (Command Injection) vulnerability in src/main/java/org/owasp/benchmark/testcode/BenchmarkTest01936.java:62#672
Open
appsecai-app[bot] wants to merge 1 commit intomainfrom
Conversation
Apply allowlist validation [a-zA-Z0-9 ]* to HTTP request header input before passing to Runtime.exec(). Blocks shell metacharacters and prevents arbitrary OS command execution. Fixes: 69c734eb (CWE-78 Medium)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What we found
69c734ebsrc/main/java/org/owasp/benchmark/testcode/BenchmarkTest01936.java:62Description: User-controlled input from an HTTP request header flows directly into an OS command execution sink without validation. An attacker can inject arbitrary operating system commands by crafting malicious header values, potentially achieving full system compromise.
Why this matters
Risk if not fixed: An attacker could execute arbitrary operating system commands on the server by sending a crafted HTTP request header. This enables:
Attack surface: Any HTTP client can send arbitrary header values; no special privileges or authentication required.
Why we're changing it
The vulnerable code path:
BenchmarkTest01936is read directly intoparam— attacker controls this value%3B→;,%26→&), widening the attack surfacedoSomething()performs no sanitization; it retrievesparamunchanged and returns it asbarRuntime.exec(cmd + bar)concatenates the tainted value into a command string passed to the OSExploitation example: An attacker sends header
BenchmarkTest01936: test; cat /etc/passwd. After URL-decoding and flowing throughdoSomething(), this becomes part of the command executed by the OS, allowing arbitrary command injection.How we confirmed
The vulnerability was confirmed through:
doSomething()(lines 77-84) with no transformation or validationRuntime.exec()at line 62 as part of a concatenated command stringVulnerability Flow Diagram
%%{init: {'theme':'base','themeVariables':{'fontFamily':'ui-sans-serif, Inter, system-ui, sans-serif','primaryColor':'#EDE9FE','primaryTextColor':'#1A1A2E','primaryBorderColor':'#7C3AED','lineColor':'#5B21B6','secondaryColor':'#FEF3C7','tertiaryColor':'#DCFCE7'}}}%% flowchart TD A["HTTP Request Header<br/>BenchmarkTest01936"] --> B["URL Decode<br/>Line 49"] B --> C["doSomething()<br/>Lines 77-84<br/>No Sanitization"] C --> D["Runtime.exec<br/>Line 62"] D --> E["❌ Arbitrary OS Command<br/>Executed"] F["✅ Allowlist Validation<br/>a-zA-Z0-9 only"] -.-> G["Safe Input Passes"] H["Malicious Input<br/>Blocked & Rejected"] -.-> I["Early Return<br/>Error Response"] style A fill:#EDE9FE,stroke:#7C3AED style B fill:#EDE9FE,stroke:#7C3AED style C fill:#FFE5E5,stroke:#F65A5A style D fill:#FFE5E5,stroke:#F65A5A style E fill:#FEF3C7,stroke:#F59E0B style F fill:#DCFCE7,stroke:#16A34A style G fill:#DCFCE7,stroke:#16A34A style H fill:#DCFCE7,stroke:#16A34A style I fill:#DCFCE7,stroke:#16A34AVulnerable flow: src/main/java/org/owasp/benchmark/testcode/BenchmarkTest01936.java:62
Command Injection
%%{init: {'theme':'base','themeVariables':{'fontFamily':'ui-sans-serif, Inter, system-ui, sans-serif','primaryColor':'#EDE9FE','primaryTextColor':'#1A1A2E','primaryBorderColor':'#7C3AED','lineColor':'#5B21B6','secondaryColor':'#FEF3C7','tertiaryColor':'#DCFCE7'}}}%% flowchart TD subgraph Vulnerable["❌ Vulnerable Flow"] direction LR A1["Project"] --> A2["OS command injection via user input"] A2 --> A3["💥 OS Command Executed"] end Vulnerable ~~~ Fixed subgraph Fixed["✅ Fixed Flow"] direction LR B1["Project"] --> B2["Command allowlist or subprocess array"] B3["🛡️ Attack Blocked"] B2 --> B3 end style A2 fill:#FFE5E5,color:#000 style A3 fill:#ffa94d,color:#000 style B2 fill:#74c0fc,color:#000 style B3 fill:#DCFCE7,color:#000How we fixed it
Root cause: User-controlled data from the HTTP request header reaches
Runtime.exec()without any validation, allowing arbitrary command injection.Fix approach: An allowlist regex validation
[a-zA-Z0-9 ]*is applied tobarimmediately before it is placed into the command array. Any input containing characters outside the permitted set causes an early return with a sanitized error response, preventing the tainted value from ever reachingRuntime.exec().Why this approach:
Alternatives considered and rejected:
encodeForOS()— encoding still passes user-controlled data to the OS command and does not eliminate the CWE-78 sink; SAST would continue to flag itVulnerabilities Addressed
CWE-78
Tainted Cmd From Http Request
How we validated it
[a-zA-Z0-9 ]*is verified before use;,&,|,$, backticks, etc.) is rejected with an error responseRuntime.exec()How to verify
Manual verification steps:
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest01936.javaRuntime.exec()[a-zA-Z0-9 ]*is applied to thebarvariableRunnable Verification Script (click to expand)
Save this script and run with
bash verify_fix.sh:Before you merge
Learn more
This fix was generated by AppSecAI. Please review before merging.