class Dependabot::GoModules::ReplaceStubber

Given a go.mod file, find all `replace` directives pointing to a path on the local filesystem outside of the current checkout, and return a hash mapping the original path to a hash of the path.

This lets us substitute all parts of the go.mod that are dependent on the layout of the filesystem with a structure we can reproduce (i.e. no paths such as ../../../foo), run the Go tooling, then reverse the process afterwards.

Public Class Methods

new(repo_contents_path) click to toggle source
# File lib/dependabot/go_modules/replace_stubber.rb, line 14
def initialize(repo_contents_path)
  @repo_contents_path = repo_contents_path
end

Public Instance Methods

stub_paths(manifest, directory) click to toggle source
# File lib/dependabot/go_modules/replace_stubber.rb, line 18
def stub_paths(manifest, directory)
  (manifest["Replace"] || []).
    map { |r| r["New"]["Path"] }.
    compact.
    select { |p| stub_replace_path?(p, directory) }.
    map { |p| [p, "./" + Digest::SHA2.hexdigest(p)] }.
    to_h
end

Private Instance Methods

absolute_path?(path) click to toggle source
# File lib/dependabot/go_modules/replace_stubber.rb, line 41
def absolute_path?(path)
  path.start_with?("/")
end
module_pathname(directory) click to toggle source
# File lib/dependabot/go_modules/replace_stubber.rb, line 50
def module_pathname(directory)
  @module_pathname ||= Pathname.new(@repo_contents_path).join(directory.sub(%r{^/}, ""))
end
relative_replacement_path?(path) click to toggle source
# File lib/dependabot/go_modules/replace_stubber.rb, line 45
def relative_replacement_path?(path)
  # https://golang.org/ref/mod#go-mod-file-replace
  path.start_with?("./") || path.start_with?("../")
end
stub_replace_path?(path, directory) click to toggle source
# File lib/dependabot/go_modules/replace_stubber.rb, line 29
def stub_replace_path?(path, directory)
  return true if absolute_path?(path)
  return false unless relative_replacement_path?(path)
  return true if @repo_contents_path.nil?

  resolved_path = module_pathname(directory).join(path).realpath
  inside_repo_contents_path = resolved_path.to_s.start_with?(@repo_contents_path.to_s)
  !inside_repo_contents_path
rescue Errno::ENOENT
  true
end