control_check 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: 183473f0407cc5c7db0692572c8f4a74effabb1e205cd5fe819eef2c23470bba
4
+ data.tar.gz: 699d9058ab5037e8b5dcccb5a311af4e295a7358fd4c5bed3b582b4491cc8234
5
+ SHA512:
6
+ metadata.gz: b136f41c6614ffecaf86c1b52588f749d6f720cab8871b23a456be6fc0f34e6dfce6049fccd16556c15dc630e2d1126d53b489c7a791ede9af24b4cc6432fcaf
7
+ data.tar.gz: 78ca3646b4b5039bf6f2808a2505e75e132b03f59dfa79e290ba39837dcdd8bc42b9f67d939e1f5df7a18e750aa4375fef77be99883ea4ee6a819032350de8ed
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 DYB
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # control_check
2
+
3
+ Reports whether the controls a Rails codebase declares exist in its committed code.
4
+
5
+ The checker shows that a control exists in code. It does not show compliance with
6
+ any standard, and a passing report is not a certification.
7
+
8
+ It reads the source committed at `HEAD` with the Prism parser and never boots the
9
+ app. Files are listed from the commit and read from it, so an ignored file, an
10
+ untracked file or an uncommitted edit changes nothing in the report.
11
+
12
+ ## Usage
13
+
14
+ Commit a control file named `.control_check.yml` at the root of the codebase. It names
15
+ each attribute that is sensitive, under the class that declares it:
16
+
17
+ ```yaml
18
+ encryption:
19
+ User:
20
+ - ssn
21
+ - phone
22
+ Billing::Invoice:
23
+ - account_number
24
+ ```
25
+
26
+ Run the check from the root of the codebase:
27
+
28
+ ```sh
29
+ bundle exec control_check check
30
+ bundle exec control_check check --out tmp/control_check
31
+ ```
32
+
33
+ It writes `control_check_report.json` and `control_check_summary.json` to the folder it
34
+ runs in, or to the folder `--out` names, and prints the summary.
35
+
36
+ ### The encryption control
37
+
38
+ For each attribute the control file names, the report says whether it is encrypted
39
+ with Rails' `encrypts`. An `encrypts` in a module the class includes counts, including
40
+ one inside an `included do` block of a concern. Models are read from `app/` and from
41
+ each hub folder, `hubs/<name>/app/`.
42
+
43
+ Each attribute that is not encrypted is a finding naming the class, the attribute, and
44
+ the file and line where the class is defined.
45
+
46
+ ### The report
47
+
48
+ The report names the commit, the checker version and the SHA-256 digest of the control
49
+ file, and holds each control's result. Its keys are sorted and it holds no time, so two
50
+ runs on one commit with one checker version write the same bytes.
51
+
52
+ ### The summary
53
+
54
+ The summary names the commit, the checker version, each control as `passed` or
55
+ `failed`, and the SHA-256 digest of the report file.
56
+
57
+ ### Exit status
58
+
59
+ - `0` when every control passed.
60
+ - `1` when any control has a finding.
61
+ - `2` when no `.control_check.yml` is committed at `HEAD`.
62
+ - `64` when the command is not `check`.
63
+
64
+ ## Installation
65
+
66
+ ```ruby
67
+ gem "control_check", group: :development, require: false
68
+ ```
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/exe/control_check ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "control_check/cli"
5
+
6
+ exit ControlCheck::CLI.new(root: Dir.pwd, stdout: $stdout).run(ARGV)
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require_relative "committed_files"
5
+ require_relative "model_index"
6
+ require_relative "report"
7
+ require_relative "controls/encryption"
8
+
9
+ module ControlCheck
10
+ class Check
11
+ def initialize(root)
12
+ @files = CommittedFiles.new(root)
13
+ end
14
+
15
+ def report
16
+ control_file = read_control_file
17
+ sensitive = YAML.safe_load(control_file).fetch("encryption", {})
18
+ Report.new(
19
+ commit: @files.commit,
20
+ control_file: control_file,
21
+ controls: { "encryption" => Controls::Encryption.new(sensitive, model_index).result }
22
+ )
23
+ end
24
+
25
+ private
26
+
27
+ def read_control_file
28
+ path = Report::CONTROL_FILE_PATH
29
+ raise Error, "No #{path} is committed at HEAD." unless @files.paths.include?(path)
30
+
31
+ @files.read(path)
32
+ end
33
+
34
+ def model_index
35
+ paths = @files.paths.select { |path| ModelIndex.model_path?(path) }
36
+ ModelIndex.new(paths.to_h { |path| [path, @files.read(path)] })
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+ require "fileutils"
5
+ require_relative "check"
6
+ require_relative "summary"
7
+
8
+ module ControlCheck
9
+ class CLI
10
+ REPORT_FILE = "control_check_report.json"
11
+ SUMMARY_FILE = "control_check_summary.json"
12
+ USAGE = "Usage: control_check check [--out DIR]"
13
+
14
+ def initialize(root:, stdout:, stderr: $stderr)
15
+ @root = root
16
+ @stdout = stdout
17
+ @stderr = stderr
18
+ end
19
+
20
+ def run(argv)
21
+ command, *rest = argv
22
+ return usage unless command == "check"
23
+
24
+ check(rest)
25
+ end
26
+
27
+ private
28
+
29
+ def usage
30
+ @stderr.puts(USAGE)
31
+ 64
32
+ end
33
+
34
+ def check(argv)
35
+ out = options(argv).fetch(:out, @root)
36
+ report = Check.new(@root).report
37
+ write(out, report)
38
+ report.passed? ? 0 : 1
39
+ rescue Error => e
40
+ @stderr.puts(e.message)
41
+ 2
42
+ end
43
+
44
+ def write(out, report)
45
+ FileUtils.mkdir_p(out)
46
+ File.write(File.join(out, REPORT_FILE), report.to_json)
47
+ summary = Summary.new(report).to_json
48
+ File.write(File.join(out, SUMMARY_FILE), summary)
49
+ @stdout.write(summary)
50
+ end
51
+
52
+ def options(argv)
53
+ parsed = {}
54
+ OptionParser.new { |parser| parser.on("--out DIR") { |dir| parsed[:out] = dir } }.parse(argv)
55
+ parsed
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+ require_relative "error"
5
+
6
+ module ControlCheck
7
+ class CommittedFiles
8
+ def initialize(root)
9
+ @root = root
10
+ end
11
+
12
+ def commit
13
+ git("rev-parse", "HEAD").strip
14
+ end
15
+
16
+ def paths
17
+ git("ls-tree", "-r", "-z", "--name-only", "HEAD").split("\0")
18
+ end
19
+
20
+ def read(path)
21
+ git("cat-file", "blob", "HEAD:#{path}")
22
+ end
23
+
24
+ private
25
+
26
+ def git(*args)
27
+ output, status = Open3.capture2("git", "-C", @root, *args)
28
+ raise Error, "git #{args.first} failed in #{@root}" unless status.success?
29
+
30
+ output
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ControlCheck
4
+ module Controls
5
+ class Encryption
6
+ def initialize(sensitive, index)
7
+ @sensitive = sensitive
8
+ @index = index
9
+ end
10
+
11
+ def result
12
+ found = findings
13
+ { "passed" => found.empty?, "attributes" => attributes, "findings" => found }
14
+ end
15
+
16
+ private
17
+
18
+ def findings
19
+ attributes.reject { |attribute| attribute["encrypted"] }.map do |attribute|
20
+ file, line = @index.location(attribute["class"])
21
+ { "class" => attribute["class"], "attribute" => attribute["attribute"], "file" => file, "line" => line }
22
+ end
23
+ end
24
+
25
+ def attributes
26
+ @sensitive.flat_map do |class_name, names|
27
+ names.map do |name|
28
+ { "class" => class_name, "attribute" => name,
29
+ "encrypted" => @index.encrypted_attributes(class_name).include?(name) }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ControlCheck
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ module ControlCheck
6
+ class ModelIndex
7
+ def self.model_path?(path)
8
+ path.start_with?("app/") || path.match?(%r{\Ahubs/[^/]+/app/})
9
+ end
10
+
11
+ def initialize(sources)
12
+ @encrypted = Hash.new { |hash, name| hash[name] = [] }
13
+ @includes = Hash.new { |hash, name| hash[name] = [] }
14
+ @locations = {}
15
+ sources.select { |path, _| self.class.model_path?(path) }.sort.each { |path, source| read(path, source) }
16
+ end
17
+
18
+ def encrypted_attributes(class_name)
19
+ included = @includes[class_name].flat_map { |nesting, name| @encrypted[resolve(nesting, name)] }
20
+ (@encrypted[class_name] + included).uniq
21
+ end
22
+
23
+ def location(class_name)
24
+ @locations[class_name]
25
+ end
26
+
27
+ def add_encrypted(name, attributes)
28
+ @encrypted[name].concat(attributes)
29
+ end
30
+
31
+ def add_include(class_name, nesting, module_name)
32
+ @includes[class_name] << [nesting, module_name]
33
+ end
34
+
35
+ def add_location(class_name, path, line)
36
+ @locations[class_name] ||= [path, line]
37
+ end
38
+
39
+ private
40
+
41
+ def read(path, source)
42
+ Prism.parse(source).value.accept(Collector.new(self, path))
43
+ end
44
+
45
+ def resolve(nesting, name)
46
+ candidates = nesting.size.downto(0).map { |depth| (nesting.first(depth) + [name]).join("::") }
47
+ candidates.find { |candidate| @encrypted.key?(candidate) } || name
48
+ end
49
+
50
+ class Collector < Prism::Visitor
51
+ def initialize(index, path)
52
+ super()
53
+ @index = index
54
+ @path = path
55
+ @nesting = []
56
+ @classes = []
57
+ end
58
+
59
+ def visit_class_node(node)
60
+ @nesting.push(node.constant_path.full_name)
61
+ @classes.push(current_name)
62
+ @index.add_location(current_name, @path, node.location.start_line)
63
+ super
64
+ @classes.pop
65
+ @nesting.pop
66
+ end
67
+
68
+ def visit_module_node(node)
69
+ @nesting.push(node.constant_path.full_name)
70
+ super
71
+ @nesting.pop
72
+ end
73
+
74
+ def visit_call_node(node)
75
+ record_call(node) if node.receiver.nil? && @nesting.any?
76
+ super
77
+ end
78
+
79
+ private
80
+
81
+ def record_call(node)
82
+ case node.name
83
+ when :encrypts then @index.add_encrypted(current_name, symbol_arguments(node))
84
+ when :include then record_include(node)
85
+ end
86
+ end
87
+
88
+ def record_include(node)
89
+ return unless @classes.last == current_name
90
+
91
+ constant_arguments(node).each { |name| @index.add_include(current_name, @nesting.dup, name) }
92
+ end
93
+
94
+ def current_name
95
+ @nesting.join("::")
96
+ end
97
+
98
+ def symbol_arguments(node)
99
+ node.arguments&.arguments.to_a.grep(Prism::SymbolNode).map(&:unescaped)
100
+ end
101
+
102
+ def constant_arguments(node)
103
+ node.arguments&.arguments.to_a.select { |argument| constant?(argument) }.map(&:full_name)
104
+ end
105
+
106
+ def constant?(argument)
107
+ argument.is_a?(Prism::ConstantReadNode) || argument.is_a?(Prism::ConstantPathNode)
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "digest"
5
+ require_relative "version"
6
+
7
+ module ControlCheck
8
+ class Report
9
+ CONTROL_FILE_PATH = ".control_check.yml"
10
+
11
+ def initialize(commit:, control_file:, controls:)
12
+ @commit = commit
13
+ @control_file = control_file
14
+ @controls = controls
15
+ end
16
+
17
+ def to_h
18
+ {
19
+ "checker_version" => VERSION,
20
+ "commit" => @commit,
21
+ "control_file" => { "path" => CONTROL_FILE_PATH, "sha256" => Digest::SHA256.hexdigest(@control_file) },
22
+ "controls" => @controls
23
+ }
24
+ end
25
+
26
+ def passed?
27
+ @controls.values.all? { |result| result["passed"] }
28
+ end
29
+
30
+ def to_json(*)
31
+ "#{JSON.pretty_generate(sorted(to_h))}\n"
32
+ end
33
+
34
+ private
35
+
36
+ def sorted(value)
37
+ case value
38
+ when Hash then value.sort.to_h { |key, inner| [key, sorted(inner)] }
39
+ when Array then value.map { |inner| sorted(inner) }
40
+ else value
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "digest"
5
+
6
+ module ControlCheck
7
+ class Summary
8
+ def initialize(report)
9
+ @report = report
10
+ end
11
+
12
+ def to_h
13
+ report = @report.to_h
14
+ {
15
+ "checker_version" => report["checker_version"],
16
+ "commit" => report["commit"],
17
+ "controls" => report["controls"].sort.to_h { |name, result| [name, result["passed"] ? "passed" : "failed"] },
18
+ "report_sha256" => Digest::SHA256.hexdigest(@report.to_json)
19
+ }
20
+ end
21
+
22
+ def to_json(*)
23
+ "#{JSON.pretty_generate(to_h)}\n"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ControlCheck
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "control_check/version"
4
+ require_relative "control_check/error"
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: control_check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tylercschneider
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2026-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: prism
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: control_check reads a Rails codebase's committed source at one commit,
28
+ checks each control its control file names, and writes a JSON report and a summary
29
+ carrying the report's digest.
30
+ email:
31
+ - tylercschneider@gmail.com
32
+ executables:
33
+ - control_check
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - LICENSE
38
+ - README.md
39
+ - exe/control_check
40
+ - lib/control_check.rb
41
+ - lib/control_check/check.rb
42
+ - lib/control_check/cli.rb
43
+ - lib/control_check/committed_files.rb
44
+ - lib/control_check/controls/encryption.rb
45
+ - lib/control_check/error.rb
46
+ - lib/control_check/model_index.rb
47
+ - lib/control_check/report.rb
48
+ - lib/control_check/summary.rb
49
+ - lib/control_check/version.rb
50
+ homepage: https://github.com/DYB-Development/control_check
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ allowed_push_host: https://rubygems.org
55
+ homepage_uri: https://github.com/DYB-Development/control_check
56
+ source_code_uri: https://github.com/DYB-Development/control_check
57
+ rubygems_mfa_required: 'true'
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.2.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.5.16
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Reports whether the controls a Rails codebase declares exist in its committed
77
+ code.
78
+ test_files: []