tainted 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: 1b26e5de6db90715d8868475b14f828ee951472378e2ab3d751bb36e0ab570a1
4
+ data.tar.gz: 4e84f893cefc8c37f52b9b353ff80ea3a63eba096f625d6acb2061aefb83d299
5
+ SHA512:
6
+ metadata.gz: 2a73d2e0c62366eb397b64c7f4cd8d78f643ba1179bafa4534664c901cadc683d3b1d375a205024e0716930d17634076de122e36e3f96c2c8f53f0310ac747c9
7
+ data.tar.gz: f1444e63ab1f1fa2cbb328b19d2676c5eabc5948ae6a4bc7af0167ce08c7dd898ea5ee203629ab30adec04c76d0ed652c1966ff008c4806775d8e4039726aaeb
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at faraaz98@live.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Syed Faraaz Ahmad
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,52 @@
1
+ # tainted
2
+
3
+ A gem for taint-checking your Ruby code.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ gem install tainted
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ # fixtures/simple.rb
15
+
16
+ a = tainted()
17
+ b = a + 1
18
+ c = b + 2
19
+ d = b + c
20
+ unsafe(d)
21
+ unsafe(c)
22
+ ```
23
+
24
+ ```ruby
25
+ # test.rb
26
+
27
+ require 'tainted'
28
+
29
+ file = "#{__dir__}/../fixtures/simple.rb"
30
+ lint = Tainted::Lint.new(file, %i[tainted], %i[unsafe])
31
+ lint.analyze
32
+ # Method `unsafe()` consuming tainted variable `d`
33
+ # Method `unsafe()` consuming tainted variable `c`
34
+ ```
35
+
36
+ ## Development
37
+
38
+ 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.
39
+
40
+ 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).
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/faraazahmad/tainted. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/faraazahmad/tainted/blob/main/CODE_OF_CONDUCT.md).
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
49
+
50
+ ## Code of Conduct
51
+
52
+ Everyone is expected to follow the [code of conduct](https://github.com/faraazahmad/tainted/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ a = tainted()
4
+ b = a + 1
5
+ c = b + 2
6
+ d = b + c
7
+ unsafe(d)
8
+ unsafe(c)
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tainted
4
+ class DataFlow
5
+ def initialize(file_path)
6
+ @file_path = file_path
7
+ @cfg = nil
8
+ @dfg = nil
9
+ @iseq = nil
10
+ @var_flows = {}
11
+ end
12
+
13
+ def generate
14
+ @iseq = RubyVM::InstructionSequence.compile_file(@file_path)
15
+ @iseq = SyntaxTree::YARV::InstructionSequence.from(@iseq.to_a)
16
+ @cfg = SyntaxTree::YARV::ControlFlowGraph.compile(@iseq)
17
+ @dfg = SyntaxTree::YARV::DataFlowGraph.compile(@cfg)
18
+ end
19
+
20
+ def tainted
21
+ @dfg.insn_flows.each_key do |key|
22
+ flow = @dfg.insn_flows[key]
23
+ next if flow.in.empty? && flow.out.empty?
24
+
25
+ # Check out
26
+ next if flow.out.empty?
27
+
28
+ flow.out.each do |out_flow|
29
+ insn = @cfg.insns[out_flow.length]
30
+
31
+ variable = nil
32
+ variable = @iseq.local_table.locals[insn.index] if insn.is_a?(
33
+ SyntaxTree::YARV::SetLocalWC0
34
+ )
35
+ next if variable.nil?
36
+
37
+ @var_flows[variable.name] = { from: [] } unless @var_flows.key?(
38
+ variable.name
39
+ )
40
+ @var_flows[variable.name][:from] = [
41
+ *@var_flows[variable.name][:from],
42
+ *trace_flows(flow)
43
+ ]
44
+ end
45
+ end
46
+
47
+ @var_flows
48
+ end
49
+
50
+ def var_from_insn(flow)
51
+ return flow if flow.is_a? Symbol
52
+
53
+ insn = @cfg.insns[flow.length]
54
+ return unless local?(insn)
55
+
56
+ @iseq.local_table.locals[insn.index].name
57
+ end
58
+
59
+ def trace_flows(flow)
60
+ from = []
61
+
62
+ flow.in.each do |incoming_flow|
63
+ unless incoming_flow.is_a?(
64
+ SyntaxTree::YARV::DataFlowGraph::LocalArgument
65
+ )
66
+ next
67
+ end
68
+
69
+ from = [
70
+ *from,
71
+ incoming_flow,
72
+ *trace_flows(@dfg.insn_flows[incoming_flow.length])
73
+ ]
74
+ end
75
+
76
+ from.map { |in_flow| var_from_insn(in_flow) }.reject(&:nil?)
77
+ end
78
+
79
+ def local?(insn)
80
+ [
81
+ SyntaxTree::YARV::GetLocalWC0,
82
+ SyntaxTree::YARV::GetLocalWC1,
83
+ SyntaxTree::YARV::GetLocal,
84
+ SyntaxTree::YARV::SetLocalWC0,
85
+ SyntaxTree::YARV::SetLocalWC1,
86
+ SyntaxTree::YARV::SetLocal
87
+ ].include?(insn.class)
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tainted
4
+ class Lint
5
+ def initialize(filepath, sources, sinks)
6
+ @filepath = filepath
7
+
8
+ t = Tainted::DataFlow.new(@filepath)
9
+ t.generate
10
+ var_dependencies = t.tainted
11
+ State.instance.var_dependencies = var_dependencies
12
+
13
+ @visitor = Static.new(sources, sinks)
14
+ end
15
+
16
+ def analyze
17
+ @visitor.visit(SyntaxTree.parse_file(@filepath))
18
+ @visitor.result
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ class State
6
+ include Singleton
7
+ attr_accessor :var_dependencies
8
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tainted
4
+ class Static < SyntaxTree::Visitor
5
+ attr_reader :result
6
+
7
+ def initialize(sources, _sinks)
8
+ super()
9
+
10
+ @sources = sources
11
+ @result = []
12
+ end
13
+
14
+ def visit(node)
15
+ nodes = node.child_nodes[0].child_nodes
16
+
17
+ # First visit all assignments
18
+ # mark tainted variables
19
+ nodes
20
+ .select { |child| child.is_a?(SyntaxTree::Assign) }
21
+ .each { |child| parse_assign(child) }
22
+
23
+ # Visit all call nodes
24
+ # check if a tainted variable is passed to it
25
+ nodes
26
+ .select { |child| child.is_a?(SyntaxTree::CallNode) }
27
+ .each { |child| parse_call(child) }
28
+ end
29
+
30
+ def parse_assign(node)
31
+ variable_name = node.target.value.value
32
+ # pp node.value.class
33
+ return unless node.value.is_a?(SyntaxTree::CallNode)
34
+
35
+ method_name = node.value.message.value
36
+ return unless @sources.include?(method_name&.to_sym)
37
+
38
+ State.instance.var_dependencies[variable_name.to_sym][:tainted] = true
39
+ end
40
+
41
+ def parse_call(node)
42
+ arguments = node.arguments.arguments.parts
43
+
44
+ taint_statuses =
45
+ arguments.map { |arg| [arg, taint_status(arg.value.value.to_sym)] }
46
+
47
+ method_name = node.message.value
48
+ taint_statuses.each do |status|
49
+ next unless status[1]
50
+
51
+ @result << "Method `#{method_name}()` consuming tainted variable `#{status[0].value.value}`"
52
+ end
53
+ end
54
+
55
+ def taint_status(var)
56
+ if State.instance.var_dependencies[var].key?(:tainted)
57
+ return State.instance.var_dependencies[var][:tainted]
58
+ end
59
+
60
+ tainted = false
61
+ unless State.instance.var_dependencies[var][:from].empty?
62
+ State.instance.var_dependencies[var][:from].each do |from|
63
+ tainted ||= taint_status(from)
64
+ end
65
+ end
66
+ State.instance.var_dependencies[var][:tainted] = tainted
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tainted
4
+ VERSION = "0.1.0"
5
+ end
data/lib/tainted.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "syntax_tree"
4
+
5
+ require_relative "tainted/state"
6
+ require_relative "tainted/static"
7
+ require_relative "tainted/lint"
8
+ require_relative "tainted/dataflow"
9
+ require_relative "tainted/version"
10
+
11
+ module Tainted
12
+ end
data/sig/tainted.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Tainted
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
data/tainted.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/tainted/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "tainted"
7
+ spec.version = Tainted::VERSION
8
+ spec.authors = ["Syed Faraaz Ahmad"]
9
+ spec.email = ["faraaz98@live.com"]
10
+
11
+ spec.summary = "Gem to perform taint checking on your Ruby code"
12
+ spec.homepage = "https://github.com/faraazahmad/tainted"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/faraazahmad/tainted"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(__dir__) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (File.expand_path(f) == __FILE__) ||
24
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tainted
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Syed Faraaz Ahmad
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - faraaz98@live.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CODE_OF_CONDUCT.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - fixtures/simple.rb
27
+ - lib/tainted.rb
28
+ - lib/tainted/dataflow.rb
29
+ - lib/tainted/lint.rb
30
+ - lib/tainted/state.rb
31
+ - lib/tainted/static.rb
32
+ - lib/tainted/version.rb
33
+ - sig/tainted.rbs
34
+ - tainted.gemspec
35
+ homepage: https://github.com/faraazahmad/tainted
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://github.com/faraazahmad/tainted
40
+ source_code_uri: https://github.com/faraazahmad/tainted
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.6.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.4.20
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Gem to perform taint checking on your Ruby code
60
+ test_files: []