paraxial 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1bd3d81eda937e486869f0796af505440ba3268ef365a59170ac0545dcf06a10
4
+ data.tar.gz: '099afef44bbc41beb2fdad948610864fa4592837428701c77d0f965d047e7d68'
5
+ SHA512:
6
+ metadata.gz: 59db9199f831f0d4b65d8aa56bfca1ed976da91c03799b237c5b2d4fe62d5df1fd76c9de6dbf4969c90c6a0c50d6cfedfe57762f89a36f63997f1df7f811178c
7
+ data.tar.gz: 5efcc6c94e05db287a6679e06188f86b354ec4c098c8f2f4276e10a73385895cbbf3b2c6bc2a7364e11c26d3ae4e953ff18109193d533eec6e41e0356895c9b8
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-07-19
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 dt
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,13 @@
1
+ # Paraxial
2
+
3
+ The Paraxial.io ruby agent.
4
+
5
+ ## Development
6
+
7
+ 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.
8
+
9
+ 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).
10
+
11
+ ## License
12
+
13
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/exe/paraxial ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'paraxial/cli'
3
+ Paraxial::CLI.start
@@ -0,0 +1,34 @@
1
+ require 'thor'
2
+ require 'paraxial'
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'json'
6
+ require 'time'
7
+
8
+ module Paraxial
9
+ class CLI < Thor
10
+ desc "scan", "Run scan"
11
+ def scan
12
+ puts "[Paraxial] Scan NOW"
13
+ cops = "Paraxial,Security/Eval,Security/IoMethods,Security/JSONLoad,Security/MarshalLoad,Security/Open,Security/YAMLLoad"
14
+ rubocop = `rubocop --only #{cops} --format json`
15
+ lockfile = File.read("./Gemfile.lock")
16
+ api_key = ENV['PARAXIAL_API_KEY']
17
+ uri = URI.parse(ENV['PARAXIAL_URL'] + "/api/ruby_scan")
18
+ headers = { 'Content-Type': 'application/json' }
19
+
20
+ body = { rubocop: rubocop, lockfile: lockfile, api_key: api_key, timestamp: Paraxial.get_timestamp() }
21
+ response = Net::HTTP.post(uri, body.to_json, headers)
22
+ puts response.body
23
+
24
+ if ENV['PARAXIAL_API_KEY'] == nil
25
+ puts "[Paraxial] Environment variable PARAXIAL_API_KEY not found, set with: "
26
+ puts "[Paraxial] export PARAXIAL_API_KEY=your_site_api_key_here"
27
+ puts "[Paraxial] Exiting"
28
+ exit()
29
+ else
30
+ puts "[Paraxial] Scan result here"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ module Paraxial
2
+ if defined?(Rails::Engine)
3
+ class Engine < ::Rails::Engine
4
+ initializer 'paraxial.load_initializers' do
5
+ # Load the initializers from the `initializers` directory
6
+ Dir.glob(File.expand_path('initializers/*.rb', __dir__)).each do |file|
7
+ require_dependency file
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,25 @@
1
+ require 'bundler'
2
+ require 'paraxial'
3
+ Bundler.setup
4
+
5
+ Rails.application.config.to_prepare do
6
+ # Your code here
7
+ puts "[Paraxial] Runtime start"
8
+
9
+ deps_and_licenses = []
10
+ Bundler.load.specs.each do |spec|
11
+ # Print the gem name and license
12
+ h = { name: spec.name, version: spec.version.to_s, description: Paraxial.trim_dep(spec.description), license: spec.license || 'None' }
13
+ deps_and_licenses << h
14
+ end
15
+ deps_and_licenses << { name: "ruby", version: RUBY_VERSION, description: "The Ruby Programming Language", license: "Ruby"}
16
+ api_key = ENV['PARAXIAL_API_KEY']
17
+ uri = URI.parse(ENV['PARAXIAL_URL'] + "/api/ruby_app_lic")
18
+ headers = { 'Content-Type': 'application/json' }
19
+
20
+ body = { app_lic: deps_and_licenses, api_key: api_key, timestamp: Paraxial.get_timestamp() }
21
+ Thread.new do
22
+ response = Net::HTTP.post(uri, body.to_json, headers)
23
+ end
24
+
25
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paraxial
4
+ VERSION = "0.1.0"
5
+ end
data/lib/paraxial.rb ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+ require 'paraxial/engine'
5
+ require 'rubocop'
6
+ require_relative 'rubocop/cop/paraxial/csrf'
7
+ require_relative 'rubocop/cop/paraxial/system'
8
+ require_relative 'rubocop/cop/paraxial/send'
9
+ require_relative 'rubocop/cop/paraxial/constantize'
10
+ require_relative 'rubocop/cop/paraxial/html_safe'
11
+ require_relative 'rubocop/cop/paraxial/sql'
12
+ require_relative "paraxial/version"
13
+ require_relative 'paraxial/cli'
14
+
15
+
16
+ module Paraxial
17
+ class Error < StandardError; end
18
+ # Your code goes here...
19
+
20
+ def self.get_timestamp
21
+ utc_time = Time.now.utc
22
+ utc_time.strftime("%Y-%m-%d %H:%M:%S.%6N") + "Z"
23
+ end
24
+
25
+ def self.trim_dep(input)
26
+ if input == nil
27
+ nil
28
+ else
29
+ cleaned_string = input.gsub(/\n/, '')
30
+
31
+ # Find the position of the first period
32
+ period_index = cleaned_string.index('.')
33
+
34
+ # If there's a period, truncate the string up to that point
35
+ if period_index
36
+ cleaned_string = cleaned_string[0..period_index]
37
+ end
38
+
39
+ cleaned_string
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,23 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Paraxial
4
+ class Constantize < Base
5
+ MSG = '`constantize` methods cause remote code execution if called on user input.'
6
+
7
+ def on_send(node)
8
+ method_name = node.method_name
9
+ return unless send_methods.include?(method_name)
10
+
11
+ add_offense(node, message: format(MSG, method: method_name))
12
+ end
13
+
14
+ private
15
+
16
+ def send_methods
17
+ [:constantize, :safe_constantize, :const_get, :qualified_const_get]
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Paraxial
4
+ class CSRF < Base
5
+ MSG = 'CSRF, no protect_from_forgery in ApplicationController.'
6
+
7
+ def_node_search :protect_from_forgery_call, <<~PATTERN
8
+ (send nil? :protect_from_forgery ...)
9
+ PATTERN
10
+
11
+ def on_class(node)
12
+ class_name = node.loc.name.source
13
+
14
+ return unless class_name == 'ApplicationController'
15
+
16
+ protect_from_forgery = protect_from_forgery_call(node).first
17
+
18
+ add_offense(node) unless protect_from_forgery
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Paraxial
4
+ class HTMLSafe < Base
5
+ MSG = '`html_safe` leads to XSS when called on user input'
6
+
7
+ def on_send(node)
8
+ method_name = node.method_name
9
+ return unless send_methods.include?(method_name)
10
+
11
+ add_offense(node, message: format(MSG, method: method_name))
12
+ end
13
+
14
+ private
15
+
16
+ def send_methods
17
+ [:html_safe]
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Paraxial
4
+ class Send < Base
5
+ MSG = '`send` causes remote code execution if called on user input.'
6
+
7
+ def on_send(node)
8
+ method_name = node.method_name
9
+ return unless send_methods.include?(method_name)
10
+
11
+ add_offense(node, message: format(MSG, method: method_name))
12
+ end
13
+
14
+ private
15
+
16
+ def send_methods
17
+ [:send, :try, :__send__, :public_send]
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Paraxial
6
+ # This cop checks that Active Record queries use literal keys in all of
7
+ # their conditions. Using dynamic keys in queries can make code
8
+ # susceptible to SQL injection attacks.
9
+ #
10
+ # @example
11
+ # # bad
12
+ # Model.where("order_count > #{params[:orders]}")
13
+ # Model.having("order_count > #{params[:orders]}")
14
+ # Model.exists?("order_count > #{params[:orders]}")
15
+ #
16
+ # # good
17
+ # Model.where('order_count > ?', params[:orders])
18
+ # Model.having('order_count > ?', params[:orders])
19
+ # Model.exists?('order_count > ?', params[:orders])
20
+ #
21
+ # @example EnforcedStyle: params (default)
22
+ # # bad
23
+ # Model.find_by(params['id'])
24
+ #
25
+ # # good
26
+ # Model.find_by(id: params['id'])
27
+ # Model.find_by(Group.arel_table[:id].in([1, 2, 3]))
28
+ #
29
+ # @example EnforcedStyle: all
30
+ #
31
+ # # bad
32
+ # Model.find_by(params['id'])
33
+ # # Value from Strong Parameters could still cause a SQLi.
34
+ # Model.find_by(model_params['id'])
35
+ # # Unfortunately there are false positives too.
36
+ # Model.find_by(Group.arel_table[:id].in([1, 2, 3]))
37
+ #
38
+ # # good
39
+ # Model.find_by(id: params['id'])
40
+ # Model.find_by(id: model_params['id'])
41
+ class SQL < Base
42
+ include ConfigurableEnforcedStyle
43
+
44
+ MSG = 'SQL injection via dynamic query key.'
45
+
46
+ RESTRICT_ON_SEND = %i[
47
+ all
48
+ average
49
+ calculate
50
+ count
51
+ count_by_sql
52
+ create_with
53
+ delete_all
54
+ delete_by
55
+ destroy_all
56
+ destroy_by
57
+ exists?
58
+ find_by
59
+ find_by!
60
+ find_by_sql
61
+ find_or_create_by
62
+ find_or_create_by!
63
+ find_or_initialize_by
64
+ find_or_initialize_by!
65
+ first
66
+ from
67
+ group
68
+ having
69
+ joins
70
+ last
71
+ lock
72
+ maximum
73
+ minimum
74
+ named_scope
75
+ not
76
+ order
77
+ pluck
78
+ reorder
79
+ reselect
80
+ rewhere
81
+ scope
82
+ select
83
+ sql
84
+ sum
85
+ update_all
86
+ where
87
+ ].freeze
88
+
89
+ def_node_matcher :non_literal_condition?, <<~'PATTERN'
90
+ (
91
+ send _ _ # Match `where` and `Model.find_by`
92
+ ({dstr <begin ...> | send #matching_send?} ...) # Match where(params[:id]) and where("#{method}")
93
+ )
94
+ PATTERN
95
+
96
+ def_node_matcher :params?, '(send nil? :params)'
97
+
98
+ def matching_send?(node)
99
+ style == :all ? true : params?(node)
100
+ end
101
+
102
+ def on_send(node)
103
+ return unless non_literal_condition?(node)
104
+
105
+ add_offense(node)
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,31 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Paraxial
4
+ class System < Base
5
+ MSG = '`system` causes remote code execution if called on user input.'
6
+
7
+ # Restrict the cop to only the `puts` method
8
+ RESTRICT_ON_SEND = %i[system].freeze
9
+
10
+ # @!method puts_call?(node)
11
+ def_node_matcher :system_call?, <<~PATTERN
12
+ (send nil? :system ...)
13
+ PATTERN
14
+
15
+ def on_send(node)
16
+ return unless in_app_directory?(node)
17
+ system_call?(node) do
18
+ add_offense(node.loc.selector, message: MSG)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def in_app_directory?(node)
25
+ processed_source.file_path.start_with?(File.join(Dir.pwd, 'app'))
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
data/sig/paraxial.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Paraxial
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paraxial
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Lubas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - michael@paraxial.io
44
+ executables:
45
+ - paraxial
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - CHANGELOG.md
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - exe/paraxial
54
+ - lib/paraxial.rb
55
+ - lib/paraxial/cli.rb
56
+ - lib/paraxial/engine.rb
57
+ - lib/paraxial/initializers/startup.rb
58
+ - lib/paraxial/version.rb
59
+ - lib/rubocop/cop/paraxial/constantize.rb
60
+ - lib/rubocop/cop/paraxial/csrf.rb
61
+ - lib/rubocop/cop/paraxial/html_safe.rb
62
+ - lib/rubocop/cop/paraxial/send.rb
63
+ - lib/rubocop/cop/paraxial/sql.rb
64
+ - lib/rubocop/cop/paraxial/system.rb
65
+ - sig/paraxial.rbs
66
+ homepage: https://paraxial.io/
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ allowed_push_host: https://rubygems.org/
71
+ homepage_uri: https://paraxial.io/
72
+ source_code_uri: https://paraxial.io/
73
+ changelog_uri: https://paraxial.io/
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.6.0
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.3.7
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Paraxial.io Ruby Agent
93
+ test_files: []