Skip to content

Unauthorized gem replacement for full names ending in numbers

High
segiddins published GHSA-rxcq-2m4f-94wm Aug 17, 2023

Package

bundler RubyGems.org Service (RubyGems)

Affected versions

< 2023-08-14

Patched versions

>= 2023-08-14

Description

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.

Severity

High
7.4
/ 10

CVSS base metrics

Attack vector
Network
Attack complexity
Low
Privileges required
None
User interaction
Required
Scope
Changed
Confidentiality
None
Integrity
High
Availability
None
CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:N/I:H/A:N

CVE ID

CVE-2023-40165

Weaknesses

Credits