What Is CWE-116? Improper Output Encoding Explained
CWE-116 improper output encoding is the weakness behind XSS, log forging and a critical Synology NAS RCE. How it becomes code execution and how to prevent it.
CWE-116, improper encoding or escaping of output, is the weakness where a program builds a structured message for another component and fails to escape the data inside it, so the receiver reads part of the data as instructions. It sits behind cross-site scripting, log forging, header injection and command injection. It is also the weakness class MITRE assigned to Synology DSM CVE-2024-10441, the CVSS 9.8 NAS bug now in CISA's exploited list. Understanding it tells you why a two-year-old encoding bug still yields unauthenticated code execution.
What is CWE-116?
CWE-116 is MITRE's category for output that is not encoded or escaped correctly for the component that will consume it. The sending program mixes raw data with control characters, and the receiving program cannot tell which is which. The result is that attacker-supplied data changes the structure of a query, command, log line or page.
MITRE's own description is compact: the product builds a structured message for another component, but encoding or escaping of the data is either missing or done incorrectly, so the message's intended structure is not preserved. Every structured format has characters that carry meaning. In HTML it is angle brackets and quotes. In a shell command it is semicolons, pipes and backticks. In a log file it is the newline. In a URL it is the ampersand and the percent sign. Output encoding is the step that neutralizes those characters before the data crosses the boundary.
Why is CWE-116 different from input validation?
Input validation checks data when it arrives. Output encoding transforms data when it leaves, for the specific destination it is going to. MITRE lists input validation (CWE-20) as related but says it is not always sufficient, because the same string can be safe for one destination and dangerous for another.
A username that is harmless in a database query can break an HTML page. A file path that is fine on disk can inject a command when passed to a shell. Validation at the front door cannot know every place the data will end up. Encoding at each exit can. That is why secure coding guidance treats the two as separate controls and expects both.
How does an encoding bug become remote code execution?
The chain runs from CWE-116 to one of the injection weaknesses under CWE-74. MITRE marks CWE-116 as "CanPrecede" CWE-74, meaning the encoding failure is the root cause and the injection is the symptom. Three of MITRE's demonstrative examples show the range:
- A web page prints a form value without encoding, and the browser executes it as script. That is cross-site scripting, CWE-79.
- A chat front end sends
SAYcommands to a back end using a pipe as the separator and does not encode pipes in the message text. A user types a pipe followed byBANand the back end runs an admin command. That is a protocol-level injection. - A Perl script encodes a denylist of dangerous characters but misses the semicolon. The operating system runs whatever follows it. That is OS command injection, CWE-78.
The Synology case follows the same pattern at the system level. Per the CVE record, the system plugin daemon in DSM and BeeStation OS has an improper encoding or escaping flaw that lets a remote attacker execute arbitrary code with no authentication and no user interaction. Synology calls the vector "unspecified" and has not published details, but the weakness class alone tells you the shape: data from the network reached a component that interpreted part of it as a command. The full story of how that bug ended up in a ransomware campaign is in our report on Synology DSM CVE-2024-10441 and C3RB3R ransomware.
Where CWE-116 sits in the CWE tree
The parent is CWE-707, improper neutralization, which is one of MITRE's pillar weaknesses. CWE-116 has three children that name specific contexts:
- CWE-117, improper output neutralization for logs. Newlines in user data let an attacker forge log entries.
- CWE-644, improper neutralization of HTTP headers for scripting syntax.
- CWE-838, inappropriate encoding for output context, where encoding was applied but for the wrong destination, such as HTML-encoding data that goes into JavaScript.
CRLF injection, which we covered in what CRLF injection is, is a close cousin: it is the delimiter-injection consequence that an unencoded carriage return or line feed produces in headers and logs.
What are the consequences MITRE lists?
MITRE's common consequences for CWE-116 span integrity, confidentiality, availability and access control. The listed impacts are modifying application data, executing unauthorized code or commands, and bypassing protection mechanisms. In practice the severity depends entirely on what the downstream component is. If it is a web browser, you get XSS. If it is a shell or an interpreter running as root on an appliance, you get full device compromise, which is the Synology outcome.
How do you prevent CWE-116?
MITRE's mitigations are specific and worth checking against your own code and your vendors' advisories:
- Use a vetted encoding library for each output context rather than hand-written escaping. MITRE names OWASP ESAPI as an example. Hand-rolled denylists are how the Perl example above failed.
- Prefer structured mechanisms that separate data from code. Parameterized queries and stored procedures remove the need to escape SQL at all. The same idea applies to any API that accepts data and command as separate arguments.
- Know exactly what encoding the downstream component expects and specify it fully. Ambiguity about character sets is itself an attack surface.
- Use allowlist input validation as defense in depth, especially when some control syntax must legitimately pass through.
- Make both components agree on character encoding and set it explicitly wherever the protocol allows.
For the people operating appliances rather than writing them, the lesson is about attack surface. An encoding bug in a daemon that listens on the network is reachable by anyone who can reach the port. Reducing who can reach the port, which is the core of securing a Synology NAS against ransomware, limits the blast radius of the bugs you have not patched yet.
How to read CWE-116 in an advisory
When a vendor advisory or a CISA KEV entry lists CWE-116, ask three questions. Does the affected component accept network input without authentication? If yes, the bug is likely pre-auth. What does the component hand its output to? A shell, an interpreter or a privileged service means code execution. Does the fix change encoding logic or does it add input filtering? Filtering alone tends to get bypassed, which is why the same product often ships a second advisory for the same component months later.