saboteur 0.8.0 → 0.9.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.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +215 -0
  3. data/README.md +44 -6
  4. data/exe/saboteur +6 -2
  5. data/lib/saboteur/attribute_finder.rb +116 -0
  6. data/lib/saboteur/baseline/run_evidence.rb +2 -1
  7. data/lib/saboteur/cli/agent_provenance.rb +59 -0
  8. data/lib/saboteur/cli/dirty_tree_notice.rb +28 -0
  9. data/lib/saboteur/cli/doctor_command.rb +41 -0
  10. data/lib/saboteur/cli/error_cause.rb +40 -0
  11. data/lib/saboteur/cli/execution_options.rb +46 -0
  12. data/lib/saboteur/cli/exit_status.rb +12 -14
  13. data/lib/saboteur/cli/explicit_file_options.rb +21 -0
  14. data/lib/saboteur/cli/explicit_sources.rb +46 -0
  15. data/lib/saboteur/cli/failure_output.rb +35 -0
  16. data/lib/saboteur/cli/help_command.rb +23 -0
  17. data/lib/saboteur/cli/matrix_option.rb +17 -0
  18. data/lib/saboteur/cli/output_options.rb +27 -0
  19. data/lib/saboteur/cli/presets.rb +41 -1
  20. data/lib/saboteur/cli/preview_command.rb +26 -0
  21. data/lib/saboteur/cli/run_budget_options.rb +6 -1
  22. data/lib/saboteur/cli/run_command.rb +33 -0
  23. data/lib/saboteur/cli/run_output.rb +38 -0
  24. data/lib/saboteur/cli/run_reporting.rb +6 -5
  25. data/lib/saboteur/cli/run_validation.rb +54 -0
  26. data/lib/saboteur/cli/source_set.rb +0 -18
  27. data/lib/saboteur/cli.rb +29 -305
  28. data/lib/saboteur/config.rb +76 -7
  29. data/lib/saboteur/config_file.rb +12 -8
  30. data/lib/saboteur/constant_subject_finder.rb +3 -0
  31. data/lib/saboteur/custom_map.rb +24 -5
  32. data/lib/saboteur/deep_source.rb +49 -0
  33. data/lib/saboteur/dirty_tree_warning.rb +6 -4
  34. data/lib/saboteur/doctor.rb +10 -0
  35. data/lib/saboteur/engine/baseline_guard.rb +118 -15
  36. data/lib/saboteur/engine/baseline_parts.rb +51 -0
  37. data/lib/saboteur/engine/boot.rb +11 -3
  38. data/lib/saboteur/engine/bytecode_equivalence.rb +17 -1
  39. data/lib/saboteur/engine/candidate_selection.rb +16 -5
  40. data/lib/saboteur/engine/candidates.rb +4 -1
  41. data/lib/saboteur/engine/coverage_inputs_snapshot.rb +146 -0
  42. data/lib/saboteur/engine/coverage_miss_note.rb +86 -0
  43. data/lib/saboteur/engine/coverage_partial.rb +48 -0
  44. data/lib/saboteur/engine/coverage_parts.rb +50 -0
  45. data/lib/saboteur/engine/coverage_shadow.rb +92 -0
  46. data/lib/saboteur/engine/coverage_store.rb +296 -21
  47. data/lib/saboteur/engine/diagnostics.rb +24 -7
  48. data/lib/saboteur/engine/distribution.rb +2 -2
  49. data/lib/saboteur/engine/gated_run.rb +4 -1
  50. data/lib/saboteur/engine/load_coverage.rb +72 -8
  51. data/lib/saboteur/engine/partial_invalidation.rb +115 -0
  52. data/lib/saboteur/engine/partial_merge.rb +40 -0
  53. data/lib/saboteur/engine/pipelines.rb +11 -3
  54. data/lib/saboteur/engine/reference_specs.rb +71 -0
  55. data/lib/saboteur/engine/result_cache.rb +68 -5
  56. data/lib/saboteur/engine/resume_manifest.rb +110 -0
  57. data/lib/saboteur/engine/run_budget.rb +10 -7
  58. data/lib/saboteur/engine/run_fingerprint.rb +2 -1
  59. data/lib/saboteur/engine/sandbox_policy.rb +26 -5
  60. data/lib/saboteur/engine/scheduler.rb +33 -25
  61. data/lib/saboteur/engine/selection_announcement.rb +9 -3
  62. data/lib/saboteur/engine/shadow_comparison.rb +121 -0
  63. data/lib/saboteur/engine/shadow_run.rb +38 -0
  64. data/lib/saboteur/engine/subject_selection.rb +144 -64
  65. data/lib/saboteur/engine/suite_partition.rb +22 -0
  66. data/lib/saboteur/engine/test_guess.rb +186 -32
  67. data/lib/saboteur/engine/unchanged_syntax.rb +10 -2
  68. data/lib/saboteur/engine.rb +44 -11
  69. data/lib/saboteur/environment_hints.rb +55 -0
  70. data/lib/saboteur/execution/bundle_gemfile_pin.rb +72 -0
  71. data/lib/saboteur/execution/external_runner.rb +123 -1
  72. data/lib/saboteur/execution/fiddle_loader.rb +34 -0
  73. data/lib/saboteur/execution/gemfile_path_sources.rb +166 -0
  74. data/lib/saboteur/execution/marker_sweep.rb +2 -1
  75. data/lib/saboteur/execution/process_table.rb +7 -3
  76. data/lib/saboteur/execution/reevaluator.rb +7 -0
  77. data/lib/saboteur/execution/sandbox.rb +39 -20
  78. data/lib/saboteur/execution/seatbelt.rb +10 -3
  79. data/lib/saboteur/execution/supervisor.rb +42 -8
  80. data/lib/saboteur/execution/supervisor_batch.rb +105 -0
  81. data/lib/saboteur/execution/test_command_shape.rb +149 -0
  82. data/lib/saboteur/execution/worker_pool.rb +48 -4
  83. data/lib/saboteur/frameworks/rails.rb +2 -5
  84. data/lib/saboteur/frameworks/sqlite_memory_database.rb +32 -2
  85. data/lib/saboteur/gates.rb +121 -28
  86. data/lib/saboteur/install.rb +6 -1
  87. data/lib/saboteur/integration/base.rb +19 -2
  88. data/lib/saboteur/integration/rspec.rb +145 -18
  89. data/lib/saboteur/line_hits.rb +81 -0
  90. data/lib/saboteur/method_calls.rb +91 -0
  91. data/lib/saboteur/mutation_source/argument_swap.rb +54 -0
  92. data/lib/saboteur/mutation_source/builtin.rb +33 -21
  93. data/lib/saboteur/mutation_source/condition_negation.rb +3 -12
  94. data/lib/saboteur/mutation_source/hazard_guard.rb +94 -13
  95. data/lib/saboteur/mutation_source/jump_value.rb +59 -0
  96. data/lib/saboteur/mutation_source/literal_replacement.rb +6 -23
  97. data/lib/saboteur/mutation_source/operator_examples.rb +6 -0
  98. data/lib/saboteur/mutation_source/operator_registry.rb +6 -0
  99. data/lib/saboteur/mutation_source/operator_visit.rb +7 -1
  100. data/lib/saboteur/mutation_source/rescue_removal.rb +69 -0
  101. data/lib/saboteur/mutation_source/unary_removal.rb +47 -0
  102. data/lib/saboteur/mutation_source/when_removal.rb +51 -0
  103. data/lib/saboteur/preview.rb +13 -0
  104. data/lib/saboteur/refinement_activations.rb +22 -10
  105. data/lib/saboteur/reporter/agent.rb +2 -2
  106. data/lib/saboteur/reporter/agent_incomplete.rb +31 -5
  107. data/lib/saboteur/reporter/agent_summary.rb +16 -10
  108. data/lib/saboteur/reporter/review_comments.rb +151 -0
  109. data/lib/saboteur/reporter/safe_out.rb +32 -0
  110. data/lib/saboteur/reporter/stryker.rb +8 -6
  111. data/lib/saboteur/reporter/survivor_classification.rb +41 -3
  112. data/lib/saboteur/reporter/survivor_hints.rb +5 -0
  113. data/lib/saboteur/reporter/terminal.rb +45 -5
  114. data/lib/saboteur/result.rb +14 -4
  115. data/lib/saboteur/selection/coverage_map.rb +150 -9
  116. data/lib/saboteur/selection/external_test_map.rb +4 -0
  117. data/lib/saboteur/selection/git_diff.rb +24 -2
  118. data/lib/saboteur/selection/layout_only_lines.rb +45 -1
  119. data/lib/saboteur/selection/recorder.rb +52 -22
  120. data/lib/saboteur/selection/trace_recorder.rb +83 -0
  121. data/lib/saboteur/selection/untracked_changes.rb +1 -2
  122. data/lib/saboteur/subject.rb +16 -0
  123. data/lib/saboteur/subject_finder.rb +91 -6
  124. data/lib/saboteur/text.rb +31 -0
  125. data/lib/saboteur/version.rb +1 -1
  126. data/lib/saboteur/working_tree.rb +7 -2
  127. data/lib/saboteur.rb +9 -0
  128. metadata +48 -16
