Common Weakness Enumeration

A Community-Developed List of Software Weakness Types

CWE/SANS Top 25 Most Dangerous Software Errors

CWE-838: Inappropriate Encoding for Output Context

Weakness ID: 838
Abstraction: Base
Status: Incomplete
Presentation Filter:
+ Description

Description Summary


Extended Description





+ Applicable Platforms



+ Common Consequences

Technical Impact: 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, possibly injecting commands.

+ Demonstrative Examples

Example 1

This code dynamically builds an HTML page using POST data:

(Bad Code)
Example Language: PHP 
$username = $_POST['username'];
$picSource = $_POST['picsource'];
$picAltText = $_POST['picalttext'];


For example, an attacker can set picAltText to:

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

This will result in the generated HTML image tag:

Example Language: HTML 


+ Observed Examples
Server does not properly handle requests that do not contain UTF-8 data; browser assumes UTF-8, allowing XSS.
+ Potential Mitigations

Phase: Implementation

Strategy: 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.

Phase: Architecture and Design

Strategy: 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.

Phase: Architecture and Design

Strategy: Libraries or Frameworks


For example, consider using the ESAPI Encoding control [R.838.7] 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.

+ Relationships
NatureTypeIDNameView(s) this relationship pertains toView(s)
ChildOfWeakness ClassWeakness Class116Improper Encoding or Escaping of Output
Development Concepts (primary)699
Research Concepts (primary)1000
ChildOfCategoryCategory8672011 Top 25 - Weaknesses On the Cusp
MemberOfViewView884CWE Cross-section
CWE Cross-section (primary)884
+ Taxonomy Mappings
Mapped Taxonomy NameNode IDFitMapped Node Name
CERT Java Secure CodingIDS13-JUse compatible encodings on both sides of file or network IO
+ References
[R.838.1] Jim Manico. "Injection-safe templating languages". 2010-06-30. <>.
[R.838.2] Dinis Cruz. "Can we please stop saying that XSS is boring and easy to fix!". 2010-09-25. <>.
[R.838.3] Ivan Ristic. "Canoe: XSS prevention via context-aware output encoding". 2010-09-24. <>.
[R.838.4] Jim Manico. "What is the Future of Automated XSS Defense Tools?". 2011-03-08. <>.
[R.838.5] [REF-15] Jeremiah Grossman, Robert "RSnake" Hansen, Petko "pdp" D. Petkov, Anton Rager and Seth Fogie. "XSS Attacks". Preventing XSS Attacks. Syngress. 2007.
[R.838.6] [REF-20] OWASP. "DOM based XSS Prevention Cheat Sheet". <>.
[R.838.7] [REF-21] OWASP. "OWASP Enterprise Security API (ESAPI) Project". <>.
+ Content History
Submission DateSubmitterOrganizationSource
2011-03-24MITREInternal CWE Team
Modification DateModifierOrganizationSource
2011-06-01CWE Content TeamMITREInternal
updated Common_Consequences, Relationships, Taxonomy_Mappings
2011-06-27CWE Content TeamMITREInternal
updated Demonstrative_Examples, Related_Attack_Patterns, Relationships
2012-05-11CWE Content TeamMITREInternal
updated Potential_Mitigations, References, Relationships, Taxonomy_Mappings
More information is available — Please select a different filter.
Page Last Updated: January 18, 2017 ﻿