My CI scan job went red with no security warning at all. The only output was Brakeman 8.1.0 is not the latest version 8.1.1 and exit code 5. Brakeman 8.1.1 had been released the day before, and the Rails 8 default bin/brakeman binstub runs with --ensure-latest, so any newer Brakeman fails the build.
That's a problem if you follow a package cooldown policy. My rule is that I never use a version published less than 14 days ago, in any ecosystem. The cooldown forbids upgrading to 8.1.1, and --ensure-latest demands it. CI would stay red for two weeks.
Checking for updates past the cooldown
What I wanted was "fail if the installed version is older than the newest version that is past the cooldown." The binstub can ask the RubyGems API for Brakeman's versions, filter out prereleases and anything younger than 14 days, and compare:
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "json"
require "net/http"
require "time"
COOLDOWN = 14 * 24 * 60 * 60
begin
response = Net::HTTP.get_response(URI("https://rubygems.org/api/v1/versions/brakeman.json"))
raise "rubygems.org answered #{response.code}" unless response.is_a?(Net::HTTPSuccess)
eligible = JSON.parse(response.body)
.reject { |v| v["prerelease"] || Time.now - Time.parse(v["created_at"]) < COOLDOWN }
.map { |v| Gem::Version.new(v["number"]) }.max
installed = Gem.loaded_specs.fetch("brakeman").version
if eligible && installed < eligible
warn "Brakeman #{installed} is behind #{eligible}, the newest version past the 14-day cooldown"
exit 5
end
rescue StandardError => e
warn "Couldn't check for a newer Brakeman (#{e.message}); scanning anyway"
end
load Gem.bin_path("brakeman", "brakeman")A few details matter here:
- It exits 5, the same code
--ensure-latestuses, so anything that already treats that code specially keeps working. - Prereleases are skipped, so a release candidate never becomes the bar you're held to.
- If RubyGems can't be reached, or the response isn't a 200, it warns and scans anyway. A network blip shouldn't block a deploy, and the scan itself is the part that protects you.
- The final line loads the real Brakeman executable, so all the normal options still work.
On the day 8.1.1 came out, 8.1.0 was the newest eligible version, so CI stayed green. Once 8.1.1 is 14 days old, the same job fails until I bump it, which is the behavior I wanted from --ensure-latest in the first place. If you hold your other gems back the same way, my post on a two-week gem cooldown for Ruby projects covers the lockfile side.
Takeaway
"Always latest" checks and cooldown policies contradict each other, and the contradiction only shows up on release day, when a build that was green yesterday goes red for no reason you can act on. Have the freshness check skip anything still in the cooldown, instead of choosing one rule over the other.
Brakeman is just the case I hit. Any tool that refuses to run when it's out of date, such as a linter or scanner, has the same conflict, and the same fix: compare against the newest version past the cooldown, not the newest version.