secupdater 0.8.0 → 0.9.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.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +16 -0
  4. data/examples/central-project/.gitlab-ci.yml +87 -33
  5. data/lib/secupdater/cli.rb +1 -1
  6. data/lib/secupdater/ecosystem/rubygems.rb +7 -0
  7. data/lib/secupdater/ecosystem.rb +34 -4
  8. data/lib/secupdater/executor.rb +10 -8
  9. data/lib/secupdater/publisher/gitlab.rb +29 -11
  10. data/lib/secupdater/publisher.rb +7 -8
  11. data/lib/secupdater/update.rb +3 -5
  12. data/lib/secupdater/version.rb +1 -1
  13. data/lib/secupdater/yarn_support/audit.rb +86 -0
  14. data/lib/secupdater/yarn_support/lockfile.rb +79 -0
  15. data/lib/secupdater/yarn_support/package_name.rb +14 -0
  16. data/lib/secupdater/yarn_support/range.rb +152 -0
  17. data/lib/secupdater/yarn_support/version.rb +65 -0
  18. data/lib/secupdater.rb +4 -2
  19. data/spec/fixtures/yarn/audit.ndjson +9 -0
  20. data/spec/fixtures/yarn/protocols.yarn.lock +99 -0
  21. data/spec/fixtures/yarn/yarn.lock +265 -0
  22. data/spec/fixtures/yarn/yarn1.lock +8 -0
  23. data/spec/secupdater/cli_spec.rb +11 -0
  24. data/spec/secupdater/ecosystem/rubygems_spec.rb +40 -0
  25. data/spec/secupdater/ecosystem_spec.rb +71 -0
  26. data/spec/secupdater/executor_spec.rb +23 -5
  27. data/spec/secupdater/publisher/gitlab_spec.rb +85 -14
  28. data/spec/secupdater/update_spec.rb +22 -9
  29. data/spec/secupdater/yarn_support/audit_spec.rb +144 -0
  30. data/spec/secupdater/yarn_support/lockfile_spec.rb +171 -0
  31. data/spec/secupdater/yarn_support/package_name_spec.rb +40 -0
  32. data/spec/secupdater/yarn_support/range_spec.rb +158 -0
  33. data/spec/secupdater/yarn_support/version_spec.rb +61 -0
  34. data/spec/secupdater_spec.rb +9 -0
  35. metadata +15 -1
@@ -168,6 +168,46 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
168
168
  end
169
169
  end
170
170
 
171
+ describe "#branch_name(name, version)" do
172
+ it "is security/<name>-<version>, as before there were other ecosystems" do
173
+ expect(rubygems.branch_name("css_parser", Gem::Version.new("1.1.0"))).to eq("security/css_parser-1.1.0")
174
+ end
175
+ end
176
+
177
+ describe "#dependency_from_branch(branch)" do
178
+ it "gives the gem a branch is for, including ones opened before there were other ecosystems" do
179
+ {
180
+ "security/css_parser-1.0.5" => "css_parser",
181
+ "security/net-ssh-7.3.8" => "net-ssh",
182
+ "security/css_parser-extra-1.1.0" => "css_parser-extra",
183
+ "security/rack-2.0" => "rack",
184
+ "security/nokogiri-1.16.0.rc1" => "nokogiri"
185
+ }.each do |branch, name|
186
+ expect(rubygems.dependency_from_branch(branch)).to eq(name), branch
187
+ end
188
+ end
189
+
190
+ it "is nil for other ecosystems' branches, as gem names never contain /, and anything else" do
191
+ ["security/yarn/lodash-4.17.21", "security/yarn/@babel/core-7.23.2", "secupdater/ruby-3.4.2", "security/rack"].each do |branch|
192
+ expect(rubygems.dependency_from_branch(branch)).to be_nil, branch
193
+ end
194
+ end
195
+ end
196
+
197
+ describe "#fixes?(version, advisory)" do
198
+ def advisory(*patched) = Secupdater::SecurityAdvisory.new("css_parser", "cve/2024-0001", patched)
199
+
200
+ it "is true when the version satisfies any of the advisory's patched versions" do
201
+ expect(rubygems.fixes?(Gem::Version.new("1.1.5"), advisory("~> 1.1.4"))).to be(true)
202
+ expect(rubygems.fixes?("2.0.0", advisory("~> 1.1.4", ">= 2.0.0"))).to be(true)
203
+ end
204
+
205
+ it "is false when it satisfies none of them" do
206
+ expect(rubygems.fixes?("1.2.0", advisory("~> 1.1.4"))).to be(false)
207
+ expect(rubygems.fixes?("1.0.0", advisory("~> 7.3, >= 7.3.5", ">= 8.0.0"))).to be(false)
208
+ end
209
+ end
210
+
171
211
  describe "#try_update" do