@@ -0,0 +1,91 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ module Saboteur
5
+ # Records which Ruby methods were entered, by [first line, name] per file,
6
+ # with a TracePoint on :call. Coverage's methods report does the same job
7
+ # but its read walks every method in the process (about 8 ms per read once
8
+ # an app is loaded), and the recorder reads twice per example. :call
9
+ # events are rare next to :line events, and an endless def fires one too
10
+ # (#107). Pass `files` (canonical paths) to keep only those; nil keeps all.
11
+ class MethodCalls
12
+ # The tracer the executable arms before the app loads; the engine reads
13
+ # it once after boot and disables it.
14
+ def self.boot
15
+ @boot ||= new.enable
16
+ end
17
+
18
+ def self.booted
19
+ @boot
20
+ end
21
+
22
+ def initialize(files = nil, canonical: ->(path) { path })
23
+ @wanted = files && Hash.new { |hash, path| hash[path] = wanted?(files, canonical, path) }
24
+ @hits = new_hits
25
+ @trace = TracePoint.new(:call, &method(:record))
26
+ end
27
+
28
+ def enable
29
+ @trace.enable unless @trace.enabled?
30
+ self
31
+ end
32
+
33
+ def disable
34
+ @trace.disable if @trace.enabled?
35
+ self
36
+ end
37
+
38
+ # Stops recording without touching the TracePoint: toggling it costs
39
+ # milliseconds once an app is loaded, so the recorder pauses per read.
40
+ def pause
41
+ @paused = true
42
+ self
43
+ end
44
+
45
+ def resume
46
+ @paused = false
47
+ self
48
+ end
49
+
50
+ # file => Set of [line, name] since the last drain. A failure inside the
51
+ # tracer is raised here: raising inside the TracePoint would unwind the
52
+ # program being traced (and kill a forked child outright).
53
+ def drain(clear: true)
54
+ failure = @failure
55
+ @failure = nil
56
+ raise failure if failure
57
+
58
+ hits = @hits
59
+ @hits = new_hits if clear
60
+ hits
61
+ end
62
+
63
+ private
64
+
65
+ def new_hits
66
+ Hash.new { |hash, file| hash[file] = Set.new }
67
+ end
68
+
69
+ # A path the canonicalizer refuses (a NUL byte in an eval'd file name
70
+ # raises ArgumentError) is not a wanted file.
71
+ def wanted?(files, canonical, path)
72
+ files.include?(canonical.call(path))
73
+ rescue ArgumentError
74
+ false
75
+ end
76
+
77
+ def record(event)
78
+ # self.equal?(...) never sends a message to the traced receiver, which
79
+ # may be a BasicObject proxy that answers every message (ActiveSupport's
80
+ # deprecation proxies do).
81
+ return if @paused || equal?(event.self) || event.event != :call
82
+
83
+ path = event.path
84
+ return if @wanted && !@wanted[path]
85
+
86
+ @hits[path] << [event.lineno, event.method_id]
87
+ rescue StandardError => e
88
+ @failure ||= e
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,54 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "base"
5
+ require_relative "operator_visit"
6
+
7
+ module Saboteur
8
+ module MutationSource
9
+ # Swap two explicit positional arguments without moving their separators.
10
+ # Order-insensitive calls (includes, attr_reader, validates) can be equivalent; arid patterns can exclude them.
11
+ class ArgumentSwap < Base
12
+ include OperatorVisit
13
+
14
+ EXCLUDED = [Prism::KeywordHashNode, Prism::SplatNode, Prism::ForwardingArgumentsNode].freeze
15
+
16
+ def initialize(arid: []) # rubocop:disable Lint/MissingSuper
17
+ @arid = arid.map { |pattern| Regexp.new(pattern) }
18
+ end
19
+
20
+ def name
21
+ "argument_swap"
22
+ end
23
+
24
+ private
25
+
26
+ def mutate_node(node, subject, found)
27
+ return unless node.is_a?(Prism::CallNode) && node.opening_loc&.slice == "("
28
+
29
+ arguments = node.arguments&.arguments
30
+ return unless arguments&.size == 2
31
+ return if arguments.any? { |argument| excluded?(argument) }
32
+
33
+ left, right = arguments
34
+ return if left.slice == right.slice
35
+
36
+ # Prism forwards location offsets on nodes; removing .location here is equivalent.
37
+ edits = [[left, right], [right, left]].map do |original, replacement|
38
+ Edit.new(file: subject.file, start_byte: original.location.start_offset,
39
+ end_byte: original.location.end_offset, replacement: replacement.slice)
40
+ end
41
+ found << Mutation.new(subject_id: subject.id, operator: name, edits: edits, source: name)
42
+ end
43
+
44
+ def excluded?(argument)
45
+ EXCLUDED.any? { |type| argument.is_a?(type) } || heredoc?(argument)
46
+ end
47
+
48
+ def heredoc?(node)
49
+ (node.respond_to?(:opening_loc) && node.opening_loc&.slice&.start_with?("<<")) ||
50
+ node.compact_child_nodes.any? { |child| heredoc?(child) }
51
+ end
52
+ end
53
+ end
54
+ end
@@ -6,6 +6,7 @@ require_relative "base"
6
6
  require_relative "filter_replacement"
