[high] semgrep: Improper neutralization of special elements used in an OS command ('OS Command Injection') #597

Closed
opened 2026-11-03 19:26:22 +00:00 by pruefwerk · 4 comments
Member

high Finding

Scanner: semgrep
Severity: high
Rule: gitlab-csharp_injection_rule-CommandInjection
CWE: CWE-78
CRA: cra-ai-20 — Automatisierte Sicherheitstests (Annex I)

Description

OS command injection is a critical vulnerability that can lead to a full system
compromise as it may allow an adversary to pass in arbitrary commands or arguments
to be executed.

User input should never be used in constructing commands or command arguments
to functions which execute OS commands. This includes filenames supplied by
user uploads or downloads.

Ensure your application does not:

  • Use user-supplied information in the process name to execute.
  • Use user-supplied information in an OS command execution function which does
    not escape shell meta-characters.
  • Use user-supplied information in arguments to OS commands.

The application should have a hardcoded set of arguments that are to be passed
to OS commands. If filenames are being passed to these functions, it is
recommended that a hash of the filename be used instead, or some other unique
identifier. It is strongly recommended that a native library that implements
the same functionality be used instead of using OS system commands, due to the
risk of unknown attacks against third party commands.

When specifying the OS command, ensure the application uses the full path
information, otherwise the OS may attempt to look up which process to execute
and could be vulnerable to untrusted search path vulnerabilities (CWE-426).

Example of safely executing an OS command:

public void ExecuteCommand(string userFileData) {
    // generate a random filename, do not using user input
    string fileName = "C:\\Temp\\" + Guid.NewGuid();
    File.WriteAllText(fileName, userFileData);

    using (Process process = new Process())
    {
        // hardcode the full process path
        ProcessStartInfo processInfo = new ProcessStartInfo("C:\\App\\FileReader.exe");
        // only pass in trust arguments, and never direct user input.
        processInfo.Arguments = fileName;
        processInfo.UseShellExecute = false;
        process.StartInfo = processInfo;
        process.Start();
    }
}

For more information on OS command injection, see OWASP's guide:
https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html

Location

File: compliance-agent/rules/cra_semgrep_csharp.cs (line 67)

Code

            psi.Arguments = $"--target {host}";

Remediation


Fingerprint: 46efe8fa511098716ea5981dbca61d2d997d108cf4c33318078391cbd0af59ec
Generated by compliance-scanner
Open in Prüfwerk / In Prüfwerk öffnen

## high Finding **Scanner:** semgrep **Severity:** high **Rule:** gitlab-csharp_injection_rule-CommandInjection **CWE:** CWE-78 **CRA:** cra-ai-20 — Automatisierte Sicherheitstests (Annex I) ### Description OS command injection is a critical vulnerability that can lead to a full system compromise as it may allow an adversary to pass in arbitrary commands or arguments to be executed. User input should never be used in constructing commands or command arguments to functions which execute OS commands. This includes filenames supplied by user uploads or downloads. Ensure your application does not: - Use user-supplied information in the process name to execute. - Use user-supplied information in an OS command execution function which does not escape shell meta-characters. - Use user-supplied information in arguments to OS commands. The application should have a hardcoded set of arguments that are to be passed to OS commands. If filenames are being passed to these functions, it is recommended that a hash of the filename be used instead, or some other unique identifier. It is strongly recommended that a native library that implements the same functionality be used instead of using OS system commands, due to the risk of unknown attacks against third party commands. When specifying the OS command, ensure the application uses the full path information, otherwise the OS may attempt to look up which process to execute and could be vulnerable to untrusted search path vulnerabilities (CWE-426). Example of safely executing an OS command: ``` public void ExecuteCommand(string userFileData) { // generate a random filename, do not using user input string fileName = "C:\\Temp\\" + Guid.NewGuid(); File.WriteAllText(fileName, userFileData); using (Process process = new Process()) { // hardcode the full process path ProcessStartInfo processInfo = new ProcessStartInfo("C:\\App\\FileReader.exe"); // only pass in trust arguments, and never direct user input. processInfo.Arguments = fileName; processInfo.UseShellExecute = false; process.StartInfo = processInfo; process.Start(); } } ``` For more information on OS command injection, see OWASP's guide: https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html ### Location **File:** `compliance-agent/rules/cra_semgrep_csharp.cs` (line 67) ### Code ``` psi.Arguments = $"--target {host}"; ``` ### Remediation --- *Fingerprint:* `46efe8fa511098716ea5981dbca61d2d997d108cf4c33318078391cbd0af59ec` *Generated by compliance-scanner* [Open in Prüfwerk / In Prüfwerk öffnen](https://pruefwerk.breakpilot.com/findings?target=6ae9aa2160f5d8045fc570a7)
pruefwerk added the pruefwerkcra:annex-iseverity:high labels 2026-11-03 19:26:22 +00:00
Author
Member

Prüfwerk: Die Datei liegt in Testdaten (z. B. fixtures/ oder testdata/), die von der Prüfung ausgenommen sind.

The file lies in test data (such as fixtures/ or testdata/), which is left out of the scan.

**Prüfwerk:** Die Datei liegt in Testdaten (z. B. fixtures/ oder testdata/), die von der Prüfung ausgenommen sind. The file lies in test data (such as fixtures/ or testdata/), which is left out of the scan.
Author
Member

Prüfwerk: Die Datei liegt in Testdaten (z. B. fixtures/ oder testdata/), die von der Prüfung ausgenommen sind.

The file lies in test data (such as fixtures/ or testdata/), which is left out of the scan.

**Prüfwerk:** Die Datei liegt in Testdaten (z. B. fixtures/ oder testdata/), die von der Prüfung ausgenommen sind. The file lies in test data (such as fixtures/ or testdata/), which is left out of the scan.
Author
Member

Prüfwerk: Die Datei liegt in Testdaten (z. B. fixtures/ oder testdata/), die von der Prüfung ausgenommen sind.

The file lies in test data (such as fixtures/ or testdata/), which is left out of the scan.

**Prüfwerk:** Die Datei liegt in Testdaten (z. B. fixtures/ oder testdata/), die von der Prüfung ausgenommen sind. The file lies in test data (such as fixtures/ or testdata/), which is left out of the scan.
Author
Member

Prüfwerk: Die Datei liegt in Testdaten (z. B. fixtures/ oder testdata/), die von der Prüfung ausgenommen sind.

The file lies in test data (such as fixtures/ or testdata/), which is left out of the scan.

**Prüfwerk:** Die Datei liegt in Testdaten (z. B. fixtures/ oder testdata/), die von der Prüfung ausgenommen sind. The file lies in test data (such as fixtures/ or testdata/), which is left out of the scan.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: platform/pruefwerk#597