Guides

Securing your Gemfile: advisories, cooldowns and lockfiles against malicious gems

Guides · · Published 2026-11-21

An old brass folding magnifier with two lenses, engraved C. A. Todd
Vetting a dependency is a habit, not a one-off inspection.

Most Rails applications run more code from their Gemfile than from their own app directory. That code can hurt you in two ways: a legitimate gem with a security bug, or a gem that is malicious from the start or turns malicious after an account takeover. They need different defences. This guide sets out a layered routine that covers both, using tools that ship with Bundler or cost nothing.

Why this matters in 2026

Malicious gems are no longer a theoretical risk. In August 2025, The Hacker News reported research by Socket on 60 malicious gems posing as automation tools for social networks; they had been published since at least March 2023 and downloaded "more than 275,000 times", although downloads do not equal infections. In July 2026 the same outlet described the SleeperGem campaign: three gems, two of them dormant since 2017 and 2018, suddenly received malicious releases that installed a persistent daemon on developer machines while skipping CI servers. Old, forgotten gems with abandoned maintainer accounts are an attractive target precisely because nobody watches them.

The registry does fight back. In a post on how RubyGems.org handles malicious gems, the RubyGems team says its automated scanning and risk scoring catch "roughly 70-80%" of malicious packages before anyone reports them. That leaves a window, usually days, during which a bad release can be installed. Your own routine is what covers that window.

Layer 1: known vulnerabilities

The first threat is the ordinary one: a popular gem ships a bug that becomes a CVE. Two free tools read public advisory data and compare it with your Gemfile.lock.

Use both: Dependabot proposes the fix, bundler-audit stops a vulnerable lockfile from being merged when the bot is ignored or switched off. Our checklist for evaluating a gem before adding it covers the step before this one: reading a gem's advisory history before it ever enters the Gemfile.

Layer 2: brand-new malicious releases

Advisory databases cannot help with a release that was published an hour ago. The defence here is time, and Bundler now provides it directly. The RubyGems guide on delaying new gem versions with a cooldown explains the reasoning in one line: "Most malicious gem releases are detected and yanked within days of publication." A cooldown makes Bundler ignore versions younger than a given number of days during resolution.

bundle config set cooldown 7

# or, in the Gemfile, for one source only
source "https://rubygems.org", cooldown: 7

A few details matter in practice:

Seven days is a reasonable default for an application. A library you publish should not set one for its users; that is their call.

Layer 3: install exactly what was reviewed

A cooldown only helps if resolution happens where someone can see it. Commit Gemfile.lock, review its diff in every pull request, and make CI and deployment install the lockfile as written. The RubyGems guide on using Bundler in CI notes that when a lockfile is present the standard GitHub Actions setup installs in deployment mode, "so the job fails if the lockfile is out of date instead of silently resolving new versions". Locally, bundle config set frozen true gives the same guarantee.

When reviewing a lockfile diff, look for three things: a new top-level dependency nobody mentioned, a transitive gem that appeared with a minor bump, and any gem whose name is one letter away from a famous one. Typosquatting still works because a Gemfile line is easy to mistype and hard to notice in review.

Layer 4: if you publish gems

Many Rails teams also maintain a gem or two: an internal engine, a client library, a fork. Account takeover is how a trusted gem turns malicious, so the publisher side matters as much as the consumer side.

A routine you can adopt this week

ThreatControlWhere
Known vulnerability in a locked gembundle-audit check --updateCI, every build
Patched version availableDependabot security updatesRepository settings
Malicious release published days agobundle config set cooldown 7Project config or Gemfile
Silent re-resolutionCommitted lockfile, frozen installsCI and deploy
TyposquattingLockfile diff reviewPull requests
Takeover of your own gemMFA, trusted publishingRubyGems.org account

What none of this replaces

These controls reduce exposure; they do not make a dependency trustworthy. Fewer gems remain the cheapest defence of all, and Rails covers more ground every release: authentication generators, rate limiting in the framework itself (see our rate limiting guide), Solid Queue for jobs. The long history traced in from plugins to engines shows the same pattern over and over: what was once a plugin often ends up in Rails, and the plugin becomes one more line to remove.