7
7
  require_relative "literal_replacement"
8
8
  require_relative "argument_removal"
9
+ require_relative "argument_swap"
9
10
  require_relative "chain_removal"
10
11
  require_relative "pair_removal"
11
12
  require_relative "element_removal"
@@ -17,7 +18,11 @@ require_relative "condition_negation"
17
18
  require_relative "method_swap"
18
19
  require_relative "operand_replacement"
19
20
  require_relative "interpolation_removal"
21
+ require_relative "jump_value"
20
22
  require_relative "regex_replacement"
23
+ require_relative "rescue_removal"
24
+ require_relative "when_removal"
25
+ require_relative "unary_removal"
21
26
 
22
27
  module Saboteur
23
28
  module MutationSource
@@ -27,6 +32,11 @@ module Saboteur
27
32
  DEFAULT_ARID = [
28
33
  '\A(puts|pp|p|print|warn)\b',
29
34
  '\A(@|self\.|::)?(Rails\.)?logger\b',
35
+ # A logger at the end of a receiver chain: Sentry.sdk_logger.warn
36
+ # (#665). The match is case-sensitive, so SDK_LOGGER stays
37
+ # ordinary. The segment must END in logger, so
38
+ # @loggers.each and catalogger.warnings still mutate.
39
+ '\A(\w+\.)*\w*logger\.(debug|info|warn|error|fatal)\b',
30
40
  '\A(::)?(StatsD|Statsd|Datadog|NewRelic)\b',
31
41
  '\.instrument\b'
32
42
  ].freeze
