secupdater 0.3.0 → 0.5.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.
@@ -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
@@ -89,6 +97,23 @@ RSpec.describe Secupdater::Publisher::Gitlab do
89
97
  .to_return(status: 201, body: { iid: 9 }.to_json)
90
98
  end
91
99
 
100
+ let(:head_pipeline) { nil }
101
+ let(:merge_request_details) do
102
+ { sha: "def456", draft: false, merge_when_pipeline_succeeds: false, head_pipeline: }
103
+ end
104
+
105
+ let!(:get_merge_request_stub) do
106
+ stub_request(:get, %r{\A#{project_api}/merge_requests/\d+\z})
107
+ .with(headers: headers)
108
+ .to_return(status: 200, body: merge_request_details.to_json)
109
+ end
110
+
111
+ let!(:auto_merge_stub) do
112
+ stub_request(:put, %r{\A#{project_api}/merge_requests/\d+/merge\z})
113
+ .with(body: { auto_merge: true, sha: "def456", should_remove_source_branch: true }, headers: headers)
114
+ .to_return(status: 200, body: "{}")
115
+ end
116
+
92
117
  def publish = gitlab.call(ecosystem, bump, advisories, changes)
93
118
 
94
119
  describe "#call(ecosystem, bump, advisories, changes)" do
@@ -99,6 +124,37 @@ RSpec.describe Secupdater::Publisher::Gitlab do
99
124
  expect(create_commit_stub).to have_been_requested
100
125
  expect(create_mr_stub).to have_been_requested
101
126
  end
127
+
128
+ it "leaves auto-merge to the next run, as the commit has no pipeline yet" do
129
+ publish
130
+
131
+ expect(get_merge_request_stub).not_to have_been_requested
132
+ expect(auto_merge_stub).not_to have_been_requested
133
+ end
134
+ end
135
+
136
+ context "with advisories for other gems the update also fixes" do
137
+ let(:advisories) do
138
+ [
139
+ Secupdater::SecurityAdvisory.new("css_parser", "cve/2024-0001", [">= 1.1.0"]),
140
+ Secupdater::SecurityAdvisory.new("addressable", "ghsa/abcd-efgh", [">= 2.9.0"])
141
+ ]
142
+ end
143
+
144
+ let!(:create_mr_stub) do
145
+ stub_request(:post, "#{project_api}/merge_requests")
146
+ .with(body: hash_including(
147
+ source_branch: "security/css_parser-1.1.0",
148
+ 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"
149
+ ))
150
+ .to_return(status: 201, body: { iid: 9 }.to_json)
151
+ end
152
+
153
+ it "names the other gem against its advisories" do
154
+ publish
155
+
156
+ expect(create_mr_stub).to have_been_requested
157
+ end
102
158
  end
103
159
 
104
160
  context "deleting a file" do
@@ -172,17 +228,141 @@ RSpec.describe Secupdater::Publisher::Gitlab do
172
228
  end
173
229
 
174
230
  context "an open MR, labelled or not" do
175
- let(:existing_merge_requests) { [{ iid: 7, state: "opened", labels: [] }, { iid: 3, state: "closed", labels: ["secupdater"] }] }
231
+ let(:description) { "Upgrade RubyGems dependency css_parser to version 1.1.0 to address the following security advisories:\n\n* cve/2024-0001\n" }
232
+ let(:existing_merge_requests) { [{ iid: 7, state: "opened", labels: [], description: }, { iid: 3, state: "closed", labels: ["secupdater"] }] }
176
233
  let(:branch_exists) { true }
177
234
 
235
+ let!(:update_description_stub) do
236
+ stub_request(:put, "#{project_api}/merge_requests/7")
237
+ .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)
238
+ .to_return(status: 200, body: "{}")
239
+ end
240
+
178
241
  context "whose branch already has these changes" do
179
242
  let(:branch_lockfile) { "abc" }
180
243
 
244
+ context "but whose description lists different advisories (e.g. the update has since absorbed another gem's)" do
245
+ let(:description) { "Upgrade RubyGems dependency css_parser to version 1.1.0 to address the following security advisories:\n\n* cve/2023-9999\n" }
246
+
247
+ it "updates the description, without committing" do
248
+ expect(publish).to eq([:published, nil])
249
+
250
+ expect(update_description_stub).to have_been_requested
251
+ expect(a_request(:post, "#{project_api}/repository/commits")).not_to have_been_made
252
+ end
253
+ end
254
+
181
255
  it "leaves it alone" do
182
256
  expect(publish).to eq([:up_to_date, nil])
183
257
 
184
258
  expect(a_request(:post, //)).not_to have_been_made
185
259
  end
260
+
261
+ describe "auto-merge" do
262
+ context "with a pipeline running for the branch's head commit" do
263
+ let(:head_pipeline) { { sha: "def456", status: "running" } }
264
+
265
+ it "sets the MR to auto-merge, pinned to that commit" do
266
+ expect(publish).to eq([:up_to_date, nil])
267
+
268
+ expect(a_request(:put, "#{project_api}/merge_requests/7/merge")).to have_been_made
269
+ end
270
+
271
+ context "already set to auto-merge" do
272
+ let(:merge_request_details) { super().merge(merge_when_pipeline_succeeds: true) }
273
+
274
+ it "doesn't set it again" do
275
+ publish
276
+
277
+ expect(auto_merge_stub).not_to have_been_requested
278
+ end
279
+ end
280
+
281
+ context "on a draft MR" do
282
+ let(:merge_request_details) { super().merge(draft: true) }
283
+
284
+ it "leaves it for whoever marked it draft" do
285
+ publish
286
+
287
+ expect(auto_merge_stub).not_to have_been_requested
288
+ end
289
+ end
290
+
291
+ context "that GitLab won't merge (e.g. it has conflicts)" do
292
+ let!(:auto_merge_stub) do
293
+ stub_request(:put, "#{project_api}/merge_requests/7/merge")
294
+ .to_return(status: 405, body: '{"message":"405 Method Not Allowed"}')
295
+ end
296
+
297
+ it "says so, without failing the update" do
298
+ expect(publish).to eq([:up_to_date, "security/css_parser-1.1.0: couldn't set !7 to auto-merge, GitLab returned 405: {\"message\":\"405 Method Not Allowed\"}"])
299
+ end
300
+ end
301
+
302
+ context "when the token isn't allowed to merge" do
303
+ let!(:auto_merge_stub) do
304
+ stub_request(:put, "#{project_api}/merge_requests/7/merge")
305
+ .to_return(status: 401, body: '{"message":"401 Unauthorized"}')
306
+ end
307
+
308
+ it "fails the update" do
309
+ expect { publish }.to raise_error(Secupdater::PublishFailed, /401/)
310
+ end
311
+ end
312
+ end
313
+
314
+ context "without a pipeline" do
315
+ it "waits for one" do
316
+ publish
317
+
318
+ expect(get_merge_request_stub).to have_been_requested
319
+ expect(auto_merge_stub).not_to have_been_requested
320
+ end
321
+ end
322
+
323
+ context "with a pipeline for an older commit" do
324
+ let(:head_pipeline) { { sha: "abc000", status: "success" } }
325
+
326
+ it "waits for one for the head commit" do
327
+ publish
328
+
329
+ expect(auto_merge_stub).not_to have_been_requested
330
+ end
331
+ end
332
+
333
+ %w[failed canceled skipped].each do |status|
334
+ context "with a #{status} pipeline" do
335
+ let(:head_pipeline) { { sha: "def456", status: } }
336
+
337
+ it "doesn't set it, as the pipeline can't pass" do
338
+ publish
339
+
340
+ expect(auto_merge_stub).not_to have_been_requested
341
+ end
342
+ end
343
+ end
344
+ end
345
+
346
+ context "but main has changed other files since the branch was cut" do
347
+ let(:target_diffs_since_branch) { [{ old_path: "app/models/user.rb", new_path: "app/models/user.rb" }] }
348
+
349
+ it "leaves it alone" do
350
+ expect(publish).to eq([:up_to_date, nil])
351
+
352
+ expect(a_request(:post, //)).not_to have_been_made
353
+ end
354
+ end
355
+
356
+ context "but main's Gemfile.lock has changed since the branch was cut (e.g. another MR made some of the same bumps)" do
357
+ let(:target_diffs_since_branch) { [{ old_path: "Gemfile.lock", new_path: "Gemfile.lock" }] }
358
+
359
+ it "replaces the branch's commit, from start_sha, so it doesn't show changes main already has" do
360
+ expect(publish).to eq([:published, nil])
361
+
362
+ expect(force_commit_stub).to have_been_requested
363
+ expect(create_mr_stub).not_to have_been_requested
364
+ end
365
+ end
186
366
  end
187
367
 
188
368
  context "whose branch is out of date" do
@@ -192,6 +372,16 @@ RSpec.describe Secupdater::Publisher::Gitlab do
192
372
  expect(force_commit_stub).to have_been_requested
193
373
  expect(create_mr_stub).not_to have_been_requested
194
374
  end
375
+
376
+ context "with a pipeline for the replaced commit" do
377
+ let(:head_pipeline) { { sha: "def456", status: "running" } }
378
+
379
+ it "leaves auto-merge to the next run, once the new commit has a pipeline" do
380
+ publish
381
+
382
+ expect(auto_merge_stub).not_to have_been_requested
383
+ end
384
+ end
195
385
  end
196
386
 
197
387
  context "whose branch has a commit from someone else" do
@@ -202,10 +392,72 @@ RSpec.describe Secupdater::Publisher::Gitlab do
202
392
  ]
203
393
  end
204
394
 
395
+ let(:existing_notes) { [] }
396
+
397
+ let!(:notes_stub) do
398
+ stub_request(:get, "#{project_api}/merge_requests/7/notes")
399
+ .with(query: { per_page: "100" }, headers: headers)
400
+ .to_return(status: 200, body: existing_notes.to_json)
401
+ end
402
+
403
+ let!(:comment_stub) do
404
+ stub_request(:post, "#{project_api}/merge_requests/7/notes")
405
+ .with(body: { body: <<~BODY.chomp }, headers: headers)
406
+ <!-- secupdater:skipping -->
407
+ This branch has commits from Jo Bloggs, so secupdater won't update it any more, including rebasing it when `main` changes.
408
+
409
+ To hand it back, delete the branch, or force-push it without those commits, and secupdater will take it over on its next run.
410
+ BODY
411
+ .to_return(status: 201, body: "{}")
412
+ end
413
+
205
414
  it "leaves the branch alone and says why" do
206
415
  expect(publish).to eq([:skipped, "security/css_parser-1.1.0 has commits from Jo Bloggs, so it won't be updated"])
207
416
 
208
- expect(a_request(:post, //)).not_to have_been_made
417
+ expect(a_request(:post, "#{project_api}/repository/commits")).not_to have_been_made
418
+ expect(a_request(:put, //)).not_to have_been_made
419
+ end
420
+
421
+ it "comments on the MR to say secupdater has stopped updating it" do
422
+ publish
423
+
424
+ expect(comment_stub).to have_been_requested
425
+ end
426
+
427
+ context "when it has already said so" do
428
+ let(:existing_notes) do
429
+ [
430
+ { author: { id: 7 }, body: "Thanks, I've fixed the spec" },
431
+ { author: { id: 42 }, body: "<!-- secupdater:skipping -->\nThis branch has commits from Jo Bloggs…" }
432
+ ]
433
+ end
434
+
435
+ it "doesn't say it again" do
436
+ expect(publish.first).to eq(:skipped)
437
+
438
+ expect(a_request(:post, //)).not_to have_been_made
439
+ end
440
+ end
441
+
442
+ context "when only someone else has posted the marker (e.g. quoting it)" do
443
+ let(:existing_notes) { [{ author: { id: 7 }, body: "> <!-- secupdater:skipping -->" }] }
444
+
445
+ it "still says so" do
446
+ publish
447
+
448
+ expect(comment_stub).to have_been_requested
449
+ end
450
+ end
451
+ end
452
+
453
+ context "a branch without an MR, with a commit from someone else" do
454
+ let(:existing_merge_requests) { [] }
455
+ let(:branch_commits) { [{ author_name: "Jo Bloggs", author_email: "jo@example.com" }] }
456
+
457
+ it "skips it without commenting anywhere" do
458
+ expect(publish.first).to eq(:skipped)
459
+
460
+ expect(a_request(:any, %r{/notes})).not_to have_been_made
209
461
  end
210
462
  end
211
463
 
@@ -279,6 +531,32 @@ RSpec.describe Secupdater::Publisher::Gitlab do
279
531
  end
280
532
  end
281
533
 
534
+ context "an MR for a dependency whose advisories this run's MR for another dependency also fixes" do
535
+ let(:advisories) do
536
+ [
537
+ Secupdater::SecurityAdvisory.new("css_parser", "cve/2024-0001", [">= 1.1.0"]),
538
+ Secupdater::SecurityAdvisory.new("addressable", "ghsa/abcd-efgh", [">= 2.9.0"])
539
+ ]
540
+ end
541
+ let(:open_secupdater_merge_requests) do
542
+ [
543
+ { iid: 4, source_branch: "security/addressable-2.9.0" },
544
+ { iid: 9, source_branch: "security/css_parser-1.1.0" }
545
+ ]
546
+ end
547
+
548
+ before do
549
+ stub_request(:post, "#{project_api}/merge_requests").to_return(status: 201, body: { iid: 9 }.to_json)
550
+ publish
551
+ end
552
+
553
+ it "closes it, saying which MR superseded it" do
554
+ expect(gitlab.finish(["addressable", "css_parser"])).to eq(["closed !4 (security/addressable-2.9.0): Superseded by !9."])
555
+
556
+ expect(a_request(:put, //)).to have_been_made.once
557
+ end
558
+ end
559
+
282
560
  context "an MR for a dependency that no longer has advisories" do
283
561
  let(:open_secupdater_merge_requests) { [{ iid: 4, source_branch: "security/net-ssh-7.3.8" }] }
284
562
 
@@ -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, :unchanged, :fail, :fail_unretryable or :abort, or a hash of those by level
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
- after = JSON.generate(JSON.parse(before).merge(name => outcome))
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
@@ -382,6 +384,127 @@ RSpec.describe Secupdater::Update do
382
384
  end
383
385
  end
384
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
+
385
508
  context "a mix of outcomes" do
386
509
  let(:lockfile) { { "rack" => "1.0", "nokogiri" => "1.0", "puma" => "1.0", "rails" => "1.0" } }
387
510
  let(:outcomes) { { "rack" => "2.1", "nokogiri" => "1.5", "puma" => :unchanged, "rails" => :fail } }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: secupdater
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Anderson
@@ -79,8 +79,10 @@ files:
79
79
  - README.md
80
80
  - Rakefile
81
81
  - bin/secupdater
82
+ - examples/central-project/.gitlab-ci.yml
82
83
  - lib/secupdater.rb
83
84
  - lib/secupdater/cli.rb
85
+ - lib/secupdater/discovery/gitlab.rb
84
86
  - lib/secupdater/ecosystem.rb
85
87
  - lib/secupdater/ecosystem/rubygems.rb
86
88
  - lib/secupdater/publisher.rb
@@ -91,6 +93,7 @@ files:
91
93
  - sig/secupdater.rbs
92
94
  - spec/e2e/secupdater_spec.rb
93
95
  - spec/secupdater/cli_spec.rb
96
+ - spec/secupdater/discovery/gitlab_spec.rb
94
97
  - spec/secupdater/ecosystem/rubygems_spec.rb
95
98
  - spec/secupdater/ecosystem_spec.rb
96
99
  - spec/secupdater/publisher/gitlab_spec.rb