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.)
Inappropriate Encoding for Output Context
This vulnerability occurs when a system uses one type of encoding for its output, but the component receiving that data expects a different encoding. The mismatch causes the downstream component to…
What is CWE-838?
Real-world CVEs caused by CWE-838
-
Server does not properly handle requests that do not contain UTF-8 data; browser assumes UTF-8, allowing XSS.
Angreiferpfad Schritt für Schritt
- 1
This code dynamically builds an HTML page using POST data:
- 2
The programmer attempts to avoid XSS exploits (CWE-79) by encoding the POST values so they will not be interpreted as valid HTML. However, the htmlentities() encoding is not appropriate when the data are used as HTML attributes, allowing more attributes to be injected.
- 3
For example, an attacker can set picAltText to:
- 4
This will result in the generated HTML image tag:
- 5
The attacker can inject arbitrary javascript into the tag due to this incorrect encoding.
Vulnerable PHP
This code dynamically builds an HTML page using POST data:
$username = $_POST['username'];
$picSource = $_POST['picsource'];
$picAltText = $_POST['picalttext'];
```
...*
echo "<title>Welcome, " . htmlentities($username) ."</title>";
echo "<img src='". htmlentities($picSource) ." ' alt='". htmlentities($picAltText) . '" />';
*...* For example, an attacker can set picAltText to:
"altTextHere' onload='alert(document.cookie)" Secure pseudo
// Validate, sanitize, or use a safe API before reaching the sink.
function handleRequest(input) {
const safe = validateAndEscape(input);
return executeWithGuards(safe);
} How to prevent CWE-838
- Implementation 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 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 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 CWE-838
Plexicus erkennt CWE-838 automatisch und öffnet in unter 60 Sekunden einen Fix-PR.
Codex Remedium scannt jeden Commit, identifiziert genau diese Schwachstelle und liefert einen reviewer-ready Pull Request mit dem Patch. Keine Tickets. Keine Hand-offs.
Frequently asked questions
Was ist CWE-838?
This vulnerability occurs when a system uses one type of encoding for its output, but the component receiving that data expects a different encoding. The mismatch causes the downstream component to interpret the data incorrectly.
Wie gravierend ist CWE-838?
MITRE hat für diese Schwachstelle keine Exploit-Wahrscheinlichkeit veröffentlicht. Behandle sie als mittlere Auswirkung, bis dein Threat Model anderes belegt.
Welche Sprachen oder Plattformen sind von CWE-838 betroffen?
MITRE hat für diese CWE keine betroffenen Plattformen spezifiziert — sie kann in den meisten Anwendungs-Stacks auftreten.
Wie kann ich CWE-838 verhindern?
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…
Wie erkennt und behebt Plexicus CWE-838?
Die SAST-Engine von Plexicus erkennt die Datenfluss-Signatur von CWE-838 bei jedem Commit. Bei einem Treffer öffnet unser Codex-Remedium-Agent einen Fix-PR mit korrigiertem Code, Tests und einer einzeiligen Zusammenfassung für den Reviewer.
Wo erfahre ich mehr über CWE-838?
MITRE veröffentlicht die kanonische Definition unter https://cwe.mitre.org/data/definitions/838.html. Für ergänzende Hinweise kannst du auch die OWASP- und NIST-Dokumentation heranziehen.
Weaknesses related to CWE-838
Improper Encoding or Escaping of Output
This vulnerability occurs when an application builds a structured message—like a query, command, or request—for another component but…
Improper Output Neutralization for Logs
This vulnerability occurs when an application creates log entries using unvalidated external data, allowing attackers to inject malicious…
Improper Neutralization of HTTP Headers for Scripting Syntax
This vulnerability occurs when an application fails to properly sanitize or escape user-controlled data placed within HTTP response…
Further reading
- MITRE — offizielle CWE-838 https://cwe.mitre.org/data/definitions/838.html
- Injection-safe templating languages https://manicode.blogspot.com/2010/06/injection-safe-templating-languages_30.html
- Can we please stop saying that XSS is boring and easy to fix! http://diniscruz.blogspot.com/2010/09/can-we-please-stop-saying-that-xss-is.html
- Canoe: XSS prevention via context-aware output encoding https://blog.ivanristic.com/2010/09/introducing-canoe-context-aware-output-encoding-for-xss-prevention.html
- What is the Future of Automated XSS Defense Tools? http://software-security.sans.org/downloads/appsec-2011-files/manico-appsec-future-tools.pdf
- DOM based XSS Prevention Cheat Sheet http://www.owasp.org/index.php/DOM_based_XSS_Prevention_Cheat_Sheet
Schluss mit dem Bezahlen pro Entwickler.
Schließ den Kreislauf.
Plexicus ist die KI-native ASPM, die scannt, filtert, fixt, pentestet und erklärt — autonom. Unbegrenzte Entwickler, unbegrenzte Repos, Fair-Use-KI-Aktionen. Echter kostenloser Tarif, €269/mo jährlich, wenn du bereit bist.