@@ -53,29 +63,30 @@ module Saboteur
53
63
  Prism::ArrayNode, Prism::HashNode, Prism::SymbolNode].freeze
54
64
  OR_WRITE = [Prism::LocalVariableOrWriteNode, Prism::InstanceVariableOrWriteNode,
55
65
  Prism::ClassVariableOrWriteNode, Prism::GlobalVariableOrWriteNode,
56
- Prism::ConstantOrWriteNode, Prism::ConstantPathOrWriteNode,
57
66
  Prism::CallOrWriteNode, Prism::IndexOrWriteNode].freeze
67
+ AND_WRITE = [Prism::LocalVariableAndWriteNode, Prism::InstanceVariableAndWriteNode,
68
+ Prism::ClassVariableAndWriteNode, Prism::GlobalVariableAndWriteNode,
69
+ Prism::CallAndWriteNode, Prism::IndexAndWriteNode].freeze
70
+ # Inside a def, X = a is a syntax error (dynamic constant assignment), so
71
+ # constant targets only get the mutant for a define_method body, which is a block.
72
+ CONSTANT_WRITE = { Prism::ConstantOrWriteNode => "or_assign", Prism::ConstantPathOrWriteNode => "or_assign",
73
+ Prism::ConstantAndWriteNode => "and_assign",
74
+ Prism::ConstantPathAndWriteNode => "and_assign" }.freeze
58
75
  CATCH_ALL = %w[StandardError ::StandardError Exception ::Exception].freeze
59
76
  SCOPE_NODES = [Prism::DefNode, Prism::ClassNode, Prism::ModuleNode, Prism::SingletonClassNode].freeze
77
+ # The single-purpose sources, in the order they run after the shared walk.
78
+ DELEGATED = [
79
+ FilterReplacement, LiteralReplacement, ArgumentRemoval, PairRemoval, ElementRemoval, ChainRemoval,
80
+ EndpointReplacement, RangeReplacement, OperatorAssignment, ConditionNegation, MethodSwap,
81
+ OperandReplacement, InterpolationRemoval, RegexReplacement, ArgumentSwap, JumpValue, RescueRemoval,
82
+ WhenRemoval, UnaryRemoval
83
+ ].freeze
60
84
 
61
85
  def initialize(arid: DEFAULT_ARID, operators: [], exclude_operators: [])
62
86
  super()
63
87
  @operators = OperatorRegistry.select(operators, exclude_operators)
64
88
  @arid = arid.map { |pattern| pattern.is_a?(Regexp) ? pattern : Regexp.new(pattern) }
65
- @filter_replacement = FilterReplacement.new(arid: arid)
66
- @literal_replacement = LiteralReplacement.new(arid: arid)
67
- @argument_removal = ArgumentRemoval.new(arid: arid)
68
- @pair_removal = PairRemoval.new(arid: arid)
69
- @element_removal = ElementRemoval.new(arid: arid)
70
- @chain_removal = ChainRemoval.new(arid: arid)
71
- @endpoint_replacement = EndpointReplacement.new(arid: arid)
72
- @range_replacement = RangeReplacement.new(arid: arid)
73
- @operator_assignment = OperatorAssignment.new(arid: arid)
74
- @condition_negation = ConditionNegation.new(arid: arid)
75
- @method_swap = MethodSwap.new(arid: arid)
76
- @operand_replacement = OperandReplacement.new(arid: arid)
77
- @interpolation_removal = InterpolationRemoval.new(arid: arid)
78
- @regex_replacement = RegexReplacement.new(arid: arid)
89
+ @delegated = DELEGATED.map { |source| source.new(arid: arid) }
79
90
  end
80
91
 
81
92
  def name
@@ -96,12 +107,7 @@ module Saboteur
96
107
 
97
108
  # The single-purpose sources run after the shared walk, in registration
98
109
  # order; each filters its own operator through the same policy below.
99
- def delegated
100
- [@filter_replacement, @literal_replacement, @argument_removal, @pair_removal, @element_removal,
101
- @chain_removal,
102
- @endpoint_replacement, @range_replacement, @operator_assignment, @condition_negation,
103
- @method_swap, @operand_replacement, @interpolation_removal, @regex_replacement]
104
- end
110
+ attr_reader :delegated
105
111
 
106
112
  private
107
113
 
@@ -178,9 +184,15 @@ module Saboteur
178
184
  when Prism::UntilNode then mutate_condition(node.predicate, hang_on: "false")
179
185
  when Prism::IntegerNode, Prism::FloatNode then mutate_number(node)
