hrw 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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 670978eb8167255d8fbe62416a0a4b2bc8915074b34e4c4cc75091d2cc6cd3d3
4
+ data.tar.gz: 26d18f588f3145076d70d7ba2bac78badcb35169a16e2c8af917ac8299bb0238
5
+ SHA512:
6
+ metadata.gz: d0b77d983c3036917919aac670ed757c7660de56bd151f3141b83227a5afa5e391e765dbbef7676b5ddbe60602f78707db86e2ca6aed0f29e53c51478a311ddd
7
+ data.tar.gz: 28ad93ca22a29c4239f76b609a6a077878a563e93bf84a3b390a3178d2bbd045ab2659b1880794269435ef0999aba2c8037f8c13c36602b85319a190c26217e2
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.1
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hrw.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 TODO: Write your name
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.
@@ -0,0 +1,39 @@
1
+ # Hrw
2
+
3
+ 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/hrw`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hrw'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hrw
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. Then, run `rake spec` to run the tests. 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 tags, 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]/hrw.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hrw"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/hrw ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ostruct'
3
+ require 'optparse'
4
+ require 'hrw'
5
+
6
+ ARGV << '--help' if ARGV.empty?
7
+
8
+ options = OpenStruct.new
9
+ OptionParser.new do |opt|
10
+ opt.banner = "usage: #{__FILE__} [OPTIONS]"
11
+
12
+ opt.on('-u', '--url [URL]', 'remote api url') do |url|
13
+ options.url = url
14
+ end
15
+ end.parse!
16
+
17
+ if options.url.nil?
18
+ if ENV.key?(Hrw::ENV)
19
+ options.url = ENV[Hrw::ENV]
20
+ else
21
+ puts '[-] no url'
22
+ exit(1)
23
+ end
24
+ end
25
+
26
+ begin
27
+ scanner = Hrw::Detector.detect
28
+ specs = scanner.scan
29
+
30
+ api = Hrw::API.new(options.url)
31
+ hash = api.submit(specs)
32
+ result = api.retrieve(hash)
33
+
34
+ formatter = Hrw::Formatter.new
35
+ vulnerable_deps = formatter.format(result)
36
+
37
+ unless vulnerable_deps.empty?
38
+ formatter.print_vulnerable_deps(vulnerable_deps)
39
+ exit(1)
40
+ end
41
+ rescue StandardError => ex
42
+ require 'pry-byebug'
43
+ binding.pry
44
+ puts ex.message
45
+ end
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path('lib', __dir__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hrw/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'hrw'
8
+ spec.version = Hrw::VERSION
9
+ spec.authors = ['hi_ztz']
10
+ spec.email = ['hi_ztz@protonmail.com']
11
+
12
+ spec.summary = 'Hrw helps you to secure your ruby apps.'
13
+ spec.description = 'Hrw helps you to secure your ruby apps.'
14
+ spec.homepage = 'https://github.com/zt2/hrw'
15
+ spec.license = 'MIT'
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_development_dependency 'bundler', '~> 2.0'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+
30
+ spec.add_runtime_dependency 'http', '~> 4.1', '>= 4.1.1'
31
+ spec.add_runtime_dependency 'rainbow', '~> 3.0'
32
+ spec.add_runtime_dependency 'pry-byebug', '~> 3.7'
33
+ end
@@ -0,0 +1,23 @@
1
+ #
2
+ # Used to scan vulnerabilities in dependencies
3
+ #
4
+ module Hrw
5
+ #
6
+ # Errors
7
+ #
8
+ class Error < StandardError; end
9
+
10
+ #
11
+ # Constants
12
+ #
13
+ ENV = 'HRW_URL'.freeze
14
+
15
+ #
16
+ # Load libraries
17
+ #
18
+ require 'hrw/api'
19
+ require 'hrw/detector'
20
+ require 'hrw/formatter'
21
+ require 'hrw/scanner'
22
+ require 'hrw/version'
23
+ end
@@ -0,0 +1,66 @@
1
+ #
2
+ # Standard libraries
3
+ #
4
+ require 'digest'
5
+
6
+ #
7
+ # Third-party libraries
8
+ #
9
+ require 'http'
10
+
11
+ module Hrw
12
+ #
13
+ # Used to interact with Horus server
14
+ #
15
+ class API
16
+ #
17
+ # Errors
18
+ #
19
+ class HTTPCodeError < StandardError; end
20
+
21
+ # Class constructor
22
+ #
23
+ # @param [String] url API remote address
24
+ def initialize(url)
25
+ @submit_url = url.chomp('/') + '/dependency'
26
+ @retrieve_url = url.chomp('/') + '/ancestry'
27
+ end
28
+
29
+ # Submit dependencies to server
30
+ #
31
+ # @param [Array] dependencies
32
+ # @return [String] Checksum for those dependencies
33
+ def submit(dependencies)
34
+ body = {
35
+ hash: _calc_hash(dependencies),
36
+ pkg_manager: {
37
+ name: :rubygems
38
+ },
39
+ packages: dependencies
40
+ }
41
+
42
+ res = HTTP.post(@submit_url, json: body)
43
+ raise HTTPCodeError, "#{res.code}: #{res.body}" if res.code != 200
44
+
45
+ body[:hash]
46
+ end
47
+
48
+ # Used to retrieve scan results
49
+ #
50
+ # @param [String] hash
51
+ # @return [Hash] result
52
+ def retrieve(hash)
53
+ res = HTTP.get(@retrieve_url + "/#{hash}")
54
+ raise HTTPCodeError, "#{res.code}: #{res.body}" if res.code != 200
55
+
56
+ res.parse
57
+ end
58
+
59
+ private
60
+
61
+ def _calc_hash(dependencies)
62
+ str = dependencies.map { |d| "#{d[:name]}@#{d[:version]}" }.sort.join('|')
63
+ Digest::SHA256.hexdigest(str)
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,38 @@
1
+ require 'hrw/scanner'
2
+
3
+ module Hrw
4
+ #
5
+ # To detect which platform used by current project
6
+ #
7
+ module Detector
8
+ #
9
+ # Errors
10
+ #
11
+ class MultipleChoiceError < StandardError; end
12
+
13
+ #
14
+ # Constants
15
+ #
16
+ DETECTABLE_FILES = {
17
+ 'Gemfile.lock': Scanner::Gemfile
18
+ }.freeze
19
+
20
+ # Detect package manager in root dir
21
+ #
22
+ # @param [String] root root dir for project
23
+ # @return [Hrw::Scanner] scanner
24
+ def self.detect(root = Dir.pwd)
25
+ files = Dir['*', base: root].map { |file| File.basename(file).to_sym }
26
+ pkg_files = DETECTABLE_FILES.keys & files
27
+
28
+ if pkg_files.empty?
29
+ nil
30
+ elsif pkg_files.size > 1
31
+ raise MultipleChoiceError
32
+ else
33
+ file = pkg_files.first
34
+ DETECTABLE_FILES[file].new(root, file.to_s)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,50 @@
1
+ #
2
+ # Standard libraries
3
+ #
4
+ require 'json'
5
+
6
+ #
7
+ # Third-party libraries
8
+ #
9
+ require 'rainbow'
10
+
11
+ module Hrw
12
+ #
13
+ # Format result and pretty print it
14
+ #
15
+ class Formatter
16
+ # Format result
17
+ #
18
+ # @param [Hash] result scan result
19
+ # @return [Boolean] vulnerable or not
20
+ def format(result)
21
+ deps = []
22
+
23
+ result['ancestry']['layers'].each do |layer|
24
+ layer['detected_features'].each do |feature|
25
+ deps << feature if feature.key?('vulnerabilities')
26
+ end
27
+ end
28
+
29
+ deps
30
+ end
31
+
32
+ # Pretty print result
33
+ #
34
+ def print_vulnerable_deps(deps)
35
+ deps.each do |dep|
36
+ dep['vulnerabilities'].each do |vuln|
37
+ patched_version = JSON.parse(vuln['fixed_by'])
38
+
39
+ puts "Name: #{dep['name']}"
40
+ puts "Version: #{dep['version']}"
41
+ puts "Advisory: #{vuln['name']}"
42
+ puts "Severity: #{vuln['severity']}"
43
+ puts "Link: #{vuln['link']}"
44
+ puts "Patched version: #{patched_version['spec'].join(', ')}"
45
+ puts
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ module Hrw
2
+ #
3
+ # Used to scan dependencies
4
+ #
5
+ module Scanner
6
+ require 'hrw/scanner/gemfile'
7
+ end
8
+ end
@@ -0,0 +1,37 @@
1
+ #
2
+ # Third-party libraries
3
+ #
4
+ require 'bundler'
5
+ require 'bundler/lockfile_parser'
6
+
7
+ module Hrw
8
+ module Scanner
9
+ #
10
+ # Used to scan gem lock file
11
+ #
12
+ class Gemfile
13
+ # Initialize a scanner
14
+ #
15
+ # @param [String] root The path to the project root
16
+ # @param [String] lockfile
17
+ # The name for the lock file, default is `Gemfile.lock`
18
+ def initialize(root = Dir.pwd, lockfile = 'Gemfile.lock')
19
+ @root = File.expand_path(root)
20
+ @lockfile = Bundler::LockfileParser.new(
21
+ File.read(File.join(@root, lockfile))
22
+ )
23
+ end
24
+
25
+ # Scan the lock file
26
+ # @return [Hash]
27
+ def scan
28
+ @lockfile.specs.map do |spec|
29
+ {
30
+ name: spec.name,
31
+ version: spec.version.to_s
32
+ }
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Hrw
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hrw
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hi_ztz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: http
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.1'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 4.1.1
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '4.1'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 4.1.1
75
+ - !ruby/object:Gem::Dependency
76
+ name: rainbow
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: pry-byebug
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.7'
96
+ type: :runtime
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.7'
103
+ description: Hrw helps you to secure your ruby apps.
104
+ email:
105
+ - hi_ztz@protonmail.com
106
+ executables:
107
+ - hrw
108
+ extensions: []
109
+ extra_rdoc_files: []
110
+ files:
111
+ - ".gitignore"
112
+ - ".rspec"
113
+ - ".travis.yml"
114
+ - Gemfile
115
+ - LICENSE.txt
116
+ - README.md
117
+ - Rakefile
118
+ - bin/console
119
+ - bin/setup
120
+ - exe/hrw
121
+ - hrw.gemspec
122
+ - lib/hrw.rb
123
+ - lib/hrw/api.rb
124
+ - lib/hrw/detector.rb
125
+ - lib/hrw/formatter.rb
126
+ - lib/hrw/scanner.rb
127
+ - lib/hrw/scanner/gemfile.rb
128
+ - lib/hrw/version.rb
129
+ homepage: https://github.com/zt2/hrw
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubygems_version: 3.0.3
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Hrw helps you to secure your ruby apps.
152
+ test_files: []