XML external entity (XXE) vulnerability

XML external entity (XXE) vulnerability

Description

In github.com/lestrrat-go/libxml2/parser, XMLParseNoEnt enables entity substitution. Omit it for untrusted XML and check the deployed libxml2 version and resource loaders.

Potential impact

File contents may reach results or errors. A network loader may contact internal services. Large inputs or entity expansion can exhaust CPU or memory.

Remediation

  • Disable entity substitution and external DTD loading. Verify that the installed Go binding and libxml2 loaders block both file and network access. Limit the process’s permissions, too.
  • Cap request bytes while reading. If you validate the schema or Content-Type, configure and verify the external-resource restrictions separately.

Examples

The examples require the Go binding and libxml2. Production authentication and TLS settings are omitted. The first handler allocates from ContentLength and calls Read once, so it mishandles unknown lengths and partial reads.

Before

go
package main

import (
    "fmt"
    "net/http"
    "github.com/lestrrat-go/libxml2/parser"
)

// Before: parse uploaded XML directly
func parseXMLUnsafe(w http.ResponseWriter, r *http.Request) {
    body := make([]byte, r.ContentLength)
    if _, err := r.Body.Read(body); err != nil {
        http.Error(w, "read error", http.StatusBadRequest)
        return
    }

    // CWE-611: enable entity substitution and external entity loading
    p := parser.New(parser.XMLParseNoEnt)

    doc, err := p.ParseString(string(body))
    if err != nil {
        http.Error(w, "parse error", http.StatusBadRequest)
        return
    }
    defer doc.Free()

    fmt.Fprintln(w, "XML parsed (unsafe)")
}

func main() {
    http.HandleFunc("/upload", parseXMLUnsafe)
    http.ListenAndServe(":8080", nil)
}

After

go
package main

import (
    "fmt"
    "io"
    "net/http"
    "github.com/lestrrat-go/libxml2/parser"
)

// Limit input size and omit the entity-substitution option
func parseXMLSafe(w http.ResponseWriter, r *http.Request) {
    const maxBody = 1 << 20
    body, err := io.ReadAll(io.LimitReader(r.Body, maxBody+1))
    if err != nil {
        http.Error(w, "read error", http.StatusBadRequest)
        return
    }

    if len(body) > maxBody {
        http.Error(w, "body too large", http.StatusRequestEntityTooLarge)
        return
    }

    // 1) Do not enable substitution or external DTD loading
    p := parser.New() // No XMLParseNoEnt

    // 2) Perform required schema validation separately
    //    Schema validation does not replace blocking external resources

    doc, err := p.ParseString(string(body))
    if err != nil {
        http.Error(w, "parse error", http.StatusBadRequest)
        return
    }
    defer doc.Free()

    fmt.Fprintln(w, "XML parsed safely")
}

func main() {
    http.HandleFunc("/upload", parseXMLSafe)
    http.ListenAndServe(":8080", nil)
}

The handler returns only status text. What the parser can read depends on the loader and the process’s permissions.

xml
<!DOCTYPE d [<!ENTITY x SYSTEM "file:///etc/passwd">]>
<data>&x;</data>

With io.LimitReader the handler reads up to one byte beyond 1 MiB and returns 413 if the body exceeds the limit. parser.New() receives no entity substitution or external DTD loading option. Check global settings and other loaders, and set separate limits on parsing resources.

References