secupdater 0.2.0alpha4 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/secupdater/ecosystem/rubygems.rb +20 -2
- data/lib/secupdater/publisher/gitlab.rb +94 -8
- data/lib/secupdater/publisher.rb +11 -1
- data/lib/secupdater/update.rb +123 -39
- data/lib/secupdater/version.rb +1 -1
- data/spec/e2e/secupdater_spec.rb +11 -1
- data/spec/secupdater/ecosystem/rubygems_spec.rb +80 -11
- data/spec/secupdater/publisher/gitlab_spec.rb +235 -4
- data/spec/secupdater/update_spec.rb +151 -3
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4f19feed6401e9c35fd600135fcd76372d6f2b1146648996958119ac0063d5e3
|
|
4
|
+
data.tar.gz: 7fa86bc7f193bbe245dd672f9b1b362e3227917ebd92a78e28792cd810dbae4d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c0577d54657596f3fd338660330dde5da2dbee749a2c1349570ee64706583663850b4c080b8caccb275e63e1bd439ac3576ae7b43570498f1d7f4f82de3339ca
|
|
7
|
+
data.tar.gz: 4e07f424812cd76787ffd12226fc15a26c1c2ccc2c1b1075da706e28d3496f9acaccfc39a90b1a48910502723355a2e6b28be7910c7f6c4ad7fb48143406c86c
|
|
@@ -35,7 +35,7 @@ module Secupdater
|
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
def run_update(names, level)
|
|
38
|
-
output, status = Open3.capture2e(bundle_env, "bundle", "lock", "--update", *Array(names), "--conservative", "--#{level}", "--strict")
|
|
38
|
+
output, status = Open3.capture2e(bundle_env, "bundle", *bundler_version_arg, "lock", "--update", *Array(names), "--conservative", "--#{level}", "--strict")
|
|
39
39
|
|
|
40
40
|
if status.exitstatus == FROZEN_EXIT_CODE
|
|
41
41
|
raise Secupdater::Error, "bundle lock can't change Gemfile.lock (is BUNDLE_DEPLOYMENT set?)\n#{output}"
|
|
@@ -68,7 +68,7 @@ module Secupdater
|
|
|
68
68
|
advisory_id = advisory["id"] if advisory_id.empty?
|
|
69
69
|
|
|
70
70
|
SecurityAdvisory.new(result["gem"]["name"], advisory_id, advisory["patched_versions"])
|
|
71
|
-
end
|
|
71
|
+
end.uniq # bundle-audit reports an advisory once per platform the gem is locked for
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
def satisfies_requirement?(version, requirement)
|
|
@@ -79,6 +79,24 @@ module Secupdater
|
|
|
79
79
|
|
|
80
80
|
private
|
|
81
81
|
|
|
82
|
+
def bundler_version_arg
|
|
83
|
+
version = Bundler::LockfileParser.new(Bundler.read_file("Gemfile.lock")).bundler_version
|
|
84
|
+
return [] unless version
|
|
85
|
+
|
|
86
|
+
install_bundler(version)
|
|
87
|
+
["_#{version}_"]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def install_bundler(version)
|
|
91
|
+
@installed_bundlers ||= Set.new
|
|
92
|
+
return if @installed_bundlers.include?(version)
|
|
93
|
+
|
|
94
|
+
output, status = Open3.capture2e(bundle_env, "gem", "install", "bundler", "--version", version.to_s, "--conservative", "--no-document")
|
|
95
|
+
raise Secupdater::Error, "Couldn't install Bundler #{version}, which Gemfile.lock was bundled with\n#{output}" unless status.success?
|
|
96
|
+
|
|
97
|
+
@installed_bundlers << version
|
|
98
|
+
end
|
|
99
|
+
|
|
82
100
|
# When secupdater itself runs under `bundle exec` (e.g. an image whose
|
|
83
101
|
# entrypoint is `bundle exec`), undo what that set up so bundle lock works on
|
|
84
102
|
# the Gemfile and Gemfile.lock here. PATH is left alone, as callers may have
|
|
@@ -11,6 +11,7 @@ module Secupdater
|
|
|
11
11
|
@project_id = project_id
|
|
12
12
|
@target_branch = target_branch
|
|
13
13
|
@start_sha = start_sha
|
|
14
|
+
@current_merge_requests = {}
|
|
14
15
|
end
|
|
15
16
|
|
|
16
17
|
# Added to every MR secupdater opens. Closing one declines that version of
|
|
@@ -28,21 +29,55 @@ module Secupdater
|
|
|
28
29
|
|
|
29
30
|
if branch_exists?(http)
|
|
30
31
|
others = other_authors(http)
|
|
31
|
-
|
|
32
|
+
if others.any?
|
|
33
|
+
explain_skipping(http, open_merge_request, others) if open_merge_request
|
|
34
|
+
return [:skipped, "#{reference} has commits from #{others.join(", ")}, so it won't be updated"]
|
|
35
|
+
end
|
|
32
36
|
|
|
33
|
-
up_to_date = branch_has_changes?(http)
|
|
37
|
+
up_to_date = branch_has_changes?(http) && !target_manifests_moved_on?(http)
|
|
34
38
|
commit(http, force: true) unless up_to_date
|
|
35
39
|
else
|
|
36
40
|
commit(http)
|
|
37
41
|
end
|
|
38
42
|
|
|
39
|
-
|
|
43
|
+
if open_merge_request
|
|
44
|
+
remember(open_merge_request)
|
|
45
|
+
|
|
46
|
+
# e.g. when this update has since absorbed another dependency's advisories
|
|
47
|
+
described = open_merge_request["description"] == description
|
|
48
|
+
request(http, Net::HTTP::Put, "merge_requests/#{open_merge_request["iid"]}", { description: description }) unless described
|
|
49
|
+
|
|
50
|
+
return [:up_to_date, nil] if up_to_date && described
|
|
51
|
+
else
|
|
52
|
+
remember(JSON.parse(create_merge_request(http).body))
|
|
53
|
+
end
|
|
40
54
|
|
|
41
|
-
create_merge_request(http) unless open_merge_request
|
|
42
55
|
[:published, nil]
|
|
43
56
|
end
|
|
44
57
|
end
|
|
45
58
|
|
|
59
|
+
# Closes open secupdater MRs for a dependency that this run opened or kept a
|
|
60
|
+
# different MR for, or that no longer has any advisories. MRs for
|
|
61
|
+
# dependencies this run couldn't update are left alone.
|
|
62
|
+
def finish(vulnerable_names)
|
|
63
|
+
Net::HTTP.start("gitlab.com", 443, use_ssl: true) do |http|
|
|
64
|
+
find_open_secupdater_merge_requests(http).filter_map do |merge_request|
|
|
65
|
+
name = merge_request["source_branch"][REFERENCE_PATTERN, :name]
|
|
66
|
+
current = @current_merge_requests[name]
|
|
67
|
+
|
|
68
|
+
reason = if current && current != merge_request["iid"]
|
|
69
|
+
"Superseded by !#{current}."
|
|
70
|
+
elsif name && !vulnerable_names.include?(name)
|
|
71
|
+
"#{name} no longer has any security advisories on #{target_branch}, so this is no longer needed."
|
|
72
|
+
end
|
|
73
|
+
next unless reason
|
|
74
|
+
|
|
75
|
+
close_merge_request(http, merge_request["iid"], reason)
|
|
76
|
+
"closed !#{merge_request["iid"]} (#{merge_request["source_branch"]}): #{reason}"
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
46
81
|
private
|
|
47
82
|
|
|
48
83
|
def commit(http, force: false)
|
|
@@ -57,6 +92,44 @@ module Secupdater
|
|
|
57
92
|
request(http, Net::HTTP::Post, "repository/commits", data)
|
|
58
93
|
end
|
|
59
94
|
|
|
95
|
+
# Against every dependency the MR fixes advisories for, so finish closes
|
|
96
|
+
# their own MRs once another update has absorbed them
|
|
97
|
+
def remember(merge_request)
|
|
98
|
+
[bump.dependency.name, *advisories.map(&:name)].uniq.each do
|
|
99
|
+
@current_merge_requests[it] = merge_request["iid"]
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def find_open_secupdater_merge_requests(http)
|
|
104
|
+
query = URI.encode_www_form(state: "opened", labels: LABEL, target_branch: target_branch, per_page: 100)
|
|
105
|
+
JSON.parse(request(http, Net::HTTP::Get, "merge_requests?#{query}").body)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Hidden in the rendered comment; marks the comment so it's only posted once
|
|
109
|
+
SKIPPING_MARKER = "<!-- secupdater:skipping -->"
|
|
110
|
+
|
|
111
|
+
# Says on the MR, once, that secupdater has stopped updating it, so whoever
|
|
112
|
+
# committed knows rebasing and refreshing it is now up to them
|
|
113
|
+
def explain_skipping(http, merge_request, others)
|
|
114
|
+
notes_path = "merge_requests/#{merge_request["iid"]}/notes"
|
|
115
|
+
notes = JSON.parse(request(http, Net::HTTP::Get, "#{notes_path}?#{URI.encode_www_form(per_page: 100)}").body)
|
|
116
|
+
return if notes.any? { it.dig("author", "id") == own_user(http)["id"] && it["body"].to_s.include?(SKIPPING_MARKER) }
|
|
117
|
+
|
|
118
|
+
body = <<~BODY.chomp
|
|
119
|
+
#{SKIPPING_MARKER}
|
|
120
|
+
This branch has commits from #{others.join(", ")}, so secupdater won't update it any more, \
|
|
121
|
+
including rebasing it when `#{target_branch}` changes.
|
|
122
|
+
|
|
123
|
+
To hand it back, delete the branch, or force-push it without those commits, and secupdater will take it over on its next run.
|
|
124
|
+
BODY
|
|
125
|
+
request(http, Net::HTTP::Post, notes_path, { body: })
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def close_merge_request(http, iid, reason)
|
|
129
|
+
request(http, Net::HTTP::Post, "merge_requests/#{iid}/notes", { body: reason })
|
|
130
|
+
request(http, Net::HTTP::Put, "merge_requests/#{iid}", { state_event: "close" })
|
|
131
|
+
end
|
|
132
|
+
|
|
60
133
|
def create_merge_request(http)
|
|
61
134
|
data = {
|
|
62
135
|
source_branch: reference,
|
|
@@ -88,10 +161,12 @@ module Secupdater
|
|
|
88
161
|
end
|
|
89
162
|
|
|
90
163
|
def own_emails(http)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
164
|
+
own_user(http).values_at("email", "commit_email", "public_email").reject { it.nil? || it.empty? }
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Whoever owns the access token
|
|
168
|
+
def own_user(http)
|
|
169
|
+
@own_user ||= JSON.parse(api_request(http, Net::HTTP::Get, "user").body)
|
|
95
170
|
end
|
|
96
171
|
|
|
97
172
|
def branch_has_changes?(http)
|
|
@@ -102,6 +177,17 @@ module Secupdater
|
|
|
102
177
|
end
|
|
103
178
|
end
|
|
104
179
|
|
|
180
|
+
# Whether a manifest has changed on the target branch since the branch was
|
|
181
|
+
# cut from it. The update can come out the same even then (e.g. when another
|
|
182
|
+
# MR made some of the same bumps), but the branch would show changes the
|
|
183
|
+
# target already has, and may conflict.
|
|
184
|
+
def target_manifests_moved_on?(http)
|
|
185
|
+
query = URI.encode_www_form(from: reference, to: start_sha)
|
|
186
|
+
diffs = JSON.parse(request(http, Net::HTTP::Get, "repository/compare?#{query}").body)["diffs"]
|
|
187
|
+
|
|
188
|
+
diffs.any? { ecosystem.manifest_paths.intersect?(it.values_at("old_path", "new_path")) }
|
|
189
|
+
end
|
|
190
|
+
|
|
105
191
|
def request(http, type, path, data = nil, allow_missing: false)
|
|
106
192
|
api_request(http, type, "projects/#{URI.encode_www_form_component(project_id)}/#{path}", data, allow_missing:)
|
|
107
193
|
end
|
data/lib/secupdater/publisher.rb
CHANGED
|
@@ -17,13 +17,23 @@ module Secupdater
|
|
|
17
17
|
run
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
+
# Called once every dependency has been through #call, with the names of all
|
|
21
|
+
# the dependencies that have advisories. Returns messages about anything it
|
|
22
|
+
# tidied up.
|
|
23
|
+
def finish(vulnerable_names) = []
|
|
24
|
+
|
|
25
|
+
REFERENCE_PATTERN = %r{\Asecurity/(?<name>.+)-(?<version>[^-]+)\z}
|
|
26
|
+
|
|
20
27
|
def reference = "security/#{bump.dependency.name}-#{bump.to}"
|
|
21
28
|
|
|
22
29
|
def title = "Upgrade #{ecosystem.ecosystem_name} dependency #{bump.dependency.name} to version #{bump.to}"
|
|
23
30
|
|
|
24
31
|
def description
|
|
25
32
|
heading = "Upgrade #{ecosystem.ecosystem_name} dependency #{bump.dependency.name} to version #{bump.to} to address the following security advisories:"
|
|
26
|
-
|
|
33
|
+
# Advisories for other dependencies this update also fixes say which one
|
|
34
|
+
items = advisories.map do |advisory|
|
|
35
|
+
advisory.name == bump.dependency.name ? "* #{advisory.report_id}" : "* #{advisory.report_id} (#{advisory.name})"
|
|
36
|
+
end.join("\n")
|
|
27
37
|
"#{heading}\n\n#{items}\n"
|
|
28
38
|
end
|
|
29
39
|
|
data/lib/secupdater/update.rb
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
module Secupdater
|
|
2
2
|
class Update
|
|
3
|
+
# What updating one vulnerable dependency on its own came to, before anything
|
|
4
|
+
# is published. diff is every dependency the update moved, not just this one.
|
|
5
|
+
Candidate = Struct.new(:dependency, :advisories, :status, :message, :level, :bump, :diff, :file_changes, keyword_init: true) do
|
|
6
|
+
def name = dependency.name
|
|
7
|
+
def resolved? = status == :resolved
|
|
8
|
+
end
|
|
9
|
+
|
|
3
10
|
attr_reader :logger, :ecosystem, :publisher, :original
|
|
4
11
|
|
|
5
12
|
def initialize(logger, ecosystem, publisher: nil)
|
|
@@ -16,71 +23,148 @@ module Secupdater
|
|
|
16
23
|
missing = deps_needing_checking.keys - original.keys
|
|
17
24
|
raise Secupdater::Error, "Advisories for dependencies not in the lockfile: #{missing.join(', ')}" if missing.any?
|
|
18
25
|
|
|
19
|
-
deps_needing_checking.to_h do |name, dep_advisories|
|
|
20
|
-
[name,
|
|
26
|
+
candidates = deps_needing_checking.to_h do |name, dep_advisories|
|
|
27
|
+
[name, try_update(original[name], dep_advisories)]
|
|
21
28
|
end
|
|
29
|
+
|
|
30
|
+
results = publish(candidates)
|
|
31
|
+
|
|
32
|
+
publisher&.finish(deps_needing_checking.keys)&.each { logger.info it }
|
|
33
|
+
|
|
34
|
+
deps_needing_checking.keys.to_h { [it, results.fetch(it)] }
|
|
22
35
|
end
|
|
23
36
|
|
|
24
|
-
|
|
25
|
-
logger.info("starting update for #{dep.name}, #{advisories.length} advisories")
|
|
37
|
+
private
|
|
26
38
|
|
|
27
|
-
|
|
39
|
+
def try_update(dep, advisories)
|
|
40
|
+
logger.info("starting update for #{dep.name}, #{advisories.length} advisories")
|
|
28
41
|
|
|
29
42
|
levels = ecosystem.update_levels
|
|
43
|
+
candidate = nil
|
|
30
44
|
|
|
31
45
|
levels.each do |level|
|
|
32
|
-
|
|
33
|
-
return
|
|
46
|
+
candidate = attempt_update(dep, advisories, level)
|
|
47
|
+
return candidate if candidate.resolved?
|
|
34
48
|
|
|
35
|
-
logger.debug "#{level} update: #{message}" unless level == levels.last
|
|
49
|
+
logger.debug "#{level} update: #{candidate.message}" unless level == levels.last
|
|
36
50
|
end
|
|
37
51
|
|
|
38
|
-
|
|
39
|
-
logger.error message
|
|
40
|
-
else
|
|
41
|
-
logger.warn message
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
status
|
|
52
|
+
candidate
|
|
45
53
|
rescue Secupdater::UnretryableUpdateFailed => e
|
|
46
|
-
|
|
47
|
-
:failed
|
|
48
|
-
rescue Secupdater::PublishFailed => e
|
|
49
|
-
logger.error "#{dep.name}: #{e.message}"
|
|
50
|
-
:failed
|
|
54
|
+
Candidate.new(dependency: dep, advisories:, status: :failed, message: "#{dep.name}: #{e.message}\n#{e.output}")
|
|
51
55
|
end
|
|
52
56
|
|
|
53
|
-
private
|
|
54
|
-
|
|
55
57
|
def attempt_update(dep, advisories, level)
|
|
56
58
|
file_changes = ecosystem.try_update(dep.name, level:)
|
|
57
|
-
|
|
59
|
+
diff = ecosystem.diff_snapshot(original, ecosystem.snapshot)
|
|
60
|
+
bump = diff.detect { it.dependency.name == dep.name }
|
|
61
|
+
attempt = { dependency: dep, advisories:, level:, bump:, diff:, file_changes: }
|
|
58
62
|
|
|
59
|
-
return
|
|
63
|
+
return Candidate.new(**attempt, status: :unchanged, message: "#{dep.name} didn't update (stuck at #{dep.version})") unless bump
|
|
60
64
|
|
|
61
|
-
|
|
62
|
-
|
|
65
|
+
unless fixes?(bump, advisories)
|
|
66
|
+
return Candidate.new(**attempt, status: :unresolved, message: "#{dep.name} didn't update to a version that resolves the security warnings (went from #{bump.from} to #{bump.to})")
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
Candidate.new(**attempt, status: :resolved)
|
|
70
|
+
rescue Secupdater::UnretryableUpdateFailed
|
|
71
|
+
raise
|
|
72
|
+
rescue Secupdater::UpdateCommandFailed => e
|
|
73
|
+
Candidate.new(dependency: dep, advisories:, level:, status: :failed, message: "#{dep.name}: #{e.message}\n#{e.output}")
|
|
74
|
+
ensure
|
|
75
|
+
restore(file_changes)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Publishes each update, with the advisories of any dependencies it absorbs.
|
|
79
|
+
# If an absorbing update is declined, the dependencies it absorbed are
|
|
80
|
+
# published without it (absorbing each other where they can).
|
|
81
|
+
def publish(candidates)
|
|
82
|
+
absorbed = absorptions(candidates)
|
|
83
|
+
results = {}
|
|
84
|
+
|
|
85
|
+
candidates.each_value do |candidate|
|
|
86
|
+
next if absorbed.key?(candidate.name)
|
|
87
|
+
|
|
88
|
+
covered = absorbed.filter_map { |name, absorber| candidates[name] if absorber == candidate.name }
|
|
89
|
+
results[candidate.name] = publish_candidate(candidate, covered)
|
|
90
|
+
next if covered.empty?
|
|
91
|
+
|
|
92
|
+
if results[candidate.name] == :declined
|
|
93
|
+
logger.info "#{candidate.name}'s update was declined, so updating #{covered.map(&:name).join(', ')} without it"
|
|
94
|
+
results.merge!(publish(covered.to_h { [it.name, it] }))
|
|
95
|
+
else
|
|
96
|
+
covered.each { results[it.name] = :absorbed }
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
results
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Which dependencies' updates go into another's MR, as { absorbed => absorber }.
|
|
104
|
+
# A absorbs B when A's update also moves B to a version that fixes all of B's
|
|
105
|
+
# advisories. The updates that cover the most go first, then by name so runs
|
|
106
|
+
# agree, and each dependency ends up in only one MR.
|
|
107
|
+
def absorptions(candidates)
|
|
108
|
+
resolved = candidates.values.select(&:resolved?)
|
|
109
|
+
covers = resolved.to_h do |candidate|
|
|
110
|
+
[candidate.name, candidates.values.select { it != candidate && covers?(candidate, it) }.map(&:name)]
|
|
63
111
|
end
|
|
64
112
|
|
|
65
|
-
|
|
66
|
-
|
|
113
|
+
absorbed = {}
|
|
114
|
+
covers.sort_by { |name, names| [-names.length, name] }.each do |name, names|
|
|
115
|
+
next if absorbed.key?(name)
|
|
116
|
+
|
|
117
|
+
names.each do |other|
|
|
118
|
+
absorbed[other] = name unless absorbed.key?(other) || absorbed.value?(other)
|
|
119
|
+
end
|
|
67
120
|
end
|
|
121
|
+
absorbed
|
|
122
|
+
end
|
|
68
123
|
|
|
69
|
-
|
|
70
|
-
|
|
124
|
+
def covers?(candidate, other)
|
|
125
|
+
bump = candidate.diff.detect { it.dependency.name == other.name }
|
|
126
|
+
bump && (bump.to.nil? || fixes?(bump, other.advisories))
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def fixes?(bump, advisories)
|
|
130
|
+
advisories.all? do |advisory|
|
|
131
|
+
advisory.patched_versions.any? { ecosystem.satisfies_requirement?(bump.to, it) }
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def publish_candidate(candidate, covered)
|
|
136
|
+
unless candidate.resolved?
|
|
137
|
+
if candidate.status == :failed
|
|
138
|
+
logger.error candidate.message
|
|
139
|
+
else
|
|
140
|
+
logger.warn candidate.message
|
|
141
|
+
end
|
|
142
|
+
return candidate.status
|
|
71
143
|
end
|
|
72
144
|
|
|
73
|
-
|
|
145
|
+
log_resolution(candidate, covered)
|
|
146
|
+
|
|
147
|
+
return :resolved unless publisher
|
|
74
148
|
|
|
75
|
-
status, message = publisher.call(ecosystem, bump, advisories, file_changes)
|
|
149
|
+
status, message = publisher.call(ecosystem, candidate.bump, candidate.advisories + covered.flat_map(&:advisories), candidate.file_changes)
|
|
76
150
|
logger.warn message if message
|
|
77
|
-
|
|
78
|
-
rescue Secupdater::
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
151
|
+
status
|
|
152
|
+
rescue Secupdater::PublishFailed => e
|
|
153
|
+
logger.error "#{candidate.name}: #{e.message}"
|
|
154
|
+
:failed
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def log_resolution(candidate, covered)
|
|
158
|
+
bump = candidate.bump
|
|
159
|
+
upgrade = "upgrading #{candidate.name} from #{bump.from} to #{bump.to} (#{candidate.level})"
|
|
160
|
+
|
|
161
|
+
candidate.advisories.each { logger.info "#{upgrade} would resolve #{it}" }
|
|
162
|
+
|
|
163
|
+
covered.each do |other|
|
|
164
|
+
other_bump = candidate.diff.detect { it.dependency.name == other.name }
|
|
165
|
+
moves = other_bump.to ? "moves #{other.name} from #{other_bump.from} to #{other_bump.to}" : "removes #{other.name}"
|
|
166
|
+
other.advisories.each { logger.info "#{upgrade} #{moves}, which would resolve #{it}" }
|
|
167
|
+
end
|
|
84
168
|
end
|
|
85
169
|
|
|
86
170
|
def restore(file_changes)
|
data/lib/secupdater/version.rb
CHANGED
data/spec/e2e/secupdater_spec.rb
CHANGED
|
@@ -54,6 +54,8 @@ RSpec.describe "secupdater end to end" do
|
|
|
54
54
|
FileUtils.mkdir("bin")
|
|
55
55
|
File.write("bin/bundle", "#!/bin/sh\ncp Gemfile.lock.after Gemfile.lock\n")
|
|
56
56
|
File.chmod(0o755, "bin/bundle")
|
|
57
|
+
File.write("bin/gem", "#!/bin/sh\n")
|
|
58
|
+
File.chmod(0o755, "bin/gem")
|
|
57
59
|
|
|
58
60
|
system("git init -q && git add Gemfile.lock && git -c user.name=test -c user.email=test@example.com commit -qm init", exception: true)
|
|
59
61
|
|
|
@@ -70,7 +72,14 @@ RSpec.describe "secupdater end to end" do
|
|
|
70
72
|
let(:env) { { "SECUPDATER_GITLAB_TOKEN" => "token", "CI_PROJECT_ID" => "123", "CI_DEFAULT_BRANCH" => "main" } }
|
|
71
73
|
|
|
72
74
|
let!(:find_mr_stub) do
|
|
73
|
-
stub_request(:get,
|
|
75
|
+
stub_request(:get, "https://gitlab.com/api/v4/projects/123/merge_requests")
|
|
76
|
+
.with(query: hash_including(source_branch: "security/css_parser-1.1.0"))
|
|
77
|
+
.to_return(status: 200, body: "[]")
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
let!(:find_open_secupdater_mrs_stub) do
|
|
81
|
+
stub_request(:get, "https://gitlab.com/api/v4/projects/123/merge_requests")
|
|
82
|
+
.with(query: hash_including(state: "opened", labels: "secupdater"))
|
|
74
83
|
.to_return(status: 200, body: "[]")
|
|
75
84
|
end
|
|
76
85
|
|
|
@@ -99,6 +108,7 @@ RSpec.describe "secupdater end to end" do
|
|
|
99
108
|
expect { run_cli }.not_to raise_error
|
|
100
109
|
|
|
101
110
|
expect(find_mr_stub).to have_been_requested.once
|
|
111
|
+
expect(find_open_secupdater_mrs_stub).to have_been_requested.once
|
|
102
112
|
expect(create_commit_stub).to have_been_requested.once
|
|
103
113
|
expect(create_mr_stub).to have_been_requested.once
|
|
104
114
|
expect(File.read("Gemfile.lock")).to eq(lockfile("1.0.0"))
|
|
@@ -75,6 +75,21 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
75
75
|
expect(rubygems.parse_advisories(StringIO.new(report.to_json)).map(&:report_id)).to eq(["RUBYSEC-2024-1"])
|
|
76
76
|
end
|
|
77
77
|
end
|
|
78
|
+
|
|
79
|
+
context "the same advisory reported once per platform the gem is locked for" do
|
|
80
|
+
let(:result) do
|
|
81
|
+
{
|
|
82
|
+
type: "unpatched_gem",
|
|
83
|
+
gem: { name: "nokogiri", version: "1.19.4" },
|
|
84
|
+
advisory: { id: "GHSA-3w22-6jfg-527w", ghsa: "3w22-6jfg-527w", patched_versions: [">= 1.19.5"] }
|
|
85
|
+
}
|
|
86
|
+
end
|
|
87
|
+
let(:report) { { results: [result, result] } }
|
|
88
|
+
|
|
89
|
+
it "returns it once" do
|
|
90
|
+
expect(rubygems.parse_advisories(StringIO.new(report.to_json)).map(&:report_id)).to eq(["ghsa/3w22-6jfg-527w"])
|
|
91
|
+
end
|
|
92
|
+
end
|
|
78
93
|
end
|
|
79
94
|
|
|
80
95
|
describe "#snapshot" do
|
|
@@ -145,11 +160,65 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
145
160
|
}
|
|
146
161
|
end
|
|
147
162
|
|
|
148
|
-
|
|
163
|
+
let(:install_status) { instance_double(Process::Status, success?: true, exitstatus: 0) }
|
|
164
|
+
|
|
165
|
+
before do
|
|
166
|
+
allow(Bundler).to receive(:original_env).and_return(original_env)
|
|
167
|
+
allow(Open3).to receive(:capture2e).with(anything, "gem", any_args).and_return(["", install_status])
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
context "lockfile bundled with a Bundler other than the one running" do
|
|
171
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["", status]) }
|
|
172
|
+
|
|
173
|
+
it "installs that Bundler if it's missing, then runs bundle lock with it" do
|
|
174
|
+
rubygems.try_update("css_parser", level: :patch)
|
|
175
|
+
|
|
176
|
+
expect(Open3).to have_received(:capture2e).with(bundle_env, "gem", "install", "bundler", "--version", "2.5.0", "--conservative", "--no-document").ordered
|
|
177
|
+
expect(Open3).to have_received(:capture2e).with(bundle_env, "bundle", "_2.5.0_", "lock", any_args).ordered
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
it "only installs it once per run" do
|
|
181
|
+
rubygems.try_update("css_parser", level: :patch)
|
|
182
|
+
rubygems.try_update("css_parser", level: :minor)
|
|
183
|
+
|
|
184
|
+
expect(Open3).to have_received(:capture2e).with(anything, "gem", any_args).once
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
context "lockfile without BUNDLED WITH" do
|
|
189
|
+
before do
|
|
190
|
+
File.write("Gemfile.lock", lockfile("1.0.0").sub(/\nBUNDLED WITH\n.*\n\z/, ""))
|
|
191
|
+
allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["", status])
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
it "runs bundle lock with whichever Bundler RubyGems picks, without installing one" do
|
|
195
|
+
rubygems.try_update("css_parser", level: :patch)
|
|
196
|
+
|
|
197
|
+
expect(Open3).not_to have_received(:capture2e).with(anything, "gem", any_args)
|
|
198
|
+
expect(Open3).to have_received(:capture2e).with(bundle_env, "bundle", "lock", "--update", "css_parser", "--conservative", "--patch", "--strict")
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
context "locked Bundler can't be installed" do
|
|
203
|
+
let(:install_status) { instance_double(Process::Status, success?: false, exitstatus: 2) }
|
|
204
|
+
|
|
205
|
+
before do
|
|
206
|
+
allow(Open3).to receive(:capture2e).with(anything, "gem", any_args).and_return(["Could not find a valid gem 'bundler' (= 2.5.0)", install_status])
|
|
207
|
+
allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
it "raises Secupdater::Error rather than UpdateCommandFailed, so the run stops, and doesn't run bundle lock" do
|
|
211
|
+
expect { rubygems.try_update("css_parser", level: :patch) }.to raise_error(Secupdater::Error) do
|
|
212
|
+
expect(it).not_to be_a(Secupdater::UpdateCommandFailed)
|
|
213
|
+
expect(it.message).to include("Bundler 2.5.0", "Could not find a valid gem")
|
|
214
|
+
end
|
|
215
|
+
expect(Open3).not_to have_received(:capture2e).with(anything, "bundle", any_args)
|
|
216
|
+
end
|
|
217
|
+
end
|
|
149
218
|
|
|
150
219
|
context "command succeeds" do
|
|
151
220
|
before do
|
|
152
|
-
allow(Open3).to receive(:capture2e) do
|
|
221
|
+
allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args) do
|
|
153
222
|
File.write("Gemfile.lock", lockfile("1.1.0"))
|
|
154
223
|
["Bundle updated!", status]
|
|
155
224
|
end
|
|
@@ -158,7 +227,7 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
158
227
|
it "runs a strict, conservative, unfrozen bundle lock --update at the given level" do
|
|
159
228
|
rubygems.try_update("css_parser", level: :minor)
|
|
160
229
|
|
|
161
|
-
expect(Open3).to have_received(:capture2e).with(bundle_env, "bundle", "lock", "--update", "css_parser", "--conservative", "--minor", "--strict")
|
|
230
|
+
expect(Open3).to have_received(:capture2e).with(bundle_env, "bundle", "_2.5.0_", "lock", "--update", "css_parser", "--conservative", "--minor", "--strict")
|
|
162
231
|
end
|
|
163
232
|
|
|
164
233
|
it "returns the lockfile change with its new and original content" do
|
|
@@ -169,7 +238,7 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
169
238
|
end
|
|
170
239
|
|
|
171
240
|
context "command succeeds without changing anything" do
|
|
172
|
-
before { allow(Open3).to receive(:capture2e).and_return(["Bundle updated!", status]) }
|
|
241
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["Bundle updated!", status]) }
|
|
173
242
|
|
|
174
243
|
it "returns no file changes" do
|
|
175
244
|
expect(rubygems.try_update("css_parser", level: :patch)).to eq([])
|
|
@@ -177,12 +246,12 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
177
246
|
end
|
|
178
247
|
|
|
179
248
|
context "gem name containing shell metacharacters" do
|
|
180
|
-
before { allow(Open3).to receive(:capture2e).and_return(["", status]) }
|
|
249
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["", status]) }
|
|
181
250
|
|
|
182
251
|
it "passes it as a single argument without going through a shell" do
|
|
183
252
|
rubygems.try_update("css_parser; touch pwned", level: :patch)
|
|
184
253
|
|
|
185
|
-
expect(Open3).to have_received(:capture2e).with(bundle_env, "bundle", "lock", "--update", "css_parser; touch pwned", "--conservative", "--patch", "--strict")
|
|
254
|
+
expect(Open3).to have_received(:capture2e).with(bundle_env, "bundle", "_2.5.0_", "lock", "--update", "css_parser; touch pwned", "--conservative", "--patch", "--strict")
|
|
186
255
|
end
|
|
187
256
|
end
|
|
188
257
|
|
|
@@ -196,14 +265,14 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
196
265
|
}
|
|
197
266
|
end
|
|
198
267
|
|
|
199
|
-
before { allow(Open3).to receive(:capture2e).and_return(["", status]) }
|
|
268
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["", status]) }
|
|
200
269
|
|
|
201
270
|
it "runs bundle lock with Bundler's settings from before bundle exec, on the Gemfile in the working directory" do
|
|
202
271
|
rubygems.try_update("css_parser", level: :patch)
|
|
203
272
|
|
|
204
273
|
expect(Open3).to have_received(:capture2e).with(
|
|
205
274
|
bundle_env.merge("GEM_HOME" => "/usr/local/bundle", "RUBYOPT" => "-W0"),
|
|
206
|
-
"bundle", "lock", "--update", "css_parser", "--conservative", "--patch", "--strict"
|
|
275
|
+
"bundle", "_2.5.0_", "lock", "--update", "css_parser", "--conservative", "--patch", "--strict"
|
|
207
276
|
)
|
|
208
277
|
end
|
|
209
278
|
end
|
|
@@ -211,7 +280,7 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
211
280
|
context "command exits 1" do
|
|
212
281
|
let(:status) { instance_double(Process::Status, success?: false, exitstatus: 1) }
|
|
213
282
|
|
|
214
|
-
before { allow(Open3).to receive(:capture2e).and_return(["Could not find gem 'css_parser'", status]) }
|
|
283
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["Could not find gem 'css_parser'", status]) }
|
|
215
284
|
|
|
216
285
|
it "raises a retryable UpdateCommandFailed with the integer exit code and combined output" do
|
|
217
286
|
expect { rubygems.try_update("css_parser", level: :patch) }.to raise_error(Secupdater::UpdateCommandFailed) do
|
|
@@ -233,7 +302,7 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
233
302
|
context "command exits #{exit_code} (#{reason})" do
|
|
234
303
|
let(:status) { instance_double(Process::Status, success?: false, exitstatus: exit_code) }
|
|
235
304
|
|
|
236
|
-
before { allow(Open3).to receive(:capture2e).and_return([reason, status]) }
|
|
305
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return([reason, status]) }
|
|
237
306
|
|
|
238
307
|
it "raises UnretryableUpdateFailed with the exit code and output" do
|
|
239
308
|
expect { rubygems.try_update("css_parser", level: :patch) }.to raise_error(Secupdater::UnretryableUpdateFailed) do
|
|
@@ -247,7 +316,7 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
247
316
|
context "command exits 16 (frozen lockfile)" do
|
|
248
317
|
let(:status) { instance_double(Process::Status, success?: false, exitstatus: 16) }
|
|
249
318
|
|
|
250
|
-
before { allow(Open3).to receive(:capture2e).and_return(["frozen mode is set", status]) }
|
|
319
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["frozen mode is set", status]) }
|
|
251
320
|
|
|
252
321
|
it "raises Secupdater::Error rather than UpdateCommandFailed, so the run stops" do
|
|
253
322
|
expect { rubygems.try_update("css_parser", level: :patch) }.to raise_error(Secupdater::Error) do
|
|
@@ -53,9 +53,17 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
53
53
|
.to_return(status: 200, body: { commits: branch_commits }.to_json)
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
+
let(:target_diffs_since_branch) { [] }
|
|
57
|
+
|
|
58
|
+
let!(:target_moved_on_stub) do
|
|
59
|
+
stub_request(:get, "#{project_api}/repository/compare")
|
|
60
|
+
.with(query: { from: "security/css_parser-1.1.0", to: "abc123" })
|
|
61
|
+
.to_return(status: 200, body: { commits: [], diffs: target_diffs_since_branch }.to_json)
|
|
62
|
+
end
|
|
63
|
+
|
|
56
64
|
let!(:user_stub) do
|
|
57
65
|
stub_request(:get, "#{api}/user")
|
|
58
|
-
.to_return(status: 200, body: { email: bot_email, commit_email: bot_email, public_email: "" }.to_json)
|
|
66
|
+
.to_return(status: 200, body: { id: 42, email: bot_email, commit_email: bot_email, public_email: "" }.to_json)
|
|
59
67
|
end
|
|
60
68
|
|
|
61
69
|
let!(:branch_file_stub) do
|
|
@@ -86,7 +94,7 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
86
94
|
title: "Upgrade RubyGems dependency css_parser to version 1.1.0",
|
|
87
95
|
description: "Upgrade RubyGems dependency css_parser to version 1.1.0 to address the following security advisories:\n\n* cve/2024-0001\n",
|
|
88
96
|
}, headers: headers)
|
|
89
|
-
.to_return(status: 201, body:
|
|
97
|
+
.to_return(status: 201, body: { iid: 9 }.to_json)
|
|
90
98
|
end
|
|
91
99
|
|
|
92
100
|
def publish = gitlab.call(ecosystem, bump, advisories, changes)
|
|
@@ -101,6 +109,30 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
101
109
|
end
|
|
102
110
|
end
|
|
103
111
|
|
|
112
|
+
context "with advisories for other gems the update also fixes" do
|
|
113
|
+
let(:advisories) do
|
|
114
|
+
[
|
|
115
|
+
Secupdater::SecurityAdvisory.new("css_parser", "cve/2024-0001", [">= 1.1.0"]),
|
|
116
|
+
Secupdater::SecurityAdvisory.new("addressable", "ghsa/abcd-efgh", [">= 2.9.0"])
|
|
117
|
+
]
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
let!(:create_mr_stub) do
|
|
121
|
+
stub_request(:post, "#{project_api}/merge_requests")
|
|
122
|
+
.with(body: hash_including(
|
|
123
|
+
source_branch: "security/css_parser-1.1.0",
|
|
124
|
+
description: "Upgrade RubyGems dependency css_parser to version 1.1.0 to address the following security advisories:\n\n* cve/2024-0001\n* ghsa/abcd-efgh (addressable)\n"
|
|
125
|
+
))
|
|
126
|
+
.to_return(status: 201, body: { iid: 9 }.to_json)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
it "names the other gem against its advisories" do
|
|
130
|
+
publish
|
|
131
|
+
|
|
132
|
+
expect(create_mr_stub).to have_been_requested
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
104
136
|
context "deleting a file" do
|
|
105
137
|
let(:changes) { [Secupdater::FileChange.new("Gemfile.lock", "delete", nil, "xyz")] }
|
|
106
138
|
|
|
@@ -172,17 +204,56 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
172
204
|
end
|
|
173
205
|
|
|
174
206
|
context "an open MR, labelled or not" do
|
|
175
|
-
let(:
|
|
207
|
+
let(:description) { "Upgrade RubyGems dependency css_parser to version 1.1.0 to address the following security advisories:\n\n* cve/2024-0001\n" }
|
|
208
|
+
let(:existing_merge_requests) { [{ iid: 7, state: "opened", labels: [], description: }, { iid: 3, state: "closed", labels: ["secupdater"] }] }
|
|
176
209
|
let(:branch_exists) { true }
|
|
177
210
|
|
|
211
|
+
let!(:update_description_stub) do
|
|
212
|
+
stub_request(:put, "#{project_api}/merge_requests/7")
|
|
213
|
+
.with(body: { description: "Upgrade RubyGems dependency css_parser to version 1.1.0 to address the following security advisories:\n\n* cve/2024-0001\n" }, headers: headers)
|
|
214
|
+
.to_return(status: 200, body: "{}")
|
|
215
|
+
end
|
|
216
|
+
|
|
178
217
|
context "whose branch already has these changes" do
|
|
179
218
|
let(:branch_lockfile) { "abc" }
|
|
180
219
|
|
|
220
|
+
context "but whose description lists different advisories (e.g. the update has since absorbed another gem's)" do
|
|
221
|
+
let(:description) { "Upgrade RubyGems dependency css_parser to version 1.1.0 to address the following security advisories:\n\n* cve/2023-9999\n" }
|
|
222
|
+
|
|
223
|
+
it "updates the description, without committing" do
|
|
224
|
+
expect(publish).to eq([:published, nil])
|
|
225
|
+
|
|
226
|
+
expect(update_description_stub).to have_been_requested
|
|
227
|
+
expect(a_request(:post, "#{project_api}/repository/commits")).not_to have_been_made
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
|
|
181
231
|
it "leaves it alone" do
|
|
182
232
|
expect(publish).to eq([:up_to_date, nil])
|
|
183
233
|
|
|
184
234
|
expect(a_request(:post, //)).not_to have_been_made
|
|
185
235
|
end
|
|
236
|
+
|
|
237
|
+
context "but main has changed other files since the branch was cut" do
|
|
238
|
+
let(:target_diffs_since_branch) { [{ old_path: "app/models/user.rb", new_path: "app/models/user.rb" }] }
|
|
239
|
+
|
|
240
|
+
it "leaves it alone" do
|
|
241
|
+
expect(publish).to eq([:up_to_date, nil])
|
|
242
|
+
|
|
243
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
context "but main's Gemfile.lock has changed since the branch was cut (e.g. another MR made some of the same bumps)" do
|
|
248
|
+
let(:target_diffs_since_branch) { [{ old_path: "Gemfile.lock", new_path: "Gemfile.lock" }] }
|
|
249
|
+
|
|
250
|
+
it "replaces the branch's commit, from start_sha, so it doesn't show changes main already has" do
|
|
251
|
+
expect(publish).to eq([:published, nil])
|
|
252
|
+
|
|
253
|
+
expect(force_commit_stub).to have_been_requested
|
|
254
|
+
expect(create_mr_stub).not_to have_been_requested
|
|
255
|
+
end
|
|
256
|
+
end
|
|
186
257
|
end
|
|
187
258
|
|
|
188
259
|
context "whose branch is out of date" do
|
|
@@ -202,10 +273,72 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
202
273
|
]
|
|
203
274
|
end
|
|
204
275
|
|
|
276
|
+
let(:existing_notes) { [] }
|
|
277
|
+
|
|
278
|
+
let!(:notes_stub) do
|
|
279
|
+
stub_request(:get, "#{project_api}/merge_requests/7/notes")
|
|
280
|
+
.with(query: { per_page: "100" }, headers: headers)
|
|
281
|
+
.to_return(status: 200, body: existing_notes.to_json)
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
let!(:comment_stub) do
|
|
285
|
+
stub_request(:post, "#{project_api}/merge_requests/7/notes")
|
|
286
|
+
.with(body: { body: <<~BODY.chomp }, headers: headers)
|
|
287
|
+
<!-- secupdater:skipping -->
|
|
288
|
+
This branch has commits from Jo Bloggs, so secupdater won't update it any more, including rebasing it when `main` changes.
|
|
289
|
+
|
|
290
|
+
To hand it back, delete the branch, or force-push it without those commits, and secupdater will take it over on its next run.
|
|
291
|
+
BODY
|
|
292
|
+
.to_return(status: 201, body: "{}")
|
|
293
|
+
end
|
|
294
|
+
|
|
205
295
|
it "leaves the branch alone and says why" do
|
|
206
296
|
expect(publish).to eq([:skipped, "security/css_parser-1.1.0 has commits from Jo Bloggs, so it won't be updated"])
|
|
207
297
|
|
|
208
|
-
expect(a_request(:post,
|
|
298
|
+
expect(a_request(:post, "#{project_api}/repository/commits")).not_to have_been_made
|
|
299
|
+
expect(a_request(:put, //)).not_to have_been_made
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
it "comments on the MR to say secupdater has stopped updating it" do
|
|
303
|
+
publish
|
|
304
|
+
|
|
305
|
+
expect(comment_stub).to have_been_requested
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
context "when it has already said so" do
|
|
309
|
+
let(:existing_notes) do
|
|
310
|
+
[
|
|
311
|
+
{ author: { id: 7 }, body: "Thanks, I've fixed the spec" },
|
|
312
|
+
{ author: { id: 42 }, body: "<!-- secupdater:skipping -->\nThis branch has commits from Jo Bloggs…" }
|
|
313
|
+
]
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
it "doesn't say it again" do
|
|
317
|
+
expect(publish.first).to eq(:skipped)
|
|
318
|
+
|
|
319
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
320
|
+
end
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
context "when only someone else has posted the marker (e.g. quoting it)" do
|
|
324
|
+
let(:existing_notes) { [{ author: { id: 7 }, body: "> <!-- secupdater:skipping -->" }] }
|
|
325
|
+
|
|
326
|
+
it "still says so" do
|
|
327
|
+
publish
|
|
328
|
+
|
|
329
|
+
expect(comment_stub).to have_been_requested
|
|
330
|
+
end
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
context "a branch without an MR, with a commit from someone else" do
|
|
335
|
+
let(:existing_merge_requests) { [] }
|
|
336
|
+
let(:branch_commits) { [{ author_name: "Jo Bloggs", author_email: "jo@example.com" }] }
|
|
337
|
+
|
|
338
|
+
it "skips it without commenting anywhere" do
|
|
339
|
+
expect(publish.first).to eq(:skipped)
|
|
340
|
+
|
|
341
|
+
expect(a_request(:any, %r{/notes})).not_to have_been_made
|
|
209
342
|
end
|
|
210
343
|
end
|
|
211
344
|
|
|
@@ -243,4 +376,102 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
243
376
|
end
|
|
244
377
|
end
|
|
245
378
|
end
|
|
379
|
+
|
|
380
|
+
describe "#finish(vulnerable_names)" do
|
|
381
|
+
let(:open_secupdater_merge_requests) { [] }
|
|
382
|
+
|
|
383
|
+
let!(:find_open_stub) do
|
|
384
|
+
stub_request(:get, "#{project_api}/merge_requests")
|
|
385
|
+
.with(query: { state: "opened", labels: "secupdater", target_branch: "main", per_page: "100" }, headers: headers)
|
|
386
|
+
.to_return(status: 200, body: open_secupdater_merge_requests.to_json)
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
let!(:note_stub) { stub_request(:post, %r{\A#{project_api}/merge_requests/\d+/notes\z}).to_return(status: 201, body: "{}") }
|
|
390
|
+
let!(:close_stub) do
|
|
391
|
+
stub_request(:put, %r{\A#{project_api}/merge_requests/\d+\z})
|
|
392
|
+
.with(body: { state_event: "close" })
|
|
393
|
+
.to_return(status: 200, body: "{}")
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
context "an older MR for a dependency this run opened a new MR for" do
|
|
397
|
+
let(:open_secupdater_merge_requests) do
|
|
398
|
+
[
|
|
399
|
+
{ iid: 5, source_branch: "security/css_parser-1.0.5" },
|
|
400
|
+
{ iid: 9, source_branch: "security/css_parser-1.1.0" }
|
|
401
|
+
]
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
before { publish }
|
|
405
|
+
|
|
406
|
+
it "closes it, saying which MR superseded it" do
|
|
407
|
+
expect(gitlab.finish(["css_parser"])).to eq(["closed !5 (security/css_parser-1.0.5): Superseded by !9."])
|
|
408
|
+
|
|
409
|
+
expect(a_request(:post, "#{project_api}/merge_requests/5/notes").with(body: { body: "Superseded by !9." })).to have_been_made
|
|
410
|
+
expect(a_request(:put, "#{project_api}/merge_requests/5")).to have_been_made
|
|
411
|
+
expect(a_request(:put, //)).to have_been_made.once
|
|
412
|
+
end
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
context "an MR for a dependency whose advisories this run's MR for another dependency also fixes" do
|
|
416
|
+
let(:advisories) do
|
|
417
|
+
[
|
|
418
|
+
Secupdater::SecurityAdvisory.new("css_parser", "cve/2024-0001", [">= 1.1.0"]),
|
|
419
|
+
Secupdater::SecurityAdvisory.new("addressable", "ghsa/abcd-efgh", [">= 2.9.0"])
|
|
420
|
+
]
|
|
421
|
+
end
|
|
422
|
+
let(:open_secupdater_merge_requests) do
|
|
423
|
+
[
|
|
424
|
+
{ iid: 4, source_branch: "security/addressable-2.9.0" },
|
|
425
|
+
{ iid: 9, source_branch: "security/css_parser-1.1.0" }
|
|
426
|
+
]
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
before do
|
|
430
|
+
stub_request(:post, "#{project_api}/merge_requests").to_return(status: 201, body: { iid: 9 }.to_json)
|
|
431
|
+
publish
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
it "closes it, saying which MR superseded it" do
|
|
435
|
+
expect(gitlab.finish(["addressable", "css_parser"])).to eq(["closed !4 (security/addressable-2.9.0): Superseded by !9."])
|
|
436
|
+
|
|
437
|
+
expect(a_request(:put, //)).to have_been_made.once
|
|
438
|
+
end
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
context "an MR for a dependency that no longer has advisories" do
|
|
442
|
+
let(:open_secupdater_merge_requests) { [{ iid: 4, source_branch: "security/net-ssh-7.3.8" }] }
|
|
443
|
+
|
|
444
|
+
it "closes it, saying it's no longer needed" do
|
|
445
|
+
expect(gitlab.finish(["css_parser"])).to eq(
|
|
446
|
+
["closed !4 (security/net-ssh-7.3.8): net-ssh no longer has any security advisories on main, so this is no longer needed."]
|
|
447
|
+
)
|
|
448
|
+
|
|
449
|
+
expect(a_request(:put, "#{project_api}/merge_requests/4")).to have_been_made
|
|
450
|
+
expect(a_request(:put, //)).to have_been_made.once
|
|
451
|
+
end
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
context "an MR for a dependency that still has advisories, but that this run didn't publish" do
|
|
455
|
+
let(:open_secupdater_merge_requests) { [{ iid: 4, source_branch: "security/net-ssh-7.3.8" }] }
|
|
456
|
+
|
|
457
|
+
it "leaves it alone" do
|
|
458
|
+
expect(gitlab.finish(["net-ssh"])).to eq([])
|
|
459
|
+
|
|
460
|
+
expect(a_request(:put, //)).not_to have_been_made
|
|
461
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
462
|
+
end
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
context "the MR this run opened or kept" do
|
|
466
|
+
let(:open_secupdater_merge_requests) { [{ iid: 9, source_branch: "security/css_parser-1.1.0" }] }
|
|
467
|
+
|
|
468
|
+
before { publish }
|
|
469
|
+
|
|
470
|
+
it "leaves it alone" do
|
|
471
|
+
expect(gitlab.finish(["css_parser"])).to eq([])
|
|
472
|
+
|
|
473
|
+
expect(a_request(:put, //)).not_to have_been_made
|
|
474
|
+
end
|
|
475
|
+
end
|
|
476
|
+
end
|
|
246
477
|
end
|
|
@@ -5,7 +5,8 @@ require "stringio"
|
|
|
5
5
|
require "tmpdir"
|
|
6
6
|
|
|
7
7
|
module UpdateSpec
|
|
8
|
-
# outcomes: gem name => version try_update moves it to,
|
|
8
|
+
# outcomes: gem name => version try_update moves it to, a hash of gem name => version when the update
|
|
9
|
+
# moves other gems too, :unchanged, :fail, :fail_unretryable or :abort, or a hash of those by level
|
|
9
10
|
class FakeEcosystem
|
|
10
11
|
LOCKFILE = "Gemfile.lock"
|
|
11
12
|
|
|
@@ -35,7 +36,7 @@ module UpdateSpec
|
|
|
35
36
|
attempts << [name, level]
|
|
36
37
|
|
|
37
38
|
outcome = outcomes.fetch(name)
|
|
38
|
-
outcome = outcome.fetch(level) if outcome.is_a?(Hash)
|
|
39
|
+
outcome = outcome.fetch(level) if outcome.is_a?(Hash) && outcome.keys.all?(Symbol)
|
|
39
40
|
|
|
40
41
|
case outcome
|
|
41
42
|
when :fail
|
|
@@ -47,7 +48,8 @@ module UpdateSpec
|
|
|
47
48
|
when :unchanged
|
|
48
49
|
[]
|
|
49
50
|
else
|
|
50
|
-
|
|
51
|
+
moves = outcome.is_a?(Hash) ? outcome : { name => outcome }
|
|
52
|
+
after = JSON.generate(JSON.parse(before).merge(moves))
|
|
51
53
|
File.write(LOCKFILE, after)
|
|
52
54
|
[Secupdater::FileChange.new(LOCKFILE, "update", after, before)]
|
|
53
55
|
end
|
|
@@ -85,6 +87,7 @@ RSpec.describe Secupdater::Update do
|
|
|
85
87
|
|
|
86
88
|
before do
|
|
87
89
|
allow(publisher).to receive(:call) { |*args| published << args; [:published, nil] } if publisher
|
|
90
|
+
allow(publisher).to receive(:finish).and_return([]) if publisher
|
|
88
91
|
end
|
|
89
92
|
|
|
90
93
|
describe "#call(advisories)" do
|
|
@@ -274,6 +277,30 @@ RSpec.describe Secupdater::Update do
|
|
|
274
277
|
end
|
|
275
278
|
end
|
|
276
279
|
|
|
280
|
+
context "once every gem has been through the publisher" do
|
|
281
|
+
let(:outcomes) { { "rack" => "2.1", "nokogiri" => :unchanged } }
|
|
282
|
+
|
|
283
|
+
before { allow(publisher).to receive(:finish).and_return(["closed !5 (security/rack-2.0): Superseded by !9."]) }
|
|
284
|
+
|
|
285
|
+
it "lets the publisher tidy up, with the names of every gem that has advisories" do
|
|
286
|
+
update.call([advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")])
|
|
287
|
+
|
|
288
|
+
expect(publisher).to have_received(:finish).with(["rack", "nokogiri"])
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
it "logs what it tidied up" do
|
|
292
|
+
update.call([advisory("rack", "cve/1")])
|
|
293
|
+
|
|
294
|
+
expect(log.string).to match(%r{^INFO closed !5 \(security/rack-2\.0\): Superseded by !9\.})
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
it "does so even when there are no advisories, so MRs whose fixes have landed get closed" do
|
|
298
|
+
expect(update.call([])).to eq({})
|
|
299
|
+
|
|
300
|
+
expect(publisher).to have_received(:finish).with([])
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
277
304
|
context "publishing fails for the first gem" do
|
|
278
305
|
let(:outcomes) { { "rack" => "2.1", "nokogiri" => "2.0" } }
|
|
279
306
|
|
|
@@ -357,6 +384,127 @@ RSpec.describe Secupdater::Update do
|
|
|
357
384
|
end
|
|
358
385
|
end
|
|
359
386
|
|
|
387
|
+
context "one gem's update also fixes other vulnerable gems (e.g. rails-html-sanitizer needing newer loofah and nokogiri)" do
|
|
388
|
+
let(:lockfile) { { "rails-html-sanitizer" => "1.0", "loofah" => "1.0", "nokogiri" => "1.0" } }
|
|
389
|
+
let(:outcomes) do
|
|
390
|
+
{
|
|
391
|
+
"rails-html-sanitizer" => { "rails-html-sanitizer" => "2.0", "loofah" => "2.0", "nokogiri" => "2.0" },
|
|
392
|
+
"loofah" => { "loofah" => "2.0", "nokogiri" => "2.0" },
|
|
393
|
+
"nokogiri" => "2.0"
|
|
394
|
+
}
|
|
395
|
+
end
|
|
396
|
+
let(:advisories) do
|
|
397
|
+
[advisory("loofah", "ghsa/l"), advisory("nokogiri", "ghsa/n1"), advisory("nokogiri", "ghsa/n2"), advisory("rails-html-sanitizer", "ghsa/r")]
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
it "publishes the update that covers the most, once, with every gem's advisories" do
|
|
401
|
+
update.call(advisories)
|
|
402
|
+
|
|
403
|
+
expect(published.length).to eq(1)
|
|
404
|
+
|
|
405
|
+
_, bump, advs, changes = published.first
|
|
406
|
+
expect(bump.dependency.name).to eq("rails-html-sanitizer")
|
|
407
|
+
expect(advs.map(&:report_id)).to contain_exactly("ghsa/r", "ghsa/l", "ghsa/n1", "ghsa/n2")
|
|
408
|
+
expect(JSON.parse(changes.first.content)).to eq("rails-html-sanitizer" => "2.0", "loofah" => "2.0", "nokogiri" => "2.0")
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
it "returns :absorbed for the gems it covers" do
|
|
412
|
+
expect(update.call(advisories)).to eq(
|
|
413
|
+
"loofah" => :absorbed,
|
|
414
|
+
"nokogiri" => :absorbed,
|
|
415
|
+
"rails-html-sanitizer" => :published
|
|
416
|
+
)
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
it "logs which of its moves resolves each of the other gems' advisories" do
|
|
420
|
+
update.call(advisories)
|
|
421
|
+
|
|
422
|
+
expect(log.string).to match(%r{^INFO upgrading rails-html-sanitizer from 1\.0 to 2\.0 \(patch\) moves loofah from 1\.0 to 2\.0, which would resolve loofah ghsa/l})
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
it "tells the publisher to tidy up every gem, so their own MRs get closed" do
|
|
426
|
+
update.call(advisories)
|
|
427
|
+
|
|
428
|
+
expect(publisher).to have_received(:finish).with(["loofah", "nokogiri", "rails-html-sanitizer"])
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
it "leaves the lockfile as it found it" do
|
|
432
|
+
update.call(advisories)
|
|
433
|
+
|
|
434
|
+
expect(lockfile_on_disk).to eq(lockfile)
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
context "and the covering update was declined" do
|
|
438
|
+
before do
|
|
439
|
+
allow(publisher).to receive(:call) do |*args|
|
|
440
|
+
published << args
|
|
441
|
+
args[1].dependency.name == "rails-html-sanitizer" ? [:declined, "security/rails-html-sanitizer-2.0 was closed in !7"] : [:published, nil]
|
|
442
|
+
end
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
it "publishes the other gems without it, still folding them together where it can" do
|
|
446
|
+
expect(update.call(advisories)).to eq(
|
|
447
|
+
"loofah" => :published,
|
|
448
|
+
"nokogiri" => :absorbed,
|
|
449
|
+
"rails-html-sanitizer" => :declined
|
|
450
|
+
)
|
|
451
|
+
|
|
452
|
+
loofah = published.find { it[1].dependency.name == "loofah" }
|
|
453
|
+
expect(loofah[2].map(&:report_id)).to contain_exactly("ghsa/l", "ghsa/n1", "ghsa/n2")
|
|
454
|
+
end
|
|
455
|
+
end
|
|
456
|
+
|
|
457
|
+
context "with no publisher (dry run)" do
|
|
458
|
+
let(:publisher) { nil }
|
|
459
|
+
|
|
460
|
+
it "returns :resolved for the covering gem and :absorbed for the others" do
|
|
461
|
+
expect(update.call(advisories)).to eq(
|
|
462
|
+
"loofah" => :absorbed,
|
|
463
|
+
"nokogiri" => :absorbed,
|
|
464
|
+
"rails-html-sanitizer" => :resolved
|
|
465
|
+
)
|
|
466
|
+
end
|
|
467
|
+
end
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
context "a gem whose own update fails, but another gem's update fixes it" do
|
|
471
|
+
let(:outcomes) { { "rack" => { "rack" => "2.1", "nokogiri" => "2.0" }, "nokogiri" => :fail } }
|
|
472
|
+
|
|
473
|
+
it "returns :absorbed for it, without logging its failure as an error" do
|
|
474
|
+
expect(update.call([advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")])).to eq("rack" => :published, "nokogiri" => :absorbed)
|
|
475
|
+
expect(log.string).not_to match(/^ERROR/)
|
|
476
|
+
end
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
context "a gem's update moves another vulnerable gem, but not far enough to fix it" do
|
|
480
|
+
let(:outcomes) { { "rack" => { "rack" => "2.1", "nokogiri" => "1.5" }, "nokogiri" => "2.0" } }
|
|
481
|
+
|
|
482
|
+
it "publishes each separately" do
|
|
483
|
+
expect(update.call([advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")])).to eq("rack" => :published, "nokogiri" => :published)
|
|
484
|
+
|
|
485
|
+
expect(published.map { [it[1].dependency.name, it[2].map(&:report_id)] }).to contain_exactly(["rack", ["cve/1"]], ["nokogiri", ["cve/2"]])
|
|
486
|
+
end
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
context "a gem's update fixes another gem, but doesn't fix its own advisories" do
|
|
490
|
+
let(:outcomes) { { "rack" => { "rack" => "1.5", "nokogiri" => "2.0" }, "nokogiri" => "2.0" } }
|
|
491
|
+
|
|
492
|
+
it "doesn't absorb anything into it" do
|
|
493
|
+
expect(update.call([advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")])).to eq("rack" => :unresolved, "nokogiri" => :published)
|
|
494
|
+
end
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
context "two gems whose updates each fix the other" do
|
|
498
|
+
let(:outcomes) do
|
|
499
|
+
{ "rack" => { "rack" => "2.1", "nokogiri" => "2.0" }, "nokogiri" => { "rack" => "2.1", "nokogiri" => "2.0" } }
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
it "publishes one of them, picked by name, so every run agrees" do
|
|
503
|
+
expect(update.call([advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")])).to eq("rack" => :absorbed, "nokogiri" => :published)
|
|
504
|
+
expect(published.length).to eq(1)
|
|
505
|
+
end
|
|
506
|
+
end
|
|
507
|
+
|
|
360
508
|
context "a mix of outcomes" do
|
|
361
509
|
let(:lockfile) { { "rack" => "1.0", "nokogiri" => "1.0", "puma" => "1.0", "rails" => "1.0" } }
|
|
362
510
|
let(:outcomes) { { "rack" => "2.1", "nokogiri" => "1.5", "puma" => :unchanged, "rails" => :fail } }
|