cheap_coder 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 23428dc000cbf99177eaae110a76c95afa23915ed9d78bbbf323184001d5a171
4
+ data.tar.gz: 1533efcc940e9c6eacf517760d69b0e2df782fc173175b00dfb4c1fd4d6410ac
5
+ SHA512:
6
+ metadata.gz: 9ea49f36d027d9a720dab4785f75098cc0ee567ce4ee2c0b9efb69989f7430f97d42515ce5a96edd46ccc2c0289ff425949677d6e751afeb99575f44e4f698cb
7
+ data.tar.gz: 395594efeebfabb96fa21e46c0883970a62c25be665277a5ba3e896d58934f78c0248d20c91c2ab5bf1bc59c4ba41afb1abae94f6c81c252d859f33a9913a57a
data/.env.sample ADDED
@@ -0,0 +1 @@
1
+ CHEAP_CODER_LOGGER_VERBOSE=false
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 3.4
4
+ SuggestExtensions: false
5
+
6
+ Layout/LineLength:
7
+ Exclude:
8
+ - '*.gemspec'
9
+
10
+ Style/Documentation:
11
+ Enabled: false
12
+
13
+ Style/TrailingCommaInArrayLiteral:
14
+ EnforcedStyleForMultiline: diff_comma
15
+
16
+ Style/TrailingCommaInHashLiteral:
17
+ EnforcedStyleForMultiline: diff_comma
18
+
19
+ Metrics/BlockLength:
20
+ Exclude:
21
+ - '*.gemspec'
22
+ - 'spec/**/*'
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.4.5
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-08-03
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Daiji Tsutsui
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,45 @@
1
+ # CheapCoder
2
+
3
+ The **CheapCoder** gem can censor Ruby scripts.
4
+
5
+ This allows only non-structed scripts which do not contain any shell command.
6
+
7
+ ## Usage
8
+
9
+ The sample code below provides a script [samples/sample1.rb](https://github.com/daiji-tsutsui/cheap_coder/blob/develop/samples/sample1.rb) rejected any built-in methods, *e.g.* `puts` and `==` (received by a constant).
10
+ ```ruby
11
+ require 'cheap_coder'
12
+ require 'parser/current'
13
+
14
+ CODEPATH = 'samples/sample1.rb'
15
+
16
+ expr = Parser::CurrentRuby.parse(File.read(CODEPATH))
17
+ censor = CheapCoder::Censor.new
18
+ expr = censor.process(expr)
19
+ puts Unparser.unparse(expr)
20
+ ```
21
+
22
+ The censor rejects also any shell commands.
23
+ For example, please see the censord result of [samples/sample2.rb](https://github.com/daiji-tsutsui/cheap_coder/blob/develop/samples/sample1.rb).
24
+
25
+ ### Whitelist
26
+ One can set a whitelist of methods.
27
+ The sample below provides a script admits the build-in `puts`.
28
+ ```ruby
29
+ method_whitelist = %i[puts]
30
+ censor = CheapCoder::Censor.new(
31
+ allowed_methods: method_whitelist,
32
+ )
33
+ expr = censor.process(expr)
34
+ ```
35
+
36
+ ### Evaluator
37
+ One can set an evaluator to a censor instance.
38
+ For example, the below evaluates the [ABC size metric](https://wiki.c2.com/?AbcMetric) (not strictly).
39
+ ```ruby
40
+ censor = CheapCoder::Censor.new(
41
+ evaluator: CheapCoder::AbcEvaluator.new
42
+ )
43
+ censor.process(expr)
44
+ censor.score # gives ABC size
45
+ ```
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,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheapCoder
4
+ class AbcEvaluator
5
+ def initialize
6
+ @detectors = {
7
+ A: NodeDetector::Assignment.new,
8
+ B: NodeDetector::Branch.new,
9
+ C: NodeDetector::Condition.new,
10
+ }
11
+ end
12
+
13
+ def score
14
+ @detectors.transform_values(&:score)
15
+ end
16
+
17
+ def check(node)
18
+ @detectors.each_value do |detector|
19
+ detector.check(node)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheapCoder
4
+ class Authorizer
5
+ include AST::Processor::Mixin
6
+
7
+ def initialize(allowed_methods)
8
+ @allowed_methods = allowed_methods || []
9
+ end
10
+
11
+ def allow?(node)
12
+ raise ArgumentError, "not SEND node: #{node}" unless node.type == :send
13
+
14
+ receiver, method = node.to_a
15
+ return false unless receiver.nil?
16
+
17
+ @allowed_methods.include?(method)
18
+ end
19
+
20
+ def on_def(node)
21
+ print_debug('DEF', node)
22
+ @allowed_methods.push(node.to_a[0])
23
+ default_handler(node)
24
+ end
25
+
26
+ def handler_missing(node)
27
+ default_handler(node)
28
+ end
29
+
30
+ private
31
+
32
+ def default_handler(node)
33
+ node.children.each do |child|
34
+ next child unless child.is_a?(AST::Node)
35
+
36
+ process(child)
37
+ end
38
+ end
39
+
40
+ def print_debug(msg, node)
41
+ return unless ENV['DEBUG'] == 'true'
42
+
43
+ puts "[#{msg}] #{Unparser.unparse(node)}"
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheapCoder
4
+ class Censor
5
+ include AST::Processor::Mixin
6
+
7
+ def initialize(**option)
8
+ @authorizer = Authorizer.new(option[:allowed_methods])
9
+ @evaluator = option[:evaluator]
10
+ end
11
+
12
+ def score
13
+ @evaluator&.score
14
+ end
15
+
16
+ def process(expr)
17
+ @authorizer.process(expr)
18
+ super
19
+ end
20
+
21
+ def on_xstr(node)
22
+ print_debug('!XSTR', node)
23
+ node.to_a[0]
24
+ end
25
+
26
+ def on_send(node)
27
+ return node if @authorizer.allow?(node)
28
+
29
+ print_debug('!SEND', node)
30
+ Parser::AST::Node.new(:nil, [])
31
+ end
32
+
33
+ def handler_missing(node)
34
+ default_handler(node)
35
+ end
36
+
37
+ private
38
+
39
+ def default_handler(node)
40
+ @evaluator&.check(node)
41
+
42
+ type = node.type
43
+ children = node.children.map do |child|
44
+ next child unless child.is_a?(AST::Node)
45
+
46
+ process(child)
47
+ end
48
+ Parser::AST::Node.new(type, children)
49
+ end
50
+
51
+ def print_debug(msg, node)
52
+ return unless ENV['CHEAP_CODER_LOGGER_VERBOSE'] == 'true'
53
+
54
+ puts "[#{msg}] #{Unparser.unparse(node)}"
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheapCoder
4
+ module NodeDetector
5
+ class Assignment < Base
6
+ def detected?(node)
7
+ node.type.to_s.end_with?('asgn')
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheapCoder
4
+ module NodeDetector
5
+ class Base
6
+ attr_reader :score
7
+
8
+ def initialize
9
+ @score = 0
10
+ end
11
+
12
+ def check(node)
13
+ return unless detected?(node)
14
+
15
+ print_debug(node)
16
+ @score += 1
17
+ end
18
+
19
+ def print_debug(node)
20
+ return unless ENV['CHEAP_CODER_LOGGER_VERBOSE'] == 'true'
21
+
22
+ class_type = self.class.name.split('::').last
23
+ puts "[#{class_type}] #{Unparser.unparse(node)}"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheapCoder
4
+ module NodeDetector
5
+ class Branch < Base
6
+ BRANCH_NODES = %i[send csend yield].freeze
7
+ private_constant :BRANCH_NODES
8
+
9
+ def detected?(node)
10
+ BRANCH_NODES.include?(node.type)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheapCoder
4
+ module NodeDetector
5
+ class Condition < Base
6
+ CONDITION_NODES = %i[if while until for csend block block_pass
7
+ rescue when in_pattern and or or_asgn and_asgn].freeze
8
+ private_constant :CONDITION_NODES
9
+
10
+ def detected?(node)
11
+ CONDITION_NODES.include?(node.type)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheapCoder
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'parser/current'
4
+ require 'unparser'
5
+
6
+ module CheapCoder
7
+ require_relative 'cheap_coder/version'
8
+ require_relative 'cheap_coder/abc_evaluator'
9
+ require_relative 'cheap_coder/authorizer'
10
+ require_relative 'cheap_coder/censor'
11
+
12
+ module NodeDetector
13
+ require_relative 'cheap_coder/node_detector/base'
14
+ require_relative 'cheap_coder/node_detector/assignment'
15
+ require_relative 'cheap_coder/node_detector/branch'
16
+ require_relative 'cheap_coder/node_detector/condition'
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ FLAG = :B
4
+
5
+ # This is a test code for parsing ruby syntax
6
+ def main
7
+ test_print(name)
8
+ end
9
+
10
+ def name
11
+ if FLAG == :A
12
+ 'Adam'
13
+ else
14
+ 'Bob'
15
+ end
16
+ end
17
+
18
+ def test_print(name)
19
+ str = "Hello, #{name}!!"
20
+ puts str
21
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ def main
4
+ shell_result = `ls`
5
+ puts shell_result
6
+
7
+ shell_result2 = %x(pwd) # rubocop:disable Style/CommandLiteral
8
+ puts shell_result2
9
+
10
+ system_result = system('cd ~')
11
+ puts system_result
12
+
13
+ exec_result = exec('ps')
14
+ puts exec_result
15
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cheap_coder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daiji Tsutsui
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: dotenv
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '3.1'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '3.1'
26
+ - !ruby/object:Gem::Dependency
27
+ name: parser
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.3'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.3'
40
+ - !ruby/object:Gem::Dependency
41
+ name: unparser
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.8'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.8'
54
+ description: This gem can censor Ruby scripts and only allows non-structed scripts
55
+ which do not contain any system command.
56
+ email:
57
+ - daiji.tsutsui417@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".env.sample"
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - ".ruby-version"
66
+ - CHANGELOG.md
67
+ - CODE_OF_CONDUCT.md
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - lib/cheap_coder.rb
72
+ - lib/cheap_coder/abc_evaluator.rb
73
+ - lib/cheap_coder/authorizer.rb
74
+ - lib/cheap_coder/censor.rb
75
+ - lib/cheap_coder/node_detector/assignment.rb
76
+ - lib/cheap_coder/node_detector/base.rb
77
+ - lib/cheap_coder/node_detector/branch.rb
78
+ - lib/cheap_coder/node_detector/condition.rb
79
+ - lib/cheap_coder/version.rb
80
+ - samples/sample1.rb
81
+ - samples/sample2.rb
82
+ homepage: https://github.com/daiji-tsutsui/cheap_coder
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ allowed_push_host: https://rubygems.org
87
+ homepage_uri: https://github.com/daiji-tsutsui/cheap_coder
88
+ source_code_uri: https://github.com/daiji-tsutsui/cheap_coder
89
+ changelog_uri: https://github.com/daiji-tsutsui/cheap_coder/blob/master/CHANGELOG.md
90
+ rubygems_mfa_required: 'true'
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 3.4.0
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.6.9
106
+ specification_version: 4
107
+ summary: A Ruby script censor who prefers very simple scripts.
108
+ test_files: []