CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection')

Description:
The product constructs all or part of a command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended command when it is sent to a downstream component.

Extended Description:
Many protocols and products have their own custom command language. While OS or shell command strings are frequently discovered and targeted, developers may not realize that these other command languages might also be vulnerable to attacks.

Affected languages: Not Language-Specific
Affected technologies: AI/ML
Parent weaknesses: CWE-74, CWE-74

Consequences:
  - Integrity: Execute Unauthorized Code or Commands

Detection methods:
  - Automated Static Analysis: Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without havin

Mitigations:
  - [Architecture and Design] If at all possible, use library calls rather than external processes to recreate the desired functionality.
  - [Implementation] If possible, ensure that all external commands called from the program are statically created.
  - [Implementation] Assume all input is malicious. Use an "accept known good" input validation strategy, i.e., use a list of acceptable inputs that strictly conform to specifications. Reject any input that does not strictly conform to specifications, or transform it into something that does. 
                   When pe
  - [Operation] Run time: Run time policy enforcement may be used in an allowlist fashion to prevent use of any non-sanctioned commands.

Demonstrative examples:
Consider a "CWE Differentiator" application that uses an an LLM generative AI based "chatbot" to explain the difference between two weaknesses.  As input, it accepts two CWE IDs, constructs a prompt string, sends the prompt to the chatbot, and prints the results. The prompt string effectively acts as a command to the chatbot component. Assume that invokeChatbot() calls the chatbot and returns the response as a string; the implementation details are not important here.
To avoid XSS risks, the code ensures that the response from the chatbot is properly encoded for HTML output. If the user provides CWE-77 and CWE-78, then the resulting prompt would look like:
prompt = "Explain the difference between {} and {}".format(arg1, arg2) 
				   result = invokeChatbot(prompt) 
				   resultHTML = enco

Consider the following program. It intends to perform an "ls -l" on an input filename. The validate_name() subroutine performs validation on the input to make sure that only alphanumeric and "-" characters are allowed, which avoids path traversal (CWE-22) and OS command injection (CWE-78) weaknesses. Only filenames like "abc" or "d-e-f" are intended to be allowed.
However, validate_name() allows
			  filenames that begin with a "-". An adversary could
			  supply a filename like "-aR", producing the "ls -l -aR"
			  command (CWE-88), thereby getting a full recursive
			  listing of the entire directory and all of its
			  sub-directories. 

			   There are a couple possible mitigations for this
			  weakness. One would be to refactor the code to avoid
			  using system() altogether, instea

Real-world examples:
  - CVE-2022-1509: injection of sed script syntax ("sed injection")
  - CVE-2024-5184: API service using a large generative AI model allows direct prompt injection to leak hard-coded system prompts or execute other prompts.
  - CVE-2020-11698: anti-spam product allows injection of SNMP commands into confiuration file
  - CVE-2019-12921: image program allows injection of commands in "Magick Vector Graphics (MVG)" language.
  - CVE-2022-36069: Python-based dependency management tool avoids OS command injection when generating Git commands but allows injection of optional arguments with input beginning with a dash (CWE-88), potentially allow