secupdater 0.1.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.
@@ -0,0 +1,262 @@
1
+ require "secupdater"
2
+ require "secupdater/publisher"
3
+ require "json"
4
+ require "stringio"
5
+ require "tmpdir"
6
+
7
+ module UpdateSpec
8
+ # outcomes: gem name => version try_update moves it to, :unchanged or :fail, or a hash of those by level
9
+ class FakeEcosystem
10
+ LOCKFILE = "Gemfile.lock"
11
+
12
+ attr_reader :outcomes, :lockfiles_seen, :attempts
13
+
14
+ def initialize(outcomes)
15
+ @outcomes = outcomes
16
+ @lockfiles_seen = {}
17
+ @attempts = []
18
+ end
19
+
20
+ def update_levels = [:patch, :minor, :major]
21
+
22
+ def ecosystem_name = "Fake"
23
+
24
+ def snapshot
25
+ JSON.parse(File.read(LOCKFILE)).to_h do |name, version|
26
+ [name, Secupdater::DependencySpec.new(name, "ruby", Gem::Version.new(version))]
27
+ end
28
+ end
29
+
30
+ def diff_snapshot(a, b) = Secupdater::Ecosystem::Rubygems.new.diff_snapshot(a, b)
31
+
32
+ def try_update(name, level:)
33
+ before = File.read(LOCKFILE)
34
+ lockfiles_seen[[name, level]] = JSON.parse(before)
35
+ attempts << [name, level]
36
+
37
+ outcome = outcomes.fetch(name)
38
+ outcome = outcome.fetch(level) if outcome.is_a?(Hash)
39
+
40
+ case outcome
41
+ when :fail
42
+ raise Secupdater::UpdateCommandFailed.new("bundle update failed", 1, "could not resolve #{name}")
43
+ when :unchanged
44
+ []
45
+ else
46
+ after = JSON.generate(JSON.parse(before).merge(name => outcome))
47
+ File.write(LOCKFILE, after)
48
+ [Secupdater::FileChange.new(LOCKFILE, "update", after, before)]
49
+ end
50
+ end
51
+
52
+ def satisfies_requirement?(version, requirement)
53
+ Gem::Requirement.create(requirement).satisfied_by?(Gem::Version.create(version))
54
+ end
55
+ end
56
+ end
57
+
58
+ RSpec.describe Secupdater::Update do
59
+ subject(:update) { described_class.new(logger, ecosystem, publisher: publisher) }
60
+
61
+ let(:log) { StringIO.new }
62
+ let(:logger) { Dry.Logger(:test, stream: log, template: "%<severity>s %<message>s", level: :debug) }
63
+ let(:ecosystem) { UpdateSpec::FakeEcosystem.new(outcomes) }
64
+ let(:publisher) { instance_double(Secupdater::Publisher) }
65
+ let(:published) { [] }
66
+
67
+ let(:lockfile) { { "rack" => "1.0", "nokogiri" => "1.0" } }
68
+
69
+ def advisory(gem, id, patched = ">= 2.0") = Secupdater::SecurityAdvisory.new(gem, id, [patched])
70
+
71
+ def lockfile_on_disk = JSON.parse(File.read("Gemfile.lock"))
72
+
73
+ around do |example|
74
+ Dir.mktmpdir do |dir|
75
+ Dir.chdir(dir) do
76
+ File.write("Gemfile.lock", JSON.generate(lockfile))
77
+ example.run
78
+ end
79
+ end
80
+ end
81
+
82
+ before do
83
+ allow(publisher).to receive(:call) { |*args| published << args } if publisher
84
+ end
85
+
86
+ describe "#call(advisories)" do
87
+ context "one gem with 2 advisories updates to a patched version" do
88
+ let(:outcomes) { { "rack" => "2.1" } }
89
+ let(:advisories) { [advisory("rack", "cve/1"), advisory("rack", "cve/2")] }
90
+
91
+ it "calls the publisher ONCE for the gem, with both advisories and the changes" do
92
+ update.call(advisories)
93
+
94
+ expect(published.length).to eq(1)
95
+
96
+ eco, bump, advs, changes = published.first
97
+ expect(eco).to be(ecosystem)
98
+ expect(bump).to have_attributes(from: Gem::Version.new("1.0"), to: Gem::Version.new("2.1"))
99
+ expect(bump.dependency.name).to eq("rack")
100
+ expect(advs.map(&:report_id)).to contain_exactly("cve/1", "cve/2")
101
+ expect(changes.map(&:filename)).to eq(["Gemfile.lock"])
102
+ expect(JSON.parse(changes.first.content)).to include("rack" => "2.1")
103
+ end
104
+
105
+ it "logs which upgrade resolves each advisory" do
106
+ update.call(advisories)
107
+
108
+ expect(log.string).to match(%r{^INFO .*rack.*1\.0.*2\.1.*cve/1})
109
+ expect(log.string).to match(%r{^INFO .*rack.*1\.0.*2\.1.*cve/2})
110
+ end
111
+ end
112
+
113
+ context "update lands below the patched version" do
114
+ let(:outcomes) { { "rack" => "1.5" } }
115
+
116
+ it "warns with the from/to versions and doesn't publish" do
117
+ update.call([advisory("rack", "cve/1")])
118
+
119
+ expect(published).to be_empty
120
+ expect(log.string).to match(/^WARN .*rack.*1\.0.*1\.5/)
121
+ end
122
+ end
123
+
124
+ context "nothing changes for the first gem" do
125
+ let(:outcomes) { { "rack" => :unchanged, "nokogiri" => "2.0" } }
126
+
127
+ it "warns that it's stuck at the current version and continues to the next gem" do
128
+ update.call([advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")])
129
+
130
+ expect(log.string).to match(/^WARN .*rack.*1\.0/)
131
+ expect(published.map { it[1].dependency.name }).to eq(["nokogiri"])
132
+ end
133
+ end
134
+
135
+ context "bundle update fails for the first gem" do
136
+ let(:outcomes) { { "rack" => :fail, "nokogiri" => "2.0" } }
137
+
138
+ it "records the failure and continues to the next gem" do
139
+ update.call([advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")])
140
+
141
+ expect(log.string).to match(/^ERROR .*rack.*could not resolve rack/m)
142
+ expect(published.map { it[1].dependency.name }).to eq(["nokogiri"])
143
+ end
144
+ end
145
+
146
+ context "two vulnerable gems" do
147
+ let(:outcomes) { { "rack" => "2.1", "nokogiri" => "2.0" } }
148
+ let(:advisories) { [advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")] }
149
+
150
+ it "restores the lockfile between gems so each MR only has its own changes" do
151
+ update.call(advisories)
152
+
153
+ expect(ecosystem.lockfiles_seen[["nokogiri", :patch]]).to eq(lockfile)
154
+
155
+ nokogiri_changes = published.find { it[1].dependency.name == "nokogiri" }[3]
156
+ expect(JSON.parse(nokogiri_changes.first.content)).to eq("rack" => "1.0", "nokogiri" => "2.0")
157
+ end
158
+
159
+ it "leaves the lockfile as it found it" do
160
+ update.call(advisories)
161
+
162
+ expect(lockfile_on_disk).to eq(lockfile)
163
+ end
164
+ end
165
+
166
+ context "an advisory for a gem that isn't in the lockfile" do
167
+ let(:outcomes) { { "rack" => "2.1" } }
168
+
169
+ it "raises Secupdater::Error naming the gem, before attempting any updates" do
170
+ expect { update.call([advisory("rack", "cve/1"), advisory("left-pad", "cve/2")]) }
171
+ .to raise_error(Secupdater::Error, /left-pad/)
172
+
173
+ expect(ecosystem.lockfiles_seen).to be_empty
174
+ end
175
+ end
176
+
177
+ context "no publisher configured (dry run)" do
178
+ let(:publisher) { nil }
179
+ let(:outcomes) { { "rack" => "2.1" } }
180
+
181
+ it "updates and logs without publishing" do
182
+ update.call([advisory("rack", "cve/1")])
183
+
184
+ expect(log.string).to match(%r{^INFO .*rack.*2\.1.*cve/1})
185
+ expect(lockfile_on_disk).to eq(lockfile)
186
+ end
187
+
188
+ it "returns :resolved for the gem" do
189
+ expect(update.call([advisory("rack", "cve/1")])).to eq("rack" => :resolved)
190
+ end
191
+ end
192
+
193
+ context "the patch update resolves the advisory" do
194
+ let(:outcomes) { { "rack" => { patch: "2.0.1" } } }
195
+
196
+ it "doesn't try the minor or major updates" do
197
+ update.call([advisory("rack", "cve/1")])
198
+
199
+ expect(ecosystem.attempts).to eq([["rack", :patch]])
200
+ expect(published.length).to eq(1)
201
+ end
202
+ end
203
+
204
+ context "the patch update is unresolved but the minor update resolves it" do
205
+ let(:outcomes) { { "rack" => { patch: "1.0.1", minor: "2.1" } } }
206
+
207
+ it "restores the lockfile before trying the minor update" do
208
+ update.call([advisory("rack", "cve/1")])
209
+
210
+ expect(ecosystem.lockfiles_seen[["rack", :minor]]).to eq(lockfile)
211
+ end
212
+
213
+ it "publishes the minor update's changes" do
214
+ expect(update.call([advisory("rack", "cve/1")])).to eq("rack" => :published)
215
+
216
+ _, bump, _, changes = published.first
217
+ expect(bump.to).to eq(Gem::Version.new("2.1"))
218
+ expect(JSON.parse(changes.first.content)).to include("rack" => "2.1")
219
+ end
220
+
221
+ it "logs which level resolved it" do
222
+ update.call([advisory("rack", "cve/1")])
223
+
224
+ expect(log.string).to match(%r{^INFO .*rack.*2\.1 \(minor\).*cve/1})
225
+ end
226
+ end
227
+
228
+ context "the patch and minor updates fail but the major update resolves it" do
229
+ let(:outcomes) { { "rack" => { patch: :fail, minor: :fail, major: "2.1" } } }
230
+
231
+ it "returns :published without logging an error" do
232
+ expect(update.call([advisory("rack", "cve/1")])).to eq("rack" => :published)
233
+ expect(log.string).not_to match(/^ERROR/)
234
+ end
235
+ end
236
+
237
+ context "every level fails" do
238
+ let(:outcomes) { { "rack" => :fail } }
239
+
240
+ it "tries each level, then returns :failed" do
241
+ expect(update.call([advisory("rack", "cve/1")])).to eq("rack" => :failed)
242
+ expect(ecosystem.attempts.map(&:last)).to eq([:patch, :minor, :major])
243
+ end
244
+ end
245
+
246
+ context "a mix of outcomes" do
247
+ let(:lockfile) { { "rack" => "1.0", "nokogiri" => "1.0", "puma" => "1.0", "rails" => "1.0" } }
248
+ let(:outcomes) { { "rack" => "2.1", "nokogiri" => "1.5", "puma" => :unchanged, "rails" => :fail } }
249
+
250
+ it "returns a result per gem" do
251
+ results = update.call(%w[rack nokogiri puma rails].map { advisory(it, "cve/#{it}") })
252
+
253
+ expect(results).to eq(
254
+ "rack" => :published,
255
+ "nokogiri" => :unresolved,
256
+ "puma" => :unchanged,
257
+ "rails" => :failed
258
+ )
259
+ end
260
+ end
261
+ end
262
+ end
@@ -0,0 +1,98 @@
1
+ require "webmock/rspec"
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
6
+ # this file to always be loaded, without a need to explicitly require it in any
7
+ # files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, consider making
13
+ # a separate helper file that requires the additional dependencies and performs
14
+ # the additional setup, and require it from the spec files that actually need
15
+ # it.
16
+ #
17
+ # See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18
+ RSpec.configure do |config|
19
+ # rspec-expectations config goes here. You can use an alternate
20
+ # assertion/expectation library such as wrong or the stdlib/minitest
21
+ # assertions if you prefer.
22
+ config.expect_with :rspec do |expectations|
23
+ # This option will default to `true` in RSpec 4. It makes the `description`
24
+ # and `failure_message` of custom matchers include text for helper methods
25
+ # defined using `chain`, e.g.:
26
+ # be_bigger_than(2).and_smaller_than(4).description
27
+ # # => "be bigger than 2 and smaller than 4"
28
+ # ...rather than:
29
+ # # => "be bigger than 2"
30
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
31
+ end
32
+
33
+ # rspec-mocks config goes here. You can use an alternate test double
34
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
35
+ config.mock_with :rspec do |mocks|
36
+ # Prevents you from mocking or stubbing a method that does not exist on
37
+ # a real object. This is generally recommended, and will default to
38
+ # `true` in RSpec 4.
39
+ mocks.verify_partial_doubles = true
40
+ end
41
+
42
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
43
+ # have no way to turn it off -- the option exists only for backwards
44
+ # compatibility in RSpec 3). It causes shared context metadata to be
45
+ # inherited by the metadata hash of host groups and examples, rather than
46
+ # triggering implicit auto-inclusion in groups with matching metadata.
47
+ config.shared_context_metadata_behavior = :apply_to_host_groups
48
+
49
+ # The settings below are suggested to provide a good initial experience
50
+ # with RSpec, but feel free to customize to your heart's content.
51
+
52
+ # This allows you to limit a spec run to individual examples or groups
53
+ # you care about by tagging them with `:focus` metadata. When nothing
54
+ # is tagged with `:focus`, all examples get run. RSpec also provides
55
+ # aliases for `it`, `describe`, and `context` that include `:focus`
56
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
57
+ config.filter_run_when_matching :focus
58
+
59
+ # Allows RSpec to persist some state between runs in order to support
60
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
61
+ # you configure your source control system to ignore this file.
62
+ config.example_status_persistence_file_path = "spec/examples.txt"
63
+
64
+ # Limits the available syntax to the non-monkey patched syntax that is
65
+ # recommended. For more details, see:
66
+ # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
67
+ config.disable_monkey_patching!
68
+
69
+ # This setting enables warnings. It's recommended, but in some cases may
70
+ # be too noisy due to issues in dependencies.
71
+ config.warnings = true
72
+
73
+ # Many RSpec users commonly either run the entire suite or an individual
74
+ # file, and it's useful to allow more verbose output when running an
75
+ # individual spec file.
76
+ if config.files_to_run.one?
77
+ # Use the documentation formatter for detailed output,
78
+ # unless a formatter has already been configured
79
+ # (e.g. via a command-line flag).
80
+ config.default_formatter = "doc"
81
+ end
82
+
83
+ # Print the 10 slowest examples and example groups at the
84
+ # end of the spec run, to help surface which specs are running
85
+ # particularly slow.
86
+ config.profile_examples = 10
87
+ # Run specs in random order to surface order dependencies. If you find an
88
+ # order dependency and want to debug it, you can fix the order by providing
89
+ # the seed, which is printed after each run.
90
+ # --seed 1234
91
+ config.order = :random
92
+
93
+ # Seed global randomization in this process using the `--seed` CLI option.
94
+ # Setting this allows you to use `--seed` to deterministically reproduce
95
+ # test failures related to randomization by passing the same `--seed` value
96
+ # as the one that triggered the failure.
97
+ Kernel.srand config.seed
98
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: secupdater
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Anderson
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: dry-logger
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 1.2.2
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: 1.2.2
26
+ - !ruby/object:Gem::Dependency
27
+ name: dry-cli
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: 1.4.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 1.4.1
40
+ - !ruby/object:Gem::Dependency
41
+ name: rspec
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 3.13.2
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 3.13.2
54
+ - !ruby/object:Gem::Dependency
55
+ name: webmock
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 3.34.0
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 3.34.0
68
+ email:
69
+ - me@benanderson.nz
70
+ executables:
71
+ - secupdater
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".rspec"
76
+ - ".tool-versions"
77
+ - CHANGELOG.md
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - bin/secupdater
82
+ - lib/secupdater.rb
83
+ - lib/secupdater/cli.rb
84
+ - lib/secupdater/ecosystem.rb
85
+ - lib/secupdater/ecosystem/rubygems.rb
86
+ - lib/secupdater/publisher.rb
87
+ - lib/secupdater/publisher/gitlab.rb
88
+ - lib/secupdater/runner.rb
89
+ - lib/secupdater/update.rb
90
+ - lib/secupdater/version.rb
91
+ - sig/secupdater.rbs
92
+ - spec/e2e/secupdater_spec.rb
93
+ - spec/secupdater/cli_spec.rb
94
+ - spec/secupdater/ecosystem/rubygems_spec.rb
95
+ - spec/secupdater/ecosystem_spec.rb
96
+ - spec/secupdater/publisher/gitlab_spec.rb
97
+ - spec/secupdater/publisher_spec.rb
98
+ - spec/secupdater/runner_spec.rb
99
+ - spec/secupdater/update_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/bagedevimo/secupdater
102
+ licenses:
103
+ - MIT
104
+ metadata:
105
+ source_code_uri: https://github.com/bagedevimo/secupdater
106
+ changelog_uri: https://github.com/bagedevimo/secupdater/blob/main/CHANGELOG.md
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: 3.2.0
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubygems_version: 4.0.19
122
+ specification_version: 4
123
+ summary: A CLI tool to replace dependabot, powered by ecosystem audit reports
124
+ test_files: []