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.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/.tool-versions +1 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +4 -0
- data/bin/secupdater +5 -0
- data/lib/secupdater/cli.rb +44 -0
- data/lib/secupdater/ecosystem/rubygems.rb +70 -0
- data/lib/secupdater/ecosystem.rb +43 -0
- data/lib/secupdater/publisher/gitlab.rb +67 -0
- data/lib/secupdater/publisher.rb +39 -0
- data/lib/secupdater/runner.rb +16 -0
- data/lib/secupdater/update.rb +85 -0
- data/lib/secupdater/version.rb +5 -0
- data/lib/secupdater.rb +43 -0
- data/sig/secupdater.rbs +4 -0
- data/spec/e2e/secupdater_spec.rb +112 -0
- data/spec/secupdater/cli_spec.rb +109 -0
- data/spec/secupdater/ecosystem/rubygems_spec.rb +184 -0
- data/spec/secupdater/ecosystem_spec.rb +18 -0
- data/spec/secupdater/publisher/gitlab_spec.rb +106 -0
- data/spec/secupdater/publisher_spec.rb +10 -0
- data/spec/secupdater/runner_spec.rb +56 -0
- data/spec/secupdater/update_spec.rb +262 -0
- data/spec/spec_helper.rb +98 -0
- metadata +124 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 5bc574d1914c88ba5bb0c0624e0be6be31af1797d27beafa08edd8ea558cf7e4
|
|
4
|
+
data.tar.gz: 19900eeb514d8dbd6572a8d7f80ee678a7564361302e253f99d219f5603ffc2b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b156d7d68800aee0a99556b14ecfd8932aedb4e62b8afc4d6c40f49473a3907d809e4c7100ed565b07e825e30ffabb840c960862cd6b71dbb239326aa95a7788
|
|
7
|
+
data.tar.gz: 21c55f8f8f74c4e8589529bcf93a70832e1b4ec09e456b494c44ec66a8a7e002359c3a8a9fcc310d3c0669e568f166db6b969d2eec0ef83d83562a176b64caad
|
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--require spec_helper
|
data/.tool-versions
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ruby 4.0.6
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ben Anderson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Secupdater
|
|
2
|
+
|
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
|
4
|
+
|
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/secupdater`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
+
|
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/secupdater.
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/secupdater
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require "dry/cli"
|
|
2
|
+
|
|
3
|
+
module Secupdater
|
|
4
|
+
module CLI
|
|
5
|
+
def self.run
|
|
6
|
+
Dry::CLI.new(Commands).call
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
module Commands
|
|
10
|
+
extend Dry::CLI::Registry
|
|
11
|
+
|
|
12
|
+
class Run < Dry::CLI::Command
|
|
13
|
+
option :ecosystem, values: ["rubygems"], required: true, desc: "Ecosystem to run checks for"
|
|
14
|
+
|
|
15
|
+
option :verbose, type: :boolean, default: false
|
|
16
|
+
option :debug, type: :boolean, default: false
|
|
17
|
+
|
|
18
|
+
argument :advisories, required: true, desc: "Advisory file generated from (for example) bundle audit --output json"
|
|
19
|
+
|
|
20
|
+
def call(ecosystem:, advisories:, verbose:, debug:)
|
|
21
|
+
log_level = if debug then :debug elsif verbose then :info else :warn end
|
|
22
|
+
results = Runner.new(ecosystem: ecosystem, log_level: log_level, publisher: publisher).call(advisories)
|
|
23
|
+
|
|
24
|
+
exit(1) if results.value?(:failed)
|
|
25
|
+
rescue Secupdater::Error => e
|
|
26
|
+
err.puts e.message
|
|
27
|
+
exit(1)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def publisher
|
|
33
|
+
return unless ENV["SECUPDATER_GITLAB_TOKEN"]
|
|
34
|
+
|
|
35
|
+
Publisher::Gitlab.new(ENV["SECUPDATER_GITLAB_TOKEN"], ENV.fetch("CI_PROJECT_ID"), ENV.fetch("CI_DEFAULT_BRANCH"))
|
|
36
|
+
rescue KeyError => e
|
|
37
|
+
raise Secupdater::Error, "SECUPDATER_GITLAB_TOKEN is set but #{e.key} isn't"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
register "run", Run
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
require "open3"
|
|
2
|
+
require "json"
|
|
3
|
+
require "bundler"
|
|
4
|
+
|
|
5
|
+
module Secupdater
|
|
6
|
+
class Ecosystem
|
|
7
|
+
class Rubygems < Secupdater::Ecosystem
|
|
8
|
+
def self.ecosystem_name = "RubyGems"
|
|
9
|
+
|
|
10
|
+
def manifest_paths = ["Gemfile.lock"]
|
|
11
|
+
|
|
12
|
+
def snapshot
|
|
13
|
+
Bundler::LockfileParser.new(Bundler.read_file("Gemfile.lock"))
|
|
14
|
+
.specs
|
|
15
|
+
.to_h { [it.name, DependencySpec.new(it.name, it.platform.to_s, it.version)] }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def diff_snapshot(a, b)
|
|
19
|
+
(a.keys | b.keys).filter_map do |gem|
|
|
20
|
+
next if a[gem] == b[gem]
|
|
21
|
+
|
|
22
|
+
spec = a[gem] || b[gem]
|
|
23
|
+
|
|
24
|
+
DependencySpecDiff.new(
|
|
25
|
+
spec,
|
|
26
|
+
a[gem]&.version,
|
|
27
|
+
b[gem]&.version,
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def run_update(names, level)
|
|
33
|
+
output, status = Open3.capture2e("bundle", "update", *Array(names), "--conservative", "--#{level}", "--strict")
|
|
34
|
+
|
|
35
|
+
unless status.success?
|
|
36
|
+
raise Secupdater::UpdateCommandFailed.new(
|
|
37
|
+
"bundle update command returned #{status.exitstatus}",
|
|
38
|
+
status.exitstatus,
|
|
39
|
+
output
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def parse_advisories(io)
|
|
45
|
+
raw_report = JSON.parse(io.read)
|
|
46
|
+
|
|
47
|
+
raw_report["results"].filter_map do |result|
|
|
48
|
+
next unless result["type"] == "unpatched_gem"
|
|
49
|
+
|
|
50
|
+
advisory = result["advisory"]
|
|
51
|
+
|
|
52
|
+
advisory_id = ["cve", "osvdb", "ghsa"].filter_map do
|
|
53
|
+
if advisory[it]
|
|
54
|
+
"#{it}/#{advisory[it]}"
|
|
55
|
+
end
|
|
56
|
+
end.join("-")
|
|
57
|
+
advisory_id = advisory["id"] if advisory_id.empty?
|
|
58
|
+
|
|
59
|
+
SecurityAdvisory.new(result["gem"]["name"], advisory_id, advisory["patched_versions"])
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def satisfies_requirement?(version, requirement)
|
|
64
|
+
req = Gem::Requirement.create(requirement.split(/,\s*/))
|
|
65
|
+
ver = Gem::Version.create(version)
|
|
66
|
+
req.satisfied_by?(ver)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module Secupdater
|
|
2
|
+
class Ecosystem
|
|
3
|
+
def self.ecosystem_name = raise NotImplementedError
|
|
4
|
+
def ecosystem_name = self.class.ecosystem_name
|
|
5
|
+
|
|
6
|
+
def manifest_paths = raise NotImplementedError
|
|
7
|
+
def run_update(names, level) = raise NotImplementedError
|
|
8
|
+
def snapshot = raise NotImplementedError
|
|
9
|
+
def diff_snapshot = raise NotImplementedError
|
|
10
|
+
def parse_advisories(io) = raise NotImplementedError
|
|
11
|
+
def satisfies_requirement?(version, requirement) = raise NotImplementedError
|
|
12
|
+
|
|
13
|
+
def update_levels = [:patch, :minor, :major]
|
|
14
|
+
|
|
15
|
+
def self.resolve(name)
|
|
16
|
+
klass = self.subclasses.detect { it.ecosystem_name.downcase == name.downcase }
|
|
17
|
+
raise Secupdater::Error, "Unknown ecosystem: #{name}" unless klass
|
|
18
|
+
klass.new
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def try_update(names, level:)
|
|
22
|
+
before = read_manifests
|
|
23
|
+
run_update(names, level)
|
|
24
|
+
after = read_manifests
|
|
25
|
+
|
|
26
|
+
manifest_paths.filter_map do |path|
|
|
27
|
+
next if before[path] == after[path]
|
|
28
|
+
|
|
29
|
+
type = if before[path].nil? then "create" elsif after[path].nil? then "delete" else "update" end
|
|
30
|
+
FileChange.new(path, type, after[path], before[path])
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def read_manifests
|
|
37
|
+
manifest_paths.to_h { [it, File.exist?(it) ? File.read(it) : nil] }
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
require_relative "ecosystem/rubygems"
|
|
43
|
+
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require "net/http"
|
|
2
|
+
require "json"
|
|
3
|
+
|
|
4
|
+
module Secupdater
|
|
5
|
+
class Publisher
|
|
6
|
+
class Gitlab < Secupdater::Publisher
|
|
7
|
+
attr_reader :access_token, :project_id, :target_branch
|
|
8
|
+
|
|
9
|
+
def initialize(access_token, project_id, target_branch)
|
|
10
|
+
@access_token = access_token
|
|
11
|
+
@project_id = project_id
|
|
12
|
+
@target_branch = target_branch
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def run
|
|
16
|
+
Net::HTTP.start("gitlab.com", 443, use_ssl: true) do |http|
|
|
17
|
+
return if merge_request_exists?(http)
|
|
18
|
+
|
|
19
|
+
data = {
|
|
20
|
+
branch: reference,
|
|
21
|
+
start_branch: target_branch,
|
|
22
|
+
commit_message: title,
|
|
23
|
+
actions: changes.map { { action: it.type, file_path: it.filename, content: it.content }.compact }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
request(http, Net::HTTP::Post, "repository/commits", data)
|
|
27
|
+
|
|
28
|
+
data = {
|
|
29
|
+
source_branch: reference,
|
|
30
|
+
target_branch: target_branch,
|
|
31
|
+
title: title,
|
|
32
|
+
description: description,
|
|
33
|
+
remove_source_branch: true,
|
|
34
|
+
labels: []
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
request(http, Net::HTTP::Post, "merge_requests", data)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def merge_request_exists?(http)
|
|
44
|
+
query = URI.encode_www_form(source_branch: reference, state: "opened")
|
|
45
|
+
res = request(http, Net::HTTP::Get, "merge_requests?#{query}")
|
|
46
|
+
JSON.parse(res.body).any?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def request(http, type, path, data = nil)
|
|
50
|
+
req = type.new(
|
|
51
|
+
"/api/v4/projects/#{URI.encode_www_form_component(project_id)}/#{path}",
|
|
52
|
+
"PRIVATE-TOKEN" => access_token,
|
|
53
|
+
"Content-Type" => "application/json"
|
|
54
|
+
)
|
|
55
|
+
req.body = data.to_json if data
|
|
56
|
+
|
|
57
|
+
res = http.request(req)
|
|
58
|
+
|
|
59
|
+
unless res.is_a?(Net::HTTPSuccess)
|
|
60
|
+
raise Secupdater::Error, "GitLab #{req.method} #{req.path} returned #{res.code}: #{res.body}"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
res
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module Secupdater
|
|
2
|
+
class Publisher
|
|
3
|
+
attr_reader :ecosystem, :bump, :advisories, :changes
|
|
4
|
+
|
|
5
|
+
def initialize = raise NotImplementedError
|
|
6
|
+
def run = raise NotImplementedError
|
|
7
|
+
|
|
8
|
+
def call(ecosystem, dependency_bump, advisories, changes)
|
|
9
|
+
@ecosystem = ecosystem
|
|
10
|
+
@bump = dependency_bump
|
|
11
|
+
@advisories = advisories
|
|
12
|
+
@changes = changes
|
|
13
|
+
|
|
14
|
+
run
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def reference = "security/#{bump.dependency.name}-#{bump.to}"
|
|
18
|
+
|
|
19
|
+
def title = "Upgrade #{ecosystem.ecosystem_name} dependency #{bump.dependency.name} to version #{bump.to}"
|
|
20
|
+
|
|
21
|
+
def description
|
|
22
|
+
heading = "Upgrade #{ecosystem.ecosystem_name} dependency #{bump.dependency.name} to version #{bump.to} to address the following security advisories:"
|
|
23
|
+
items = advisories.map { "* #{it.report_id}" }.join("\n")
|
|
24
|
+
"#{heading}\n\n#{items}\n"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
def pluralize(count, singular, plural = nil)
|
|
29
|
+
if count == 1
|
|
30
|
+
"#{count} #{singular}"
|
|
31
|
+
elsif plural
|
|
32
|
+
"#{count} #{plural}"
|
|
33
|
+
else
|
|
34
|
+
"#{count} #{singular}s"
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module Secupdater
|
|
2
|
+
class Runner
|
|
3
|
+
def initialize(ecosystem:, log_level: :warn, publisher: nil)
|
|
4
|
+
@ecosystem = Secupdater::Ecosystem.resolve(ecosystem)
|
|
5
|
+
@logger = Secupdater.build_logger(log_level: log_level)
|
|
6
|
+
@publisher = publisher
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def call(advisories_path)
|
|
10
|
+
raise Secupdater::Error, "Cannot open #{advisories_path}" unless File.file?(advisories_path)
|
|
11
|
+
|
|
12
|
+
advisories = File.open(advisories_path) { @ecosystem.parse_advisories(it) }
|
|
13
|
+
Update.new(@logger, @ecosystem, publisher: @publisher).call(advisories)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
module Secupdater
|
|
2
|
+
class Update
|
|
3
|
+
attr_reader :logger, :ecosystem, :publisher, :original
|
|
4
|
+
|
|
5
|
+
def initialize(logger, ecosystem, publisher: nil)
|
|
6
|
+
@logger = logger
|
|
7
|
+
@ecosystem = ecosystem
|
|
8
|
+
@publisher = publisher
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def call(advisories)
|
|
12
|
+
@original = ecosystem.snapshot
|
|
13
|
+
|
|
14
|
+
deps_needing_checking = advisories.group_by { it.name }
|
|
15
|
+
|
|
16
|
+
missing = deps_needing_checking.keys - original.keys
|
|
17
|
+
raise Secupdater::Error, "Advisories for dependencies not in the lockfile: #{missing.join(', ')}" if missing.any?
|
|
18
|
+
|
|
19
|
+
deps_needing_checking.to_h do |name, dep_advisories|
|
|
20
|
+
[name, update_dependency(original[name], dep_advisories)]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def update_dependency(dep, advisories)
|
|
25
|
+
logger.info("starting update for #{dep.name}, #{advisories.length} advisories")
|
|
26
|
+
|
|
27
|
+
status = message = nil
|
|
28
|
+
|
|
29
|
+
ecosystem.update_levels.each do |level|
|
|
30
|
+
status, message = attempt_update(dep, advisories, level)
|
|
31
|
+
return status unless message
|
|
32
|
+
|
|
33
|
+
logger.debug "#{level} update: #{message}"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
if status == :failed
|
|
37
|
+
logger.error message
|
|
38
|
+
else
|
|
39
|
+
logger.warn message
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
status
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def attempt_update(dep, advisories, level)
|
|
48
|
+
file_changes = ecosystem.try_update(dep.name, level:)
|
|
49
|
+
bump = ecosystem.diff_snapshot(original, ecosystem.snapshot).detect { it.dependency.name == dep.name }
|
|
50
|
+
|
|
51
|
+
return [:unchanged, "#{dep.name} didn't update (stuck at #{dep.version})"] unless bump
|
|
52
|
+
|
|
53
|
+
resolved = advisories.all? do |advisory|
|
|
54
|
+
advisory.patched_versions.any? { ecosystem.satisfies_requirement?(bump.to, it) }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
unless resolved
|
|
58
|
+
return [:unresolved, "#{dep.name} didn't update to a version that resolves the security warnings (went from #{bump.from} to #{bump.to})"]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
advisories.each do |advisory|
|
|
62
|
+
logger.info "upgrading #{dep.name} from #{bump.from} to #{bump.to} (#{level}) would resolve #{advisory}"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
return [:resolved, nil] unless publisher
|
|
66
|
+
|
|
67
|
+
publisher.call(ecosystem, bump, advisories, file_changes)
|
|
68
|
+
[:published, nil]
|
|
69
|
+
rescue Secupdater::UpdateCommandFailed => e
|
|
70
|
+
[:failed, "#{dep.name}: #{e.message}\n#{e.output}"]
|
|
71
|
+
ensure
|
|
72
|
+
restore(file_changes)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def restore(file_changes)
|
|
76
|
+
file_changes&.each do |change|
|
|
77
|
+
if change.original_content
|
|
78
|
+
File.write(change.filename, change.original_content)
|
|
79
|
+
else
|
|
80
|
+
File.delete(change.filename)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
data/lib/secupdater.rb
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "dry/logger"
|
|
4
|
+
|
|
5
|
+
require_relative "secupdater/version"
|
|
6
|
+
require_relative "secupdater/cli"
|
|
7
|
+
require_relative "secupdater/ecosystem"
|
|
8
|
+
require_relative "secupdater/runner"
|
|
9
|
+
require_relative "secupdater/update"
|
|
10
|
+
require_relative "secupdater/publisher"
|
|
11
|
+
require_relative "secupdater/publisher/gitlab"
|
|
12
|
+
|
|
13
|
+
module Secupdater
|
|
14
|
+
SecurityAdvisory = Struct.new(:name, :report_id, :patched_versions) do
|
|
15
|
+
def to_s
|
|
16
|
+
"#{name} #{report_id}"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
DependencySpec = Struct.new(:name, :meta, :version)
|
|
21
|
+
DependencySpecDiff = Struct.new(:dependency, :from, :to)
|
|
22
|
+
FileChange = Struct.new(:filename, :type, :content, :original_content)
|
|
23
|
+
|
|
24
|
+
class Error < StandardError; end
|
|
25
|
+
|
|
26
|
+
class UpdateCommandFailed < Error
|
|
27
|
+
attr_reader :output, :exit_code
|
|
28
|
+
|
|
29
|
+
def initialize(message = nil, exit_code = nil, output = nil)
|
|
30
|
+
super(message)
|
|
31
|
+
@exit_code = exit_code
|
|
32
|
+
@output = output
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.build_logger(log_level: :warn)
|
|
37
|
+
Dry.Logger(
|
|
38
|
+
:secupdater,
|
|
39
|
+
template: "[%<severity>s] %<message>s",
|
|
40
|
+
level: log_level
|
|
41
|
+
)
|
|
42
|
+
end
|
|
43
|
+
end
|
data/sig/secupdater.rbs
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
require "secupdater"
|
|
2
|
+
require "fileutils"
|
|
3
|
+
require "stringio"
|
|
4
|
+
require "tmpdir"
|
|
5
|
+
|
|
6
|
+
RSpec.describe "secupdater end to end" do
|
|
7
|
+
let(:out) { StringIO.new }
|
|
8
|
+
let(:err) { StringIO.new }
|
|
9
|
+
let(:env) { {} }
|
|
10
|
+
|
|
11
|
+
def lockfile(css_parser_version)
|
|
12
|
+
<<~LOCKFILE
|
|
13
|
+
GEM
|
|
14
|
+
remote: https://rubygems.org/
|
|
15
|
+
specs:
|
|
16
|
+
css_parser (#{css_parser_version})
|
|
17
|
+
|
|
18
|
+
PLATFORMS
|
|
19
|
+
ruby
|
|
20
|
+
|
|
21
|
+
DEPENDENCIES
|
|
22
|
+
css_parser
|
|
23
|
+
|
|
24
|
+
BUNDLED WITH
|
|
25
|
+
2.5.0
|
|
26
|
+
LOCKFILE
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
let(:report) do
|
|
30
|
+
{
|
|
31
|
+
results: [
|
|
32
|
+
{
|
|
33
|
+
type: "unpatched_gem",
|
|
34
|
+
gem: { name: "css_parser", version: "1.0.0" },
|
|
35
|
+
advisory: { id: "CVE-2024-0001", cve: "2024-0001", patched_versions: [">= 1.1.0"] }
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def run_cli(*arguments)
|
|
42
|
+
Dry::CLI.new(Secupdater::CLI::Commands).call(arguments: ["run", "--ecosystem=rubygems", *arguments, "audit.json"], out: out, err: err)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
around do |example|
|
|
46
|
+
original_env = ENV.to_h
|
|
47
|
+
|
|
48
|
+
Dir.mktmpdir do |dir|
|
|
49
|
+
Dir.chdir(dir) do
|
|
50
|
+
File.write("Gemfile.lock", lockfile("1.0.0"))
|
|
51
|
+
File.write("Gemfile.lock.after", lockfile("1.1.0"))
|
|
52
|
+
File.write("audit.json", report.to_json)
|
|
53
|
+
|
|
54
|
+
FileUtils.mkdir("bin")
|
|
55
|
+
File.write("bin/bundle", "#!/bin/sh\ncp Gemfile.lock.after Gemfile.lock\n")
|
|
56
|
+
File.chmod(0o755, "bin/bundle")
|
|
57
|
+
|
|
58
|
+
ENV.delete("SECUPDATER_GITLAB_TOKEN")
|
|
59
|
+
ENV.update(env.merge("PATH" => "#{dir}/bin:#{ENV['PATH']}"))
|
|
60
|
+
example.run
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
ensure
|
|
64
|
+
ENV.replace(original_env)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
context "fixture project + bundle-audit JSON + fake bundle command that swaps in an 'after' lockfile + WebMock'd GitLab" do
|
|
68
|
+
let(:env) { { "SECUPDATER_GITLAB_TOKEN" => "token", "CI_PROJECT_ID" => "123", "CI_DEFAULT_BRANCH" => "main" } }
|
|
69
|
+
|
|
70
|
+
let!(:find_mr_stub) do
|
|
71
|
+
stub_request(:get, %r{\Ahttps://gitlab.com/api/v4/projects/123/merge_requests\?})
|
|
72
|
+
.to_return(status: 200, body: "[]")
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
let!(:create_commit_stub) do
|
|
76
|
+
stub_request(:post, "https://gitlab.com/api/v4/projects/123/repository/commits")
|
|
77
|
+
.with(body: hash_including(
|
|
78
|
+
branch: "security/css_parser-1.1.0",
|
|
79
|
+
actions: [{ action: "update", file_path: "Gemfile.lock", content: lockfile("1.1.0") }]
|
|
80
|
+
))
|
|
81
|
+
.to_return(status: 201, body: "{}")
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
let!(:create_mr_stub) do
|
|
85
|
+
stub_request(:post, "https://gitlab.com/api/v4/projects/123/merge_requests")
|
|
86
|
+
.with(body: hash_including(source_branch: "security/css_parser-1.1.0", target_branch: "main"))
|
|
87
|
+
.to_return(status: 201, body: "{}")
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it "exits 0, commits the new Gemfile.lock and opens one MR per vulnerable gem" do
|
|
91
|
+
expect { run_cli }.not_to raise_error
|
|
92
|
+
|
|
93
|
+
expect(find_mr_stub).to have_been_requested.once
|
|
94
|
+
expect(create_commit_stub).to have_been_requested.once
|
|
95
|
+
expect(create_mr_stub).to have_been_requested.once
|
|
96
|
+
expect(File.read("Gemfile.lock")).to eq(lockfile("1.0.0"))
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
context "same fixture project without GitLab configuration (dry run)" do
|
|
101
|
+
it "exits 0, reports what would be updated and makes no HTTP requests" do
|
|
102
|
+
expect { run_cli("--verbose") }.to output(/css_parser from 1\.0\.0 to 1\.1\.0 \(patch\) would resolve/).to_stdout
|
|
103
|
+
|
|
104
|
+
expect(a_request(:any, //)).not_to have_been_made
|
|
105
|
+
expect(File.read("Gemfile.lock")).to eq(lockfile("1.0.0"))
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
context "real bundle update --local against fixture vendor/cache gems", :integration do
|
|
110
|
+
it "bumps the gem in Gemfile.lock"
|
|
111
|
+
end
|
|
112
|
+
end
|