Improper Restriction of XML External Entity Reference ('XXE')

ID

scala.xxe.scala_xxe_rule_xpathxxe

Severity

high

Remediation Complexity

trivial

Remediation Risk

medium

Remediation Effort

medium

Resource

Xxe

Language

Scala

Description

XML External Entity (XXE) attacks can occur when an XML parser supports XML entities while processing XML received from an untrusted source.

Rationale

XML External Entity (XXE) attacks can occur when an XML parser supports XML entities while processing XML received from an untrusted source.

The following code illustrates a vulnerable pattern detected by this rule:

object InvoiceQuery {
  @throws[Exception]
  def runAll(sources: Array[String]): Unit = {
    fullyLockedDown(sources(0))
    externalAccessDenied(sources(0))
    defaultsLeftAlone(sources(0))
  }

  @throws[Exception]
  def fullyLockedDown(location: String): Unit = {
    val factory = DocumentBuilderFactory.newInstance
    factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "")
    factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "")
    factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true)
    factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true)

    val builder = factory.newDocumentBuilder
    val xpath = XPathFactory.newInstance.newXPath
    val query = xpath.compile("/invoice/total/text()")
    val total = query.evaluate(builder.parse(location))
  }

  @throws[Exception]
  def externalAccessDenied(location: String): Unit = {
    val factory = DocumentBuilderFactory.newInstance
    factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "")
    factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "")
    val builder = factory.newDocumentBuilder
    val xpath = XPathFactory.newInstance.newXPath
    val query = xpath.compile("/invoice/total/text()")
    val total = query.evaluate(builder.parse(location))
  }

  // Handing the expression an InputSource lets it do its own parsing, with the defaults.
  @throws[Exception]
  def defaultsLeftAlone(location: String): Unit = {
    val factory = DocumentBuilderFactory.newInstance
    val builder = factory.newDocumentBuilder
    val xpath = XPathFactory.newInstance.newXPath
    val query = xpath.compile("/invoice/total/text()")
    // VULNERABLE: Improper Restriction of XML External Entity Reference ('XXE')
    query.evaluate(new InputSource(location), null)
  }
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

References

© 2026 Xygeni. All rights reserved