Class: Bundler::Audit::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/audit/scanner.rb

Defined Under Namespace

Classes: InsecureSource, UnpatchedGem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = Dir.pwd) ⇒ Scanner

Initializes a scanner.

Parameters:

  • root (String) (defaults to: Dir.pwd)

    The path to the project root.



36
37
38
39
40
41
42
# File 'lib/bundler/audit/scanner.rb', line 36

def initialize(root=Dir.pwd)
  @root     = File.expand_path(root)
  @database = Database.new
  @lockfile = LockfileParser.new(
    File.read(File.join(@root,'Gemfile.lock'))
  )
end

Instance Attribute Details

#databaseDatabase (readonly)

The advisory database

Returns:



20
21
22
# File 'lib/bundler/audit/scanner.rb', line 20

def database
  @database
end

#lockfileBundler::LockfileParser (readonly)

The parsed Gemfile.lock from the project

Returns:

  • (Bundler::LockfileParser)


28
29
30
# File 'lib/bundler/audit/scanner.rb', line 28

def lockfile
  @lockfile
end

#rootObject (readonly)

Project root directory



23
24
25
# File 'lib/bundler/audit/scanner.rb', line 23

def root
  @root
end

Instance Method Details

#get_insecure_sourcesObject (protected)



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bundler/audit/scanner.rb', line 73

def get_insecure_sources
  insecure = []
  @lockfile.sources.each do |source|
    case source
    when Source::Git
      next unless(source.uri =~ /^(git|http):/)

      insecure << InsecureSource.new(source.uri)
    when Source::Rubygems
      source.remotes.map do |uri|
        next unless uri.scheme == 'http'

        insecure << InsecureSource.new(uri.to_s)
      end
    end
  end

  return insecure
end

#get_unpatched_gems(ignore) ⇒ Object (protected)



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bundler/audit/scanner.rb', line 93

def get_unpatched_gems(ignore)
  ignore = Set.new(ignore) # If ignore is empty the Set will contain nil,
                           # but since we should never have a nil version
                           # that's a non-issue.
  unpatched = []
  @lockfile.specs.each do |gem|
    @database.check_gem(gem) do |advisory|
      next if ignore.include?(advisory.id)

      unpatched << UnpatchedGem.new(gem,advisory)
    end
  end
  return unpatched
end

#scan(options = {}) {|result| ... } ⇒ Enumerator

Scans the project for issues.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :ignore (Array<String>)

    The advisories to ignore.

Yields:

  • (result)

    The given block will be passed the results of the scan.

Yield Parameters:

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.



62
63
64
65
66
67
68
69
# File 'lib/bundler/audit/scanner.rb', line 62

def scan(options={})
  return enum_for(__method__,options) unless block_given?

  get_insecure_sources.each { |source| yield source }
  get_unpatched_gems(options[:ignore]).each { |gem| yield gem }

  return self
end