172
212
  let(:status) { instance_double(Process::Status, success?: true, exitstatus: 0) }
173
213
  let(:original_env) { {} }
@@ -29,4 +29,75 @@ RSpec.describe Secupdater::Ecosystem do
29
29
  end
30
30
  end
31
31
  end
32
+
33
+ describe ".class_for(name)" do
34
+ it "finds the ecosystem's class without building one, whatever the case" do
35
+ expect(described_class.class_for("rubygems")).to be(Secupdater::Ecosystem::Rubygems)
36
+ expect(described_class.class_for("RubyGems")).to be(Secupdater::Ecosystem::Rubygems)
37
+ end
38
+
39
+ it "raises Secupdater::Error naming an unknown ecosystem" do
40
+ expect { described_class.class_for("npm") }.to raise_error(Secupdater::Error, "Unknown ecosystem: npm")
41
+ end
42
+ end
43
+
44
+ describe ".runtimes" do
45
+ it "is what RubyGems runs commands with, so --executor=docker only needs those containers" do
46
+ expect(Secupdater::Ecosystem::Rubygems.runtimes).to eq([:ruby])
47
+ end
48
+ end
49
+
50
+ # The base class, named like an ecosystem other than RubyGems, rather than a
51
+ # subclass, which .resolve would then find
52
+ describe "MR branches" do
53
+ subject(:ecosystem) { described_class.new.tap { allow(it).to receive(:ecosystem_name).and_return("Yarn") } }
54
+
55
+ describe "#branch_name(name, version)" do
56
+ it "puts the ecosystem in the branch, so each ecosystem's runs only touch their own MRs" do
57
+ expect(ecosystem.branch_name("lodash", "4.17.21")).to eq("security/yarn/lodash-4.17.21")
58
+ expect(ecosystem.branch_name("@babel/core", Gem::Version.new("7.23.2"))).to eq("security/yarn/@babel/core-7.23.2")
59
+ end
60
+
61
+ it "uses #branch_version for the version" do
62
+ allow(ecosystem).to receive(:branch_version).with(["4.17.20", "4.17.21"]).and_return("4.17.21")
63
+
64
+ expect(ecosystem.branch_name("lodash", ["4.17.20", "4.17.21"])).to eq("security/yarn/lodash-4.17.21")
65
+ end
66
+ end
67
+
68
+ describe "#branch_version(version)" do
69
+ it "is the version as a string" do
70
+ expect(ecosystem.branch_version(Gem::Version.new("1.0"))).to eq("1.0")
71
+ end
72
+ end
73
+
74
+ describe "#dependency_from_branch(branch)" do
75
+ it "gives the name of the dependency a branch of this ecosystem's is for" do
76
+ {
77
+ "security/yarn/lodash-4.17.21" => "lodash",
78
+ "security/yarn/@babel/core-7.23.2" => "@babel/core",
79
+ "security/yarn/is-number-7.0.0" => "is-number",
80
+ "security/yarn/utf-8-validate-5.0.10" => "utf-8-validate",
81
+ "security/yarn/core-js-3.0.0-beta.1" => "core-js",
82
+ "security/yarn/@scope/a-b-1.0.0-rc-2+build.5" => "@scope/a-b"
83
+ }.each do |branch, name|
84
+ expect(ecosystem.dependency_from_branch(branch)).to eq(name), branch
85
+ end
86
+ end
87
+
88
+ it "is nil for other ecosystems' branches, and anything else" do
89
+ [
90
+ "security/redis-5.0.0",
91
+ "security/css_parser-1.1.0",
92
+ "security/npm/lodash-4.17.21",
93
+ "security/yarn/a/b/c-1.0.0",
94
+ "security/yarn/lodash",
95
+ "security/yarn/lodash-latest",
96
+ "secupdater/ruby-3.4.2"
97
+ ].each do |branch|
98
+ expect(ecosystem.dependency_from_branch(branch)).to be_nil, branch
99
+ end
100
+ end
101
+ end
102
+ end
32
103
  end
@@ -4,24 +4,42 @@ require "tmpdir"
4
4
  RSpec.describe Secupdater::Executor do
5
5
  let(:status) { instance_double(Process::Status, success?: true, exitstatus: 0) }
6
6
 
7
- describe ".resolve(name, env)" do
7
+ describe ".resolve(name, env, runtimes:)" do
8
8
  it "returns a Local executor for 'local'" do
9
- expect(described_class.resolve("local", {})).to be_a(Secupdater::Executor::Local)
9
+ expect(described_class.resolve("local", {}, runtimes: [:ruby])).to be_a(Secupdater::Executor::Local)
10
10
  end
