Description
XML injection occurs when untrusted data is inserted where it is interpreted as markup rather than text. XmlWriter.WriteRaw and WriteRawAsync neither escape the input nor check whether it is valid XML. Assigning to an XmlNode.InnerXml property parses the string and replaces child nodes. Attackers can therefore add, replace or rearrange nodes and attributes in generated XML.
This differs from XXE, which accesses files or networks through external entities, and XPath injection, which changes query syntax.
Potential impact
- The structure and meaning of generated XML can change.
- Downstream parsers, signature/validation logic or business logic may trust attacker-created nodes or attributes.
- Later transformations into other formats or commands may introduce further security problems.
Remediation
Keep the element and attribute names in application code. Choose how to write each value according to its role.
- Write text with
XmlWriter.WriteString, the value argument ofWriteElementString, or the DOM'sInnerTextproperty. For an attribute, pass the value toWriteAttributeString. - If raw output is unavoidable,
SecurityElement.Escapecan escape text immediately before insertion into fixed XML markup. Prefer the XML APIs above. - If users select whole fragments, allow exact matches to a small set of source-code literals and restrict who can change them.
XmlConvert.EncodeName and EncodeLocalName encode XML names. If names vary, allow only elements and attributes permitted by your schema and business rules. Use the text APIs for values.
Examples
Before
using System.Xml;
void WriteProfile(XmlWriter writer, XmlElement target, string input)
{
writer.WriteRaw("<displayName>" + input + "</displayName>");
target.InnerXml = input;
}
WriteRaw writes input as markup. InnerXml parses it as child nodes and can throw XmlException for malformed XML.
After
using System.Xml;
void WriteProfile(XmlWriter writer, XmlElement target, string input)
{
writer.WriteStartElement("profile");
writer.WriteAttributeString("label", input);
writer.WriteElementString("displayName", input);
writer.WriteEndElement();
target.InnerText = input;
}
The code writes input as the label attribute value, the displayName element text and the target element's InnerText.
When raw output cannot be removed
Here the escaped value is used only as element text inside fixed markup.
using System.Security;
using System.Xml;
void WriteProfile(XmlWriter writer, string input)
{
writer.WriteRaw(
"<displayName>" + SecurityElement.Escape(input) + "</displayName>"
);
}
The fragment must match one of the local literals exactly.
using System.Collections.Generic;
using System.Xml;
void WriteSection(XmlWriter writer, string fragment)
{
var allowedFragments = new HashSet<string>
{
"<summary/>",
"<details/>"
};
if (!allowedFragments.Contains(fragment))
{
return;
}
writer.WriteRaw(fragment);
}
Validate names against the schema and business rules, and handle XXE and XPath injection separately.
Related standards
- CWE: CWE-91: XML Injection (aka Blind XPath Injection), CWE 4.20
- OWASP Top 10: A05:2025 Injection, A03:2021 Injection
References
- Official .NET and .NET Core support policy
- Microsoft Learn: XmlWriter.WriteRaw
- Microsoft Learn: XmlWriter.WriteRawAsync
- Microsoft Learn: XmlWriter.WriteString
- Microsoft Learn: XmlNode.InnerXml
- Microsoft Learn: XmlElement.InnerText
- Microsoft Learn: SecurityElement.Escape
- Microsoft Learn: XmlConvert.EncodeName
- OWASP ASVS v5.0.0-1.1.2, v5.0.0-1.2.1
- CodeQL: XML injection (C#)