Module: Bundle::Patch

Defined in:
lib/bundle/patch.rb,
lib/bundle/patch/config.rb,
lib/bundle/patch/version.rb,
lib/bundle/patch/audit/parser.rb,
lib/bundle/patch/audit/advisory.rb,
lib/bundle/patch/gemfile_editor.rb,
lib/bundle/patch/gemfile_updater.rb

Defined Under Namespace

Modules: Audit Classes: Config, GemfileEditor, GemfileUpdater

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.process_gem_advisories(name, advisories, config) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bundle/patch.rb', line 62

def self.process_gem_advisories(name, advisories, config)
  current = advisories.first.to_h.dig("gem", "version")
  current_version = Gem::Version.new(current)

  # Collect all requirements from advisories
  all_requirements = advisories.flat_map do |adv|
    adv.to_h.dig("advisory", "patched_versions").map do |req|
      Gem::Requirement.new(req) rescue nil
    end
  end.compact

  # Find versions that satisfy all requirements
  candidate_versions = all_requirements
    .flat_map { |req| versions_satisfying(req) }
    .compact
    .uniq
    .select { |v| config.allow_update?(current_version, v) }
    .sort

  if candidate_versions.any?
    {
      name: name,
      required_version: candidate_versions.last.to_s
    }
  end
end

.start(config = Config.new) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bundle/patch.rb', line 12

def self.start(config = Config.new)
  advisories = Audit::Parser.run

  if advisories.empty?
    puts "🎉 No vulnerabilities found!"
    return
  end

  puts "🔒 Found #{advisories.size} vulnerabilities:"
  patchable = []

  advisories.group_by { |adv| adv.to_h.dig("gem", "name") }.each do |name, gem_advisories|
    result = process_gem_advisories(name, gem_advisories, config)
    
    if result
      title_list = gem_advisories.map { |a| a.to_h.dig("advisory", "title") }.uniq
      puts "- #{name} (#{gem_advisories.first.to_h.dig("gem", "version")}):"
      title_list.each { |t| puts "  • #{t}" }
      puts "  ✅ Patchable → #{result[:required_version]}"
      patchable << { "name" => name, "required_version" => result[:required_version] }
    else
      puts "- #{name} (#{gem_advisories.first.to_h.dig("gem", "version")}):"
      gem_advisories.each do |adv|
        puts "  • #{adv.to_h.dig("advisory", "title")}"
      end
      puts "  ⚠️  Not patchable (no version satisfies all advisories in current mode)"
    end
  end

  if patchable.any?
    if config.dry_run
      puts "💡 Skipped Gemfile update and bundle install (dry run)"
    elsif config.skip_bundle_install
      puts "💡 Skipped bundle install (per --skip-bundle-install)"
      GemfileEditor.update!(patchable)
      GemfileUpdater.update(gemfile_path: "Gemfile", advisories: patchable)
    else
      GemfileEditor.update!(patchable)
      GemfileUpdater.update(gemfile_path: "Gemfile", advisories: patchable)
      puts "📦 Running `bundle install`..."
      success = system("bundle install")
      if success
        puts "✅ bundle install completed successfully"
      else
        puts "❌ bundle install failed. Please run it manually."
      end
    end
  end
end

.versions_satisfying(req) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bundle/patch.rb', line 89

def self.versions_satisfying(req)
  # Get all versions that satisfy the requirement
  req.requirements.map do |op, version|
    case op
    when ">="
      # For >= requirements, we need to find all versions >= this version
      # We'll approximate by using the version itself
      version
    when "~>"
      # For ~> requirements, we need to find all versions in the range
      # We'll approximate by using the upper bound
      version
    else
      version
    end
  end.compact
end