CVE-2021-27918: Vulnerability in golang - Infinite Loop in encoding/xml

golang Updated 02 Oct 2025

Summary

06/03/2024
Yes i
High
Not published

CVE-2021-27918: encoding/xml in Go before 1.15.9 and 1.16.x before 1.16.1 has an infinite loop if a custom TokenReader (for xml.NewTokenDecoder) returns EOF in the middle of an element. Affects golang across 2 version ranges.

< 1.15.9 >= 1.16.0, < 1.16.1

Fixed in 1.15.9 or 1.16.1, depending on the release line you run. How to fix ↓

Also tracked for

The same CVE-2021-27918 is tracked separately for each ecosystem and vendor. Its fix information is the same; the package and affected versions differ.

Free scan

Check if this vulnerability affects your application

Upload a manifest or SBOM and we'll show whether you're affected, plus every other risky package in the same scan.

Check my app free

No signup required · Results in about 60 seconds

Sponsored Devnack Mobile apps built by Devnack Design, build and launch iOS and Android apps with a senior product team. From idea to store release Start a project

Background

The golang package is a popular open-source programming language that provides a robust set of tools and libraries for building efficient and reliable applications. It is widely used for developing various types of software, including web applications, network servers, and command-line tools. The encoding/xml package in golang is responsible for encoding and decoding XML data. It provides functions like xml.NewTokenDecoder, Decode, DecodeElement, and Skip for parsing and processing XML documents.

Vulnerability Detail

The encoding/xml package in golang before version 1.15.9 and 1.16.x before 1.16.1 is vulnerable to an infinite loop when a custom TokenReader returns EOF in the middle of an element. This vulnerability can be triggered during the decoding process using functions like Decode, DecodeElement, or Skip. An attacker can exploit this vulnerability by crafting a malicious XML document that triggers the infinite loop, leading to a denial-of-service (DoS) condition. This can potentially impact applications that rely on the encoding/xml package for XML parsing and processing.

How To Fix

Fixed versions by release line

The minimum fixed version depends on the release line you run.

  • < 1.15.9 1.15.9
  • >= 1.16.0, < 1.16.1 1.16.1

Tip — Not sure which release line you're on? Vulert can find the fix for the version you are using. Scan free →

Workaround

As a workaround, developers can implement additional checks in their code to validate the XML data before passing it to the encoding/xml package. This can include checking for unexpected EOF conditions or using alternative XML parsing libraries that are not affected by this vulnerability. Here is an example of how to validate XML data using the encoding/xml package in golang:

func validateXML(data []byte) error {
    // Check for unexpected EOF
    if len(data) == 0 {
        return errors.New("Empty XML data")
    }

    // Parse XML
    decoder := xml.NewDecoder(bytes.NewReader(data))
    for {
        _, err := decoder.Token()
        if err != nil {
            if err == io.EOF {
                break
            }
            return err
        }
    }

    return nil
}

Conclusion

In conclusion, the golang package is vulnerable to an infinite loop issue in the encoding/xml package. This can be exploited by an attacker to cause a denial-of-service condition in applications that rely on XML parsing and processing. To mitigate this vulnerability, it is crucial to update the golang package to the fixed versions (1.15.9 or 1.16.1) and recompile any affected applications. Additionally, implementing additional checks in the code to validate XML data can provide an extra layer of protection. For further assistance or support in vulnerability remediation, please contact DevNack.com.

See every vulnerability recorded for golang →

Share this vulnerability

FAQ

This vulnerability can lead to an infinite loop in the encoding/xml package, causing a denial-of-service (DoS) condition. It can potentially impact applications that rely on the package for XML parsing and processing, leading to resource exhaustion and unresponsiveness.

You can use Vulert's playground (https://vulert.com/scanner) to check if your application is affected by this vulnerability. Vulert analyzes your package-lock.json file and provides real-time alerts for open-source vulnerabilities, including this one.

At the time of this report, there are no known public exploits for this vulnerability. However, it is important to patch the golang package to mitigate the risk of potential attacks.

Yes, the golang package has released fixed versions (1.15.9 and 1.16.1) that address this vulnerability. It is recommended to update to the latest version to ensure your applications are not vulnerable.

Yes, you can implement additional checks in your code to validate the XML data before passing it to the encoding/xml package. This can help mitigate the risk of triggering the infinite loop. Refer to the 'workaround' section in this report for an example of how to validate XML data.

Related vulnerabilities

Recent vulnerabilities recorded for golang.

How To Fix

Fixed versions by release line

The minimum fixed version depends on the release line you run.

  • < 1.15.9 1.15.9
  • >= 1.16.0, < 1.16.1 1.16.1

Tip — Not sure which release line you're on? Vulert can find the fix for the version you are using. Scan free →

Subscribe to Vulert on Telegram
Daily digest of new CVEs, critical and high severity, affected packages and fixes — plus a weekly roundup.
Join the channel →
Help improve this record
Report an issue or send feedback