11
11
 
12
12
  it "returns a Docker executor for 'docker', with the Ruby container from SECUPDATER_RUBY_CONTAINER" do
13
- expect(described_class.resolve("docker", { "SECUPDATER_RUBY_CONTAINER" => "sidecar" }))
13
+ expect(described_class.resolve("docker", { "SECUPDATER_RUBY_CONTAINER" => "sidecar" }, runtimes: [:ruby]))
14
14
  .to be_a(Secupdater::Executor::Docker).and(have_attributes(containers: { ruby: "sidecar" }))
15
15
  end
16
16
 
17
+ it "returns a Docker executor with the Node container from SECUPDATER_NODE_CONTAINER, for ecosystems that run Node" do
18
+ expect(described_class.resolve("docker", { "SECUPDATER_NODE_CONTAINER" => "node-sidecar" }, runtimes: [:node]))
19
+ .to have_attributes(containers: { node: "node-sidecar" })
20
+ end
21
+
17
22
  it "raises Secupdater::Error naming the variable for 'docker' without a container, or an empty one from CI" do
18
23
  [{}, { "SECUPDATER_RUBY_CONTAINER" => "" }].each do |env|
19
- expect { described_class.resolve("docker", env) }.to raise_error(Secupdater::Error, /SECUPDATER_RUBY_CONTAINER/)
24
+ expect { described_class.resolve("docker", env, runtimes: [:ruby]) }.to raise_error(Secupdater::Error, /SECUPDATER_RUBY_CONTAINER/)
20
25
  end
21
26
  end
22
27
 
28
+ it "only needs the containers for the runtimes the ecosystem runs, naming the missing ones" do
29
+ expect { described_class.resolve("docker", { "SECUPDATER_NODE_CONTAINER" => "node-sidecar" }, runtimes: [:ruby]) }
30
+ .to raise_error(Secupdater::Error) do
31
+ expect(it.message).to include("SECUPDATER_RUBY_CONTAINER")
32
+ expect(it.message).not_to include("SECUPDATER_NODE_CONTAINER")
33
+ end
34
+ expect { described_class.resolve("docker", { "SECUPDATER_RUBY_CONTAINER" => "sidecar" }, runtimes: [:node]) }
35
+ .to raise_error(Secupdater::Error) do
36
+ expect(it.message).to include("SECUPDATER_NODE_CONTAINER")
37
+ expect(it.message).not_to include("SECUPDATER_RUBY_CONTAINER")
38
+ end
39
+ end
40
+
23
41
  it "raises Secupdater::Error for anything else" do
24
- expect { described_class.resolve("podman", {}) }.to raise_error(Secupdater::Error, "Unknown executor: podman")
42
+ expect { described_class.resolve("podman", {}, runtimes: [:ruby]) }.to raise_error(Secupdater::Error, "Unknown executor: podman")
25
43
  end
26
44
  end
27
45
 
@@ -6,13 +6,15 @@ RSpec.describe Secupdater::Publisher::Gitlab do
6
6
  subject(:gitlab) { described_class.new("access_token", "123", "main", "abc123") }
7
7
 
8
8
  let(:ecosystem) { Secupdater::Ecosystem::Rubygems.new }
9
+ # The base class, named like another ecosystem, rather than a subclass that Ecosystem.resolve would find
10
+ let(:yarn) { Secupdater::Ecosystem.new.tap { allow(it).to receive(:ecosystem_name).and_return("Yarn") } }
9
11
  let(:dep) { Secupdater::DependencySpec.new("css_parser", "ruby", Gem::Version.new("1.0.0")) }
10
12
  let(:advisories) { [Secupdater::SecurityAdvisory.new("css_parser", "cve/2024-0001", [">= 1.1.0"])] }
11
13
  let(:bump) { Secupdater::DependencySpecDiff.new(dep, "1.0.0", "1.1.0") }
12
14
  let(:changes) { [Secupdater::FileChange.new("Gemfile.lock", "update", "abc", "xyz")] }
13
15
 
14
16
  let(:footer) do
15
- "---\nOpened by secupdater, which keeps this MR up to date with `main` and sets it to merge once its pipeline passes. " \
17
+ "---\nOpened by secupdater, which keeps this MR up to date with `main` and, if it needs approval, sets it to merge once it's approved and its pipeline passes. " \
16
18
  "Closing it declines css_parser 1.1.0; reopen it or remove its `secupdater` label to have it updated again."
17
19
  end
18
20
  let(:expected_description) do
@@ -117,6 +119,13 @@ RSpec.describe Secupdater::Publisher::Gitlab do
117
119
  .to_return(status: 200, body: merge_request_details.to_json)