180
186
  when *OR_WRITE then add("or_assign", node.operator_loc, "=")
187
+ when *AND_WRITE then add("and_assign", node.operator_loc, "=")
188
+ when *CONSTANT_WRITE.keys then mutate_constant_write(node)
181
189
  end
182
190
  end
183
191
 
192
+ def mutate_constant_write(node)
193
+ add(CONSTANT_WRITE.fetch(node.class), node.operator_loc, "=") if @subject.node.is_a?(Prism::CallNode)
194
+ end
195
+
184
196
  def mutate_call(node)
185
197
  if binary?(node)
186
198
  COMPARISON.fetch(node.name, []).each do |replacement|
@@ -2,12 +2,13 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative "base"
5
+ require_relative "operator_visit"
5
6
 
6
7
  module Saboteur
7
8
  module MutationSource
8
9
  # Negates the predicate, retaining its evaluation and branch structure.
9
10
  class ConditionNegation < Base
10
- SCOPE_NODES = [Prism::DefNode, Prism::ClassNode, Prism::ModuleNode, Prism::SingletonClassNode].freeze
11
+ include OperatorVisit
11
12
 
12
13
  def initialize(arid: []) # rubocop:disable Lint/MissingSuper
13
14
  @arid = arid.map { |pattern| Regexp.new(pattern) }
@@ -17,20 +18,10 @@ module Saboteur
17
18
  "condition_negation"
18
19
  end
19
20
 
20
- def mutations_for(subject)
21
- found = []
22
- visit(subject.body_node, subject, found)
23
- found
24
- end
25
-
26
21
  private
27
22
 
28
- def visit(node, subject, found)
29
- return if node.nil? || SCOPE_NODES.any? { |scope| node.is_a?(scope) }
30
- return if @arid.any? { |pattern| pattern.match?(node.slice) }
31
-
23
+ def mutate_node(node, subject, found)
32
24
  negate(node.predicate, subject, found) if node.is_a?(Prism::IfNode) || node.is_a?(Prism::UnlessNode)
33
- node.compact_child_nodes.each { |child| visit(child, subject, found) }
34
25
  end
35
26
 
36
27
  def negate(predicate, subject, found)
@@ -52,10 +52,11 @@ module Saboteur
52
52
  stack = [root]
53
53
  until stack.empty?
54
54
  node = stack.pop
55
- next if node.is_a?(Prism::DefNode)
55
+ # def and lambda bodies run when called, not at load.
56
+ next if node.is_a?(Prism::DefNode) || node.is_a?(Prism::LambdaNode)
56
57
 
57
58
  if hazardous?(node)
58
- slices << node.slice
59
+ slices << cite(node)
59
60
  next
60
61
  end
61
62
  stack.concat(node.compact_child_nodes)
@@ -68,6 +69,10 @@ module Saboteur
68
69
  # require_relative(begin ... end) runs arbitrary code on every
69
70
  # replay, so it is unvetted below like any other helper call.
70
71
  SAFE_LOAD_REQUIRES = %w[require require_relative].freeze
72
+ VISIBILITY_NAMES = %w[public private protected].freeze
73
+ CONSTANT_VISIBILITY_NAMES = %w[private_constant public_constant].freeze
74
+ # `X = Struct.new(:a)` / `Point = Data.define(:x)` without a block.
75
+ FACTORY_NEW = { "Struct" => :new, "Data" => :define }.freeze
71
76
 
72
77
  # Assignments whose value a replay re-runs: vetted when the value is
73
78
  # a plain literal construction.
@@ -81,11 +86,19 @@ module Saboteur
81
86
  # at load runs again on every mutant, outside the method hazard guard.
82
87
  # The vetted forms are narrow: require/require_relative, pure defs
83
88
  # (a body runs when called, not at load), class/module nesting with a
84
- # constant superclass, and constant or local writes of plain literal
85
- # values (a `.freeze` on the literal is looked through). Everything
86
- # else - helper calls, registrations, writes to disk, class factories,
87
- # top-level blocks - is named here, NOT proven unsafe: the check only
88
- # says saboteur cannot account for what re-running it does (#319).
89
+ # constant superclass, a `class << self` block of the same vetted
90
+ # forms, bare visibility calls (public/private/protected with no
91
+ # argument, literal symbols, or a def - they only re-flag methods),
92
+ # constant-visibility calls with literal names, Struct/Data class
93
+ # factories with literal arguments and no block, and constant or
94
+ # local writes of plain literal values (a `.freeze` on the literal is
95
+ # looked through; a lambda literal is data - its body runs when
96
+ # called, not at load). Everything else - helper calls,
97
+ # registrations, writes to disk, class factories, top-level blocks -
98
+ # is named here, NOT proven unsafe: the check only says saboteur
99
+ # cannot account for what re-running it does (#319). Each entry is a
100
+ # one-line citation: the statement's first line cut to 60 characters,
101
+ # with its line number (#664).
89
102
  def unvetted_load_code(root)
90
103
  bad = []
91
104
  vet_statements(root.statements.body, bad)
@@ -112,21 +125,88 @@ module Saboteur
112
125
  nil
113
126
  when Prism::ClassNode, Prism::ModuleNode
