CWE-838: Inappropriate Encoding for Output Context
The product uses or specifies an encoding when generating output to a downstream component, but the specified encoding is not the same as the encoding that is expected by the downstream component.
Last updated
Overview
This weakness can cause the downstream component to use a decoding method that produces different data than what the product intended to send. When the wrong encoding is used - even if closely related - the downstream component could decode the data incorrectly. This can have security consequences when the provided boundaries between control and data are inadvertently broken, because the resulting data could introduce control characters or special elements that were not sent by the product. The resulting data could then be used to bypass protection mechanisms such as input validation, and enable injection attacks. While using output encoding is essential for ensuring that communications between components are accurate, the use of the wrong encoding - even if closely related - could cause the downstream component to misinterpret the output. For example, HTML entity encoding is used for elements in the HTML body of a web page. However, a programmer might use entity encoding when generating output for that is used within an attribute of an HTML tag, which could contain functional Javascript that is not affected by the HTML encoding. While web applications have received the most attention for this problem, this weakness could potentially apply to any type of product that uses a communications stream that could support multiple encodings.
Real-world CVEs
8 recorded CVEs are caused by CWE-838 (Inappropriate Encoding for Output Context). The highest-severity and most recent are shown first. 0 new CWE-838 CVEs have been recorded so far in 2026 (1 in 2025).
- CVE-2025-4052Critical · CVSS 9.8 · EPSS 44th2025-05-05
- CVE-2024-11702High · CVSS 7.5 · EPSS 42th2024-11-26
- CVE-2019-6110Medium · CVSS 6.8 · EPSS 97th2019-01-31
- CVE-2023-6512Medium · CVSS 6.5 · EPSS 66th2023-12-06
- CVE-2023-5770
HTML injection in email body through email subject
Medium · CVSS 5.4 · EPSS 26th2024-01-09 - CVE-2024-34006Medium · CVSS 4.3 · EPSS 28th2024-05-31
- CVE-2023-3735Medium · CVSS 4.3 · EPSS 44th2023-08-01
- CVE-2020-7292Medium · CVSS 4.3 · EPSS 54th2020-07-15
Common consequences
What can happen when CWE-838 is exploited.
Modify Application Data, Execute Unauthorized Code or Commands
Affects: Integrity, Confidentiality, Availability
An attacker could modify the structure of the message or data being sent to the downstream component, possibly injecting commands.
How it happens
When it is introduced
Typically introduced during these phases of the software lifecycle.
How to prevent it
Practical mitigations for CWE-838, grouped by where in the lifecycle they apply.
Use context-aware encoding. That is, understand which encoding is being used by the downstream component, and ensure that this encoding is used. If an encoding can be specified, do so, instead of assuming that the default encoding is the same as the default being assumed by the downstream component.
Where possible, use communications protocols or data formats that provide strict boundaries between control and data. If this is not feasible, ensure that the protocols or formats allow the communicating components to explicitly state which encoding/decoding method is being used. Some template frameworks provide built-in support.
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.
Note that some template mechanisms provide built-in support for the appropriate encoding.
How to detect it
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 having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Effectiveness: High
Code examples
Illustrative examples from MITRE showing how the weakness appears in code.
This code dynamically builds an HTML page using POST data:
Vulnerable example
$username = $_POST['username'];Attack input
"altTextHere' onload='alert(document.cookie)"Resulting query
<img src='pic.jpg' alt='altTextHere' onload='alert(document.cookie)' />Illustrative examples
Real CVEs that MITRE cites as examples of this weakness.
- CVE-2009-2814 — Server does not properly handle requests that do not contain UTF-8 data; browser assumes UTF-8, allowing XSS.
Terminology & mappings
Mapped taxonomies
- The CERT Oracle Secure Coding Standard for Java (2011): Use compatible encodings on both sides of file or network IO (IDS13-J)
Attack patterns
CAPEC attack patterns that exploit this weakness.
Frequently asked questions
Common questions about CWE-838.
- What is CWE-838?
- The product uses or specifies an encoding when generating output to a downstream component, but the specified encoding is not the same as the encoding that is expected by the downstream component.
- What CVEs are caused by CWE-838?
- 8 recorded CVEs are attributed to CWE-838, including CVE-2025-4052, CVE-2024-11702, CVE-2019-6110.
- How do you prevent CWE-838?
- Use context-aware encoding. That is, understand which encoding is being used by the downstream component, and ensure that this encoding is used. If an encoding can be specified, do so, instead of assuming that the default encoding is the same as the default being assumed by the downstream component.
- How is CWE-838 detected?
- 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 having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
- What are the consequences of CWE-838?
- Exploiting CWE-838 can lead to: Modify Application Data, Execute Unauthorized Code or Commands.
- Is CWE-838 actively exploited?
- 8 recorded CVEs are caused by CWE-838; none are currently in CISA's KEV catalog of actively exploited flaws.
References
- MITRE CWE definition (CWE-838) (opens in a new tab)
- CWE-838 vulnerabilities on NVD (opens in a new tab)
- Learn: What is a CWE?
Weakness data is sourced from the MITRE CWE catalog (v4.20). CVE associations are aggregated and kept current by RadicalNotion.AI.
Stay ahead of CWE-838
Get alerted the moment a new CWE-838 vulnerability affects your stack, with AI-written analysis, severity context, and remediation guidance.