RubyGems.org, the primary repository for Ruby community’s gems, has resolved a critical security flaw that permitted any user with a push-capable API key to introduce an attacker-controlled regular expression into gem metadata.
Understanding the Flaw
Registry maintainers discovered during testing that this vulnerability could be combined with a memory corruption issue in Ruby’s regular expression compiler, potentially allowing remote code execution. According to a recent advisory, users of the gem do not need to take any action.
An examination of telemetry data and 29,100 gems published between October 5 and November 4 showed no direct signs of exploitation. However, the advisory acknowledged limitations in log retention and payload recording, meaning earlier attempts might not be entirely ruled out.
Technical Details and Vulnerability Scoring
Identified as GHSA-7h7w-m82c-x2xg, this flaw received a CVSS score of 9.0, though it lacks a CVE assignment. It originated from how the registry interpreted gem metadata in YAML format.
The validation process that assessed the metadata failed to flag the !ruby/regexp YAML tag, allowing it to pass schema checks as plain text. Ruby’s YAML processing library, Psych, subsequently converted this into an executable regular expression, enabling potential exploitation.
Preventative Measures and Advisory Actions
The vulnerability existed in Ruby 4.0.9, which was susceptible to several issues in its regexp engine and string case conversion, patched by the Ruby project on November 18. Even on Ruby builds not susceptible to code execution, an attacker’s pattern could crash or overload an upload worker.
To mitigate this, schema validation now rejects regexp-tagged gemspec fields by returning an HTTP 422 error. The registry now reads gem metadata with a restricted loader that avoids YAML aliases and permits only a limited set of classes. Additionally, patterns in OIDC access policies are restricted to 255 characters to enhance security further.
Implications and Future Outlook
The registry’s public code repository incorporated these changes on November 4, preceding the advisory by three weeks. Operators who self-host RubyGems.org software are advised to update to a secure commit.
While the hosted registry is now secured, Ruby applications must update to address seven runtime vulnerabilities, fixed in Ruby versions 4.0.10, 3.4.13, and 3.3.14, as detailed in the Ruby project’s security communication.
The registry supports approximately 199,000 gems with over 278 billion downloads. The maintained environment’s test privileges aligned with a gem-upload worker, though the advisory did not specify if an attacker could access published gems or credentials if the exploit was successful.
