Skip to content

Ruby bundled REXML Document#write stack exhaustion on deeply nested XML #354

Closed

Description

@hibrian827

Summary

Ruby bundles REXML 3.4.4, and that REXML version can exhaust the Ruby stack when an application serializes attacker-controlled deeply nested XML with REXML::Document#write. The parser accepts the nested document, but the default formatter recursively walks child elements without a depth guard, so a small XML body can raise SystemStackError during serialization and abort the XML-processing request or job.

Affected

  • Pinned ref: 6a9ee89182939c592410d27d6de2d474657f4975

Root cause

Ruby's bundled gems manifest pins rexml 3.4.4 from ruby/rexml in gems/bundled_gems:13, which maps this shipped Ruby tree to the affected REXML source. In that REXML version, REXML::Document.new accepts a caller-supplied source at lib/rexml/document.rb:92; for a non-empty source that is not already a Document, it calls build(source) at lib/rexml/document.rb:105, and build constructs the element tree through Parsers::TreeParser.new(source, self).parse at lib/rexml/document.rb:468. Later, the public serialization path Document#write starts at lib/rexml/document.rb:369, defaults indent to -1, selects REXML::Formatters::Default.new at lib/rexml/document.rb:399, and dispatches the document to the formatter at lib/rexml/document.rb:401. The default formatter dispatches element nodes from write to write_element at lib/rexml/formatters/default.rb:23 and lib/rexml/formatters/default.rb:33; for non-empty elements, write_element iterates node.children at lib/rexml/formatters/default.rb:80 and recursively calls write(child, output) at lib/rexml/formatters/default.rb:81 with no depth counter or limit. Deeply nested literal elements therefore consume Ruby call stack during output even though entity expansion limits are not involved.

Reproduction

require "stringio"

build_dir = ARGV.fetch(0)
$LOAD_PATH.unshift(File.join(build_dir, "lib"))
require "rexml/document"

depth = 3000
xml = ("<a>" * depth) + "x" + ("</a>" * depth)
document = REXML::Document.new(xml)

begin
  output = StringIO.new
  document.write(output)
  puts "RUBY_REXML_DEEP_WRITE_COMPLETED bytes=#{output.string.bytesize}"
rescue SystemStackError => error
  warn "RUBY_REXML_DEEP_WRITE_STACK_EXHAUSTION: #{error.class}: #{error.message}"
  warn error.backtrace.grep(%r{/rexml/formatters/default\.rb|/rexml/parent\.rb}).first(6).join("\n")
  exit 0
end

exit 1

Suggested fix

diff --git a/lib/rexml/security.rb b/lib/rexml/security.rb
index 9044d09..af3db75 100644
--- a/lib/rexml/security.rb
+++ b/lib/rexml/security.rb
@@ -24,5 +24,17 @@ module REXML
     def self.entity_expansion_text_limit
       @@entity_expansion_text_limit
     end
+
+    @@element_write_depth_limit = 1_000
+
+    # Set the element write depth limit. By default the limit is set to 1000.
+    def self.element_write_depth_limit=( val )
+      @@element_write_depth_limit = val
+    end
+
+    # Get the element write depth limit. By default the limit is set to 1000.
+    def self.element_write_depth_limit
+      @@element_write_depth_limit
+    end
   end
 end
diff --git a/lib/rexml/formatters/default.rb b/lib/rexml/formatters/default.rb
index e1031ac..47204f6 100644
--- a/lib/rexml/formatters/default.rb
+++ b/lib/rexml/formatters/default.rb
@@ -1,4 +1,6 @@
 # frozen_string_literal: false
+
+require_relative "../security"
 
 module REXML
   module Formatters
@@ -61,6 +64,11 @@ module REXML
       end
 
       def write_element( node, output )
+        @element_write_depth ||= 0
+        @element_write_depth += 1
+        if @element_write_depth > Security.element_write_depth_limit
+          raise "number of nested elements exceeded, processing aborted."
+        end
         output << "<#{node.expanded_name}"
 
         node.attributes.to_a.map { |a|
@@ -80,6 +88,8 @@ module REXML
           output << "</#{node.expanded_name}"
         end
         output << ">"
+      ensure
+        @element_write_depth -= 1
       end
 
       def write_text( node, output )

Reported by Team Atlanta.

Activity

  1. added a commit that references this issue on Aug 16, 2026
    9694327
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions