rubik 0.9.1 → 0.10.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 +4 -4
- data/ATTRIBUTION.md +1 -1
- data/CHANGELOG.md +143 -1
- data/lib/rubik/adapter/base.rb +2 -1
- data/lib/rubik/adapter/process.rb +28 -67
- data/lib/rubik/benchmark/vakanuvis_adoption.rb +10 -3
- data/lib/rubik/buffer/apply_contract.rb +3 -3
- data/lib/rubik/buffer/apply_transaction.rb +165 -0
- data/lib/rubik/buffer/manager.rb +26 -120
- data/lib/rubik/config/deep_ops.rb +4 -1
- data/lib/rubik/config/defaults.rb +6 -3
- data/lib/rubik/config/schema.rb +8 -0
- data/lib/rubik/config/secret_policy.rb +197 -0
- data/lib/rubik/config.rb +11 -0
- data/lib/rubik/diagnostics/collector.rb +0 -9
- data/lib/rubik/errors.rb +15 -0
- data/lib/rubik/event_bus.rb +2 -1
- data/lib/rubik/kernel/dispatcher.rb +12 -7
- data/lib/rubik/kernel/unit.rb +4 -1
- data/lib/rubik/llm/registry.rb +12 -9
- data/lib/rubik/llm/router.rb +2 -5
- data/lib/rubik/log.rb +21 -0
- data/lib/rubik/lsp/client.rb +77 -273
- data/lib/rubik/lsp/manager.rb +14 -10
- data/lib/rubik/lsp/transport.rb +365 -0
- data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
- data/lib/rubik/mcp/boot_run.rb +190 -0
- data/lib/rubik/mcp/manager.rb +40 -163
- data/lib/rubik/mcp/transport/stdio.rb +4 -1
- data/lib/rubik/orchestrator/auto_apply.rb +4 -3
- data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
- data/lib/rubik/orchestrator/goal_session.rb +53 -39
- data/lib/rubik/orchestrator/loop.rb +28 -47
- data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
- data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
- data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
- data/lib/rubik/orchestrator/resume_window.rb +96 -0
- data/lib/rubik/orchestrator/shell_deps.rb +16 -0
- data/lib/rubik/orchestrator/stream_manager.rb +2 -1
- data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
- data/lib/rubik/orchestrator/subagent/instance.rb +7 -12
- data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
- data/lib/rubik/orchestrator/subagent/questioner.rb +3 -1
- data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
- data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
- data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
- data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
- data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
- data/lib/rubik/orchestrator/subagent/step_runner.rb +10 -2
- data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
- data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
- data/lib/rubik/platform/io_drain.rb +2 -1
- data/lib/rubik/platform/process.rb +144 -54
- data/lib/rubik/platform.rb +2 -1
- data/lib/rubik/plugin/manager.rb +2 -1
- data/lib/rubik/repl/commands/agent_context.rb +8 -7
- data/lib/rubik/repl/commands/agent_execution.rb +13 -32
- data/lib/rubik/repl/commands/agent_models.rb +4 -3
- data/lib/rubik/repl/commands/agent_providers.rb +43 -2
- data/lib/rubik/repl/commands/base.rb +47 -2
- data/lib/rubik/repl/commands/buffer.rb +5 -4
- data/lib/rubik/repl/commands/lsp.rb +2 -1
- data/lib/rubik/repl/commands/pruner.rb +2 -1
- data/lib/rubik/repl/commands/secrets.rb +151 -0
- data/lib/rubik/repl/commands/workspace.rb +60 -150
- data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
- data/lib/rubik/repl/config_wiring.rb +3 -3
- data/lib/rubik/repl/hooks.rb +2 -1
- data/lib/rubik/repl/input_execution.rb +158 -76
- data/lib/rubik/repl/input_loop.rb +2 -1
- data/lib/rubik/repl/lifecycle.rb +59 -209
- data/lib/rubik/repl/notice_router.rb +319 -0
- data/lib/rubik/repl/paste_support.rb +3 -1
- data/lib/rubik/repl/presentation.rb +5 -3
- data/lib/rubik/repl/pruner/gate.rb +3 -1
- data/lib/rubik/repl/pruner/harness.rb +2 -1
- data/lib/rubik/repl/shell.rb +4 -3
- data/lib/rubik/repl/turn_recorder.rb +6 -5
- data/lib/rubik/runtime/discovery.rb +3 -2
- data/lib/rubik/runtime/loop_service.rb +4 -3
- data/lib/rubik/runtime/manifest_store.rb +9 -1
- data/lib/rubik/runtime/overlay_guard.rb +127 -0
- data/lib/rubik/runtime/overlay_storage.rb +92 -146
- data/lib/rubik/runtime/task_manager.rb +64 -25
- data/lib/rubik/sandbox/driver/docker.rb +5 -1
- data/lib/rubik/sandbox/runner.rb +16 -15
- data/lib/rubik/security/command_gate.rb +117 -93
- data/lib/rubik/security/guard.rb +6 -2
- data/lib/rubik/security/secret_store.rb +463 -0
- data/lib/rubik/security/synthesis_gate.rb +8 -11
- data/lib/rubik/session/manager.rb +7 -1
- data/lib/rubik/skills/manager.rb +2 -3
- data/lib/rubik/storage/search.rb +49 -0
- data/lib/rubik/storage/store.rb +13 -20
- data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
- data/lib/rubik/tools/base.rb +22 -0
- data/lib/rubik/tools/builtins/files.rb +3 -15
- data/lib/rubik/tools/builtins/grep.rb +1 -6
- data/lib/rubik/tools/builtins/lifecycle.rb +6 -5
- data/lib/rubik/tools/lifecycle_rollbacks.rb +14 -1
- data/lib/rubik/tools/truncator.rb +48 -36
- data/lib/rubik/ui/banner.rb +4 -3
- data/lib/rubik/ui/spinner.rb +3 -1
- data/lib/rubik/ui/statusline.rb +6 -5
- data/lib/rubik/version.rb +1 -1
- data/lib/rubik.rb +1 -2
- metadata +13 -1
data/lib/rubik/sandbox/runner.rb
CHANGED
|
@@ -4,6 +4,7 @@ require_relative "driver"
|
|
|
4
4
|
require_relative "driver/process"
|
|
5
5
|
require_relative "../security/errors"
|
|
6
6
|
require_relative "../platform"
|
|
7
|
+
require_relative "../storage/layout"
|
|
7
8
|
|
|
8
9
|
module Rubik
|
|
9
10
|
module Sandbox
|
|
@@ -21,10 +22,10 @@ module Rubik
|
|
|
21
22
|
# re-verification, where an empty-allowlist guard would silently
|
|
22
23
|
# re-block commands the operator allowlisted.
|
|
23
24
|
@guard = guard
|
|
24
|
-
@allow_network = allow_network || @config
|
|
25
|
+
@allow_network = allow_network || Rubik::Storage::Layout.config_get(@config, "sandbox.allow_network", false) || false
|
|
25
26
|
@allowed_external_dirs = (
|
|
26
27
|
Array(allowed_external_dirs) +
|
|
27
|
-
Array(@config
|
|
28
|
+
Array(Rubik::Storage::Layout.config_get(@config, "workspace.allowed_external_dirs", []))
|
|
28
29
|
).map { |d| File.expand_path(d.to_s, @root_dir) }.uniq
|
|
29
30
|
# Baseline captured BEFORE the first reconfigure: entries from
|
|
30
31
|
# construction arguments plus the config's current value. Dirs added
|
|
@@ -63,15 +64,15 @@ module Rubik
|
|
|
63
64
|
|
|
64
65
|
def sandbox_enabled?
|
|
65
66
|
return true if @preferred_driver && !@preferred_driver.is_a?(String) && !@preferred_driver.is_a?(Symbol)
|
|
66
|
-
return false if @config
|
|
67
|
+
return false if Rubik::Storage::Layout.config_get(@config, "sandbox.driver", nil).to_s.downcase.then { |d| ["process", "none"].include?(d) }
|
|
67
68
|
return false if @preferred_driver.to_s.downcase.then { |d| ["process", "none"].include?(d) }
|
|
68
69
|
|
|
69
|
-
val = @config
|
|
70
|
+
val = Rubik::Storage::Layout.config_get(@config, "sandbox.enabled", false)
|
|
70
71
|
val == true || val.to_s.downcase == "true"
|
|
71
72
|
end
|
|
72
73
|
|
|
73
74
|
def reconfigure!
|
|
74
|
-
@allow_network = @config
|
|
75
|
+
@allow_network = Rubik::Storage::Layout.config_get(@config, "sandbox.allow_network", @allow_network)
|
|
75
76
|
if @config && @allowed_dirs_config_touched
|
|
76
77
|
# Config authoritative (user has edited the key at some scope):
|
|
77
78
|
# recompute config-sourced entries from scratch so REMOVING a dir
|
|
@@ -83,7 +84,7 @@ module Rubik
|
|
|
83
84
|
# A malformed (non-Array) value must fail CLOSED: treating it as an
|
|
84
85
|
# empty config list and warning, rather than silently keeping the
|
|
85
86
|
# previously-merged union (which would leave stale dirs allowed).
|
|
86
|
-
raw_allowed = @config
|
|
87
|
+
raw_allowed = Rubik::Storage::Layout.config_get(@config, "workspace.allowed_external_dirs", [])
|
|
87
88
|
unless raw_allowed.is_a?(Array)
|
|
88
89
|
warn "[SANDBOX] workspace.allowed_external_dirs must be an Array (got #{raw_allowed.class}); " \
|
|
89
90
|
"treating as empty — the config is authoritative, so stale dirs are revoked."
|
|
@@ -101,7 +102,7 @@ module Rubik
|
|
|
101
102
|
|
|
102
103
|
driver_opts = {
|
|
103
104
|
image: @config&.fetch("sandbox.image", "rubik-sandbox:latest"),
|
|
104
|
-
shell: @config
|
|
105
|
+
shell: Rubik::Storage::Layout.config_get(@config, "sandbox.shell", "auto"),
|
|
105
106
|
allowed_dirs: allowed_paths,
|
|
106
107
|
guard: @guard
|
|
107
108
|
}
|
|
@@ -111,7 +112,7 @@ module Rubik
|
|
|
111
112
|
@driver = if @preferred_driver && !@preferred_driver.is_a?(String) && !@preferred_driver.is_a?(Symbol)
|
|
112
113
|
@preferred_driver
|
|
113
114
|
elsif @sandbox_enabled
|
|
114
|
-
driver_to_use = @preferred_driver || @config
|
|
115
|
+
driver_to_use = @preferred_driver || Rubik::Storage::Layout.config_get(@config, "sandbox.driver", nil) || "auto"
|
|
115
116
|
Driver.best_available(driver_to_use, driver_opts)
|
|
116
117
|
end
|
|
117
118
|
end
|
|
@@ -119,7 +120,7 @@ module Rubik
|
|
|
119
120
|
def allowed_paths
|
|
120
121
|
roots = [@root_dir]
|
|
121
122
|
if @config
|
|
122
|
-
extra_roots = Array(@config
|
|
123
|
+
extra_roots = Array(Rubik::Storage::Layout.config_get(@config, "workspace.additional_roots", [])).map { |r| File.expand_path(r.to_s, @root_dir) }
|
|
123
124
|
roots.concat(extra_roots)
|
|
124
125
|
end
|
|
125
126
|
roots.concat(@allowed_external_dirs)
|
|
@@ -157,7 +158,7 @@ module Rubik
|
|
|
157
158
|
|
|
158
159
|
def allow_network?
|
|
159
160
|
if @config
|
|
160
|
-
cfg_val = @config
|
|
161
|
+
cfg_val = Rubik::Storage::Layout.config_get(@config, "sandbox.allow_network", nil)
|
|
161
162
|
return true if cfg_val == true || cfg_val.to_s.downcase == "true"
|
|
162
163
|
return false if cfg_val == false || cfg_val.to_s.downcase == "false"
|
|
163
164
|
|
|
@@ -179,15 +180,15 @@ module Rubik
|
|
|
179
180
|
|
|
180
181
|
parsed_timeout = timeout&.to_i
|
|
181
182
|
effective_timeout = (parsed_timeout&.positive? ? parsed_timeout : nil) ||
|
|
182
|
-
(@config
|
|
183
|
+
Rubik::Storage::Layout.config_get(@config, "sandbox.timeout_seconds", 15).to_i
|
|
183
184
|
effective_timeout = 15 if effective_timeout <= 0
|
|
184
|
-
mem_limit = @config
|
|
185
|
-
cpus = @config
|
|
185
|
+
mem_limit = Rubik::Storage::Layout.config_get(@config, "sandbox.memory_limit_mb", 512)
|
|
186
|
+
cpus = Rubik::Storage::Layout.config_get(@config, "sandbox.cpus", "1.0")
|
|
186
187
|
|
|
187
188
|
active_driver = if sandbox_enabled? && @driver
|
|
188
189
|
@driver
|
|
189
190
|
else
|
|
190
|
-
@host_driver || Driver::ProcessDriver.new(shell: @config
|
|
191
|
+
@host_driver || Driver::ProcessDriver.new(shell: Rubik::Storage::Layout.config_get(@config, "sandbox.shell", "auto"),
|
|
191
192
|
guard: @guard)
|
|
192
193
|
end
|
|
193
194
|
|
|
@@ -211,7 +212,7 @@ module Rubik
|
|
|
211
212
|
end
|
|
212
213
|
|
|
213
214
|
def disclose_isolation?
|
|
214
|
-
|
|
215
|
+
Rubik::Storage::Layout.config_get(@config, "sandbox.isolation_note", true) != false
|
|
215
216
|
end
|
|
216
217
|
|
|
217
218
|
def isolation_disclosure
|
|
@@ -151,10 +151,7 @@ module Rubik
|
|
|
151
151
|
# Safe-probe exemption (highest-priority friction fix): run the
|
|
152
152
|
# catastrophic fallback regex first — it never loosens.
|
|
153
153
|
# Strictly single, isolated command without inner commands or chains.
|
|
154
|
-
|
|
155
|
-
ast.respond_to?(:parse_warnings) && ast.parse_warnings.to_a.empty? &&
|
|
156
|
-
(!ast.commands.first.respond_to?(:inner_commands) || ast.commands.first.inner_commands.nil? || ast.commands.first.inner_commands.empty?)
|
|
157
|
-
if single_cmd && safe_probe?(cmd_str)
|
|
154
|
+
if isolated_single_command?(ast) && safe_probe?(cmd_str)
|
|
158
155
|
verify_command_fallback_regex!(cmd_str)
|
|
159
156
|
@audit.log_audit("safe_probe", command: cmd_str, reason: "safe_probe_exemption")
|
|
160
157
|
return true
|
|
@@ -164,40 +161,9 @@ module Rubik
|
|
|
164
161
|
# the per-command walk bottoms out at its sentinel while the
|
|
165
162
|
# composed evidence travels on the ExpressionResult itself.
|
|
166
163
|
expression_reason = walk_reason == "dangerous_ast" ? expression_level_reason(ast) : nil
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
# regex still runs afterwards — allowlisting never bypasses it.
|
|
171
|
-
# A bug in the gate itself must fail closed, not fall through to
|
|
172
|
-
# the regex-only downgrade below.
|
|
173
|
-
allowlisted = allowlisted_fail_closed(ast, cmd_str)
|
|
174
|
-
if allowlisted
|
|
175
|
-
@audit.log_audit("allowed_command", command: cmd_str, reason: "operator_allowlist")
|
|
176
|
-
verify_command_fallback_regex!(cmd_str)
|
|
177
|
-
return true
|
|
178
|
-
end
|
|
179
|
-
# Enrichment must never be able to un-block: a bug in the report
|
|
180
|
-
# walk downgrades to nil here, the raise below still fires.
|
|
181
|
-
report = begin
|
|
182
|
-
dangerous_segment_report(ast.commands)
|
|
183
|
-
rescue StandardError => e
|
|
184
|
-
warn "[Guard] segment report failed: #{e.class}: #{e.message}"
|
|
185
|
-
nil
|
|
186
|
-
end
|
|
187
|
-
@audit.log_audit("blocked_command", command: cmd_str, reason: reason,
|
|
188
|
-
dangerous_segments: segment_count(report))
|
|
189
|
-
# probe_shape? (not safe_probe?): probes only reach this raise when
|
|
190
|
-
# the exemption is disabled, so safe_probe? would be constantly
|
|
191
|
-
# false and the variant-(a) guidance dead code.
|
|
192
|
-
raise BlockedWriteError.new(
|
|
193
|
-
"Dangerous command execution blocked: #{cmd_str} (reason: #{reason})",
|
|
194
|
-
kind: :dangerous_command,
|
|
195
|
-
blocked_command: cmd_str,
|
|
196
|
-
rule_name: dangerous_rule_name(ast.commands) || (expression_reason ? "expression_level" : "dangerous_pattern"),
|
|
197
|
-
probe_like: probe_shape?(cmd_str),
|
|
198
|
-
segment_report: report,
|
|
199
|
-
classification_reason: expression_reason
|
|
200
|
-
)
|
|
164
|
+
return true if operator_allowlist_pass?(ast, cmd_str)
|
|
165
|
+
|
|
166
|
+
block_dangerous_command!(ast, cmd_str, reason: expression_reason || walk_reason, expression_reason: expression_reason)
|
|
201
167
|
end
|
|
202
168
|
|
|
203
169
|
# UNKNOWN is fail-closed, not pass-through: the matcher rates every
|
|
@@ -208,14 +174,7 @@ module Rubik
|
|
|
208
174
|
# No safe-probe exemption: an unknown binary's --version still
|
|
209
175
|
# executes an unvetted binary — the allowlist is the valve.
|
|
210
176
|
if ast.classification == :unknown && !allow_unknown
|
|
211
|
-
|
|
212
|
-
# trouble must never convert this block into a pass.
|
|
213
|
-
allowlisted = allowlisted_fail_closed(ast, cmd_str)
|
|
214
|
-
if allowlisted
|
|
215
|
-
@audit.log_audit("allowed_command", command: cmd_str, reason: "operator_allowlist")
|
|
216
|
-
verify_command_fallback_regex!(cmd_str)
|
|
217
|
-
return true
|
|
218
|
-
end
|
|
177
|
+
return true if operator_allowlist_pass?(ast, cmd_str)
|
|
219
178
|
|
|
220
179
|
# Workspace Trust Discovery Recognition Gate:
|
|
221
180
|
if workspace_trust_candidate_allowed?(cmd_str, ast)
|
|
@@ -225,38 +184,9 @@ module Rubik
|
|
|
225
184
|
end
|
|
226
185
|
|
|
227
186
|
# Interactive operator approval gate (Antigravity-style single keypress)
|
|
228
|
-
|
|
229
|
-
if effective_shell.respond_to?(:interactive_input?) && effective_shell.interactive_input?
|
|
230
|
-
# Defense-in-depth: Un-bypassable catastrophic fallback regex MUST pass before prompt
|
|
231
|
-
verify_command_fallback_regex!(cmd_str)
|
|
232
|
-
|
|
233
|
-
theme = (effective_shell.respond_to?(:ui_theme) && effective_shell.ui_theme) ||
|
|
234
|
-
(defined?(Rubik::UI::Theme) ? Rubik::UI::Theme.new : nil)
|
|
235
|
-
|
|
236
|
-
question = "Allow unclassified command execution: '#{cmd_str}'?"
|
|
237
|
-
answer = if defined?(Rubik::UI::Approvals)
|
|
238
|
-
Rubik::UI::Approvals.keypress_yes_no(effective_shell, question, theme: theme, default: "n")
|
|
239
|
-
else
|
|
240
|
-
"n"
|
|
241
|
-
end
|
|
187
|
+
return true if operator_interactive_approval?(cmd_str, shell)
|
|
242
188
|
|
|
243
|
-
|
|
244
|
-
@audit.log_audit("allowed_command", command: cmd_str, reason: "operator_interactive_approval")
|
|
245
|
-
return true
|
|
246
|
-
end
|
|
247
|
-
end
|
|
248
|
-
|
|
249
|
-
@audit.log_audit("blocked_command", command: cmd_str, reason: "unknown_command")
|
|
250
|
-
raise BlockedWriteError.new(
|
|
251
|
-
"Unknown command execution blocked (fail-closed): #{cmd_str}. The binary is not in the " \
|
|
252
|
-
"command database, so its effects cannot be classified. If it is genuinely safe, the " \
|
|
253
|
-
"operator can allowlist the exact prefix: !allow-cmd add \"<binary> [args]\"",
|
|
254
|
-
kind: :unknown_command,
|
|
255
|
-
blocked_command: cmd_str,
|
|
256
|
-
rule_name: "unknown_command",
|
|
257
|
-
probe_like: probe_shape?(cmd_str),
|
|
258
|
-
segment_report: nil
|
|
259
|
-
)
|
|
189
|
+
block_unknown_command!(cmd_str)
|
|
260
190
|
end
|
|
261
191
|
|
|
262
192
|
# Ambiguous parse (tree-sitter syntax errors) is a classification
|
|
@@ -266,22 +196,9 @@ module Rubik
|
|
|
266
196
|
# the valve because a real AST exists — every segment is checked
|
|
267
197
|
# against the operator entries.
|
|
268
198
|
if !allow_unknown && ast.parse_warnings.to_a.any?
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
verify_command_fallback_regex!(cmd_str)
|
|
273
|
-
return true
|
|
274
|
-
end
|
|
275
|
-
@audit.log_audit("blocked_command", command: cmd_str, reason: "parse_ambiguous",
|
|
276
|
-
parse_warnings: ast.parse_warnings.to_a.size)
|
|
277
|
-
raise BlockedWriteError.new(
|
|
278
|
-
"Command AST could not be fully parsed (tree-sitter syntax error); execution blocked (fail-closed): #{cmd_str}",
|
|
279
|
-
kind: :parse_ambiguous,
|
|
280
|
-
blocked_command: cmd_str,
|
|
281
|
-
rule_name: "parse_ambiguous",
|
|
282
|
-
probe_like: probe_shape?(cmd_str),
|
|
283
|
-
segment_report: nil
|
|
284
|
-
)
|
|
199
|
+
return true if operator_allowlist_pass?(ast, cmd_str)
|
|
200
|
+
|
|
201
|
+
block_ambiguous_parse!(ast, cmd_str)
|
|
285
202
|
end
|
|
286
203
|
|
|
287
204
|
return true if [:readonly, :local_effects, :external_effects].include?(ast.classification) && ast.parse_warnings.empty?
|
|
@@ -350,6 +267,113 @@ module Rubik
|
|
|
350
267
|
false
|
|
351
268
|
end
|
|
352
269
|
|
|
270
|
+
# AST fast-path check for the safe-probe exemption: strictly single,
|
|
271
|
+
# isolated command without inner commands or chains.
|
|
272
|
+
def isolated_single_command?(ast)
|
|
273
|
+
ast.respond_to?(:commands) && ast.commands.to_a.size == 1 &&
|
|
274
|
+
ast.respond_to?(:parse_warnings) && ast.parse_warnings.to_a.empty? &&
|
|
275
|
+
(!ast.commands.first.respond_to?(:inner_commands) || ast.commands.first.inner_commands.nil? || ast.commands.first.inner_commands.empty?)
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# Operator allowlist valve pass, shared by the dangerous / unknown /
|
|
279
|
+
# ambiguous-parse block paths. Fail-closed default: the entry must
|
|
280
|
+
# cover EVERY dangerous segment recursively; catastrophic fallback
|
|
281
|
+
# regex still runs afterwards — allowlisting never bypasses it.
|
|
282
|
+
# A bug in the gate itself must fail closed, not fall through to
|
|
283
|
+
# the regex-only downgrade below. Returns true only after the
|
|
284
|
+
# allowed_command audit and the fallback regex have run.
|
|
285
|
+
def operator_allowlist_pass?(ast, cmd_str)
|
|
286
|
+
return false unless allowlisted_fail_closed(ast, cmd_str)
|
|
287
|
+
|
|
288
|
+
@audit.log_audit("allowed_command", command: cmd_str, reason: "operator_allowlist")
|
|
289
|
+
verify_command_fallback_regex!(cmd_str)
|
|
290
|
+
true
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
# Report assembly for the dangerous-classification block path, fail-soft
|
|
294
|
+
# to nil: enrichment must never be able to un-block, so a bug in the
|
|
295
|
+
# report walk downgrades to nil here and the raise in
|
|
296
|
+
# block_dangerous_command! still fires.
|
|
297
|
+
def dangerous_segment_report_or_nil(commands)
|
|
298
|
+
dangerous_segment_report(commands)
|
|
299
|
+
rescue StandardError => e
|
|
300
|
+
warn "[Guard] segment report failed: #{e.class}: #{e.message}"
|
|
301
|
+
nil
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# Dangerous-classification block path: audit the block, then raise with
|
|
305
|
+
# the blocked-segment guidance report attached.
|
|
306
|
+
def block_dangerous_command!(ast, cmd_str, reason:, expression_reason:)
|
|
307
|
+
report = dangerous_segment_report_or_nil(ast.commands)
|
|
308
|
+
@audit.log_audit("blocked_command", command: cmd_str, reason: reason,
|
|
309
|
+
dangerous_segments: segment_count(report))
|
|
310
|
+
# probe_shape? (not safe_probe?): probes only reach this raise when
|
|
311
|
+
# the exemption is disabled, so safe_probe? would be constantly
|
|
312
|
+
# false and the variant-(a) probe guidance would be dead code.
|
|
313
|
+
raise BlockedWriteError.new(
|
|
314
|
+
"Dangerous command execution blocked: #{cmd_str} (reason: #{reason})",
|
|
315
|
+
kind: :dangerous_command,
|
|
316
|
+
blocked_command: cmd_str,
|
|
317
|
+
rule_name: dangerous_rule_name(ast.commands) || (expression_reason ? "expression_level" : "dangerous_pattern"),
|
|
318
|
+
probe_like: probe_shape?(cmd_str),
|
|
319
|
+
segment_report: report,
|
|
320
|
+
classification_reason: expression_reason
|
|
321
|
+
)
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# Interactive operator approval gate (Antigravity-style single keypress):
|
|
325
|
+
# true (audited) when the operator approves, false when there is no
|
|
326
|
+
# interactive input or the answer is not yes.
|
|
327
|
+
def operator_interactive_approval?(cmd_str, shell)
|
|
328
|
+
effective_shell = shell || @shell
|
|
329
|
+
return false unless effective_shell.respond_to?(:interactive_input?) && effective_shell.interactive_input?
|
|
330
|
+
|
|
331
|
+
# Defense-in-depth: Un-bypassable catastrophic fallback regex MUST pass before prompt
|
|
332
|
+
verify_command_fallback_regex!(cmd_str)
|
|
333
|
+
|
|
334
|
+
theme = (effective_shell.respond_to?(:ui_theme) && effective_shell.ui_theme) ||
|
|
335
|
+
(defined?(Rubik::UI::Theme) ? Rubik::UI::Theme.new : nil)
|
|
336
|
+
|
|
337
|
+
question = "Allow unclassified command execution: '#{cmd_str}'?"
|
|
338
|
+
answer = if defined?(Rubik::UI::Approvals)
|
|
339
|
+
Rubik::UI::Approvals.keypress_yes_no(effective_shell, question, theme: theme, default: "n")
|
|
340
|
+
else
|
|
341
|
+
"n"
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
return false unless ["y", "yes"].include?(answer.to_s.downcase)
|
|
345
|
+
|
|
346
|
+
@audit.log_audit("allowed_command", command: cmd_str, reason: "operator_interactive_approval")
|
|
347
|
+
true
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
def block_unknown_command!(cmd_str)
|
|
351
|
+
@audit.log_audit("blocked_command", command: cmd_str, reason: "unknown_command")
|
|
352
|
+
raise BlockedWriteError.new(
|
|
353
|
+
"Unknown command execution blocked (fail-closed): #{cmd_str}. The binary is not in the " \
|
|
354
|
+
"command database, so its effects cannot be classified. If it is genuinely safe, the " \
|
|
355
|
+
"operator can allowlist the exact prefix: !allow-cmd add \"<binary> [args]\"",
|
|
356
|
+
kind: :unknown_command,
|
|
357
|
+
blocked_command: cmd_str,
|
|
358
|
+
rule_name: "unknown_command",
|
|
359
|
+
probe_like: probe_shape?(cmd_str),
|
|
360
|
+
segment_report: nil
|
|
361
|
+
)
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def block_ambiguous_parse!(ast, cmd_str)
|
|
365
|
+
@audit.log_audit("blocked_command", command: cmd_str, reason: "parse_ambiguous",
|
|
366
|
+
parse_warnings: ast.parse_warnings.to_a.size)
|
|
367
|
+
raise BlockedWriteError.new(
|
|
368
|
+
"Command AST could not be fully parsed (tree-sitter syntax error); execution blocked (fail-closed): #{cmd_str}",
|
|
369
|
+
kind: :parse_ambiguous,
|
|
370
|
+
blocked_command: cmd_str,
|
|
371
|
+
rule_name: "parse_ambiguous",
|
|
372
|
+
probe_like: probe_shape?(cmd_str),
|
|
373
|
+
segment_report: nil
|
|
374
|
+
)
|
|
375
|
+
end
|
|
376
|
+
|
|
353
377
|
def dangerous_reason(commands)
|
|
354
378
|
commands.each do |c|
|
|
355
379
|
next unless c.classification == :dangerous
|
data/lib/rubik/security/guard.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "audit_log"
|
|
4
|
+
require_relative "../storage/layout"
|
|
4
5
|
require_relative "allowlist"
|
|
5
6
|
require_relative "command_gate"
|
|
6
7
|
require_relative "path_policy"
|
|
@@ -49,6 +50,9 @@ module Rubik
|
|
|
49
50
|
def self.for_config(config, root_dir: nil, shell: nil)
|
|
50
51
|
return new if config.nil? || !config.respond_to?(:fetch)
|
|
51
52
|
|
|
53
|
+
# Deliberate bare fetch (not Layout.config_get): config_get folds an
|
|
54
|
+
# explicit nil into the default, but here an explicit nil DISABLES
|
|
55
|
+
# auditing — the absent/nil distinction is load-bearing.
|
|
52
56
|
raw_audit = config.fetch("security.audit_log_path", default_audit_log_path)
|
|
53
57
|
audit_path =
|
|
54
58
|
case raw_audit
|
|
@@ -67,9 +71,9 @@ module Rubik
|
|
|
67
71
|
|
|
68
72
|
new(
|
|
69
73
|
audit_log_path: audit_path,
|
|
70
|
-
command_allowlist: Array(
|
|
74
|
+
command_allowlist: Array(Rubik::Storage::Layout.config_get(config, "security.command_allowlist", [])),
|
|
71
75
|
# Stringy "false" from persisted config must disable probes too.
|
|
72
|
-
allow_safe_probes:
|
|
76
|
+
allow_safe_probes: Rubik::Storage::Layout.config_get(config, "security.allow_safe_probes", true).to_s.downcase != "false",
|
|
73
77
|
root_dir: effective_root,
|
|
74
78
|
shell: shell
|
|
75
79
|
)
|