rubik 0.9.1 → 0.11.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 +203 -1
- data/lib/rubik/adapter/base.rb +2 -1
- data/lib/rubik/adapter/process.rb +28 -67
- data/lib/rubik/ast/extractor.rb +2 -1
- data/lib/rubik/ast/treesitter_extractor.rb +2 -1
- data/lib/rubik/benchmark/vakanuvis_adoption.rb +12 -4
- 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 +198 -0
- data/lib/rubik/config/store.rb +2 -1
- data/lib/rubik/config.rb +13 -2
- data/lib/rubik/context/ignore_rules.rb +193 -0
- data/lib/rubik/context/scanner.rb +46 -161
- 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/client.rb +6 -200
- data/lib/rubik/llm/connection_pool.rb +104 -0
- data/lib/rubik/llm/registry.rb +13 -9
- data/lib/rubik/llm/router.rb +2 -5
- data/lib/rubik/llm/sse_parser.rb +110 -0
- data/lib/rubik/log.rb +21 -0
- data/lib/rubik/lsp/circuit_breaker.rb +98 -0
- data/lib/rubik/lsp/client.rb +77 -273
- data/lib/rubik/lsp/client_provisioner.rb +49 -0
- data/lib/rubik/lsp/manager.rb +67 -96
- 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 +55 -166
- data/lib/rubik/mcp/transport/stdio.rb +4 -1
- data/lib/rubik/orchestrator/auto_apply.rb +8 -6
- data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
- data/lib/rubik/orchestrator/goal_ledger/ledger.rb +12 -1
- data/lib/rubik/orchestrator/goal_ledger/schema_vocabulary.rb +38 -0
- data/lib/rubik/orchestrator/goal_ledger/xml_render.rb +6 -1
- data/lib/rubik/orchestrator/goal_ledger.rb +1 -0
- data/lib/rubik/orchestrator/goal_session.rb +53 -39
- data/lib/rubik/orchestrator/loop.rb +28 -47
- data/lib/rubik/orchestrator/lsp_feedback.rb +1 -1
- 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/step_executor.rb +2 -1
- 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/notices.rb +2 -2
- 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/transcript_formatter.rb +2 -1
- 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 +5 -2
- 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/plugin/storage.rb +8 -8
- 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 +8 -6
- data/lib/rubik/repl/hooks.rb +2 -1
- data/lib/rubik/repl/input_execution.rb +162 -79
- data/lib/rubik/repl/input_loop.rb +2 -1
- data/lib/rubik/repl/lifecycle.rb +60 -210
- 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/tee_io.rb +2 -1
- data/lib/rubik/repl/turn_recorder.rb +6 -5
- data/lib/rubik/runtime/ast_service.rb +2 -1
- 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 +109 -163
- data/lib/rubik/runtime/task_manager.rb +64 -25
- data/lib/rubik/sandbox/driver/docker.rb +5 -1
- data/lib/rubik/sandbox/runner.rb +21 -19
- data/lib/rubik/security/audit_log.rb +2 -1
- data/lib/rubik/security/command_gate.rb +126 -243
- data/lib/rubik/security/guard.rb +7 -3
- data/lib/rubik/security/secret_store.rb +492 -0
- data/lib/rubik/security/segment_report.rb +161 -0
- data/lib/rubik/security/synthesis_gate.rb +8 -11
- data/lib/rubik/session/manager.rb +7 -1
- data/lib/rubik/session/recorder.rb +2 -1
- data/lib/rubik/skills/manager.rb +4 -4
- data/lib/rubik/storage/search.rb +49 -0
- data/lib/rubik/storage/store.rb +13 -20
- data/lib/rubik/storage/transcript_indexer.rb +3 -2
- data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
- data/lib/rubik/storage/vakanuvis.rb +2 -1
- data/lib/rubik/tools/base.rb +22 -0
- data/lib/rubik/tools/builtins/files.rb +3 -15
- data/lib/rubik/tools/builtins/goal_ledger.rb +12 -1
- data/lib/rubik/tools/builtins/grep.rb +1 -6
- data/lib/rubik/tools/builtins/lifecycle.rb +6 -5
- data/lib/rubik/tools/builtins/synthesis.rb +3 -3
- data/lib/rubik/tools/lifecycle_rollbacks.rb +14 -1
- data/lib/rubik/tools/preflight_runner.rb +2 -1
- data/lib/rubik/tools/truncator.rb +48 -36
- data/lib/rubik/tools/uri_resolver.rb +3 -2
- data/lib/rubik/ui/banner.rb +4 -3
- data/lib/rubik/ui/capability.rb +1 -0
- 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 +4 -5
- metadata +20 -1
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "yaml"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "open3"
|
|
6
|
+
require "tempfile"
|
|
7
|
+
require "tmpdir"
|
|
8
|
+
require_relative "../platform"
|
|
9
|
+
|
|
10
|
+
module Rubik
|
|
11
|
+
module Security
|
|
12
|
+
# Keeps provider credentials out of plain config files.
|
|
13
|
+
#
|
|
14
|
+
# Interface (class-level, the only surface callers may use):
|
|
15
|
+
# - reference?(value) -> true when value is a stored-secret pointer
|
|
16
|
+
# - reference_for(name) -> the pointer string for a SecretStore.name
|
|
17
|
+
# - resolve(value) -> pointer -> stored value; anything else as-is
|
|
18
|
+
# - set(name, value) -> store secret, return its pointer
|
|
19
|
+
# - fetch(name) -> stored value or nil (a miss never raises)
|
|
20
|
+
# - delete(name) -> idempotent removal
|
|
21
|
+
# - backend_name -> symbol for the active backend
|
|
22
|
+
#
|
|
23
|
+
# A "name" is the config dot-path that owns the secret, e.g.
|
|
24
|
+
# "providers.openai.api_key". Config files never receive the secret
|
|
25
|
+
# itself, only the pointer ("rubik-secret:<name>"), so a config file may
|
|
26
|
+
# be committed, synced, or shared without leaking credentials.
|
|
27
|
+
#
|
|
28
|
+
# Backend chain (first available wins, file fallback always exists):
|
|
29
|
+
# macOS - Keychain generic passwords via the security CLI; the
|
|
30
|
+
# secret travels on stdin (flag-then-stdin mode), never argv
|
|
31
|
+
# Windows - Credential Manager via PowerShell PasswordVault; payloads
|
|
32
|
+
# travel as env vars into FIXED scripts (secret base64)
|
|
33
|
+
# Linux - secret-service D-Bus via secret-tool; secret on stdin
|
|
34
|
+
# always - a ruthlessly simple owner-only 0600 YAML store at
|
|
35
|
+
# ~/.rubik/secrets.yml — plaintext on disk is the honest
|
|
36
|
+
# last resort, surfaced via backend_name
|
|
37
|
+
#
|
|
38
|
+
# Test seam: SecretStore.backend= injects any object responding to
|
|
39
|
+
# read/write/delete, so unit tests never touch a host keyring.
|
|
40
|
+
class SecretStore
|
|
41
|
+
REFERENCE_PREFIX = "rubik-secret:"
|
|
42
|
+
SERVICE = "rubik"
|
|
43
|
+
NAME_PATTERN = /\A\w[\w.-]*\z/
|
|
44
|
+
|
|
45
|
+
class Error < StandardError; end
|
|
46
|
+
|
|
47
|
+
@monitor = Monitor.new
|
|
48
|
+
@backend = nil
|
|
49
|
+
|
|
50
|
+
class << self
|
|
51
|
+
def backend_name
|
|
52
|
+
monitor_sync { backend_class_name(backend) }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def available?
|
|
56
|
+
true
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def reference?(value)
|
|
60
|
+
value.is_a?(String) && value.start_with?(REFERENCE_PREFIX)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def reference_for(name)
|
|
64
|
+
"#{REFERENCE_PREFIX}#{validate_name!(name)}"
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def resolve(value)
|
|
68
|
+
return value unless reference?(value)
|
|
69
|
+
|
|
70
|
+
fetch(value.delete_prefix(REFERENCE_PREFIX))
|
|
71
|
+
rescue Error
|
|
72
|
+
# A hand-edited pointer (empty name, hostile characters) must not
|
|
73
|
+
# crash provider builds or REPL boot — an unresolvable reference
|
|
74
|
+
# resolves to nil, and Registry warns loudly instead.
|
|
75
|
+
nil
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Stores the secret and returns its reference. Raises Error when the
|
|
79
|
+
# active OS backend refuses; Config decides the fallback policy.
|
|
80
|
+
def set(name, value)
|
|
81
|
+
validate_name!(name)
|
|
82
|
+
value = value.to_s unless value.is_a?(String)
|
|
83
|
+
raise Error, "secret value must be a non-empty string" unless value.is_a?(String) && !value.empty?
|
|
84
|
+
|
|
85
|
+
backend.write(name, value)
|
|
86
|
+
reference_for(name)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def fetch(name)
|
|
90
|
+
validate_name!(name)
|
|
91
|
+
backend.read(name)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def delete(name)
|
|
95
|
+
validate_name!(name)
|
|
96
|
+
backend.delete(name)
|
|
97
|
+
true
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Test seam: inject a backend object responding to read/write/delete.
|
|
101
|
+
def backend=(backend_obj)
|
|
102
|
+
monitor_sync { @backend = backend_obj }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def reset!
|
|
106
|
+
monitor_sync { @backend = nil }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
private
|
|
110
|
+
|
|
111
|
+
def backend
|
|
112
|
+
monitor_sync { @backend ||= select_backend }
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def monitor_sync(&)
|
|
116
|
+
@monitor.synchronize(&)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def validate_name!(name)
|
|
120
|
+
name_str = name.to_s
|
|
121
|
+
return name_str if name_str.match?(NAME_PATTERN)
|
|
122
|
+
|
|
123
|
+
raise Error, "invalid secret name: #{name_str.inspect} (expected a dot-path like providers.openai.api_key)"
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Shared command plumbing. Backends are the internal seam; tests may
|
|
128
|
+
# subclass and override #run to avoid touching a host keyring.
|
|
129
|
+
class CommandBackend
|
|
130
|
+
# Deadline for the stdin payload write (see #run).
|
|
131
|
+
STDIN_WRITE_TIMEOUT = 5.0
|
|
132
|
+
|
|
133
|
+
private
|
|
134
|
+
|
|
135
|
+
# Single spawn point for every backend: receives a fully-formed
|
|
136
|
+
# argument vector (never a shell string) plus optional stdin payload.
|
|
137
|
+
#
|
|
138
|
+
# The stdin payload travels through Platform.write_until so a backend
|
|
139
|
+
# that never reads its stdin (headless keychain denial, crashed
|
|
140
|
+
# secret service) fails with a deadline instead of parking the
|
|
141
|
+
# caller forever on a full OS pipe buffer. Every IO failure — a
|
|
142
|
+
# backend that died before reading stdin raising EPIPE, a spawn
|
|
143
|
+
# failure — is converted to Error, because every caller (resolve,
|
|
144
|
+
# set, delete, SecretPolicy, the !secrets commands) rescues only
|
|
145
|
+
# that class.
|
|
146
|
+
def run(argv, input: nil, env: nil)
|
|
147
|
+
env_hash = env.is_a?(Hash) ? env : {}
|
|
148
|
+
merged = ENV.to_h.merge(env_hash)
|
|
149
|
+
Open3.popen3(merged, *argv) do |pin, pout, perr, waiter|
|
|
150
|
+
begin
|
|
151
|
+
Rubik::Platform.write_until(pin, input.to_s, timeout: STDIN_WRITE_TIMEOUT)
|
|
152
|
+
ensure
|
|
153
|
+
begin
|
|
154
|
+
pin.close
|
|
155
|
+
rescue IOError, SystemCallError
|
|
156
|
+
nil
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
[pout.read, perr.read, waiter.value]
|
|
160
|
+
end
|
|
161
|
+
rescue IOError, SystemCallError => e
|
|
162
|
+
raise Error, "secret backend command failed: #{e.class}: #{e.message}"
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
class MacosKeychainBackend < CommandBackend
|
|
167
|
+
SECURITY = "/usr/bin/security"
|
|
168
|
+
|
|
169
|
+
def self.available?
|
|
170
|
+
Rubik::Platform.macos? && File.executable?(SECURITY)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def write(name, value)
|
|
174
|
+
out, err, st = run(security_write_argv(name), input: value)
|
|
175
|
+
return true if st.success?
|
|
176
|
+
|
|
177
|
+
raise Error, "keychain write failed (#{st.exitstatus}): #{err} #{out}"
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def read(name)
|
|
181
|
+
out, _err, st = run(security_read_argv(name))
|
|
182
|
+
# 44 = errSecItemNotFound (honest miss); any other nonzero exit is
|
|
183
|
+
# a denied/failed lookup and must not surface as an empty key.
|
|
184
|
+
return nil unless st.success? || st.exitstatus == 44
|
|
185
|
+
|
|
186
|
+
# The CLI appends exactly one newline; chomp (not strip) so
|
|
187
|
+
# legitimate leading/trailing whitespace in the secret survives.
|
|
188
|
+
val = out.chomp
|
|
189
|
+
val.empty? ? nil : val
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def delete(name)
|
|
193
|
+
_out, _err, _st = run(security_delete_argv(name))
|
|
194
|
+
true
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
private
|
|
198
|
+
|
|
199
|
+
# The security CLI owns this contract: "-w" with no value asks the
|
|
200
|
+
# tool to read the password from its standard input, so the secret
|
|
201
|
+
# never rides argv or `ps` output.
|
|
202
|
+
def security_write_argv(name)
|
|
203
|
+
[SECURITY, "add-generic-password", "-U", "-s", SERVICE, "-a", name, "-w"]
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def security_read_argv(name)
|
|
207
|
+
[SECURITY, "find-generic-password", "-s", SERVICE, "-a", name, "-w"]
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def security_delete_argv(name)
|
|
211
|
+
[SECURITY, "delete-generic-password", "-s", SERVICE, "-a", name]
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
class WindowsCredManBackend < CommandBackend
|
|
216
|
+
PS = "powershell.exe"
|
|
217
|
+
|
|
218
|
+
def self.available?
|
|
219
|
+
Rubik::Platform.windows? && WindowsCredManBackend.find_in_path(PS)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def self.find_in_path(binary)
|
|
223
|
+
ENV["PATH"].to_s.split(File::PATH_SEPARATOR).any? do |dir|
|
|
224
|
+
File.executable?(File.join(dir, binary))
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def write(name, value)
|
|
229
|
+
# The secret travels base64-encoded through the environment into a
|
|
230
|
+
# FIXED script: no caller-controlled text is interpolated into any
|
|
231
|
+
# command string, and the base64 alphabet cannot carry metacharacters.
|
|
232
|
+
payload = {
|
|
233
|
+
"RUBIK_SECRET_NAME" => name,
|
|
234
|
+
"RUBIK_SECRET_VALUE_B64" => [value].pack("m0")
|
|
235
|
+
}
|
|
236
|
+
_out, err, st = run(write_script_argv, env: payload)
|
|
237
|
+
# $ErrorActionPreference = 'Stop' makes the script exit nonzero on
|
|
238
|
+
# any vault failure; ignoring the status would hand back a dangling
|
|
239
|
+
# pointer while reporting success.
|
|
240
|
+
return true if st.success?
|
|
241
|
+
|
|
242
|
+
raise Error, "PasswordVault write failed (#{st.exitstatus}): #{err.to_s.strip}"
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def read(name)
|
|
246
|
+
out, = run(read_script_argv, env: name_payload(name))
|
|
247
|
+
encoded = out.to_s.strip
|
|
248
|
+
encoded.empty? ? nil : encoded.unpack1("m0").force_encoding(Encoding::UTF_8)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def delete(name)
|
|
252
|
+
run(delete_script_argv, env: name_payload(name))
|
|
253
|
+
true
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# FIXED scripts (frozen at load, no interpolation): WinRT
|
|
257
|
+
# PasswordVault via Windows PowerShell 5.1, present on every
|
|
258
|
+
# supported Windows release. cmdkey cannot read secrets back, so the
|
|
259
|
+
# vault API is the only stdlib-reachable read path.
|
|
260
|
+
WRITE_PS = <<~PS
|
|
261
|
+
$ErrorActionPreference = 'Stop'
|
|
262
|
+
[void][Windows.Security.Credentials.PasswordVault,Windows.Foundation,ContentType=WindowsRuntime]
|
|
263
|
+
$v = New-Object Windows.Security.Credentials.PasswordVault
|
|
264
|
+
$p = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($env:RUBIK_SECRET_VALUE_B64))
|
|
265
|
+
$c = New-Object Windows.Security.Credentials.PasswordCredential('rubik', $env:RUBIK_SECRET_NAME, $p)
|
|
266
|
+
$v.Add($c)
|
|
267
|
+
Write-Output 'ok'
|
|
268
|
+
PS
|
|
269
|
+
|
|
270
|
+
READ_PS = <<~PS
|
|
271
|
+
$ErrorActionPreference = 'Stop'
|
|
272
|
+
[void][Windows.Security.Credentials.PasswordVault,Windows.Foundation,ContentType=WindowsRuntime]
|
|
273
|
+
$v = New-Object Windows.Security.Credentials.PasswordVault
|
|
274
|
+
$c = $v.Retrieve('rubik', $env:RUBIK_SECRET_NAME)
|
|
275
|
+
$c.RetrievePassword()
|
|
276
|
+
Write-Output ([Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($c.Password)))
|
|
277
|
+
PS
|
|
278
|
+
|
|
279
|
+
DELETE_PS = <<~PS
|
|
280
|
+
[void][Windows.Security.Credentials.PasswordVault,Windows.Foundation,ContentType=WindowsRuntime]
|
|
281
|
+
$v = New-Object Windows.Security.Credentials.PasswordVault
|
|
282
|
+
try { $c = $v.Retrieve('rubik', $env:RUBIK_SECRET_NAME); $v.Remove($c) } catch { }
|
|
283
|
+
Write-Output 'ok'
|
|
284
|
+
PS
|
|
285
|
+
|
|
286
|
+
private
|
|
287
|
+
|
|
288
|
+
def name_payload(name)
|
|
289
|
+
{ "RUBIK_SECRET_NAME" => name }
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def write_script_argv
|
|
293
|
+
[PS, "-NoProfile", "-NonInteractive", "-Command", WRITE_PS]
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def read_script_argv
|
|
297
|
+
[PS, "-NoProfile", "-NonInteractive", "-Command", READ_PS]
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def delete_script_argv
|
|
301
|
+
[PS, "-NoProfile", "-NonInteractive", "-Command", DELETE_PS]
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
class LinuxSecretToolBackend < CommandBackend
|
|
306
|
+
TOOL = "secret-tool"
|
|
307
|
+
|
|
308
|
+
def self.available?
|
|
309
|
+
return false unless Rubik::Platform.linux? && LinuxSecretToolBackend.find_in_path(TOOL)
|
|
310
|
+
|
|
311
|
+
# Canary probe: headless boxes ship the binary without a running
|
|
312
|
+
# secret service ("Cannot autolaunch D-Bus"), so binary presence
|
|
313
|
+
# alone lies. A missed lookup (exit 1) is a working service; a
|
|
314
|
+
# crashed lookup (other nonzero) is not.
|
|
315
|
+
new.canary_ok?
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def self.find_in_path(binary)
|
|
319
|
+
ENV["PATH"].to_s.split(File::PATH_SEPARATOR).any? do |dir|
|
|
320
|
+
File.executable?(File.join(dir, binary))
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def write(name, value)
|
|
325
|
+
# secret-tool store ADDS a new item; it does not upsert, and
|
|
326
|
+
# lookup concatenates the payloads of every matching item — a
|
|
327
|
+
# re-set secret would silently resolve to "key1key2". Clear any
|
|
328
|
+
# prior items first so set is idempotent (mirroring the -U flag
|
|
329
|
+
# the macOS backend passes to add-generic-password).
|
|
330
|
+
run(clear_argv(name))
|
|
331
|
+
_out, err, st = run(store_argv(name), input: value)
|
|
332
|
+
return true if st.success?
|
|
333
|
+
|
|
334
|
+
raise Error, "secret-tool store failed (#{st.exitstatus}): #{err}"
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def read(name)
|
|
338
|
+
out, = run(lookup_argv(name))
|
|
339
|
+
val = out.to_s.strip
|
|
340
|
+
val.empty? ? nil : val
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def delete(name)
|
|
344
|
+
run(clear_argv(name))
|
|
345
|
+
true
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
def canary_ok?
|
|
349
|
+
_out, _err, st = run(lookup_argv("rubik-availability-probe"))
|
|
350
|
+
st.exitstatus == 1 || st.success?
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
private
|
|
354
|
+
|
|
355
|
+
# The secret travels on stdin — never argv — per secret-service
|
|
356
|
+
# convention; attrs (service, account) name the lookup key.
|
|
357
|
+
def store_argv(name)
|
|
358
|
+
[TOOL, "store", "--label=rubik", "service", SERVICE, "account", name]
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
def lookup_argv(name)
|
|
362
|
+
[TOOL, "lookup", "service", SERVICE, "account", name]
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
def clear_argv(name)
|
|
366
|
+
[TOOL, "clear", "service", SERVICE, "account", name]
|
|
367
|
+
end
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
# The always-present fallback: owner-only store next to the global
|
|
371
|
+
# config, created 0600 from the first byte (same discipline as
|
|
372
|
+
# Config::Store#ensure_owner_only!), atomic replace on every write.
|
|
373
|
+
class FileBackend
|
|
374
|
+
def initialize(path = default_path)
|
|
375
|
+
@path = path
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
attr_reader :path
|
|
379
|
+
|
|
380
|
+
def write(name, value)
|
|
381
|
+
ensure_owner_only!
|
|
382
|
+
data = read_all
|
|
383
|
+
data[name] = value
|
|
384
|
+
atomic_replace(data)
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
def read(name)
|
|
388
|
+
read_all[name]
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
def delete(name)
|
|
392
|
+
data = read_all
|
|
393
|
+
data.delete(name)
|
|
394
|
+
atomic_replace(data)
|
|
395
|
+
true
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
private
|
|
399
|
+
|
|
400
|
+
def read_all
|
|
401
|
+
return {} unless File.exist?(@path)
|
|
402
|
+
|
|
403
|
+
parsed = YAML.safe_load_file(@path, permitted_classes: [], aliases: false)
|
|
404
|
+
parsed.is_a?(Hash) ? parsed : {}
|
|
405
|
+
rescue Psych::Exception
|
|
406
|
+
{}
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
def atomic_replace(data)
|
|
410
|
+
dir = File.dirname(@path)
|
|
411
|
+
FileUtils.mkdir_p(dir)
|
|
412
|
+
# Tempfile.new (NOT the block form of Tempfile.create, which
|
|
413
|
+
# unlinks the file when the block exits): the temp file must
|
|
414
|
+
# SURVIVE until the rename, and must be CLOSED before it —
|
|
415
|
+
# Windows refuses to rename an open file.
|
|
416
|
+
tmp = Tempfile.new("rubik-secrets-", dir)
|
|
417
|
+
begin
|
|
418
|
+
tmp.write(YAML.dump(data))
|
|
419
|
+
tmp.flush
|
|
420
|
+
tmp.chmod(0o600)
|
|
421
|
+
tmp.close
|
|
422
|
+
File.rename(tmp.path, @path)
|
|
423
|
+
rescue SystemCallError
|
|
424
|
+
# Windows rename can still refuse an existing destination:
|
|
425
|
+
# fall back to a short delete-then-rename window.
|
|
426
|
+
tmp.close
|
|
427
|
+
FileUtils.rm_f(@path)
|
|
428
|
+
File.rename(tmp.path, @path)
|
|
429
|
+
ensure
|
|
430
|
+
tmp.close
|
|
431
|
+
tmp.unlink
|
|
432
|
+
end
|
|
433
|
+
rescue SystemCallError => e
|
|
434
|
+
raise Error, "secret file store write failed: #{e.message}"
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
def ensure_owner_only!
|
|
438
|
+
FileUtils.touch(@path) unless File.exist?(@path)
|
|
439
|
+
File.chmod(0o600, @path)
|
|
440
|
+
rescue SystemCallError
|
|
441
|
+
nil
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
def default_path
|
|
445
|
+
home = if defined?(Rubik::Platform) && Rubik::Platform.respond_to?(:home_dir)
|
|
446
|
+
begin
|
|
447
|
+
Rubik::Platform.home_dir
|
|
448
|
+
rescue StandardError
|
|
449
|
+
nil
|
|
450
|
+
end
|
|
451
|
+
else
|
|
452
|
+
ENV["USERPROFILE"] || ENV.fetch("HOME", nil)
|
|
453
|
+
end
|
|
454
|
+
# No "." fallback: an owner-only secret store must never land in
|
|
455
|
+
# the working tree just because HOME is unset.
|
|
456
|
+
raise Error, "no home directory available for the secret file store (set HOME/USERPROFILE)" if home.nil? || home.to_s.strip.empty?
|
|
457
|
+
|
|
458
|
+
File.join(home.to_s, ".rubik", "secrets.yml")
|
|
459
|
+
end
|
|
460
|
+
end
|
|
461
|
+
|
|
462
|
+
def self.select_backend
|
|
463
|
+
# Test armor (RUBIK_TEST=1, the repo-wide convention): a test run
|
|
464
|
+
# must NEVER touch the host keyring, even through an un-injected
|
|
465
|
+
# path — an interactive-authorization `security` spawn on a headless
|
|
466
|
+
# CI runner hangs forever. Un-injected tests get the owner-only
|
|
467
|
+
# file store in a temp location instead.
|
|
468
|
+
return FileBackend.new(test_file_backend_path) if ENV["RUBIK_TEST"] == "1"
|
|
469
|
+
|
|
470
|
+
[MacosKeychainBackend, WindowsCredManBackend, LinuxSecretToolBackend].each do |klass|
|
|
471
|
+
return klass.new if klass.available?
|
|
472
|
+
end
|
|
473
|
+
FileBackend.new
|
|
474
|
+
end
|
|
475
|
+
|
|
476
|
+
# Process-unique path so parallel test workers cannot observe each
|
|
477
|
+
# other's writes; stays under the OS tempdir, never the worktree.
|
|
478
|
+
def self.test_file_backend_path
|
|
479
|
+
File.join(Dir.tmpdir, "rubik-test-secrets-#{Process.pid}.yml")
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
def self.backend_class_name(backend_obj)
|
|
483
|
+
case backend_obj
|
|
484
|
+
when MacosKeychainBackend then :macos_keychain
|
|
485
|
+
when WindowsCredManBackend then :windows_credman
|
|
486
|
+
when LinuxSecretToolBackend then :linux_secret_service
|
|
487
|
+
else :file
|
|
488
|
+
end
|
|
489
|
+
end
|
|
490
|
+
end
|
|
491
|
+
end
|
|
492
|
+
end
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rubik
|
|
4
|
+
module Security
|
|
5
|
+
# Structured guidance-evidence builder for blocked dangerous commands
|
|
6
|
+
# (#500): every innermost dangerous segment with its trigger, the sibling
|
|
7
|
+
# commands that passed, a one-line wrapper preview, and the allowlist
|
|
8
|
+
# entries that would actually open the gate. A pure function from the
|
|
9
|
+
# classified command tree to a frozen plain-data hash — deliberately
|
|
10
|
+
# loadable WITHOUT the classifier stack, so BlockedWriteError guidance
|
|
11
|
+
# never drags the classifier in. nil when no single command is dangerous
|
|
12
|
+
# (expression-level elevations keep the scalar rule_name guidance path).
|
|
13
|
+
module SegmentReport
|
|
14
|
+
# Display caps for the blocked-segment report (guidance rendering, not
|
|
15
|
+
# gate semantics): one segment's argv rendering and the one-line
|
|
16
|
+
# wrapper preview never exceed these.
|
|
17
|
+
SEGMENT_DISPLAY_CAP = 120
|
|
18
|
+
WRAPPER_PREVIEW_CAP = 60
|
|
19
|
+
|
|
20
|
+
# Build the report for the given classified command tree; see the
|
|
21
|
+
# module comment for shape and the nil contract.
|
|
22
|
+
def build(commands)
|
|
23
|
+
segments = []
|
|
24
|
+
safe = []
|
|
25
|
+
safe_top_argv = []
|
|
26
|
+
wrappers = []
|
|
27
|
+
collect_dangerous_units(commands, 0, segments, safe, safe_top_argv, wrappers)
|
|
28
|
+
return nil if segments.empty?
|
|
29
|
+
|
|
30
|
+
# The gate (command_allowlisted?) matches EVERY top-level command,
|
|
31
|
+
# safe ones included — the entry set must cover them all to be
|
|
32
|
+
# honest: wrapper prefix + top-level dangerous segments + top-level
|
|
33
|
+
# safe units.
|
|
34
|
+
entries = wrappers.map { |w| wrapper_allowlist_entry(w) }
|
|
35
|
+
entries += segments.select { |s| s[:depth].zero? }.map { |s| allowlist_entry_for_argv(s[:argv]) }
|
|
36
|
+
entries += safe_top_argv.map { |argv| allowlist_entry_for_argv(argv) }
|
|
37
|
+
|
|
38
|
+
{
|
|
39
|
+
segments: segments,
|
|
40
|
+
safe: safe.uniq,
|
|
41
|
+
wrapper_preview: wrappers.empty? ? nil : wrapper_preview(wrappers.first),
|
|
42
|
+
allowlist_entries: entries.uniq
|
|
43
|
+
}.freeze
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Depth-first walk over the classified command tree. A dangerous
|
|
47
|
+
# command whose danger comes from dangerous inners contributes those
|
|
48
|
+
# inner leaves instead of itself (bash -c "rm -rf /" reports the inner
|
|
49
|
+
# rm, not the wrapper); a dangerous leaf is a segment; non-dangerous
|
|
50
|
+
# leaves join the passed roll call. Top-level wrappers (dangerous only
|
|
51
|
+
# through their inners) are tracked separately for preview + hint;
|
|
52
|
+
# top-level safe units keep their argv for the gate-honest entry set.
|
|
53
|
+
def collect_dangerous_units(commands, depth, segments, safe, safe_top_argv, wrappers)
|
|
54
|
+
Array(commands).each do |c|
|
|
55
|
+
inners = Array(c.inner_commands)
|
|
56
|
+
before = segments.size
|
|
57
|
+
collect_dangerous_units(inners, depth + 1, segments, safe, safe_top_argv, wrappers)
|
|
58
|
+
added = segments.size - before
|
|
59
|
+
|
|
60
|
+
if c.classification == :dangerous
|
|
61
|
+
if added.positive?
|
|
62
|
+
wrappers << c if depth.zero?
|
|
63
|
+
else
|
|
64
|
+
segments << dangerous_segment_data(c, depth)
|
|
65
|
+
end
|
|
66
|
+
elsif inners.empty?
|
|
67
|
+
safe << safe_segment_name(c)
|
|
68
|
+
safe_top_argv << c.argv.dup if depth.zero?
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def dangerous_segment_data(result, depth)
|
|
74
|
+
argv = Array(result.argv)
|
|
75
|
+
{
|
|
76
|
+
display: segment_display(display_tokens(argv, result)),
|
|
77
|
+
argv: argv.dup.freeze,
|
|
78
|
+
rule: result.matched_rule,
|
|
79
|
+
option: result.overriding_option,
|
|
80
|
+
reason: result.classification_reason,
|
|
81
|
+
depth: depth
|
|
82
|
+
}
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Redirect-bearing segments (delegated inners) render their effects,
|
|
86
|
+
# not just their argv: `echo pwned > /etc/shadow`, not `echo pwned`.
|
|
87
|
+
# Flat operator/target tokens keep segment_display's quoting correct;
|
|
88
|
+
# argv itself stays pure — allowlist entries derive from it.
|
|
89
|
+
def display_tokens(argv, result)
|
|
90
|
+
reds = result.respond_to?(:redirects) ? Array(result.redirects) : []
|
|
91
|
+
reds.empty? ? argv : argv + reds.flat_map { |r| [r.operator, r.target] }
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Subcommand-aware roll call of the units that passed ("git checkout",
|
|
95
|
+
# not just "git") — matches the operator's mental model of the script.
|
|
96
|
+
def safe_segment_name(result)
|
|
97
|
+
result.command.join(" ")
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# One-line wrapper preview for the guidance first line. The delegated
|
|
101
|
+
# payload (the whole -c script) is the argv token that contains
|
|
102
|
+
# whitespace — elide it so the full script is never re-embedded in the
|
|
103
|
+
# message (the defect this report exists to fix).
|
|
104
|
+
def wrapper_preview(result)
|
|
105
|
+
argv = Array(result.argv)
|
|
106
|
+
tokens = argv.dup
|
|
107
|
+
tokens[-1] = "'...'" if tokens.size > 1 && tokens[-1].to_s.match?(/\s/)
|
|
108
|
+
preview = segment_display(tokens)
|
|
109
|
+
preview.length > WRAPPER_PREVIEW_CAP ? "#{preview[0, WRAPPER_PREVIEW_CAP - 1]}..." : preview
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Shortest top-level argv prefix that actually satisfies the gate's
|
|
113
|
+
# token-prefix match (command_allowlisted?): the wrapper binary up to
|
|
114
|
+
# and including its delegation flag for flag-value payloads
|
|
115
|
+
# ("bash -c", "bash -e -c"), the bare binary for rest-argv wrappers
|
|
116
|
+
# ("sudo", "xargs"). Deriving it from delegation_source keeps the hint
|
|
117
|
+
# gate-honest for flag permutations a naive first-two-tokens guess
|
|
118
|
+
# would miss.
|
|
119
|
+
def wrapper_allowlist_entry(wrapper)
|
|
120
|
+
argv = Array(wrapper.argv)
|
|
121
|
+
return argv.first.to_s if argv.size <= 1
|
|
122
|
+
|
|
123
|
+
first_inner = Array(wrapper.inner_commands).first
|
|
124
|
+
if first_inner&.delegation_mode.to_s == "flag_value_is_expression"
|
|
125
|
+
idx = argv.index(first_inner.delegation_source)
|
|
126
|
+
return argv[0..idx].join(" ") if idx&.positive?
|
|
127
|
+
end
|
|
128
|
+
argv.first.to_s
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# An allowlist entry can only carry whitespace-free tokens:
|
|
132
|
+
# normalize_allowlist splits entries on whitespace, so an argv token
|
|
133
|
+
# containing spaces ("feature x") can never be matched by an entry.
|
|
134
|
+
# Prefix up to (excluding) the first such token.
|
|
135
|
+
def allowlist_entry_for_argv(argv)
|
|
136
|
+
prefix = []
|
|
137
|
+
Array(argv).each do |token|
|
|
138
|
+
break if token.to_s.match?(/\s/)
|
|
139
|
+
|
|
140
|
+
prefix << token
|
|
141
|
+
end
|
|
142
|
+
prefix.empty? ? Array(argv).first.to_s : prefix.join(" ")
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Display-only rendering: collapse newlines, single-quote tokens that
|
|
146
|
+
# contain whitespace, hard-cap length. Shellwords.escape would
|
|
147
|
+
# backslash-quote every token (noise on Windows paths) — quote
|
|
148
|
+
# conditionally instead.
|
|
149
|
+
def segment_display(argv)
|
|
150
|
+
text = argv.map { |t| t.to_s.tr("\r\n", " ") }
|
|
151
|
+
.map { |t| t.match?(/\s/) ? "'#{t}'" : t }
|
|
152
|
+
.join(" ")
|
|
153
|
+
text.length > SEGMENT_DISPLAY_CAP ? "#{text[0, SEGMENT_DISPLAY_CAP - 1]}..." : text
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
module_function :build, :collect_dangerous_units, :dangerous_segment_data, :display_tokens,
|
|
157
|
+
:safe_segment_name, :wrapper_preview, :wrapper_allowlist_entry,
|
|
158
|
+
:allowlist_entry_for_argv, :segment_display
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "../log"
|
|
3
4
|
require_relative "errors"
|
|
4
5
|
|
|
5
6
|
module Rubik
|
|
@@ -181,21 +182,17 @@ module Rubik
|
|
|
181
182
|
# spawn-boundary re-check). A guard lacking verify_synthesis_safety!
|
|
182
183
|
# is downgraded to the shell-only gate — the duck-typed fallback is a
|
|
183
184
|
# test-double affordance, but skipping the entire AST deny surface is
|
|
184
|
-
# security-observable, so the downgrade is loud.
|
|
185
|
-
#
|
|
186
|
-
#
|
|
187
|
-
#
|
|
188
|
-
#
|
|
185
|
+
# security-observable, so the downgrade is loud. Routed through
|
|
186
|
+
# Rubik::Log (:security, issue #452): Log.emit uses $stderr.puts, not
|
|
187
|
+
# Kernel#warn, so the line survives $VERBOSE=nil (bundler/rake -W0),
|
|
188
|
+
# and Log's error isolation absorbs a broken emit target — the gate
|
|
189
|
+
# then proceeds through verify_command_safety!, whose own failures
|
|
190
|
+
# still fail closed.
|
|
189
191
|
def self.run_synthesis_gate(guard, code)
|
|
190
192
|
if guard.respond_to?(:verify_synthesis_safety!)
|
|
191
193
|
guard.verify_synthesis_safety!(code)
|
|
192
194
|
else
|
|
193
|
-
|
|
194
|
-
$stderr.puts "[Synthesize] guard lacks verify_synthesis_safety!; downgraded to shell-only gate" # rubocop:disable Style/StderrPuts
|
|
195
|
-
rescue StandardError => e
|
|
196
|
-
raise BlockedWriteError,
|
|
197
|
-
"guard downgrade warn could not be written to stderr (#{e.class}: #{e.message}); refusing to eval"
|
|
198
|
-
end
|
|
195
|
+
Rubik::Log.warn(:security, "[Synthesize] guard lacks verify_synthesis_safety!; downgraded to shell-only gate")
|
|
199
196
|
guard.verify_command_safety!(code)
|
|
200
197
|
end
|
|
201
198
|
end
|