118
120
  end
119
121
 
122
+ let(:approvals_required) { 1 }
123
+ let!(:approvals_stub) do
124
+ stub_request(:get, %r{\A#{project_api}/merge_requests/\d+/approvals\z})
125
+ .with(headers: headers)
126
+ .to_return(status: 200, body: { approvals_required: }.to_json)
127
+ end
128
+
120
129
  let!(:auto_merge_stub) do
121
130
  stub_request(:put, %r{\A#{project_api}/merge_requests/\d+/merge\z})
122
131
  .with(body: { auto_merge: true, sha: "def456", should_remove_source_branch: true }, headers: headers)
@@ -336,6 +345,17 @@ RSpec.describe Secupdater::Publisher::Gitlab do
336
345
  expect(a_request(:put, "#{project_api}/merge_requests/7/merge")).to have_been_made
337
346
  end
338
347
 
348
+ context "on an MR that needs no approval" do
349
+ let(:approvals_required) { 0 }
350
+
351
+ it "leaves it for someone to merge by hand, as it would merge unreviewed" do
352
+ expect(publish).to eq([:up_to_date, nil])
353
+
354
+ expect(a_request(:get, "#{project_api}/merge_requests/7/approvals")).to have_been_made
355
+ expect(auto_merge_stub).not_to have_been_requested
356
+ end
357
+ end
358
+
339
359
  context "already set to auto-merge" do
340
360
  let(:merge_request_details) { super().merge(merge_when_pipeline_succeeds: true) }
341
361
 
@@ -373,8 +393,8 @@ RSpec.describe Secupdater::Publisher::Gitlab do
373
393
  .to_return(status: 401, body: '{"message":"401 Unauthorized"}')
374
394
  end
375
395
 
376
- it "fails the update" do
377
- expect { publish }.to raise_error(Secupdater::PublishFailed, /401/)
396
+ it "says so, without failing the update" do
397
+ expect(publish).to eq([:up_to_date, "security/css_parser-1.1.0: couldn't set !7 to auto-merge, GitLab returned 401: {\"message\":\"401 Unauthorized\"}"])
378
398
  end
379
399
  end
380
400
  end
@@ -564,7 +584,7 @@ RSpec.describe Secupdater::Publisher::Gitlab do
564
584
  end
565
585
  end
566
586
 
567
- describe "#merge_request_open_for?(bump)" do
587
+ describe "#merge_request_open_for?(ecosystem, bump)" do
568
588
  let(:open_secupdater_merge_requests) { [] }
569
589
 
570
590
  let!(:find_open_stub) do
@@ -577,7 +597,7 @@ RSpec.describe Secupdater::Publisher::Gitlab do
577
597
  let(:existing_merge_requests) { [{ iid: 3, state: "closed", labels: ["secupdater"] }, { iid: 7, state: "opened", labels: [] }] }
578
598
 
579
599
  it "is true" do
580
- expect(gitlab.merge_request_open_for?(bump)).to be(true)
600
+ expect(gitlab.merge_request_open_for?(ecosystem, bump)).to be(true)
581
601
  expect(find_mr_stub).to have_been_requested
582
602
  end
583
603
  end
@@ -586,7 +606,7 @@ RSpec.describe Secupdater::Publisher::Gitlab do
586
606
  let(:open_secupdater_merge_requests) { [{ iid: 4, source_branch: "security/css_parser-1.0.5" }] }
587
607
 
588
608
  it "is true" do
589
- expect(gitlab.merge_request_open_for?(bump)).to be(true)
609
+ expect(gitlab.merge_request_open_for?(ecosystem, bump)).to be(true)
590
610
  end
591
611
  end
592
612
 
@@ -596,7 +616,30 @@ RSpec.describe Secupdater::Publisher::Gitlab do
596
616
  end
597
617
 
598
618
  it "is false" do
599
- expect(gitlab.merge_request_open_for?(bump)).to be(false)
619
+ expect(gitlab.merge_request_open_for?(ecosystem, bump)).to be(false)
620
+ end
621
+ end
622
+
623
+ context "with open secupdater MRs only for another ecosystem's dependency of the same name" do
624
+ let(:open_secupdater_merge_requests) { [{ iid: 4, source_branch: "security/yarn/css_parser-1.0.5" }] }
625
+
626
+ it "is false" do
627
+ expect(gitlab.merge_request_open_for?(ecosystem, bump)).to be(false)
628
+ end
629
+ end
630
+
631
+ context "for another ecosystem, with open MRs only for a gem of the same name" do
632
+ let(:open_secupdater_merge_requests) { [{ iid: 4, source_branch: "security/css_parser-1.0.5" }] }
633
+
634
+ before do
635
+ stub_request(:get, "#{project_api}/merge_requests")
636
+ .with(query: { source_branch: "security/yarn/css_parser-1.1.0", target_branch: "main" }, headers: headers)
637
+ .to_return(status: 200, body: [].to_json)
638
+ end
639
+
640
+ it "is false, looking for the update's branch in that ecosystem" do
641
+ expect(gitlab.merge_request_open_for?(yarn, bump)).to be(false)
642
+ expect(find_mr_stub).not_to have_been_requested
600
643
  end
601
644
  end
602
645
 
@@ -604,7 +647,7 @@ RSpec.describe Secupdater::Publisher::Gitlab do
604
647
  let(:existing_merge_requests) { [{ iid: 3, state: "closed", labels: ["secupdater"] }, { iid: 4, state: "merged", labels: [] }] }
605
648
 
606
649
  it "is false" do
607
- expect(gitlab.merge_request_open_for?(bump)).to be(false)
650
+ expect(gitlab.merge_request_open_for?(ecosystem, bump)).to be(false)
608
651
  end
609
652
  end
610
653
  end
@@ -621,7 +664,7 @@ RSpec.describe Secupdater::Publisher::Gitlab do
621
664
  end
622
665
  end
623
666
 
624
- describe "#finish(vulnerable_names)" do
667
+ describe "#finish(ecosystem, vulnerable_names)" do
625
668
  let(:open_secupdater_merge_requests) { [] }
626
669
 
627
670
  let!(:find_open_stub) do
@@ -648,7 +691,7 @@ RSpec.describe Secupdater::Publisher::Gitlab do
648
691
  before { publish }
649
692
 
650
693
  it "closes it, saying which MR superseded it" do
651
- expect(gitlab.finish(["css_parser"])).to eq(["closed !5 (security/css_parser-1.0.5): Superseded by !9."])
694
+ expect(gitlab.finish(ecosystem, ["css_parser"])).to eq(["closed !5 (security/css_parser-1.0.5): Superseded by !9."])
652
695
 
653
696
  expect(a_request(:post, "#{project_api}/merge_requests/5/notes").with(body: { body: "Superseded by !9." })).to have_been_made
654
697
  expect(a_request(:put, "#{project_api}/merge_requests/5")).to have_been_made
@@ -676,7 +719,7 @@ RSpec.describe Secupdater::Publisher::Gitlab do
676
719
  end
677
720
 
678
721
  it "closes it, saying which MR superseded it" do
679
- expect(gitlab.finish(["addressable", "css_parser"])).to eq(["closed !4 (security/addressable-2.9.0): Superseded by !9."])
722
+ expect(gitlab.finish(ecosystem, ["addressable", "css_parser"])).to eq(["closed !4 (security/addressable-2.9.0): Superseded by !9."])
680
723
 
681
724
  expect(a_request(:put, //)).to have_been_made.once
682
725
  end
@@ -686,7 +729,7 @@ RSpec.describe Secupdater::Publisher::Gitlab do
686
729
  let(:open_secupdater_merge_requests) { [{ iid: 4, source_branch: "security/net-ssh-7.3.8" }] }
687
730
 
688
731
  it "closes it, saying it's no longer needed" do
689
- expect(gitlab.finish(["css_parser"])).to eq(
732
+ expect(gitlab.finish(ecosystem, ["css_parser"])).to eq(
690
733
  ["closed !4 (security/net-ssh-7.3.8): net-ssh no longer has any security advisories on main, so this is no longer needed."]
691
734
  )
692
735
 
@@ -699,20 +742,48 @@ RSpec.describe Secupdater::Publisher::Gitlab do
699
742
  let(:open_secupdater_merge_requests) { [{ iid: 4, source_branch: "security/net-ssh-7.3.8" }] }
700
743
 
701
744
  it "leaves it alone" do
702
- expect(gitlab.finish(["net-ssh"])).to eq([])
745
+ expect(gitlab.finish(ecosystem, ["net-ssh"])).to eq([])
746
+
747
+ expect(a_request(:put, //)).not_to have_been_made
748
+ expect(a_request(:post, //)).not_to have_been_made
749
+ end
750
+ end
751
+
752
+ context "MRs for another ecosystem's dependencies" do
753
+ let(:open_secupdater_merge_requests) do
754
+ [{ iid: 4, source_branch: "security/yarn/lodash-4.17.21" }, { iid: 5, source_branch: "security/yarn/@babel/core-7.23.2" }]
755
+ end
756
+
757
+ it "leaves them alone, though they have no advisories in this run" do
758
+ expect(gitlab.finish(ecosystem, ["css_parser"])).to eq([])
703
759
 
704
760
  expect(a_request(:put, //)).not_to have_been_made
705
761
  expect(a_request(:post, //)).not_to have_been_made
706
762
  end
707
763
  end
708
764
 
765
+ context "for another ecosystem" do
766
+ let(:open_secupdater_merge_requests) do
767
+ [{ iid: 4, source_branch: "security/net-ssh-7.3.8" }, { iid: 5, source_branch: "security/yarn/@babel/core-7.23.2" }]
768
+ end
769
+
770
+ it "closes only that ecosystem's MRs" do
771
+ expect(gitlab.finish(yarn, ["lodash"])).to eq(
772
+ ["closed !5 (security/yarn/@babel/core-7.23.2): @babel/core no longer has any security advisories on main, so this is no longer needed."]
773
+ )
774
+
775
+ expect(a_request(:put, "#{project_api}/merge_requests/5")).to have_been_made
776
+ expect(a_request(:put, //)).to have_been_made.once
777
+ end
778
+ end
779
+
709
780
  context "the MR this run opened or kept" do
710
781
  let(:open_secupdater_merge_requests) { [{ iid: 9, source_branch: "security/css_parser-1.1.0" }] }
711
782
 
712
783
  before { publish }
713
784
 
714
785
  it "leaves it alone" do
715
- expect(gitlab.finish(["css_parser"])).to eq([])
786
+ expect(gitlab.finish(ecosystem, ["css_parser"])).to eq([])
716
787
 
717
788
  expect(a_request(:put, //)).not_to have_been_made
718
789
  end
@@ -11,12 +11,13 @@ module UpdateSpec
11
11
  class FakeEcosystem
12
12
  LOCKFILE = "Gemfile.lock"
13
13
 
14
- attr_reader :outcomes, :lockfiles_seen, :attempts
14
+ attr_reader :outcomes, :lockfiles_seen, :attempts, :fix_checks
15
15
 
16
16
  def initialize(outcomes)
17
17
  @outcomes = outcomes
18
18
  @lockfiles_seen = {}
19
19
  @attempts = []
20
+ @fix_checks = []
20
21
  end
21
22
 
22
23
  def update_levels = [:patch, :minor, :major]
@@ -60,8 +61,9 @@ module UpdateSpec
60
61
  end
61
62
  end
62
63
 
63
- def satisfies_requirement?(version, requirement)
64
- Gem::Requirement.create(requirement).satisfied_by?(Gem::Version.create(version))
64
+ def fixes?(version, advisory)
65
+ fix_checks << [version, advisory]
66
+ advisory.patched_versions.any? { Gem::Requirement.create(it).satisfied_by?(Gem::Version.create(version)) }
65
67
  end
66
68
  end
67
69
  end
@@ -303,6 +305,17 @@ RSpec.describe Secupdater::Update do
303
305
  end
304
306
  end
305
307
 
308
+ context "checking whether an update fixes the advisories" do
309
+ let(:outcomes) { { "rack" => "2.1" } }
310
+
311
+ it "leaves it to the ecosystem, passing the version the update moved to through as it is" do
312
+ advisories = [advisory("rack", "cve/1"), advisory("rack", "cve/2")]
313
+ update.call(advisories)
314
+
315
+ expect(ecosystem.fix_checks).to eq(advisories.map { [Gem::Version.new("2.1"), it] })
316
+ end
317
+ end
318
+
306
319
  context "once every gem has been through the publisher" do
307
320
  let(:outcomes) { { "rack" => "2.1", "nokogiri" => :unchanged } }
308
321
 
@@ -311,7 +324,7 @@ RSpec.describe Secupdater::Update do
311
324
  it "lets the publisher tidy up, with the names of every gem that has advisories" do
312
325
  update.call([advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")])
313
326
 
314
- expect(publisher).to have_received(:finish).with(["rack", "nokogiri"])
327
+ expect(publisher).to have_received(:finish).with(ecosystem, ["rack", "nokogiri"])
315
328
  end
316
329
 
317
330
  it "logs what it tidied up" do
@@ -323,7 +336,7 @@ RSpec.describe Secupdater::Update do
323
336
  it "does so even when there are no advisories, so MRs whose fixes have landed get closed" do
324
337
  expect(update.call([])).to eq({})
325
338
 
326
- expect(publisher).to have_received(:finish).with([])
339
+ expect(publisher).to have_received(:finish).with(ecosystem, [])
327
340
  end
328
341
  end
329
342
 
@@ -458,7 +471,7 @@ RSpec.describe Secupdater::Update do
458
471
  it "tells the publisher to tidy up every gem, so their own MRs get closed" do
459
472
  update.call(advisories)
460
473
 
461
- expect(publisher).to have_received(:finish).with(["loofah", "nokogiri", "rails-html-sanitizer"])
474
+ expect(publisher).to have_received(:finish).with(ecosystem, ["loofah", "nokogiri", "rails-html-sanitizer"])
462
475
  end
463
476
 
464
477
  it "leaves the lockfile as it found it" do
@@ -547,7 +560,7 @@ RSpec.describe Secupdater::Update do
547
560
 
548
561
  before do
549
562
  allow(publisher).to receive(:open_merge_request_count).and_return(open_count)
550
- allow(publisher).to receive(:merge_request_open_for?) { |bump| open_merge_requests.include?(bump.dependency.name) }
563
+ allow(publisher).to receive(:merge_request_open_for?) { |_ecosystem, bump| open_merge_requests.include?(bump.dependency.name) }
551
564
  end
552
565
 
553
566
  def published_names = published.map { it[1].dependency.name }
@@ -612,7 +625,7 @@ RSpec.describe Secupdater::Update do
612
625
  end
613
626
 
614
627
  it "doesn't count updating an open MR towards the cap" do
615
- allow(publisher).to receive(:merge_request_open_for?) { |bump| bump.dependency.name == "puma" }
628
+ allow(publisher).to receive(:merge_request_open_for?) { |_ecosystem, bump| bump.dependency.name == "puma" }
616
629
 
617
630
  update.call(%w[rack nokogiri puma].map { advisory(it, "cve/#{it}") })
618
631
 
@@ -651,7 +664,7 @@ RSpec.describe Secupdater::Update do
651
664
  it "still lets the publisher tidy up every gem, including the deferred ones" do
652
665
  update.call(%w[rack nokogiri puma].map { advisory(it, "cve/#{it}") })
653
666
 
654
- expect(publisher).to have_received(:finish).with(%w[rack nokogiri puma])
667
+ expect(publisher).to have_received(:finish).with(ecosystem, %w[rack nokogiri puma])
655
668
  end
656
669
 
657
670
  context "when an update absorbs others" do
@@ -0,0 +1,144 @@
1
+ require "secupdater"
2
+ require "secupdater/yarn_support/audit"
3
+ require "secupdater/yarn_support/lockfile"
4
+ require "json"
5
+ require "stringio"
6
+
7
+ RSpec.describe Secupdater::YarnSupport::Audit do
8
+ def fixture(name) = File.read(File.expand_path("../../fixtures/yarn/#{name}", __dir__))
9
+
10
+ def line(value = "minimist", **children)
11
+ JSON.generate(
12
+ "value" => value,
13
+ "children" => {
14
+ "ID" => 1097677,
15
+ "Issue" => "Prototype Pollution in minimist",
16
+ "URL" => "https://github.com/advisories/GHSA-xvch-5gv4-984h",
17
+ "Severity" => "critical",
18
+ "Vulnerable Versions" => "<0.2.4",
19
+ "Tree Versions" => ["0.0.8"],
20
+ "Dependents" => ["a@workspace:packages/a"],
21
+ }.merge(children.transform_keys(&:to_s)),
22
+ )
23
+ end
24
+
25
+ def parse(*lines) = described_class.parse(StringIO.new(lines.join("\n")))
26
+
27
+ describe "real yarn npm audit output" do
28
+ subject(:findings) { described_class.parse(StringIO.new(fixture("audit.ndjson"))) }
29
+
30
+ it "has one finding per package and advisory" do
31
+ expect(findings.map { [it.name, it.report_id] }).to eq([
32
+ ["@babel/traverse", "GHSA-67hx-6x53-jw92"],
33
+ ["lodash", "GHSA-35jh-r3h4-6jhm"],
34
+ ["lodash", "GHSA-p6mc-m468-83gw"],
35
+ ["lodash", "GHSA-29mw-wpgm-hmr9"],
36
+ ["lodash", "GHSA-r5fr-rjxr-66jc"],
37
+ ["lodash", "GHSA-xxjr-mmjv-4gpg"],
38
+ ["minimist", "GHSA-vh95-rmgr-6w4m"],
39
+ ["minimist", "GHSA-xvch-5gv4-984h"],
40
+ ])
41
+ end
42
+
43
+ it "merges an advisory reported once per vulnerable version line" do
44
+ minimist = findings.find { it.report_id == "GHSA-xvch-5gv4-984h" }
45
+
46
+ expect(minimist).to eq(Secupdater::YarnSupport::Finding.new(
47
+ "minimist", "GHSA-xvch-5gv4-984h", "Prototype Pollution in minimist",
48
+ "https://github.com/advisories/GHSA-xvch-5gv4-984h", "critical", "<0.2.4 || >=1.0.0 <1.2.6",
49
+ ))
50
+ end
51
+
52
+ it "agrees with the lockfile about which locked versions are vulnerable" do
53
+ versions = Secupdater::YarnSupport::Lockfile.parse(fixture("yarn.lock")).versions
54
+ lines = fixture("audit.ndjson").lines.map { JSON.parse(it) }
55
+
56
+ lines.each do |entry|
57
+ range = Secupdater::YarnSupport::Range.new(entry["children"]["Vulnerable Versions"])
58
+
59
+ entry["children"]["Tree Versions"].each do |version|
60
+ expect(versions[entry["value"]]).to include(version)
61
+ expect(range).to include(version)
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ it "accepts a string and ignores blank lines" do
68
+ findings = described_class.parse("\n#{line}\n \n\n")
69
+
70
+ expect(findings.map(&:name)).to eq(["minimist"])
71
+ end
72
+
73
+ it "keeps the highest severity when merging" do
74
+ findings = parse(
75
+ line(Severity: "low", "Vulnerable Versions": "<0.1.0"),
76
+ line(Severity: "high", "Vulnerable Versions": ">=1.0.0 <1.2.6", ID: 2),
77
+ line(Severity: "moderate", "Vulnerable Versions": ">=2.0.0 <2.0.1", ID: 3),
78
+ )
79
+
80
+ expect(findings.map(&:severity)).to eq(["high"])
81
+ end
82
+
83
+ it "doesn't repeat an identical range when merging" do
84
+ findings = parse(line, line(ID: 2))
85
+
86
+ expect(findings.first.vulnerable_versions).to eq("<0.2.4")
87
+ end
88
+
89
+ it "keeps a blank range, meaning any version, when merging" do
90
+ findings = parse(line("Vulnerable Versions": ""), line(ID: 2, "Vulnerable Versions": "<1"))
91
+
92
+ expect(findings.first.vulnerable_versions).to eq("* || <1")
93
+ end
94
+
95
+ it "doesn't merge the same advisory across packages" do
96
+ findings = parse(line("minimist"), line("mkdirp"))
97
+
98
+ expect(findings.map(&:name)).to eq(["minimist", "mkdirp"])
99
+ end
100
+
101
+ it "ranks every npm severity" do
102
+ severities = %w[info low moderate high critical].shuffle(random: Random.new(2))
103
+ findings = parse(*severities.each_with_index.map { |s, i| line(Severity: s, ID: i) })
104
+
105
+ expect(findings.first.severity).to eq("critical")
106
+ end
107
+
108
+ it "raises on an unknown severity" do
109
+ expect { parse(line(Severity: "spicy")) }.to raise_error(Secupdater::Error, /"spicy"/)
110
+ end
111
+
112
+ [nil, "https://example.com/advisories/1097677", "https://github.com/advisories/GHSA-zzzz", "https://github.com/advisories/GHSA-xvch-5gv4-984h/../x"].each do |url|
113
+ it "falls back to the numeric ID when the URL is #{url.inspect}" do
114
+ expect(parse(line(URL: url)).first.report_id).to eq("1097677")
115
+ end
116
+ end
117
+
118
+ it "raises when there's no GHSA id and no numeric ID" do
119
+ expect { parse(line(URL: nil, ID: "1; rm -rf /")) }.to raise_error(Secupdater::Error)
120
+ end
121
+
122
+ it "raises on a line that isn't JSON" do
123
+ expect { parse(line, "not json") }.to raise_error(Secupdater::Error, /line 2/)
124
+ end
125
+
126
+ ['{"children": {}}', '{"value": "minimist"}', '["minimist"]', '{"value": "minimist", "children": []}', "null"].each do |bad|
127
+ it "raises on #{bad}" do
128
+ expect { parse(bad) }.to raise_error(Secupdater::Error, /line 1/)
129
+ end
130
+ end
131
+
132
+ it "raises a Secupdater::Error on invalid bytes" do
133
+ expect { parse("{\"value\":\"\u00e9\"}".dup.force_encoding("US-ASCII")) }.to raise_error(Secupdater::Error, /line 1/)
134
+ expect { parse("{\"value\":\"\xFF\",\"children\":{}}") }.to raise_error(Secupdater::Error, /line 1/)
135
+ end
136
+
137
+ it "raises naming a bad package name" do
138
+ expect { parse(line("--upgrade-all")) }.to raise_error(Secupdater::Error, /"--upgrade-all"/)
139
+ end
140
+
141
+ it "raises naming an unparseable vulnerable range" do
142
+ expect { parse(line("Vulnerable Versions": "sometimes")) }.to raise_error(Secupdater::Error, /"sometimes"/)
143
+ end
144
+ end