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


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.
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
RubyGems.org made three changes:
!ruby/regexpis 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,TimeandDate. - 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_loadwith 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.