Headline
CVE-2023-40165: Unauthorized gem replacement for full names ending in numbers
rubygems.org is the Ruby community’s primary gem (library) hosting service. Insufficient input validation allowed malicious actors to replace any uploaded gem version that had a platform, version number, or gem name matching /-\d/
, permanently replacing the legitimate upload in the canonical gem storage bucket, and triggering an immediate CDN purge so that the malicious gem would be served immediately. The maintainers have checked all gems matching the /-\d/
pattern and can confirm that no unexpected .gem
s were found. As a result, we believe this vulnerability was not exploited. The easiest way to ensure that a user’s applications were not exploited by this vulnerability is to check that all of your downloaded .gems have a checksum that matches the checksum recorded in the RubyGems.org database. RubyGems contributor Maciej Mensfeld wrote a tool to automatically check that all downloaded .gem files match the checksums recorded in the RubyGems.org database. You can use it by running: bundle add bundler-integrity
followed by bundle exec bundler-integrity
. Neither this tool nor anything else can prove you were not exploited, but the can assist your investigation by quickly comparing RubyGems API-provided checksums with the checksums of files on your disk. The issue has been patched with improved input validation and the changes are live. No action is required on the part of the user. Users are advised to validate their local gems.
Summary
Insufficient input validation allowed malicious actors to replace any uploaded gem version that had a platform, version number, or gem name matching /-\d/, permanently replacing the legitimate upload in the canonical gem storage bucket, and triggering an immediate CDN purge so that the malicious gem would be served immediately.
Impact
We have checked all gems matching the /-\d/ pattern and can confirm that no unexpected .gems were found. As a result, we believe this vulnerability was not exploited.
Verification
The easiest way to ensure that your applications were not exploited by this vulnerability is to check that all of your downloaded .gems have a checksum that matches the checksum recorded in the RubyGems.org database.
RubyGems contributor Maciej Mensfeld wrote a tool to automatically check that all downloaded .gem files match the checksums recorded in the RubyGems.org database. You can use it by running:
bundle add bundler-integrity
bundle exec bundler-integrity
Neither this tool nor anything else can prove you were not exploited, but the can assist your investigation by quickly comparing RubyGems API-provided checksums with the checksums of files on your disk.
Patches
The issue has been patched with improved input validation.
Details
This is similar to GHSA-2jmx-8mh8-pm8w, but did not require advanced upload of the gem.
This vulnerability was limited to platform-specific gems ending in a number, for example sorbet-static, version 0.5.9995 , platform universal-darwin-20.
An attacker could craft a gem file for upload with a hand-crafted YAML-format gem spec in the .gem’s metadata section.
For example, to overwrite sorbet-static-0.5.9995-universal-darwin-20, the attacker would send the following gemspec:
— !ruby/hash-with-ivars:Gem::Specification ivars: '@name’: sorbet-static-0.5.9995-universal-darwin '@version’: 20 '@original_platform’: something '@new_platform’: ruby
RubyGems.org would create a version record for the gem sorbet-static-0.5.9995-universal-darwin with version 20 and platform something. RubyGems.org would treat that version as having a full_name of sorbet-static-0.5.9995-universal-darwin-20-something, which would be accepted, as it was unique from the full_name of the legitimate gem.
When uploading the .gem and .gemspec.rz for the new version, however, RubyGems.org used the original_name method from the uploaded specification to determine the file names to store. The original_name method would return sorbet-static-0.5.9995-universal-darwin-20, and RubyGems.org would upload sorbet-static-0.5.9995-universal-darwin-20.gem, overwriting the legitimate version and immediately purging the CDN cache to ensure the newly uploaded malicious version would be served from the CDN.