Vulnerable gems
Your Gemfile.lock is a list of other people’s bugs
Every gem you install ships its vulnerabilities to production with it. Finding them takes one command; fixing them takes a process.
Rails shipped a fix for CVE-2013-0156 in January 2013. It was a bug in how Rails parsed XML request parameters, and it let an attacker run arbitrary code on the server. Exploit code was public almost immediately. In late May, five months after the patch, attackers started sweeping the internet for Rails servers that still hadn’t updated, and the ones they found got a cron job that downloaded and compiled an IRC bot (CSO’s write-up). Every one of those servers had the fix available for five months. It just wasn’t in their lockfile.
A Rails app with a few years on it has well over a hundred gems in Gemfile.lock, most of them pulled in by other gems you’ve never thought about. When one of them gets a CVE, you’re vulnerable until you update, whether or not you knew it was there.
The basic bug
nokogiri (1.13.3)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
Nokogiri before 1.13.4 has CVE-2022-24836: an inefficient regular expression in its HTML encoding detection that crafted input can use to tie up a CPU (a ReDoS). Nokogiri is in basically every Rails app, because Rails depends on it through rails-html-sanitizer and loofah, so this one wasn’t optional.
The basic fix
bundle update nokogiri --conservative
--conservative updates the gem you named without bumping its shared dependencies further than it has to. It keeps the diff small enough that someone will actually review it.
The hardcore version
legacy_pdf (2.1.0)
nokogiri (~> 1.12.0)
nokogiri (1.12.5)
Now bundle update nokogiri does nothing, because some PDF gem you added in 2021 pins it below the fix. (legacy_pdf is made up; the pattern isn’t.) Bundler happily resolves to the vulnerable version, nothing errors, and the CVE stays in production until someone reads the lockfile line by line.
The JavaScript version
$ head -1 vendor/assets/javascripts/jquery.min.js
/*! jQuery v1.12.4 | (c) jQuery Foundation | jquery.org/license */
$ bundle-audit check --update
No vulnerabilities found
bundler-audit reads Gemfile.lock and nothing else. The copy of jQuery someone dropped into vendor/ in 2016, the plugins that came with it, and the charting library pinned in config/importmap.rb are all invisible to it. jQuery before 3.5.0 has CVE-2020-11022 and CVE-2020-11023, XSS bugs in how it handles HTML passed to methods like .html(), even HTML you sanitized first. Plenty of Rails apps still serve 1.x out of the asset pipeline because the admin screens depend on it.
$ bin/importmap audit
$ npm audit
Get every JavaScript dependency into something a tool can read, then run that tool in the same CI step as bundler-audit. For importmaps that’s bin/importmap audit; for anything in package.json it’s npm audit (or yarn npm audit). Whatever has to stay in vendor/ gets a line in the README with its version and where it came from, so the next person can at least check it by hand.
The hijacked gem version
- rest-client (1.6.9)
+ rest-client (1.6.13)
In August 2019, someone logged into the RubyGems account of rest-client’s maintainer with a reused password from an older breach and published versions 1.6.10 through 1.6.13 with a backdoor that fetched more code from Pastebin and sent the host’s credentials to the attacker. It was one of several gems backdoored that summer. bundler-audit can’t help on day zero, because there’s no advisory until somebody notices. Until then, the only thing standing between you and that release is whether a person or a robot bumped the lockfile without reading it.
{
"extends": ["config:recommended"],
"minimumReleaseAge": "7 days"
}
env:
BUNDLE_FROZEN: "true"
minimumReleaseAge makes Renovate sit on a new version for a week before proposing it, which gives everyone else time to notice a bad release and get it yanked. Dependabot’s cooldown option does the same thing. BUNDLE_FROZEN makes CI fail if the lockfile doesn’t match the Gemfile, so nothing quietly resolves a new version on the way to production. And read lockfile diffs in review like the code they are: a patch release of a gem nobody’s touched in four years is worth a look at what changed. If you publish gems yourself, turn on MFA for your RubyGems account. A reused password was all it took here.
Fix #1: Update or replace whatever is pinning it
bundle update legacy_pdf nokogiri --conservative
Update the gem doing the pinning along with the vulnerable one. If the pinning gem has no release that allows the fix, you’re choosing between forking it (gem "legacy_pdf", github: "yourco/legacy_pdf" with the constraint loosened and your tests run against it) or replacing it. Abandoned gems that pin core dependencies are worth replacing anyway.
Fix #2: Fail the build when the lockfile is vulnerable
- name: Audit gems
run: |
gem install bundler-audit
bundle-audit check --update
bundler-audit checks your lockfile against the Ruby Advisory Database, and --update pulls the newest advisories first. It exits non-zero when it finds something, so a new CVE breaks the next build instead of sitting there until someone thinks to look. It catches transitive gems too, which is how you find the legacy_pdf situation before an attacker does.
This is the check that catches 2013. Point it at a lockfile with rails (3.2.10) in it and it names CVE-2013-0156 and fails the build, and the build stays red until somebody runs bundle update rails. Five months of red builds is a lot harder to ignore than five months of nothing.
Fix #3: Make updating boring
version: 2
updates:
- package-ecosystem: "bundler"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10
Dependabot (or Renovate) opens a PR for every outdated gem, every week. The hard part was never knowing about updates. It’s merging them, and that’s a test suite problem: if you trust your suite, you merge on green and the lockfile stays current; if you don’t, the PRs pile up and you’re back to the hardcore version. Small, weekly updates are how you avoid the terrifying Rails 5 to 7 jump later.
Brakeman reads your code; bundler-audit reads everyone else’s. Run both.