# CVE-2023-40165: Unauthorized gem replacement for full names ending in numbers on rubygems.org

> Attackers could replace legitimate Ruby gems with malicious versions due to insufficient input validation, potentially leading to code execution in dependent applications.

- **Severity:** High 7.4 (`CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:N/I:H/A:N`)
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Over the internet
- **Victim action needed:** A user has to click or open something
- **Fix available:** No
- **Published:** 17 Aug 2023
- **Updated:** 1 Oct 2024
- **Also known as:** GHSA-rxcq-2m4f-94wm

## What is CVE-2023-40165?

CVE-2023-40165 is a high-severity vulnerability in rubygems.org, published on 17 Aug 2023. An attacker can exploit it over the internet without logging in, and only if a user opens a file or follows a link.

### Official description

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.

Weakness: CWE-20 (Improper Input Validation)

## Which versions are affected by CVE-2023-40165?

It affects 1 product.


Products: Rubygems Rubygems.org.

## How do I fix CVE-2023-40165?

No fix has been published for CVE-2023-40165 yet. Limit exposure of the affected software until one is available.

## Is CVE-2023-40165 being exploited?

There is no known exploitation of CVE-2023-40165 in the wild and no public exploit code.

## Other vulnerabilities in rubygems.org

- [CVE-2022-29176](https://insights.sbomify.com/CVE-2022-29176/): Unauthorized gem takeover for some gems on rubygems.org (critical 9.9)
- [CVE-2022-29218](https://insights.sbomify.com/CVE-2022-29218/): Unauthorized takeover for new versions of some platform-specific gems (high 7.7)
- [CVE-2024-21654](https://insights.sbomify.com/CVE-2024-21654/): rubygems.org MFA Bypass through password reset function could allow account takeover (medium 4.8)

## Fix prompt for AI coding agents

```text
Check this codebase for CVE-2023-40165 and fix it if it is affected.

What it is: CVE-2023-40165 is a high-severity vulnerability in rubygems.org, published on 17 Aug 2023. An attacker can exploit it over the internet without logging in, and only if a user opens a file or follows a link.
Exploitation: There is no known exploitation of CVE-2023-40165 in the wild and no public exploit code.

Affected product: rubygems.org. No package-level version data is available, so check the vendor advisory linked from the reference page.

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/CVE-2023-40165/ (Markdown: https://insights.sbomify.com/CVE-2023-40165/index.md)
```

## Sources

- CVE record: https://www.cve.org/CVERecord?id=CVE-2023-40165
- NVD: https://nvd.nist.gov/vuln/detail/CVE-2023-40165
- OSV: https://osv.dev/vulnerability/CVE-2023-40165

---
From sbomify Insights: https://insights.sbomify.com/CVE-2023-40165/
Summaries are generated and may contain mistakes; the official description above is the record.
