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

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.
- bundler-audit. Maintained by RubySec, alongside the community Ruby Advisory Database. Run
bundle-audit check --updatelocally and in CI; the job fails when a locked version matches an advisory. It works offline once the database is fetched, and it does not depend on where your code is hosted. - Dependabot (or an equivalent on other forges). GitHub's documentation explains how Dependabot security updates open pull requests that bump a vulnerable dependency to the minimum patched version. Bundler is one of the supported ecosystems.
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:
- The command-line flag beats the config setting, which beats the per-source value. A private gem server can be exempted with
cooldown: 0on itssourceline. bundle outdatedstill lists newer versions and marks the ones "in cooldown".- A cooldown also delays security fixes. When an advisory lands, bypass it for that gem only:
bundle update GEM --cooldown 0 --conservative. - Filtering relies on the server publishing a creation date for each version. Versions without that metadata, including those served by older private registries, are treated as outside the window and stay installable.
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.
- Turn on multi-factor authentication for your RubyGems.org account, following the RubyGems MFA guide, and set
rubygems_mfa_requiredto"true"in the gemspec metadata: from that release on, every owner needs MFA enabled to push a new version. - Use trusted publishing for releases from CI. RubyGems describes trusted publishing as a way to push gems "without using long-lived secret credentials": GitHub Actions exchanges a short-lived identity token for a short-lived API token scoped to that gem. Nothing to leak from a repository secret.
- Retire what you no longer maintain. SleeperGem used gems that had been idle for years. If you own one, add a deprecation notice, remove unneeded co-owners and keep MFA on, or transfer it to someone who will maintain it.
A routine you can adopt this week
| Threat | Control | Where |
|---|---|---|
| Known vulnerability in a locked gem | bundle-audit check --update | CI, every build |
| Patched version available | Dependabot security updates | Repository settings |
| Malicious release published days ago | bundle config set cooldown 7 | Project config or Gemfile |
| Silent re-resolution | Committed lockfile, frozen installs | CI and deploy |
| Typosquatting | Lockfile diff review | Pull requests |
| Takeover of your own gem | MFA, trusted publishing | RubyGems.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.
GuidesEvaluating a Rails gem
A practical checklist for judging a Ruby gem: maintenance signals, compatibility with Rails 7 and 8, dependencies, licence, and security advisories with bundler-audit.
GuidesFrom plugins to engines
How Rails extensions moved from vendor/plugins to gems, Railties and engines between Rails 2 and Rails 4, and what that means when you read old tutorials or upgrade an old app.
Category guidesRate limiting
Ten login attempts every three minutes, one signup per address, a ceiling for API clients: Rails' own rate_limit and Rack::Attack compared, with the cache store trap.