Be first to know. Be ready to defend.

RubyGems.org fixes a gem upload flaw that could have run code on its servers

Illustration of the crafted gemspec: a POST to /api/v1/gems with a summary field tagged !ruby/regexp, which the validator ignored and to_ruby compiled before the gem ownership check ranIllustration of the crafted gemspec: a POST to /api/v1/gems with a summary field tagged !ruby/regexp, which the validator ignored and to_ruby compiled before the gem ownership check ran
On this page
  1. How it worked
  2. From a regex to running code
  3. The fix
  4. What to do

RubyGems.org, the registry that serves Ruby’s packages, has fixed a flaw in how it checks uploaded gems. Anyone with an account and an API key allowed to push gems could have sent metadata that made the registry compile an attacker’s regular expression. Paired with a memory bug in Ruby’s regex engine, that was enough to run code on the upload worker.

The RubyGems.org team published the advisory on 26 November and rates it 9.0 out of 10. It has no CVE number yet.

How it worked

Every gem carries a gemspec, a YAML file with its name, version, summary and other details. When someone pushes a gem to POST /api/v1/gems, RubyGems.org checks that YAML against a schema, then converts it into Ruby objects.

The checker had a list of YAML tags it ignored, and !ruby/regexp was on it. So a field such as summary: !ruby/regexp /.../ passed the schema check as if it were plain text. The conversion step then read the tag and turned the value into a real Ruby regular expression, compiling whatever pattern the attacker had written.

How the RubyGems.org upload flaw worked. Step 1: push a gem with any account that has a push-capable API key. Step 2: hide a regex in the gemspec, for example summary: !ruby/regexp /…/. Step 3: the validator skips the tag, so the field passes as plain text. Step 4: the parser compiles the pattern before anyone checks who owns the gem. Step 5: a Ruby 4.0.9 regex bug leads to memory corruption, then code execution. RubyGems.org reproduced code execution in local testing, not against the live service, and found no evidence of exploitation in 29,100 gems pushed between 5 October and 4 November.How the RubyGems.org upload flaw worked. Step 1: push a gem with any account that has a push-capable API key. Step 2: hide a regex in the gemspec, for example summary: !ruby/regexp /…/. Step 3: the validator skips the tag, so the field passes as plain text. Step 4: the parser compiles the pattern before anyone checks who owns the gem. Step 5: a Ruby 4.0.9 regex bug leads to memory corruption, then code execution. RubyGems.org reproduced code execution in local testing, not against the live service, and found no evidence of exploitation in 29,100 gems pushed between 5 October and 4 November.

All of this happened before RubyGems.org checked whether the account owned the gem name. An attacker didn’t need to control any existing gem.

From a regex to running code

Compiling an attacker’s pattern is only dangerous if the regex engine has a bug to exploit. The advisory says that on Ruby 4.0.9, crafted patterns achieved remote code execution in local testing, with the permissions of the gem upload process. On builds without such a bug, a crafted pattern could still crash a worker or make it run out of memory.

The advisory doesn’t say which bug it used. But 4.0.9 is one of the versions Ruby itself fixed on 18 November, when it released 4.0.10, 3.4.13 and 3.3.14 for seven memory bugs. Several of them affect apps that compile attacker-controlled patterns, and Ruby says it knows of proofs of concept that reach code execution.

RubyGems.org stresses that it reproduced the attack locally, not against the live service. It reviewed production telemetry and the 29,100 gems published between 5 October and 4 November, and found no direct evidence of exploitation. It can’t rule out earlier attempts, because it keeps logs for a limited time and doesn’t log upload contents.

The fix

The three changes RubyGems.org made. Reject the tag: gemspec fields tagged !ruby/regexp now fail validation with HTTP 422, before anything is deserialised. Restrict the parser: only the eight classes a real gemspec uses are allowed, and YAML aliases are refused. Cap OIDC patterns: trusted-publisher patterns are limited to 255 characters, as extra protection for the regex compiler. Hosted RubyGems.org users need do nothing. Self-hosted operators should update to a commit with the fix.The three changes RubyGems.org made. Reject the tag: gemspec fields tagged !ruby/regexp now fail validation with HTTP 422, before anything is deserialised. Restrict the parser: only the eight classes a real gemspec uses are allowed, and YAML aliases are refused. Cap OIDC patterns: trusted-publisher patterns are limited to 255 characters, as extra protection for the regex compiler. Hosted RubyGems.org users need do nothing. Self-hosted operators should update to a commit with the fix.

RubyGems.org made three changes:

  • !ruby/regexp is off the ignore list, so a tagged field now fails validation with an HTTP 422 error before anything is converted.
  • The conversion now uses Psych’s restricted loader. It refuses YAML aliases and only creates the classes a genuine gemspec needs, such as Gem::Specification, Gem::Version, Symbol, Time and Date.
  • Patterns in its trusted publishing access policies are capped at 255 characters before they’re compiled, as extra protection.

Because the bug was in the registry’s own code, there’s no gem to update.

What to do

  • If you only download or publish gems on RubyGems.org, you don’t need to do anything.
  • If you run your own copy of the RubyGems.org application, update to a commit that contains the fix.
  • If you run Ruby 4.0, 3.4 or 3.3 anywhere that compiles patterns from untrusted input, upgrade to 4.0.10, 3.4.13 or 3.3.14. Ruby 3.2 and older get no fixes.
  • If your own code loads YAML from users, use YAML.safe_load with an explicit list of permitted classes, as RubyGems.org now does.

It’s been a busy week for package registries. On 24 November, Hex.pm, the Erlang and Elixir registry, disclosed an intrusion into its web servers. That was a real attack. The RubyGems.org flaw is a separate bug with no sign anyone used it.