114
127
  vet_scope(node, bad)
128
+ when Prism::SingletonClassNode
129
+ if vetted_singleton_target?(node.expression)
130
+ vet_scope(node, bad)
131
+ else
132
+ bad << cite(node)
133
+ end
115
134
  when *WRITE_NODES
116
- bad << node.slice unless vetted_value?(node.value)
135
+ bad << cite(node) unless vetted_value?(node.value)
117
136
  when Prism::CallNode
118
- bad << node.slice unless vetted_call?(node)
137
+ bad << cite(node) unless vetted_call?(node) || visibility_call?(node)
119
138
  else
120
- bad << node.slice
139
+ bad << cite(node)
121
140
  end
122
141
  end
123
142
 
143
+ # "first line of the statement (line N)": a whole class body or a
144
+ # long call must not flood the warning (#664).
145
+ def cite(node)
146
+ first = node.slice.lines.first.to_s.strip
147
+ first = "#{first[0, 57]}..." if first.length > 60
148
+ "#{first} (line #{node.location.start_line})"
149
+ end
150
+
151
+ # `class << self` opens self's singleton class, and a constant or
152
+ # ivar target is only read. Anything else - `class << factory.build` -
153
+ # RUNS the expression at load, so the whole statement is unvetted.
154
+ def vetted_singleton_target?(node)
155
+ node.is_a?(Prism::SelfNode) || constant_read?(node) || node.is_a?(Prism::InstanceVariableReadNode)
156
+ end
157
+
124
158
  def vet_scope(node, bad)
125
159
  superclass = node.respond_to?(:superclass) ? node.superclass : nil
126
- bad << superclass.slice if superclass && !constant_read?(superclass)
160
+ bad << cite(superclass) if superclass && !constant_read?(superclass)
127
161
  vet_statements(node.body&.body || [], bad)
128
162
  end
129
163
 
164
+ # A bare visibility call re-flags methods and nothing else, so a
165
+ # replay is harmless: no receiver, no block, and only literal symbol
166
+ # or def arguments (`private :x`, `private def x`). The constant
167
+ # twins (`private_constant :X`) re-flag a constant's visibility the
168
+ # same way, with literal symbol or string arguments (#537).
169
+ def visibility_call?(node)
170
+ return false unless node.receiver.nil? && node.block.nil?
171
+
172
+ arguments = node.arguments&.arguments || []
173
+ if VISIBILITY_NAMES.include?(node.name.to_s)
174
+ arguments.all? { |argument| argument.is_a?(Prism::SymbolNode) || argument.is_a?(Prism::DefNode) }
175
+ elsif CONSTANT_VISIBILITY_NAMES.include?(node.name.to_s)
176
+ arguments.all? { |argument| argument.is_a?(Prism::SymbolNode) || argument.is_a?(Prism::StringNode) }
177
+ else
178
+ false
179
+ end
180
+ end
181
+
182
+ # `.freeze` on a vetted literal freezes data: no side effect.
183
+ def freeze_call?(node)
184
+ node.name == :freeze && node.arguments.nil? && node.block.nil? && vetted_value?(node.receiver)
185
+ end
186
+
187
+ # A Struct/Data class factory with only literal arguments and no
188
+ # block just builds a class: replaying it rebuilds the class, so the
189
+ # file it sits in stays vetted (#537, real run: ruby-jwt #747's
190
+ # `VerificationContext = Struct.new(:payload, keyword_init: true)`).
191
+ # A block is class_eval'd at load and stays unvetted, as does any
192
+ # computed argument.
193
+ def factory_new?(node)
194
+ return false unless node.receiver.is_a?(Prism::ConstantReadNode) && node.block.nil?
195
+ return false unless FACTORY_NEW[node.receiver.name.to_s] == node.name
196
+
197
+ (node.arguments&.arguments || []).all? do |argument|
198
+ argument.is_a?(Prism::SymbolNode) || argument.is_a?(Prism::StringNode) ||
199
+ keyword_bool?(argument)
200
+ end
201
+ end
202
+
203
+ def keyword_bool?(argument)
204
+ argument.is_a?(Prism::KeywordHashNode) && argument.elements.all? do |pair|
205
+ pair.is_a?(Prism::AssocNode) && pair.key.is_a?(Prism::SymbolNode) &&
206
+ (pair.value.is_a?(Prism::TrueNode) || pair.value.is_a?(Prism::FalseNode))
207
+ end
208
+ end
209
+
130
210
  def vetted_call?(node)
131
211
  return false unless SAFE_LOAD_REQUIRES.include?(node.name.to_s) && node.receiver.nil?
132
212
  return false unless node.block.nil?
@@ -146,7 +226,8 @@ module Saboteur
146
226
  case node
147
227
  when Prism::StringNode, Prism::SymbolNode, Prism::IntegerNode, Prism::FloatNode,
148
228
  Prism::TrueNode, Prism::FalseNode, Prism::NilNode, Prism::SourceFileNode,
149
- Prism::ConstantReadNode, Prism::ConstantPathNode
229
+ Prism::ConstantReadNode, Prism::ConstantPathNode, Prism::RegularExpressionNode,
230
+ Prism::LambdaNode
150
231
  true
