XML injection

XML injection in C#

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 of WriteElementString, or the DOM's InnerText property. For an attribute, pass the value to WriteAttributeString.
  • If raw output is unavoidable, SecurityElement.Escape can 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

csharp
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

csharp
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.

csharp
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.

csharp
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

References