rubik 0.9.0 → 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 +250 -43
- data/README.md +5 -1
- data/lib/rubik/adapter/base.rb +21 -1
- data/lib/rubik/adapter/generic.rb +0 -14
- data/lib/rubik/adapter/node.rb +2 -0
- data/lib/rubik/adapter/process.rb +28 -67
- data/lib/rubik/ast/treesitter/zig_walker.rb +15 -2
- 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/cli/workspace_trust.rb +16 -0
- data/lib/rubik/config/deep_ops.rb +4 -1
- data/lib/rubik/config/defaults.rb +29 -3
- data/lib/rubik/config/schema.rb +8 -0
- data/lib/rubik/config/secret_policy.rb +197 -0
- data/lib/rubik/config/store.rb +14 -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 +16 -11
- 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 +23 -1
- data/lib/rubik/lsp/client.rb +152 -445
- data/lib/rubik/lsp/document_store.rb +209 -0
- 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/client.rb +4 -3
- data/lib/rubik/mcp/manager.rb +46 -165
- data/lib/rubik/mcp/transport/stdio.rb +55 -10
- 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 +519 -0
- 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 +11 -12
- data/lib/rubik/orchestrator/subagent/journal.rb +2 -1
- data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
- data/lib/rubik/orchestrator/subagent/questioner.rb +24 -7
- 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 +11 -3
- data/lib/rubik/orchestrator/subagent/teardown.rb +4 -0
- 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/orchestrator.rb +1 -0
- data/lib/rubik/platform/io_drain.rb +2 -1
- data/lib/rubik/platform/process.rb +144 -54
- data/lib/rubik/platform.rb +17 -1
- data/lib/rubik/plugin/manager.rb +2 -1
- data/lib/rubik/plugin/storage.rb +20 -29
- data/lib/rubik/repl/commands/agent_context.rb +9 -8
- data/lib/rubik/repl/commands/agent_execution.rb +20 -502
- 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 +6 -5
- data/lib/rubik/repl/commands/lsp.rb +3 -2
- 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 +3 -2
- data/lib/rubik/repl/lifecycle.rb +82 -204
- 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 +124 -0
- data/lib/rubik/runtime/overlay_guard.rb +127 -0
- data/lib/rubik/runtime/overlay_storage.rb +269 -272
- data/lib/rubik/runtime/task_manager.rb +71 -27
- data/lib/rubik/runtime.rb +1 -0
- data/lib/rubik/sandbox/driver/docker.rb +5 -1
- data/lib/rubik/sandbox/runner.rb +16 -15
- data/lib/rubik/security/allowlist.rb +167 -0
- data/lib/rubik/security/audit_log.rb +72 -0
- data/lib/rubik/security/classifier/classifier.rb +8 -0
- data/lib/rubik/security/command_gate.rb +591 -0
- data/lib/rubik/security/guard.rb +94 -1077
- data/lib/rubik/security/path_policy.rb +117 -0
- data/lib/rubik/security/secret_store.rb +463 -0
- data/lib/rubik/security/synthesis_gate.rb +297 -0
- data/lib/rubik/security.rb +5 -0
- data/lib/rubik/session/manager.rb +7 -1
- data/lib/rubik/skills/manager.rb +2 -3
- data/lib/rubik/storage/ranking.rb +113 -0
- data/lib/rubik/storage/search.rb +49 -0
- data/lib/rubik/storage/store.rb +15 -112
- data/lib/rubik/storage/transcript_indexer.rb +2 -3
- 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 +3 -9
- data/lib/rubik/tools/builtins/lifecycle.rb +96 -92
- data/lib/rubik/tools/builtins/synthesis.rb +3 -0
- data/lib/rubik/tools/lifecycle_rollbacks.rb +100 -0
- data/lib/rubik/tools/native_search.rb +2 -1
- data/lib/rubik/tools/truncator.rb +73 -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 +23 -1
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../platform"
|
|
4
|
+
require_relative "errors"
|
|
5
|
+
|
|
6
|
+
module Rubik
|
|
7
|
+
module Security
|
|
8
|
+
# Critical-path write policy, split out of Guard (#403): the default
|
|
9
|
+
# critical patterns, the path-write verifier, the workspace-root
|
|
10
|
+
# verifier, and the command write-target walk. Receives the shared
|
|
11
|
+
# AuditLog instance — every block decision is audit-logged there.
|
|
12
|
+
class PathPolicy
|
|
13
|
+
DEFAULT_CRITICAL_PATTERNS = [
|
|
14
|
+
".git/**",
|
|
15
|
+
".rubik/**",
|
|
16
|
+
"**/.rubik/**",
|
|
17
|
+
".env*",
|
|
18
|
+
"secrets.*",
|
|
19
|
+
"*.pem",
|
|
20
|
+
"*.key",
|
|
21
|
+
"id_rsa*"
|
|
22
|
+
].freeze
|
|
23
|
+
|
|
24
|
+
def initialize(audit:, custom_patterns: [])
|
|
25
|
+
@critical_patterns = (DEFAULT_CRITICAL_PATTERNS + custom_patterns).uniq
|
|
26
|
+
@audit = audit
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def verify_path_write!(path)
|
|
30
|
+
return true if path.nil? || path.to_s.strip.empty?
|
|
31
|
+
|
|
32
|
+
raw = Rubik::Platform.normalize_path_raw(path.to_s)
|
|
33
|
+
expanded = Rubik::Platform.normalize_path_raw(File.expand_path(path.to_s))
|
|
34
|
+
|
|
35
|
+
# Check raw relative path, expanded path, and relative-from-pwd
|
|
36
|
+
candidates = [raw, expanded]
|
|
37
|
+
begin
|
|
38
|
+
pwd_raw = Rubik::Platform.normalize_path_raw(Dir.pwd)
|
|
39
|
+
candidates << expanded.delete_prefix("#{pwd_raw}/") if expanded.start_with?("#{pwd_raw}/")
|
|
40
|
+
rescue SystemCallError, ArgumentError
|
|
41
|
+
nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
candidates.uniq.each do |c|
|
|
45
|
+
if critical_path?(c)
|
|
46
|
+
@audit.log_audit("blocked_write", path: path.to_s, reason: "critical_path")
|
|
47
|
+
raise BlockedWriteError, "Write blocked on critical path: #{path}"
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Symlink re-check (fail-closed): the lexical candidates above cannot
|
|
52
|
+
# see `foo -> .git/config`. Resolve the deepest EXISTING ancestor and
|
|
53
|
+
# re-check; a missing tail is reattached lexically so ordinary
|
|
54
|
+
# new-file writes resolve to their lexical equivalent. A nil here
|
|
55
|
+
# means containment is unprovable (symlink loop / permission wall).
|
|
56
|
+
resolved = Rubik::Platform.resolved_path(path.to_s, strict: true)
|
|
57
|
+
if resolved.nil?
|
|
58
|
+
@audit.log_audit("blocked_write", path: path.to_s, reason: "path_resolution_failed")
|
|
59
|
+
raise BlockedWriteError, "Write blocked: path could not be safely resolved: #{path}"
|
|
60
|
+
end
|
|
61
|
+
if critical_path?(resolved)
|
|
62
|
+
@audit.log_audit("blocked_write", path: path.to_s, reason: "critical_path_symlink", resolved: resolved)
|
|
63
|
+
raise BlockedWriteError, "Write blocked on critical path (symlink-resolved): #{path} -> #{resolved}"
|
|
64
|
+
end
|
|
65
|
+
true
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def verify_root_safety!(path)
|
|
69
|
+
return true if path.nil? || path.to_s.strip.empty?
|
|
70
|
+
|
|
71
|
+
expanded = File.expand_path(path.to_s)
|
|
72
|
+
normalized = Rubik::Platform.normalize_path(expanded)
|
|
73
|
+
dangerous_roots = Rubik::Platform.dangerous_system_roots
|
|
74
|
+
|
|
75
|
+
if dangerous_roots.include?(normalized) ||
|
|
76
|
+
Rubik::Platform.drive_root?(expanded) ||
|
|
77
|
+
Rubik::Platform.path_equal?(expanded, Rubik::Platform.home_dir)
|
|
78
|
+
@audit.log_audit("blocked_root_addition", path: expanded, reason: "dangerous_system_root")
|
|
79
|
+
raise BlockedWriteError, "Adding dangerous system root as workspace is blocked: #{path} (#{expanded})"
|
|
80
|
+
end
|
|
81
|
+
true
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def critical_path?(path)
|
|
85
|
+
norm_path = path.to_s.tr("\\", "/").gsub(%r{^\./}, "")
|
|
86
|
+
@critical_patterns.any? do |pattern|
|
|
87
|
+
File.fnmatch?(pattern, norm_path, File::FNM_PATHNAME | File::FNM_DOTMATCH) ||
|
|
88
|
+
File.fnmatch?("**/#{pattern}", norm_path, File::FNM_PATHNAME | File::FNM_DOTMATCH)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def check_critical_path_writes!(ast, cmd_str)
|
|
93
|
+
ast_commands = ast.respond_to?(:commands) ? ast.commands.to_a : []
|
|
94
|
+
write_targets = ast_commands.flat_map do |c|
|
|
95
|
+
targets = Array(c.respond_to?(:write_paths) ? c.write_paths : nil)
|
|
96
|
+
cmd_name = c.respond_to?(:command) ? c.command.to_a.first.to_s : ""
|
|
97
|
+
targets.concat(c.positionals.to_a) if ["cp", "mv", "tee", "touch", "ln", "install"].include?(cmd_name) && c.respond_to?(:positionals)
|
|
98
|
+
targets
|
|
99
|
+
end.compact.reject(&:empty?)
|
|
100
|
+
|
|
101
|
+
write_targets.concat(ast.write_paths.to_a) if ast.respond_to?(:write_paths)
|
|
102
|
+
|
|
103
|
+
write_targets.uniq.each do |wp|
|
|
104
|
+
next unless critical_path?(wp)
|
|
105
|
+
|
|
106
|
+
@audit.log_audit("blocked_command", command: cmd_str, reason: "critical_path_write", path: wp)
|
|
107
|
+
raise BlockedWriteError.new(
|
|
108
|
+
"Command write to critical path blocked: #{wp} in `#{cmd_str}`",
|
|
109
|
+
kind: :critical_path_write,
|
|
110
|
+
blocked_command: cmd_str,
|
|
111
|
+
path: wp
|
|
112
|
+
)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
@@ -0,0 +1,463 @@
|
|
|
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
|
+
private
|
|
131
|
+
|
|
132
|
+
# Single spawn point for every backend: receives a fully-formed
|
|
133
|
+
# argument vector (never a shell string) plus optional stdin payload.
|
|
134
|
+
def run(argv, input: nil, env: nil)
|
|
135
|
+
env_hash = env.is_a?(Hash) ? env : {}
|
|
136
|
+
merged = ENV.to_h.merge(env_hash)
|
|
137
|
+
Open3.popen3(merged, *argv) do |pin, pout, perr, waiter|
|
|
138
|
+
pin.write(input.to_s)
|
|
139
|
+
pin.close
|
|
140
|
+
[pout.read, perr.read, waiter.value]
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
class MacosKeychainBackend < CommandBackend
|
|
146
|
+
SECURITY = "/usr/bin/security"
|
|
147
|
+
|
|
148
|
+
def self.available?
|
|
149
|
+
Rubik::Platform.macos? && File.executable?(SECURITY)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def write(name, value)
|
|
153
|
+
out, err, st = run(security_write_argv(name), input: value)
|
|
154
|
+
return true if st.success?
|
|
155
|
+
|
|
156
|
+
raise Error, "keychain write failed (#{st.exitstatus}): #{err} #{out}"
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def read(name)
|
|
160
|
+
out, _err, st = run(security_read_argv(name))
|
|
161
|
+
# 44 = errSecItemNotFound (honest miss); any other nonzero exit is
|
|
162
|
+
# a denied/failed lookup and must not surface as an empty key.
|
|
163
|
+
return nil unless st.success? || st.exitstatus == 44
|
|
164
|
+
|
|
165
|
+
val = out.strip
|
|
166
|
+
val.empty? ? nil : val
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def delete(name)
|
|
170
|
+
_out, _err, _st = run(security_delete_argv(name))
|
|
171
|
+
true
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
private
|
|
175
|
+
|
|
176
|
+
# The security CLI owns this contract: "-w" with no value asks the
|
|
177
|
+
# tool to read the password from its standard input, so the secret
|
|
178
|
+
# never rides argv or `ps` output.
|
|
179
|
+
def security_write_argv(name)
|
|
180
|
+
[SECURITY, "add-generic-password", "-U", "-s", SERVICE, "-a", name, "-w"]
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def security_read_argv(name)
|
|
184
|
+
[SECURITY, "find-generic-password", "-s", SERVICE, "-a", name, "-w"]
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def security_delete_argv(name)
|
|
188
|
+
[SECURITY, "delete-generic-password", "-s", SERVICE, "-a", name]
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
class WindowsCredManBackend < CommandBackend
|
|
193
|
+
PS = "powershell.exe"
|
|
194
|
+
|
|
195
|
+
def self.available?
|
|
196
|
+
Rubik::Platform.windows? && WindowsCredManBackend.find_in_path(PS)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def self.find_in_path(binary)
|
|
200
|
+
ENV["PATH"].to_s.split(File::PATH_SEPARATOR).any? do |dir|
|
|
201
|
+
File.executable?(File.join(dir, binary))
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def write(name, value)
|
|
206
|
+
# The secret travels base64-encoded through the environment into a
|
|
207
|
+
# FIXED script: no caller-controlled text is interpolated into any
|
|
208
|
+
# command string, and the base64 alphabet cannot carry metacharacters.
|
|
209
|
+
payload = {
|
|
210
|
+
"RUBIK_SECRET_NAME" => name,
|
|
211
|
+
"RUBIK_SECRET_VALUE_B64" => [value].pack("m0")
|
|
212
|
+
}
|
|
213
|
+
_out, err, st = run(write_script_argv, env: payload)
|
|
214
|
+
# $ErrorActionPreference = 'Stop' makes the script exit nonzero on
|
|
215
|
+
# any vault failure; ignoring the status would hand back a dangling
|
|
216
|
+
# pointer while reporting success.
|
|
217
|
+
return true if st.success?
|
|
218
|
+
|
|
219
|
+
raise Error, "PasswordVault write failed (#{st.exitstatus}): #{err.to_s.strip}"
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def read(name)
|
|
223
|
+
out, = run(read_script_argv, env: name_payload(name))
|
|
224
|
+
encoded = out.to_s.strip
|
|
225
|
+
encoded.empty? ? nil : encoded.unpack1("m0").force_encoding(Encoding::UTF_8)
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def delete(name)
|
|
229
|
+
run(delete_script_argv, env: name_payload(name))
|
|
230
|
+
true
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# FIXED scripts (frozen at load, no interpolation): WinRT
|
|
234
|
+
# PasswordVault via Windows PowerShell 5.1, present on every
|
|
235
|
+
# supported Windows release. cmdkey cannot read secrets back, so the
|
|
236
|
+
# vault API is the only stdlib-reachable read path.
|
|
237
|
+
WRITE_PS = <<~PS
|
|
238
|
+
$ErrorActionPreference = 'Stop'
|
|
239
|
+
[void][Windows.Security.Credentials.PasswordVault,Windows.Foundation,ContentType=WindowsRuntime]
|
|
240
|
+
$v = New-Object Windows.Security.Credentials.PasswordVault
|
|
241
|
+
$p = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($env:RUBIK_SECRET_VALUE_B64))
|
|
242
|
+
$c = New-Object Windows.Security.Credentials.PasswordCredential('rubik', $env:RUBIK_SECRET_NAME, $p)
|
|
243
|
+
$v.Add($c)
|
|
244
|
+
Write-Output 'ok'
|
|
245
|
+
PS
|
|
246
|
+
|
|
247
|
+
READ_PS = <<~PS
|
|
248
|
+
$ErrorActionPreference = 'Stop'
|
|
249
|
+
[void][Windows.Security.Credentials.PasswordVault,Windows.Foundation,ContentType=WindowsRuntime]
|
|
250
|
+
$v = New-Object Windows.Security.Credentials.PasswordVault
|
|
251
|
+
$c = $v.Retrieve('rubik', $env:RUBIK_SECRET_NAME)
|
|
252
|
+
$c.RetrievePassword()
|
|
253
|
+
Write-Output ([Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($c.Password)))
|
|
254
|
+
PS
|
|
255
|
+
|
|
256
|
+
DELETE_PS = <<~PS
|
|
257
|
+
[void][Windows.Security.Credentials.PasswordVault,Windows.Foundation,ContentType=WindowsRuntime]
|
|
258
|
+
$v = New-Object Windows.Security.Credentials.PasswordVault
|
|
259
|
+
try { $c = $v.Retrieve('rubik', $env:RUBIK_SECRET_NAME); $v.Remove($c) } catch { }
|
|
260
|
+
Write-Output 'ok'
|
|
261
|
+
PS
|
|
262
|
+
|
|
263
|
+
private
|
|
264
|
+
|
|
265
|
+
def name_payload(name)
|
|
266
|
+
{ "RUBIK_SECRET_NAME" => name }
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def write_script_argv
|
|
270
|
+
[PS, "-NoProfile", "-NonInteractive", "-Command", WRITE_PS]
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def read_script_argv
|
|
274
|
+
[PS, "-NoProfile", "-NonInteractive", "-Command", READ_PS]
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def delete_script_argv
|
|
278
|
+
[PS, "-NoProfile", "-NonInteractive", "-Command", DELETE_PS]
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
class LinuxSecretToolBackend < CommandBackend
|
|
283
|
+
TOOL = "secret-tool"
|
|
284
|
+
|
|
285
|
+
def self.available?
|
|
286
|
+
return false unless Rubik::Platform.linux? && LinuxSecretToolBackend.find_in_path(TOOL)
|
|
287
|
+
|
|
288
|
+
# Canary probe: headless boxes ship the binary without a running
|
|
289
|
+
# secret service ("Cannot autolaunch D-Bus"), so binary presence
|
|
290
|
+
# alone lies. A missed lookup (exit 1) is a working service; a
|
|
291
|
+
# crashed lookup (other nonzero) is not.
|
|
292
|
+
new.canary_ok?
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def self.find_in_path(binary)
|
|
296
|
+
ENV["PATH"].to_s.split(File::PATH_SEPARATOR).any? do |dir|
|
|
297
|
+
File.executable?(File.join(dir, binary))
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def write(name, value)
|
|
302
|
+
_out, err, st = run(store_argv(name), input: value)
|
|
303
|
+
return true if st.success?
|
|
304
|
+
|
|
305
|
+
raise Error, "secret-tool store failed (#{st.exitstatus}): #{err}"
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def read(name)
|
|
309
|
+
out, = run(lookup_argv(name))
|
|
310
|
+
val = out.to_s.strip
|
|
311
|
+
val.empty? ? nil : val
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def delete(name)
|
|
315
|
+
run(clear_argv(name))
|
|
316
|
+
true
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def canary_ok?
|
|
320
|
+
_out, _err, st = run(lookup_argv("rubik-availability-probe"))
|
|
321
|
+
st.exitstatus == 1 || st.success?
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
private
|
|
325
|
+
|
|
326
|
+
# The secret travels on stdin — never argv — per secret-service
|
|
327
|
+
# convention; attrs (service, account) name the lookup key.
|
|
328
|
+
def store_argv(name)
|
|
329
|
+
[TOOL, "store", "--label=rubik", "service", SERVICE, "account", name]
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
def lookup_argv(name)
|
|
333
|
+
[TOOL, "lookup", "service", SERVICE, "account", name]
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
def clear_argv(name)
|
|
337
|
+
[TOOL, "clear", "service", SERVICE, "account", name]
|
|
338
|
+
end
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
# The always-present fallback: owner-only store next to the global
|
|
342
|
+
# config, created 0600 from the first byte (same discipline as
|
|
343
|
+
# Config::Store#ensure_owner_only!), atomic replace on every write.
|
|
344
|
+
class FileBackend
|
|
345
|
+
def initialize(path = default_path)
|
|
346
|
+
@path = path
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
attr_reader :path
|
|
350
|
+
|
|
351
|
+
def write(name, value)
|
|
352
|
+
ensure_owner_only!
|
|
353
|
+
data = read_all
|
|
354
|
+
data[name] = value
|
|
355
|
+
atomic_replace(data)
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
def read(name)
|
|
359
|
+
read_all[name]
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def delete(name)
|
|
363
|
+
data = read_all
|
|
364
|
+
data.delete(name)
|
|
365
|
+
atomic_replace(data)
|
|
366
|
+
true
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
private
|
|
370
|
+
|
|
371
|
+
def read_all
|
|
372
|
+
return {} unless File.exist?(@path)
|
|
373
|
+
|
|
374
|
+
parsed = YAML.safe_load_file(@path, permitted_classes: [], aliases: false)
|
|
375
|
+
parsed.is_a?(Hash) ? parsed : {}
|
|
376
|
+
rescue Psych::Exception
|
|
377
|
+
{}
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
def atomic_replace(data)
|
|
381
|
+
dir = File.dirname(@path)
|
|
382
|
+
FileUtils.mkdir_p(dir)
|
|
383
|
+
# Tempfile.new (NOT the block form of Tempfile.create, which
|
|
384
|
+
# unlinks the file when the block exits): the temp file must
|
|
385
|
+
# SURVIVE until the rename, and must be CLOSED before it —
|
|
386
|
+
# Windows refuses to rename an open file.
|
|
387
|
+
tmp = Tempfile.new("rubik-secrets-", dir)
|
|
388
|
+
begin
|
|
389
|
+
tmp.write(YAML.dump(data))
|
|
390
|
+
tmp.flush
|
|
391
|
+
tmp.chmod(0o600)
|
|
392
|
+
tmp.close
|
|
393
|
+
File.rename(tmp.path, @path)
|
|
394
|
+
rescue SystemCallError
|
|
395
|
+
# Windows rename can still refuse an existing destination:
|
|
396
|
+
# fall back to a short delete-then-rename window.
|
|
397
|
+
tmp.close
|
|
398
|
+
FileUtils.rm_f(@path)
|
|
399
|
+
File.rename(tmp.path, @path)
|
|
400
|
+
ensure
|
|
401
|
+
tmp.close
|
|
402
|
+
tmp.unlink
|
|
403
|
+
end
|
|
404
|
+
rescue SystemCallError => e
|
|
405
|
+
raise Error, "secret file store write failed: #{e.message}"
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
def ensure_owner_only!
|
|
409
|
+
FileUtils.touch(@path) unless File.exist?(@path)
|
|
410
|
+
File.chmod(0o600, @path)
|
|
411
|
+
rescue SystemCallError
|
|
412
|
+
nil
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
def default_path
|
|
416
|
+
home = if defined?(Rubik::Platform) && Rubik::Platform.respond_to?(:home_dir)
|
|
417
|
+
begin
|
|
418
|
+
Rubik::Platform.home_dir
|
|
419
|
+
rescue StandardError
|
|
420
|
+
nil
|
|
421
|
+
end
|
|
422
|
+
else
|
|
423
|
+
ENV["USERPROFILE"] || ENV.fetch("HOME", nil)
|
|
424
|
+
end
|
|
425
|
+
# No "." fallback: an owner-only secret store must never land in
|
|
426
|
+
# the working tree just because HOME is unset.
|
|
427
|
+
raise Error, "no home directory available for the secret file store (set HOME/USERPROFILE)" if home.nil? || home.to_s.strip.empty?
|
|
428
|
+
|
|
429
|
+
File.join(home.to_s, ".rubik", "secrets.yml")
|
|
430
|
+
end
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
def self.select_backend
|
|
434
|
+
# Test armor (RUBIK_TEST=1, the repo-wide convention): a test run
|
|
435
|
+
# must NEVER touch the host keyring, even through an un-injected
|
|
436
|
+
# path — an interactive-authorization `security` spawn on a headless
|
|
437
|
+
# CI runner hangs forever. Un-injected tests get the owner-only
|
|
438
|
+
# file store in a temp location instead.
|
|
439
|
+
return FileBackend.new(test_file_backend_path) if ENV["RUBIK_TEST"] == "1"
|
|
440
|
+
|
|
441
|
+
[MacosKeychainBackend, WindowsCredManBackend, LinuxSecretToolBackend].each do |klass|
|
|
442
|
+
return klass.new if klass.available?
|
|
443
|
+
end
|
|
444
|
+
FileBackend.new
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
# Process-unique path so parallel test workers cannot observe each
|
|
448
|
+
# other's writes; stays under the OS tempdir, never the worktree.
|
|
449
|
+
def self.test_file_backend_path
|
|
450
|
+
File.join(Dir.tmpdir, "rubik-test-secrets-#{Process.pid}.yml")
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
def self.backend_class_name(backend_obj)
|
|
454
|
+
case backend_obj
|
|
455
|
+
when MacosKeychainBackend then :macos_keychain
|
|
456
|
+
when WindowsCredManBackend then :windows_credman
|
|
457
|
+
when LinuxSecretToolBackend then :linux_secret_service
|
|
458
|
+
else :file
|
|
459
|
+
end
|
|
460
|
+
end
|
|
461
|
+
end
|
|
462
|
+
end
|
|
463
|
+
end
|