151
232
  when Prism::ArrayNode
152
233
  node.elements.all? { |element| vetted_value?(element) }
@@ -155,7 +236,7 @@ module Saboteur
155
236
  pair.is_a?(Prism::AssocNode) && vetted_value?(pair.key) && vetted_value?(pair.value)
156
237
  end
157
238
  when Prism::CallNode
158
- node.name == :freeze && node.arguments.nil? && node.block.nil? && vetted_value?(node.receiver)
239
+ freeze_call?(node) || factory_new?(node)
159
240
  else
160
241
  false
161
242
  end
@@ -0,0 +1,59 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ require "prism"
5
+ require_relative "base"
6
+ require_relative "operator_visit"
7
+
8
+ module Saboteur
9
+ module MutationSource
10
+ # Replaces the value carried by return, next or break with nil:
11
+ # `return total` becomes `return nil`. A test that only runs the
12
+ # surrounding method without checking what the early exit hands back
13
+ # cannot tell the two apart. statement_removal drops the jump itself
14
+ # and body_nil drops the whole body; neither changes just the value.
15
+ class JumpValue < Base
16
+ include OperatorVisit
17
+
18
+ JUMPS = [Prism::ReturnNode, Prism::NextNode, Prism::BreakNode].freeze
19
+ # The caller discards what these return, so a changed value is unobservable.
20
+ IGNORED_RESULT = /\A(initialize|\[\]=|\w+=)\z/
21
+
22
+ # Base inherits Object's empty initializer; there is no parent state to set.
23
+ def initialize(arid: []) # rubocop:disable Lint/MissingSuper
24
+ @arid = arid.map { |pattern| Regexp.new(pattern) }
25
+ end
26
+
27
+ def name
28
+ "jump_value"
29
+ end
30
+
31
+ def mutations_for(subject)
32
+ return [] if subject.name.match?(IGNORED_RESULT)
33
+
34
+ super
35
+ end
36
+
37
+ private
38
+
39
+ def mutate_node(node, subject, found)
40
+ return unless JUMPS.any? { |type| node.is_a?(type) }
41
+
42
+ arguments = node.arguments
43
+ return if arguments.nil? || arguments.slice == "nil" || heredoc?(arguments)
44
+
45
+ # return(a) and return[a] have no space to keep: "returnnil" is another word.
46
+ glued = node.keyword_loc.end_offset == arguments.start_offset
47
+ edit = Edit.new(file: subject.file, start_byte: arguments.start_offset,
48
+ end_byte: arguments.end_offset, replacement: glued ? " nil" : "nil")
49
+ found << Mutation.new(subject_id: subject.id, operator: name, edits: [edit], source: name)
50
+ end
51
+
52
+ # A heredoc's body lives on later lines: replacing the argument that
53
+ # opens it would leave the body behind.
54
+ def heredoc?(node)
55
+ node.slice.start_with?("<<") || node.compact_child_nodes.any? { |child| heredoc?(child) }
56
+ end
57
+ end
58
+ end
59
+ end
@@ -3,17 +3,19 @@
3
3
 
4
4
  require "prism"
5
5
  require_relative "base"
6
+ require_relative "operator_visit"
6
7
 
7
8
  module Saboteur
8
9
  module MutationSource
9
10
  # Changes literal values without rebuilding the surrounding source.
10
11
  class LiteralReplacement < Base
11
- SCOPE_NODES = [Prism::DefNode, Prism::ClassNode, Prism::ModuleNode, Prism::SingletonClassNode].freeze
12
+ include OperatorVisit
13
+
12
14
  TEXT_NODES = [Prism::InterpolatedStringNode, Prism::SymbolNode, Prism::InterpolatedSymbolNode,
13
15
  Prism::RegularExpressionNode, Prism::InterpolatedRegularExpressionNode,
14
16
  Prism::XStringNode, Prism::InterpolatedXStringNode].freeze
15
17
  PARAMETER_NODES = [Prism::ParametersNode, Prism::BlockParametersNode].freeze
16
- BOUNDARIES = (SCOPE_NODES + TEXT_NODES + PARAMETER_NODES).freeze
18
+ BOUNDARIES = (OperatorVisit::SCOPE_NODES + TEXT_NODES + PARAMETER_NODES).freeze
17
19
 
18
20
  # Base inherits Object's empty initializer; there is no parent state to set.
19
21
  def initialize(arid: []) # rubocop:disable Lint/MissingSuper
@@ -24,29 +26,14 @@ module Saboteur
24
26
  "literal_replacement"
25
27
  end
26
28
 
27
- def mutations_for(subject)
28
- found = []
29
- visit(subject.body_node, subject, found)
30
- found
31
- end
32
-
33
29
  private
34
30
 
35
- def visit(node, subject, found)
36
- return if node.nil? || boundary?(node)
37
-
38
- if node.is_a?(Prism::StatementsNode)
39
- node.body.each { |statement| visit(statement, subject, found) unless arid?(statement) }
40
- return
41
- end
42
-
31
+ def mutate_node(node, subject, found)
43
32
  case node
