logo

Database

C Sharp Schema By Url

Description

Detects potential XML External Entity (XXE) vulnerabilities in C# code where XmlSchemaCollection.Add() loads an XML schema from a URI controlled by user input. An attacker-supplied URI can point the parser at a malicious schema with external entity references or make the server fetch internal resources.

Weakness:

083 - XML injection (XXE)

Category: Unexpected Injection

Detection Strategy

    • Looks for .Add() method calls whose receiver is defined as a new XmlSchemaCollection

    • Takes the second argument of the call, which is the schema URI

    • Reports a vulnerability when the URI comes from HTTP request input, directly or through unsanitized variable definitions

Vulnerable code example

using System;
using System.Xml.Schema;

public partial class SchemaForm : System.Web.UI.Page {
    protected void LoadSchema() {
        string schemaUrl = Request.QueryString["schema"];
        XmlSchemaCollection xsc = new XmlSchemaCollection();
        xsc.Add("urn:schema", schemaUrl);  // Vulnerable: schema URI comes from the request...

✅ Secure code example

using System;
using System.Xml.Schema;

public partial class SchemaForm : System.Web.UI.Page {
    protected void LoadSchema() {
        XmlSchemaCollection xsc = new XmlSchemaCollection();
        xsc.Add("urn:schema", "books.xsd");  // Safe: schema URI is fixed by the application
    }...