Skip to content

CWE-116: Improper Encoding or Escaping of Output

cwe.mitre.org September 8, 2026

Improper encoding or escaping can allow attackers to change the commands that are sent to another component, inserting malicious commands instead.

Most products follow a certain protocol that uses structured messages for communication between components, such as queries or commands. These structured messages can contain raw data interspersed with metadata or control information. For example, "GET /index.html HTTP/1.1" is a structured message containing a command ("GET") with a single argument ("/index.html") and metadata which protocol version is being used ("HTTP/1.1").

If an application uses attacker-supplied inputs to construct a structured message without properly encoding or escaping, then the attacker could insert special characters that will cause the data to be interpreted as control information or metadata. Consequently, the component that receives the output will perform the wrong operations, or otherwise interpret the data incorrectly.

Modify Application Data

Execute Unauthorized Code or Commands

Bypass Protection Mechanism

Architecture and Design

Strategy: Libraries or Frameworks

Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid.

For example, consider using the ESAPI Encoding control [ REF-45 ] or a similar tool, library, or framework. These will help the programmer encode outputs in a manner less prone to error.

Alternately, use built-in functions, but consider using wrappers in case those functions are discovered to have a vulnerability.

Architecture and Design

Strategy: Parameterization

If available, use structured mechanisms that automatically enforce the separation between data and code. These mechanisms may be able to provide the relevant quoting, encoding, and validation automatically, instead of relying on the developer to provide this capability at every point where output is generated.

For example, stored procedures can enforce database query structure and reduce the likelihood of SQL injection.

Architecture and Design; Implementation

Architecture and Design

Architecture and Design

Class: Not Language-Specific (Often Prevalent)

Class: Not Technology-Specific (Undetermined Prevalence)

AI/ML (Undetermined Prevalence)

Database Server (Often Prevalent)

Web Server (Often Prevalent)

This code displays an email address that was submitted as part of a form.

The value read from the form parameter is reflected back to the client browser without having been encoded prior to output, allowing various XSS attacks ( CWE-79 ).

Consider a chat application in which a front-end web application communicates with a back-end server. The back-end is legacy code that does not perform authentication or authorization, so the front-end must implement it. The chat protocol supports two commands, SAY and BAN, although only administrators can use the BAN command. Each argument must be separated by a single space. The raw inputs are URL-encoded. The messaging protocol allows multiple commands to be specified on the same line if they are separated by a "|" character.

First let's look at the back end command processor code

The front end web application receives a command, encodes it for sending to the server, performs the authorization check, and sends the command to the server.

It is clear that, while the protocol and back-end allow multiple commands to be sent in a single request, the front end only intends to send a single command. However, the UrlEncode function could leave the "|" character intact. If an attacker provides:

then the front end will see this is a "SAY" command, and the $argstr will look like "hello world | BAN user12". Since the command is "SAY", the check for the "BAN" command will fail, and the front end will send the URL-encoded command to the back end:

The back end, however, will treat these as two separate commands:

Notice, however, that if the front end properly encodes the "|" with "%7C", then the back end will only process a single command.

This example takes user input, passes it through an encoding scheme, then lists the contents of the user's directory based on the user name.

The programmer attempts to encode dangerous characters, however the denylist for encoding is incomplete ( CWE-184 ) and an attacker can still pass a semicolon, resulting in a chain with OS command injection ( CWE-78 ).

Additionally, the encoding routine is used inappropriately with command execution. An attacker doesn't even need to insert their own semicolon. The attacker can instead leverage the encoding routine to provide the semicolon to separate the commands. If an attacker supplies a string of the form:

then the program will encode the apostrophe and insert the semicolon, which functions as a command separator when passed to the system function. This allows the attacker to complete the command injection.

Note: this is a curated list of examples for users to understand the variety of ways in which this weakness can be introduced. It is not a complete list of all CVEs that are related to this CWE entry.