44
33
  when Prism::TrueNode then add(subject, found, node, "literal_boolean", "false")
45
34
  when Prism::FalseNode then add(subject, found, node, "literal_boolean", "true")
46
- when Prism::StringNode
47
- replace_string(node, subject, found)
35
+ when Prism::StringNode then replace_string(node, subject, found)
48
36
  end
49
- node.compact_child_nodes.each { |child| visit(child, subject, found) }
50
37
  end
51
38
 
52
39
  def boundary?(node)
@@ -65,10 +52,6 @@ module Saboteur
65
52
  end_byte: node.location.end_offset, replacement: replacement)
66
53
  found << Mutation.new(subject_id: subject.id, operator: operator, edits: [edit], source: name)
67
54
  end
68
-
69
- def arid?(node)
70
- @arid.any? { |pattern| pattern.match?(node.slice) }
71
- end
72
55
  end
73
56
  end
74
57
  end
@@ -17,11 +17,13 @@ module Saboteur
17
17
  "negation_removal" => "!x -> x",
18
18
  "literal_number" => "1 -> 0",
19
19
  "or_assign" => "x ||= 1 -> x = 1",
20
+ "and_assign" => "x &&= y -> x = y",
20
21
  "safe_navigation" => "x&.name -> x.name",
21
22
  "statement_removal" => "check; value -> nil; value",
22
23
  "filter_replacement" => "xs.select { |x| x } -> xs.reject { |x| x }",
23
24
  "literal_string" => "\"hello\" -> \"\"",
24
25
  "literal_boolean" => "true -> false",
26
+ "argument_swap" => "send(a, b) -> send(b, a)",
25
27
  "argument_removal" => "send(a, b) -> send(a)",
26
28
  "pair_removal" => "{a: 1, b: 2} -> {b: 2}",
27
29
  "element_removal" => "TAGS = [a, b] -> TAGS = [b]",
@@ -33,6 +35,10 @@ module Saboteur
33
35
  "method_swap" => "xs.min -> xs.max",
34
36
  "operand_replacement" => "a + b -> a",
35
37
  "interpolation_removal" => "\"hi \#{name}\" -> \"hi \"",
38
+ "jump_value" => "return total -> return nil",
39
+ "rescue_removal" => "begin; x; rescue; y; end -> begin; x; end",
40
+ "when_removal" => "case x; when 1 then a; when 2 then b; end -> case x; when 2 then b; end",
41
+ "unary_removal" => "-x -> x",
36
42
  "regex_replacement" => "/\\A\\d+\\z/ -> /\\d+\\z/"
37
43
  }.freeze
38
44
  end
@@ -24,11 +24,13 @@ module Saboteur
24
24
  "negation_removal" => "Remove a logical negation",
25
25
  "literal_number" => "Change a numeric literal",
26
26
  "or_assign" => "Replace ||= with assignment",
27
+ "and_assign" => "Replace &&= with assignment",
27
28
  "safe_navigation" => "Replace safe navigation with a regular call",
28
29
  "statement_removal" => "Replace a statement with nil",
29
30
  "filter_replacement" => "Swap select and reject",
30
31
  "literal_string" => "Empty a string literal",
31
32
  "literal_boolean" => "Swap a boolean literal",
33
+ "argument_swap" => "Swap two positional call arguments",
32
34
  "argument_removal" => "Remove a call argument",
33
35
  "pair_removal" => "Remove a hash pair",
34
36
  "element_removal" => "Remove an element of a constant array literal",
@@ -37,6 +39,10 @@ module Saboteur
37
39
  "method_swap" => "Swap a method with its semantic opposite",
38
40
  "operand_replacement" => "Replace a binary operation with one operand",
39
41
  "interpolation_removal" => "Remove part of an interpolated string",
42
+ "jump_value" => "Replace the value of return, next or break with nil",
43
+ "rescue_removal" => "Remove an exception handler so the error propagates",
44
+ "when_removal" => "Remove one when branch of a case",
45
+ "unary_removal" => "Drop a unary minus or bitwise complement",
40
46
  "regex_replacement" => "Drop a regex anchor or swap a regex quantifier"
41
47
  }.freeze
42
48
  POLICY_VERSION = 3
@@ -20,7 +20,7 @@ module Saboteur
20
20
  private
21
21
 
22
22
  def visit(node, subject, found)
23
- return if node.nil? || SCOPE_NODES.any? { |scope| node.is_a?(scope) }
23
+ return if node.nil? || boundary?(node)
24
24
 
25
25
  if node.is_a?(Prism::StatementsNode)
26
26
  node.body.each { |statement| visit(statement, subject, found) unless arid?(statement) }
@@ -31,6 +31,12 @@ module Saboteur
31
31
  node.compact_child_nodes.each { |child| visit(child, subject, found) }
32
32
  end
33
33
 
34
+ # Where the walk stops. An operator that also leaves other nodes alone
35
+ # (literal text, parameters) overrides this.
36
+ def boundary?(node)
37
+ SCOPE_NODES.any? { |scope| node.is_a?(scope) }
38
+ end
39
+
34
40
  def arid?(node)
35
41
  @arid.any? { |pattern| pattern.match?(node.slice) }
36
42
  end