If you run bundle-audit or yarn audit in CI alongside a dependency cooldown, sooner or later CI goes red and you can't fix it. An advisory is published, the audit fails, and the patched release is only a few days old. Your 14-day cooldown says you can't adopt it yet. If your host only deploys after CI passes, you're stuck with blocked deploys and nothing you're allowed to do.

That happened to me the day I added audits to CI. A serious advisory hit rack, and the fix was 13 days old, one day short of the cutoff.

Split advisories into two groups

The way out is to look at the releases that fix each advisory, and check when they were published:

An audit script that knows about the cooldown

Both tools can output JSON (bundle-audit check --format json and yarn audit --json), including the versions that fix each advisory. The script looks up when each of those versions was published and sorts the advisories into the two groups:

require "date"
require "json"
require "net/http"
require "open3"

DAYS   = Integer(ENV.fetch("COOLDOWN_DAYS", 14))
CUTOFF = Date.today - DAYS

def get_json(url) = JSON.parse(Net::HTTP.get(URI(url)))

# ">=2.0.1 <3.0.0 || >=3.0.2" => lambda that checks a version string
def npm_range(range)
  sets = range.split("||").map do |set|
    set.scan(/(>=|<=|>|<|=)?\s*v?(\d[\w.]*)/).map { |op, v| Gem::Requirement.new("#{op || '='} #{v}") }
  end
  ->(v) { sets.any? { |reqs| reqs.all? { |r| r.satisfied_by?(Gem::Version.new(v)) } } }
end

def gem_advisories
  system("bundle-audit", "update", "--quiet") || abort("bundle-audit update failed")
  out, = Open3.capture2("bundle-audit", "check", "--format", "json")
  JSON.parse(out)["results"].select { |r| r["type"] == "unpatched_gem" }.map do |r|
    name = r.dig("gem", "name")
    reqs = r.dig("advisory", "patched_versions").map { |s| Gem::Requirement.new(*s.split(/,\s*/)) }
    dates = get_json("https://rubygems.org/api/v1/versions/#{name}.json")
      .select { |v| v["platform"] == "ruby" }.to_h { |v| [v["number"], Date.parse(v["created_at"])] }
    { name: name, version: r.dig("gem", "version"), title: r.dig("advisory", "title"), dates: dates,
      fixed: ->(v) { reqs.any? { |req| req.satisfied_by?(Gem::Version.new(v)) } } }
  end
end

def npm_advisories
  out, = Open3.capture2("yarn", "audit", "--json")
  events = out.lines.filter_map { |l| JSON.parse(l) rescue nil }
  events.select { |e| e["type"] == "auditAdvisory" }.flat_map do |e|
    adv = e.dig("data", "advisory")
    dates = get_json("https://registry.npmjs.org/#{adv['module_name'].sub('/', '%2F')}")["time"]
      .reject { |k, _| %w[created modified].include?(k) }.transform_values { |t| Date.parse(t) }
    adv["findings"].map { |f| f["version"] }.uniq.map do |version|
      { name: adv["module_name"], version: version, title: adv["title"], dates: dates,
        fixed: npm_range(adv["patched_versions"]) }
    end
  end
end

# bundle-audit lists a native gem like nokogiri once per platform
found = (gem_advisories + npm_advisories).uniq { |a| a.values_at(:name, :version, :title) }
fixable, waiting = [], []

found.each do |a|
  current = Gem::Version.new(a[:version])
  fixes = a[:dates].select do |v, _|
    Gem::Version.correct?(v) && !Gem::Version.new(v).prerelease? &&
      Gem::Version.new(v) > current && a[:fixed].call(v)
  end
  ready = fixes.select { |_, d| d <= CUTOFF }.min_by { |v, _| Gem::Version.new(v) }
  label = "#{a[:name]} #{a[:version]}: #{a[:title]}"
  if ready
    fixable << "#{label} (update to #{ready[0]})"
  else
    soonest = fixes.min_by { |_, d| d }
    note = soonest ? "fix #{soonest[0]} clears the cooldown on #{soonest[1] + DAYS}" : "no fix yet"
    waiting << "#{label} (#{note})"
  end
end

waiting.each { |w| puts "::warning::#{w}" }
fixable.each { |f| puts "::error::#{f}" }
puts "#{found.size} advisories: #{fixable.size} fixable now, #{waiting.size} waiting"
exit 1 if fixable.any?

The ::warning:: and ::error:: lines are GitHub Actions workflow commands, so each advisory shows up as an annotation on the run. Outside Actions they're just readable output. The COOLDOWN_DAYS environment variable is the same one my gem cooldown check reads, so one setting controls both.

Pin the audit tools too

A security check that installs whatever version of its tools is newest defeats the point. Pinning a tool doesn't pin its dependencies, though:

- name: Install audit tools (pinned)
  run: |
    gem install thor --version 1.5.0
    gem install bundler-audit --version 0.9.3
    npm install --global yarn@1.22.22
- name: Audit dependencies
  run: ruby audit.rb

bundler-audit depends on thor, so thor gets pinned first. The advisory database is the one thing I leave unpinned, because it's data, not code, and it has to be current.

When a fix can't wait

Sometimes a fix is serious enough to break the rule. For that I lower COOLDOWN_DAYS in CI temporarily, with a dated comment so it doesn't become permanent:

env:
  # Lowered from 14 to 10 for the rack advisory. Remove on the 16th.
  COOLDOWN_DAYS: 10

The audit and the cooldown check both read it, so CI stays green on the newer release, and the comment says when to put it back.