Last active
March 27, 2020 22:06
-
-
Save dcu/3c06e4ab0e98158c5742c4fd2b31523d to your computer and use it in GitHub Desktop.
Check gems that can be affected by http://blog.rubygems.org/2016/04/06/gem-replacement-vulnerability-and-mitigation.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rest-client' | |
require 'time' | |
require 'json' | |
require 'bundler' | |
CHECK_GEMS_BEFORE = Time.parse("Feb 8, 2015") | |
def check_bundler | |
gems = Bundler::LockfileParser.new(Bundler.read_file("Gemfile.lock")) | |
gems.specs.each do |spec| | |
gem_name = spec.name | |
version = spec.version.to_s | |
next unless spec.source.kind_of?(Bundler::Source::Rubygems) | |
rubygems = spec.source.remotes.find {|r| r.to_s =~ /rubygems\.org/ } | |
if gem_name !~ /-/ || rubygems == nil | |
next | |
end | |
check_gem(gem_name, version) | |
end | |
end | |
def check_local_gems | |
Gem::Specification.each do |gem| | |
check_gem(gem.name, gem.version.to_s) | |
end | |
end | |
def check_gem(gem_name, version) | |
print "Checking #{gem_name} #{version}" | |
response = RestClient.get("https://rubygems.org/api/v1/versions/#{gem_name}.json") {|r| r } | |
if response.code != 200 | |
puts " [NOT FOUND]" | |
return | |
end | |
gem_data = JSON.parse(response) | |
gem = gem_data.find {|gd| gd["number"] == version } | |
latest_version = gem_data.first | |
if !gem | |
puts " [NOT FOUND]" | |
return | |
end | |
created_at = Time.parse(gem["created_at"]) | |
if created_at > CHECK_GEMS_BEFORE | |
puts " [VERIFIED]" | |
return | |
end | |
latest_created_at = Time.parse(latest_version["created_at"]) | |
if latest_created_at > CHECK_GEMS_BEFORE | |
puts " [NOT VERIFIED][#{latest_version["number"]} VERIFIED]" | |
else | |
puts " [NOT VERIFIED]" | |
end | |
end | |
if File.exist?("Gemfile.lock") | |
check_bundler | |
else | |
check_local_gems | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment