---
title: "CWE-838: Inappropriate Encoding for Output Context"
url: https://radicalnotion.ai/cwe/cwe-838
type: cwe
description: "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."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-838: Inappropriate Encoding for Output Context

- **Abstraction:** Base
- **Structure:** Simple
- **Status:** Incomplete
- **CVEs with this as primary weakness:** 15
- **Of those, in CISA KEV:** 0
- **New in 2026:** 4
- **CWE catalog version:** 4.21

## Description

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.

Using the correct output encoding is essential for ensuring that communications between components are accurate. 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 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.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | - |

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Integrity, Confidentiality, Availability | Modify Application Data, Execute Unauthorized Code or Commands | An attacker could modify the structure of the message or data being sent to the downstream component. When the wrong encoding is used - even if closely related - the downstream component could decode the data incorrectly and misinterpret the output. 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. |

## Potential mitigations

### Implementation (Output Encoding)

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.

### Architecture and Design (Output Encoding)

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.

### Architecture and Design (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.

Note that some template mechanisms provide built-in support for the appropriate encoding.

## 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 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

## Demonstrative examples

### Example 1

This code dynamically builds an HTML page using POST data:

Bad code:

```php
$username = $_POST['username'];
$picSource = $_POST['picsource'];
$picAltText = $_POST['picalttext'];
...

echo "<title>Welcome, " . htmlentities($username) ."</title>";
echo "<img src='". htmlentities($picSource) ." ' alt='". htmlentities($picAltText) . '" />';
...
```

Attack code:

```
"altTextHere' onload='alert(document.cookie)"
```

Result code:

```html
<img src='pic.jpg' alt='altTextHere' onload='alert(document.cookie)' />
```

## Observed examples

| CVE | Description |
| --- | --- |
| CVE-2009-2814 | Server does not properly handle requests that do not contain UTF-8 data; browser assumes UTF-8, allowing XSS. |

## Real CVEs with this weakness (sample of 12 of 15)

| CVE | Title | Severity | CVSS | EPSS | CISA KEV | Published |
| --- | --- | --- | --- | --- | --- | --- |
| CVE-2025-4052 | Inappropriate implementation in DevTools | critical | 9.8 | - | No | 2025-05-05 |
| CVE-2019-18981 | Pimcore Access Control Issues | critical | 9.8 | - | No | 2019-11-15 |
| CVE-2018-9862 | - | high | 7.8 | - | No | 2018-04-09 |
| CVE-2024-11702 | firefox: thunderbird: Inadequate Clipboard Protection in Private Browsing Mode on Android | high | 7.5 | - | No | 2024-11-26 |
| CVE-2026-114650 | Plaintext exposure of encrypted fields via ANSI code-page corruption of database name | medium | 6.9 | - | No | 2026-10-22 |
| CVE-2019-6110 | In OpenSSH 7.9, due to accepting and displaying arbitrary stderr output from the server, a malicious server (or Man-in-The-Middle attacker) can manipulate the client output, for example to use ANSI control codes to hide additional files being transferred. | medium | 6.8 | - | No | 2019-01-31 |
| CVE-2023-6512 | Inappropriate implementation in Web Browser UI | medium | 6.5 | - | No | 2023-12-06 |
| CVE-2026-119281 | MariaDB Connector/J: Server could switch character\_set\_results away from UTF-8 without the client noticing | medium | 5.9 | - | No | 2026-10-29 |
| CVE-2026-55858 | MariaDB Connector/J: Inappropriate Encoding for Output Context in org.mariadb.jdbc:mariadb-java-client | medium | 5.9 | - | No | 2026-08-28 |
| CVE-2023-5770 | HTML injection in email body through email subject | medium | 5.4 | - | No | 2024-01-09 |
| CVE-2024-34006 | moodle: unsanitized HTML in site log for config\_log\_created | medium | 4.3 | - | No | 2024-05-31 |
| CVE-2023-3735 | Inappropriate implementation in Web API Permission Prompts | medium | 4.3 | - | No | 2023-08-01 |

## Related attack patterns (CAPEC)

- [CAPEC-468](https://radicalnotion.ai/capec/capec-468): Generic Cross-Browser Cross-Domain Theft

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-116](https://radicalnotion.ai/cwe/cwe-116) | Improper Encoding or Escaping of Output |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| The CERT Oracle Secure Coding Standard for Java (2011) | Use compatible encodings on both sides of file or network IO | IDS13-J | - |

## Frequently asked questions

### 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?

15 recorded CVEs are attributed to CWE-838, including CVE-2025-4052, CVE-2019-18981 and CVE-2018-9862.

### 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?

15 recorded CVEs are caused by CWE-838; none are currently in CISA's KEV catalog of actively exploited flaws.

Primary source: [MITRE CWE](https://cwe.mitre.org/data/definitions/838.html)

---

Source: [RadicalNotion.AI](https://radicalnotion.ai/cwe/cwe-838). Licensed CC BY-NC 4.0; attribute RadicalNotion.AI and link the source page when you reuse this data.
