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
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../platform"
|
|
4
|
+
require_relative "../log"
|
|
5
|
+
|
|
6
|
+
module Rubik
|
|
7
|
+
module Runtime
|
|
8
|
+
# Guard/trust plane of the overlay storage (issue #447), single-homed here
|
|
9
|
+
# so Runtime::OverlayStorage stays the manifest/lifecycle coordinator:
|
|
10
|
+
#
|
|
11
|
+
# * Path containment: ensure_valid_overlay_dir! / ensure_contained_path! /
|
|
12
|
+
# safe_path_under? — the lexical + resolved containment policy over the
|
|
13
|
+
# overlay directories.
|
|
14
|
+
# * Trust/bootstrap screening: trust_mode?, bootstrap_opt_in? and
|
|
15
|
+
# screen_bootstrap_content — the fail-closed acceptance gate a sentinel
|
|
16
|
+
# file must pass before it is pinned into a manifest.
|
|
17
|
+
#
|
|
18
|
+
# Composition contract (the guard kwarg seam must keep working): the
|
|
19
|
+
# synthesis guard is NOT owned here — OverlayStorage forwards its own
|
|
20
|
+
# `guard:` kwarg at construction, so an injected guard (production: the
|
|
21
|
+
# service catalog's single operator-policy guard; tests: spies) is the
|
|
22
|
+
# one used, never bypassed. The lazy Rubik::Security::Guard default is
|
|
23
|
+
# deliberate (unchanged): overlay_storage.rb is a standalone-requireable
|
|
24
|
+
# file; security/guard loads only when screening actually happens, so no
|
|
25
|
+
# load-order audit is needed. Trust mode is likewise not re-read here:
|
|
26
|
+
# OverlayStorage injects the ManifestStore seam (issue #426) as a
|
|
27
|
+
# callable, so the plugin and overlay gates keep their single ENV read.
|
|
28
|
+
class OverlayGuard
|
|
29
|
+
# allowed_dirs: the [[overlay_dir, trusted_root], ...] table the storage
|
|
30
|
+
# derives from its own directory layout (it owns the layout; the guard
|
|
31
|
+
# owns the enforcement).
|
|
32
|
+
def initialize(allowed_dirs:, guard:, trust_mode:)
|
|
33
|
+
@allowed_dirs = allowed_dirs
|
|
34
|
+
@guard = guard
|
|
35
|
+
@trust_mode = trust_mode
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# ── Path containment (moved verbatim from OverlayStorage, issue #447) ──
|
|
39
|
+
|
|
40
|
+
def ensure_valid_overlay_dir!(dir)
|
|
41
|
+
valid = @allowed_dirs.any? do |allowed_dir, trusted_root|
|
|
42
|
+
Platform.path_equal?(dir, allowed_dir) && Platform.resolved_path_under?(dir, trusted_root)
|
|
43
|
+
end
|
|
44
|
+
return if valid
|
|
45
|
+
|
|
46
|
+
raise ArgumentError, "Invalid overlay directory: #{dir}"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def ensure_contained_path!(path, parent_dir)
|
|
50
|
+
raise ArgumentError, "Path traversal attempt detected: #{path}" unless safe_path_under?(path, parent_dir)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def safe_path_under?(path, parent_dir)
|
|
54
|
+
return false if path.nil? || parent_dir.nil?
|
|
55
|
+
|
|
56
|
+
ensure_valid_overlay_dir!(parent_dir)
|
|
57
|
+
|
|
58
|
+
# Lexical containment check first
|
|
59
|
+
return false unless Platform.path_under?(path, parent_dir)
|
|
60
|
+
|
|
61
|
+
# If path exists or parent exists, also check resolved path containment
|
|
62
|
+
return Platform.resolved_path_under?(path, parent_dir) if File.exist?(path) || File.exist?(parent_dir)
|
|
63
|
+
|
|
64
|
+
true
|
|
65
|
+
rescue StandardError
|
|
66
|
+
false
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# ── Trust / bootstrap screening (moved verbatim from OverlayStorage, issue #447) ──
|
|
70
|
+
|
|
71
|
+
# Ortak kaçış kapısı predicate'i: iki güvenlik kapısının (verify_integrity,
|
|
72
|
+
# verified_dir_entries) kopya ENV kontrolüyle sürünerek ayrışmasını engeller.
|
|
73
|
+
# Tek seam ManifestStore'da yaşar (issue #426); plugin tarafı da aynı kapıdan geçer.
|
|
74
|
+
def trust_mode?
|
|
75
|
+
@trust_mode.call
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# İlk-bootstrap açık rızası: manifest'siz dizindeki sentinel dosyaların ilk taramada
|
|
79
|
+
# kabul edilmesi ancak bu env ile mümkündür (kayıt kalıcıdır; sonraki açılışlar env'siz çalışır).
|
|
80
|
+
def bootstrap_opt_in?
|
|
81
|
+
ENV["RUBIK_BOOTSTRAP_OVERLAYS"] == "1"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Aday 07'den ertelenen güvenlik kalemi: manifestsız dizindeki sentinel
|
|
85
|
+
# dosya manifest'e yazılmadan önce synthesis guard'ından geçirilir.
|
|
86
|
+
# Reddedilen (veya taranamayan) dosya manifest'e YAZILMAZ — sonraki tarama
|
|
87
|
+
# yeniden tarar ve reddeder (sessiz heal, bozuk-manifest doktriniyle
|
|
88
|
+
# çelişir); load_all/load_tools'un mevcut :rejected atlaması devreye girer.
|
|
89
|
+
# Parse hatası guard içinde yutulur (çalıştırılamaz kod güvenlik olayı
|
|
90
|
+
# değil); guard'ın kendisi patlarsa da dosya fail-closed reddedilir —
|
|
91
|
+
# SystemStackError dahil: rakip içerikte derin yuvalama AST walk'ı
|
|
92
|
+
# taşırebilir, tek dosyanın reddiyesi tüm taramayı çökertmemeli.
|
|
93
|
+
# Bilinçli asimetri: dosyanın TAMAMI (Description/Schema başlığı dahil)
|
|
94
|
+
# taranır — sentez anında yalnızca kod taranıyordu. Kabul kapısı daha
|
|
95
|
+
# sıkı olmak güvenli yöndür; başlık metnine denk gelen nadir yanlış
|
|
96
|
+
# pozitif dosyayı makinede yeniden kayıtla çözülür.
|
|
97
|
+
# Emit doktrini (guard.rb run_synthesis_gate hükmü): güvenlik kararının
|
|
98
|
+
# tek kullanıcı-yüzü izi Log.warn ile yazılır — $VERBOSE=nil altında da
|
|
99
|
+
# yaşar (Log.emit $stderr.puts kullanır, Kernel#warn değil) ve varsayılan
|
|
100
|
+
# RUBIK_LOG_LEVEL=warn'da her zaman basılır (issue #452).
|
|
101
|
+
def screen_bootstrap_content(content, basename)
|
|
102
|
+
bootstrap_guard.verify_synthesis_safety!(content)
|
|
103
|
+
:ok
|
|
104
|
+
rescue Rubik::Security::BlockedWriteError => e
|
|
105
|
+
Rubik::Log.warn(:security, "[SECURITY] Bootstrap overlay '#{basename}' refused by synthesis guard: #{e.message}")
|
|
106
|
+
:rejected
|
|
107
|
+
rescue StandardError, ScriptError, SystemStackError => e
|
|
108
|
+
Rubik::Log.warn(:security, "[SECURITY] Bootstrap overlay '#{basename}' could not be screened (#{e.class}: #{e.message}); refusing.")
|
|
109
|
+
:rejected
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
|
|
114
|
+
# Bootstrap kabul taramasında kullanılacak guard: kwarg'la geçilen örnek
|
|
115
|
+
# (üretimde service catalog'un tek operatör-policy guard'ı) ya da lazy
|
|
116
|
+
# default. Lazy çözüm bilinçli: overlay_storage.rb standalone require
|
|
117
|
+
# edilebilen bir dosyadır; security/guard yalnızca tarama gerçekten
|
|
118
|
+
# gerektiğinde yüklenir, load-order denetimi gerekmez.
|
|
119
|
+
def bootstrap_guard
|
|
120
|
+
@bootstrap_guard ||= begin
|
|
121
|
+
require_relative "../security/guard"
|
|
122
|
+
@guard || Rubik::Security::Guard.new
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -6,14 +6,29 @@ require "digest"
|
|
|
6
6
|
require "time"
|
|
7
7
|
# save_manifest atomik yazımı Platform.atomic_write kullanır; dosyanın tek başına
|
|
8
8
|
# require edilmesi durumunda da çözümlenebilsin diye açık yüklenir.
|
|
9
|
+
require_relative "../errors"
|
|
9
10
|
require_relative "../platform"
|
|
11
|
+
require_relative "../log"
|
|
10
12
|
require_relative "../plugin/package_inventory"
|
|
11
13
|
require_relative "synthesized_command_context"
|
|
12
14
|
require_relative "synthesized_tool_context"
|
|
15
|
+
require_relative "overlay_guard"
|
|
13
16
|
|
|
14
17
|
module Rubik
|
|
15
18
|
module Runtime
|
|
19
|
+
# Overlay manifest/lifecycle coordinator: directory layout, manifest CRUD,
|
|
20
|
+
# command/tool lifecycle, and the scrubbed-binding eval sink. The
|
|
21
|
+
# path-containment guards and the trust/bootstrap screening live on
|
|
22
|
+
# OverlayGuard (issue #447); the manifest primitives live on ManifestStore
|
|
23
|
+
# (issue #426, integrity records included).
|
|
16
24
|
class OverlayStorage
|
|
25
|
+
# Registration refusal (issue #448): register_command silently returns
|
|
26
|
+
# false on a built-in name collision; eval_command turns that into
|
|
27
|
+
# this typed refusal so the [OVERLAY] fallback path and the loaded
|
|
28
|
+
# list stay honest. Callers rescue StandardError, so the class change
|
|
29
|
+
# is transparent to them.
|
|
30
|
+
class RegistrationRefusedError < Rubik::Error; end
|
|
31
|
+
|
|
17
32
|
attr_reader :root_dir, :home_dir
|
|
18
33
|
|
|
19
34
|
COMMAND_SENTINEL = "# Rubik Custom Command:"
|
|
@@ -22,9 +37,16 @@ module Rubik
|
|
|
22
37
|
def initialize(root_dir: Dir.pwd, home_dir: ENV.fetch("HOME", nil), guard: nil, runtime_manager: nil, runtime_context: nil)
|
|
23
38
|
@root_dir = File.expand_path(root_dir)
|
|
24
39
|
@home_dir = File.expand_path(home_dir || ENV["HOME"] || ".")
|
|
25
|
-
@guard = guard
|
|
26
40
|
@runtime_manager = runtime_manager
|
|
27
41
|
@runtime_context = runtime_context
|
|
42
|
+
# Issue #447: the guard/trust plane is OverlayGuard. The storage owns
|
|
43
|
+
# the directory layout and hands it over as a frozen table; the
|
|
44
|
+
# `guard:` kwarg seam is forwarded as-is (an injected synthesis guard
|
|
45
|
+
# — production's operator-policy guard, test spies — composes, never
|
|
46
|
+
# bypasses), and the ManifestStore trust-mode seam (issue #426) is
|
|
47
|
+
# injected as the single RUBIK_TRUST_OVERLAYS read.
|
|
48
|
+
@overlay_guard = OverlayGuard.new(allowed_dirs: allowed_overlay_dirs, guard: guard,
|
|
49
|
+
trust_mode: -> { manifest_store.trust_mode? })
|
|
28
50
|
ensure_directories
|
|
29
51
|
end
|
|
30
52
|
|
|
@@ -32,8 +54,8 @@ module Rubik
|
|
|
32
54
|
# store wires the overlay-dir validation + containment checks into it.
|
|
33
55
|
def manifest_store
|
|
34
56
|
@manifest_store ||= ManifestStore.new(validate_path: lambda { |path, dir|
|
|
35
|
-
ensure_valid_overlay_dir!(dir)
|
|
36
|
-
ensure_contained_path!(path, dir)
|
|
57
|
+
@overlay_guard.ensure_valid_overlay_dir!(dir)
|
|
58
|
+
@overlay_guard.ensure_contained_path!(path, dir)
|
|
37
59
|
})
|
|
38
60
|
end
|
|
39
61
|
|
|
@@ -81,15 +103,15 @@ module Rubik
|
|
|
81
103
|
refuse_corrupt_manifest!(dir, :record, filename)
|
|
82
104
|
|
|
83
105
|
path = File.join(dir, filename)
|
|
84
|
-
ensure_contained_path!(path, dir)
|
|
106
|
+
@overlay_guard.ensure_contained_path!(path, dir)
|
|
85
107
|
hash = Digest::SHA256.hexdigest(normalize_content(File.binread(path)))
|
|
86
108
|
manifest_store.record_hash(dir, filename, hash, scope: scope)
|
|
87
109
|
end
|
|
88
110
|
|
|
89
111
|
def verify_integrity(file_path, dir, bootstrap_eligible: false)
|
|
90
|
-
return :trusted if trust_mode?
|
|
112
|
+
return :trusted if @overlay_guard.trust_mode?
|
|
91
113
|
|
|
92
|
-
unless safe_path_under?(file_path, dir)
|
|
114
|
+
unless @overlay_guard.safe_path_under?(file_path, dir)
|
|
93
115
|
warn "[SECURITY] Path traversal attempt detected for overlay '#{file_path}'. Skipping."
|
|
94
116
|
return :rejected
|
|
95
117
|
end
|
|
@@ -116,7 +138,7 @@ module Rubik
|
|
|
116
138
|
# verified_dir_entries taramanın başında verir. Manifest varken bootstrap asla çalışmaz.
|
|
117
139
|
# Kabul ÖNCE synthesis guard'ından geçer (Aday 07 ertelemesi): klonlanan depodaki
|
|
118
140
|
# dosya bu ortamda sentezlenmemişti; reddedilen dosya manifest'e yazılmaz.
|
|
119
|
-
verdict = screen_bootstrap_content(current_content, basename)
|
|
141
|
+
verdict = @overlay_guard.screen_bootstrap_content(current_content, basename)
|
|
120
142
|
return verdict unless verdict == :ok
|
|
121
143
|
|
|
122
144
|
scope_match = current_content.match(/# Scope:\s*(.*)/)
|
|
@@ -138,7 +160,7 @@ module Rubik
|
|
|
138
160
|
|
|
139
161
|
# Issue #407: the command/tool quadruplets collapse onto shared skeletons
|
|
140
162
|
# (save / delete / load_entries in the private section) behind the Kind
|
|
141
|
-
# adapter at the bottom of this
|
|
163
|
+
# adapter at the bottom of this class. Public surface unchanged: these
|
|
142
164
|
# delegators keep the exact pre-refactor signatures and per-kind defaults.
|
|
143
165
|
def save_command(name, code, scope: :project, description: "Custom command")
|
|
144
166
|
save(COMMAND_KIND, name, code, scope: scope, description: description)
|
|
@@ -233,7 +255,7 @@ module Rubik
|
|
|
233
255
|
end
|
|
234
256
|
# register_command, built-in çakışmasında sessizce false döner; red,
|
|
235
257
|
# load_all'un [OVERLAY] yoluna düşsün ve loaded listesi doğru kalsın.
|
|
236
|
-
raise "registration refused for '#{cmd_name}' (name collides with a built-in)" if registered == false
|
|
258
|
+
raise RegistrationRefusedError, "registration refused for '#{cmd_name}' (name collides with a built-in)" if registered == false
|
|
237
259
|
end
|
|
238
260
|
|
|
239
261
|
def save_tool(name, code, scope: :project, description: "Custom native tool", params_schema: nil, risk: :execute)
|
|
@@ -313,8 +335,64 @@ module Rubik
|
|
|
313
335
|
end
|
|
314
336
|
end
|
|
315
337
|
|
|
338
|
+
# ── Kind adapter (issue #407, merged into the single class body by
|
|
339
|
+
# issue #447 — the re-opened class block is gone) ────────────────────
|
|
340
|
+
# Per-kind asymmetries it parameterizes: name normalization
|
|
341
|
+
# (name_regex/strip_bang/colon_ok — commands keep ':' in the name but
|
|
342
|
+
# substitute it in the filename), scope→dir selection
|
|
343
|
+
# (global_dir/project_dir; anything but :global is project), byte-exact
|
|
344
|
+
# header emission (sentinel/bang/metadata_lines), and the warn
|
|
345
|
+
# asymmetries (debug_warn on save failure, warn_on_unusable on load).
|
|
346
|
+
Kind = Struct.new(:label, :sentinel, :bang, :name_regex, :strip_bang, :colon_ok, :debug_warn,
|
|
347
|
+
:warn_on_unusable, :metadata_lines, :global_dir, :project_dir, keyword_init: true) do
|
|
348
|
+
def normalize_name(raw)
|
|
349
|
+
name = raw.to_s.downcase
|
|
350
|
+
name = name.delete_prefix("!") if strip_bang
|
|
351
|
+
name.gsub(name_regex, "_")
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def filename_for(clean_name)
|
|
355
|
+
"#{colon_ok ? clean_name.gsub(':', '_') : clean_name}.rb"
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
# Byte-exact header template: frozen-string line, sentinel line, Scope,
|
|
359
|
+
# tool-only Risk/Schema lines around Description, trailing blank line.
|
|
360
|
+
# eval_* parse these lines back and the integrity manifest hashes the
|
|
361
|
+
# whole file, so every byte is contract (pinned by the header-bytes tests).
|
|
362
|
+
def header_lines(name:, scope:, description:, risk:, schema:)
|
|
363
|
+
lines = ["# frozen_string_literal: true",
|
|
364
|
+
"#{sentinel} #{bang}#{name}",
|
|
365
|
+
"# Scope: #{scope}"]
|
|
366
|
+
lines << "# Risk: #{risk}" if metadata_lines
|
|
367
|
+
lines << "# Description: #{description}"
|
|
368
|
+
lines << "# Schema: #{schema}" if metadata_lines
|
|
369
|
+
lines << ""
|
|
370
|
+
lines
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
COMMAND_KIND = Kind.new(label: "command", sentinel: COMMAND_SENTINEL, bang: "!", name_regex: /[^a-z0-9_:]/,
|
|
375
|
+
strip_bang: true, colon_ok: true, debug_warn: false, warn_on_unusable: false,
|
|
376
|
+
metadata_lines: false, global_dir: :global_commands_dir, project_dir: :project_commands_dir).freeze
|
|
377
|
+
|
|
378
|
+
TOOL_KIND = Kind.new(label: "tool", sentinel: TOOL_SENTINEL, bang: "", name_regex: /[^a-z0-9_]/,
|
|
379
|
+
strip_bang: false, colon_ok: false, debug_warn: true, warn_on_unusable: true,
|
|
380
|
+
metadata_lines: true, global_dir: :global_tools_dir, project_dir: :project_tools_dir).freeze
|
|
381
|
+
|
|
316
382
|
private
|
|
317
383
|
|
|
384
|
+
# Directory layout → guard wiring (issue #447): the containment guard
|
|
385
|
+
# needs the legit overlay dirs and their trusted roots; the storage
|
|
386
|
+
# owns the layout, the guard owns the enforcement.
|
|
387
|
+
def allowed_overlay_dirs
|
|
388
|
+
[
|
|
389
|
+
[project_commands_dir, @root_dir],
|
|
390
|
+
[global_commands_dir, @home_dir],
|
|
391
|
+
[project_tools_dir, @root_dir],
|
|
392
|
+
[global_tools_dir, @home_dir]
|
|
393
|
+
].freeze
|
|
394
|
+
end
|
|
395
|
+
|
|
318
396
|
# Single home of the fail-closed corrupt-manifest contract (issue #407,
|
|
319
397
|
# single-homed in ManifestStore by issue #426): record_hash and the
|
|
320
398
|
# delete skeleton refuse through this one raise. `verb` reproduces the
|
|
@@ -336,7 +414,7 @@ module Rubik
|
|
|
336
414
|
|
|
337
415
|
filename = kind.filename_for(clean_name)
|
|
338
416
|
file_path = File.join(target_dir, filename)
|
|
339
|
-
ensure_contained_path!(file_path, target_dir)
|
|
417
|
+
@overlay_guard.ensure_contained_path!(file_path, target_dir)
|
|
340
418
|
|
|
341
419
|
header = kind.header_lines(name: clean_name, scope: scope, description: description,
|
|
342
420
|
risk: risk, schema: params_schema ? JSON.dump(params_schema) : "{}")
|
|
@@ -358,7 +436,7 @@ module Rubik
|
|
|
358
436
|
target_dir = kind_dir(kind, scope)
|
|
359
437
|
filename = kind.filename_for(clean_name)
|
|
360
438
|
file_path = File.join(target_dir, filename)
|
|
361
|
-
ensure_contained_path!(file_path, target_dir)
|
|
439
|
+
@overlay_guard.ensure_contained_path!(file_path, target_dir)
|
|
362
440
|
|
|
363
441
|
refuse_corrupt_manifest!(target_dir, :delete, filename)
|
|
364
442
|
|
|
@@ -387,7 +465,7 @@ module Rubik
|
|
|
387
465
|
file_path = meta[:path]
|
|
388
466
|
target_dir = kind_dir(kind, meta[:scope])
|
|
389
467
|
|
|
390
|
-
ensure_contained_path!(file_path, target_dir)
|
|
468
|
+
@overlay_guard.ensure_contained_path!(file_path, target_dir)
|
|
391
469
|
code = File.read(file_path)
|
|
392
470
|
if yield(name, code, meta[:scope])
|
|
393
471
|
loaded << name
|
|
@@ -420,86 +498,6 @@ module Rubik
|
|
|
420
498
|
scope.to_sym == :global ? public_send(kind.global_dir) : public_send(kind.project_dir)
|
|
421
499
|
end
|
|
422
500
|
|
|
423
|
-
def ensure_valid_overlay_dir!(dir)
|
|
424
|
-
allowed = [
|
|
425
|
-
[project_commands_dir, @root_dir],
|
|
426
|
-
[global_commands_dir, @home_dir],
|
|
427
|
-
[project_tools_dir, @root_dir],
|
|
428
|
-
[global_tools_dir, @home_dir]
|
|
429
|
-
]
|
|
430
|
-
valid = allowed.any? do |allowed_dir, trusted_root|
|
|
431
|
-
Platform.path_equal?(dir, allowed_dir) && Platform.resolved_path_under?(dir, trusted_root)
|
|
432
|
-
end
|
|
433
|
-
return if valid
|
|
434
|
-
|
|
435
|
-
raise ArgumentError, "Invalid overlay directory: #{dir}"
|
|
436
|
-
end
|
|
437
|
-
|
|
438
|
-
def ensure_contained_path!(path, parent_dir)
|
|
439
|
-
raise ArgumentError, "Path traversal attempt detected: #{path}" unless safe_path_under?(path, parent_dir)
|
|
440
|
-
end
|
|
441
|
-
|
|
442
|
-
def safe_path_under?(path, parent_dir)
|
|
443
|
-
return false if path.nil? || parent_dir.nil?
|
|
444
|
-
|
|
445
|
-
ensure_valid_overlay_dir!(parent_dir)
|
|
446
|
-
|
|
447
|
-
# Lexical containment check first
|
|
448
|
-
return false unless Platform.path_under?(path, parent_dir)
|
|
449
|
-
|
|
450
|
-
# If path exists or parent exists, also check resolved path containment
|
|
451
|
-
return Platform.resolved_path_under?(path, parent_dir) if File.exist?(path) || File.exist?(parent_dir)
|
|
452
|
-
|
|
453
|
-
true
|
|
454
|
-
rescue StandardError
|
|
455
|
-
false
|
|
456
|
-
end
|
|
457
|
-
|
|
458
|
-
# Bootstrap kabul taramasında kullanılacak guard: kwarg'la geçilen örnek
|
|
459
|
-
# (üretimde service catalog'un tek operatör-policy guard'ı) ya da lazy
|
|
460
|
-
# default. Lazy çözüm bilinçli: overlay_storage.rb standalone require
|
|
461
|
-
# edilebilen bir dosyadır; security/guard yalnızca tarama gerçekten
|
|
462
|
-
# gerektiğinde yüklenir, load-order denetimi gerekmez.
|
|
463
|
-
def bootstrap_guard
|
|
464
|
-
@bootstrap_guard ||= begin
|
|
465
|
-
require_relative "../security/guard"
|
|
466
|
-
@guard || Rubik::Security::Guard.new
|
|
467
|
-
end
|
|
468
|
-
end
|
|
469
|
-
|
|
470
|
-
# Aday 07'den ertelenen güvenlik kalemi: manifestsız dizindeki sentinel
|
|
471
|
-
# dosya manifest'e yazılmadan önce synthesis guard'ından geçirilir.
|
|
472
|
-
# Reddedilen (veya taranamayan) dosya manifest'e YAZILMAZ — sonraki tarama
|
|
473
|
-
# yeniden tarar ve reddeder (sessiz heal, bozuk-manifest doktriniyle
|
|
474
|
-
# çelişir); load_all/load_tools'un mevcut :rejected atlaması devreye girer.
|
|
475
|
-
# Parse hatası guard içinde yutulur (çalıştırılamaz kod güvenlik olayı
|
|
476
|
-
# değil); guard'ın kendisi patlarsa da dosya fail-closed reddedilir —
|
|
477
|
-
# SystemStackError dahil: rakip içerikte derin yuvalama AST walk'ı
|
|
478
|
-
# taşırebilir, tek dosyanın reddiyesi tüm taramayı çökertmemeli.
|
|
479
|
-
# Bilinçli asimetri: dosyanın TAMAMI (Description/Schema başlığı dahil)
|
|
480
|
-
# taranır — sentez anında yalnızca kod taranıyordu. Kabul kapısı daha
|
|
481
|
-
# sıkı olmak güvenli yöndür; başlık metnine denk gelen nadir yanlış
|
|
482
|
-
# pozitif dosyayı makinede yeniden kayıtla çözülür.
|
|
483
|
-
# Emit doktrini (guard.rb run_synthesis_gate hükmü): güvenlik kararının
|
|
484
|
-
# tek kullanıcı-yüzü izi $VERBOSE=nil altında da yaşamalıdır — $stderr.puts.
|
|
485
|
-
def screen_bootstrap_content(content, basename)
|
|
486
|
-
bootstrap_guard.verify_synthesis_safety!(content)
|
|
487
|
-
:ok
|
|
488
|
-
rescue Rubik::Security::BlockedWriteError => e
|
|
489
|
-
$stderr.puts "[SECURITY] Bootstrap overlay '#{basename}' refused by synthesis guard: #{e.message}" # rubocop:disable Style/StderrPuts
|
|
490
|
-
:rejected
|
|
491
|
-
rescue StandardError, ScriptError, SystemStackError => e
|
|
492
|
-
$stderr.puts "[SECURITY] Bootstrap overlay '#{basename}' could not be screened (#{e.class}: #{e.message}); refusing." # rubocop:disable Style/StderrPuts
|
|
493
|
-
:rejected
|
|
494
|
-
end
|
|
495
|
-
|
|
496
|
-
# Ortak kaçış kapısı predicate'i: iki güvenlik kapısının (verify_integrity,
|
|
497
|
-
# verified_dir_entries) kopya ENV kontrolüyle sürünerek ayrışmasını engeller.
|
|
498
|
-
# Tek seam ManifestStore'da yaşar (issue #426); plugin tarafı da aynı kapıdan geçer.
|
|
499
|
-
def trust_mode?
|
|
500
|
-
manifest_store.trust_mode?
|
|
501
|
-
end
|
|
502
|
-
|
|
503
501
|
# Üç durumlu manifest yoklaması ve bozuk-manifest reddi ManifestStore'da
|
|
504
502
|
# tek home'da (issue #426): kayıt yolu (record_hash) ve delete iskeleti
|
|
505
503
|
# bozuk manifestte {} davranışına değil, fail-closed redde bağlı.
|
|
@@ -514,14 +512,14 @@ module Rubik
|
|
|
514
512
|
files = Dir.glob(File.join(dir, "*.rb"))
|
|
515
513
|
|
|
516
514
|
# Kaçış kapısı: trust modu manifest durumundan bağımsız olarak tüm dosyaları güvenilir sayar.
|
|
517
|
-
return files.map { |f| { name: File.basename(f, ".rb"), scope: scope, path: f, integrity: :trusted } } if trust_mode?
|
|
515
|
+
return files.map { |f| { name: File.basename(f, ".rb"), scope: scope, path: f, integrity: :trusted } } if @overlay_guard.trust_mode?
|
|
518
516
|
|
|
519
517
|
case manifest_state(dir)
|
|
520
518
|
when :corrupt
|
|
521
519
|
warn "[SECURITY] Corrupt manifest in #{dir}; skipping #{files.size} overlay(s)."
|
|
522
520
|
[]
|
|
523
521
|
when :missing
|
|
524
|
-
if bootstrap_opt_in?
|
|
522
|
+
if @overlay_guard.bootstrap_opt_in?
|
|
525
523
|
bootstrapped = 0
|
|
526
524
|
entries = files.map do |f|
|
|
527
525
|
integrity = verify_integrity(f, dir, bootstrap_eligible: true)
|
|
@@ -548,12 +546,6 @@ module Rubik
|
|
|
548
546
|
end
|
|
549
547
|
end
|
|
550
548
|
|
|
551
|
-
# İlk-bootstrap açık rızası: manifest'siz dizindeki sentinel dosyaların ilk taramada
|
|
552
|
-
# kabul edilmesi ancak bu env ile mümkündür (kayıt kalıcıdır; sonraki açılışlar env'siz çalışır).
|
|
553
|
-
def bootstrap_opt_in?
|
|
554
|
-
ENV["RUBIK_BOOTSTRAP_OVERLAYS"] == "1"
|
|
555
|
-
end
|
|
556
|
-
|
|
557
549
|
def sentinel_content?(content)
|
|
558
550
|
content.include?(COMMAND_SENTINEL) || content.include?(TOOL_SENTINEL)
|
|
559
551
|
end
|
|
@@ -570,51 +562,5 @@ module Rubik
|
|
|
570
562
|
Object.new.instance_eval { binding }
|
|
571
563
|
end
|
|
572
564
|
end
|
|
573
|
-
|
|
574
|
-
# ── Private Kind adapter (issue #407) ──────────────────────────────────────
|
|
575
|
-
# Defined at the bottom so the frozen instances below can reference the
|
|
576
|
-
# sentinel constants declared in the class body above. Per-kind asymmetries
|
|
577
|
-
# it parameterizes: name normalization (name_regex/strip_bang/colon_ok —
|
|
578
|
-
# commands keep ':' in the name but substitute it in the filename), scope→
|
|
579
|
-
# dir selection (global_dir/project_dir; anything but :global is project),
|
|
580
|
-
# byte-exact header emission (sentinel/bang/metadata_lines), and the warn
|
|
581
|
-
# asymmetries (debug_warn on save failure, warn_on_unusable on load).
|
|
582
|
-
class OverlayStorage
|
|
583
|
-
Kind = Struct.new(:label, :sentinel, :bang, :name_regex, :strip_bang, :colon_ok, :debug_warn,
|
|
584
|
-
:warn_on_unusable, :metadata_lines, :global_dir, :project_dir, keyword_init: true) do
|
|
585
|
-
def normalize_name(raw)
|
|
586
|
-
name = raw.to_s.downcase
|
|
587
|
-
name = name.delete_prefix("!") if strip_bang
|
|
588
|
-
name.gsub(name_regex, "_")
|
|
589
|
-
end
|
|
590
|
-
|
|
591
|
-
def filename_for(clean_name)
|
|
592
|
-
"#{colon_ok ? clean_name.gsub(':', '_') : clean_name}.rb"
|
|
593
|
-
end
|
|
594
|
-
|
|
595
|
-
# Byte-exact header template: frozen-string line, sentinel line, Scope,
|
|
596
|
-
# tool-only Risk/Schema lines around Description, trailing blank line.
|
|
597
|
-
# eval_* parse these lines back and the integrity manifest hashes the
|
|
598
|
-
# whole file, so every byte is contract (pinned by the header-bytes tests).
|
|
599
|
-
def header_lines(name:, scope:, description:, risk:, schema:)
|
|
600
|
-
lines = ["# frozen_string_literal: true",
|
|
601
|
-
"#{sentinel} #{bang}#{name}",
|
|
602
|
-
"# Scope: #{scope}"]
|
|
603
|
-
lines << "# Risk: #{risk}" if metadata_lines
|
|
604
|
-
lines << "# Description: #{description}"
|
|
605
|
-
lines << "# Schema: #{schema}" if metadata_lines
|
|
606
|
-
lines << ""
|
|
607
|
-
lines
|
|
608
|
-
end
|
|
609
|
-
end
|
|
610
|
-
|
|
611
|
-
COMMAND_KIND = Kind.new(label: "command", sentinel: COMMAND_SENTINEL, bang: "!", name_regex: /[^a-z0-9_:]/,
|
|
612
|
-
strip_bang: true, colon_ok: true, debug_warn: false, warn_on_unusable: false,
|
|
613
|
-
metadata_lines: false, global_dir: :global_commands_dir, project_dir: :project_commands_dir).freeze
|
|
614
|
-
|
|
615
|
-
TOOL_KIND = Kind.new(label: "tool", sentinel: TOOL_SENTINEL, bang: "", name_regex: /[^a-z0-9_]/,
|
|
616
|
-
strip_bang: false, colon_ok: false, debug_warn: true, warn_on_unusable: true,
|
|
617
|
-
metadata_lines: true, global_dir: :global_tools_dir, project_dir: :project_tools_dir).freeze
|
|
618
|
-
end
|
|
619
565
|
end
|
|
620
566
|
end
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require "open3"
|
|
4
4
|
require "monitor"
|
|
5
5
|
require "time"
|
|
6
|
+
require_relative "../log"
|
|
6
7
|
require_relative "../platform"
|
|
7
8
|
require_relative "../security/guard" unless defined?(Rubik::Security::Guard)
|
|
8
9
|
|
|
@@ -91,7 +92,13 @@ module Rubik
|
|
|
91
92
|
|
|
92
93
|
@at_exit_installed = true
|
|
93
94
|
at_exit do
|
|
94
|
-
|
|
95
|
+
# Error-isolated sweep: one manager's kill_all raising must not
|
|
96
|
+
# leave the remaining managers uncleaned at shutdown.
|
|
97
|
+
@instances.each do |mgr|
|
|
98
|
+
mgr.kill_all
|
|
99
|
+
rescue StandardError => e
|
|
100
|
+
warn "[TaskManager] at_exit kill_all failed: #{e.class}: #{e.message}"
|
|
101
|
+
end
|
|
95
102
|
end
|
|
96
103
|
end
|
|
97
104
|
|
|
@@ -140,28 +147,49 @@ module Rubik
|
|
|
140
147
|
end
|
|
141
148
|
|
|
142
149
|
ring_buf = RingBuffer.new(@max_buffer_lines)
|
|
150
|
+
# Deliberately not single-homed on Platform.popen_capture (issue
|
|
151
|
+
# #444): that core is the synchronous drain-to-string capture shape
|
|
152
|
+
# (Timeout-wrapped, stdin closed immediately, whole-output strings).
|
|
153
|
+
# This spawn is the async/interactive shape — stdin stays open for
|
|
154
|
+
# send_input, the readers stream chomped lines into the per-task
|
|
155
|
+
# RingBuffer, and completion is watched by a dedicated thread with
|
|
156
|
+
# no Timeout — so parameterizing the sink alone cannot unify them.
|
|
143
157
|
begin
|
|
144
158
|
stdin, stdout, stderr, wait_thr = Open3.popen3(cmd_str, chdir: chdir)
|
|
145
159
|
rescue StandardError => e
|
|
146
160
|
rollback_failed_start(target_ctx, nil, e)
|
|
147
161
|
raise e
|
|
148
162
|
end
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
163
|
+
# The spawn succeeded but the task is NOT yet registered, so nothing
|
|
164
|
+
# (kill, kill_all, at_exit) can reach the child yet. Any failure in
|
|
165
|
+
# this window must kill the spawned process directly before rolling
|
|
166
|
+
# back the context, or the child leaks as an unmanaged orphan.
|
|
167
|
+
begin
|
|
168
|
+
stdin.binmode if stdin.respond_to?(:binmode)
|
|
169
|
+
stdin.sync = true if stdin.respond_to?(:sync=)
|
|
170
|
+
|
|
171
|
+
task = Task.new(
|
|
172
|
+
id: task_id,
|
|
173
|
+
command: cmd_str,
|
|
174
|
+
pid: wait_thr.pid,
|
|
175
|
+
started_at: Time.now,
|
|
176
|
+
ended_at: nil,
|
|
177
|
+
status: :running,
|
|
178
|
+
exit_code: nil,
|
|
179
|
+
ring_buffer: ring_buf,
|
|
180
|
+
stdin_io: stdin,
|
|
181
|
+
wait_thread: wait_thr,
|
|
182
|
+
collector_threads: []
|
|
183
|
+
)
|
|
184
|
+
rescue StandardError => e
|
|
185
|
+
begin
|
|
186
|
+
Process.kill("KILL", wait_thr.pid)
|
|
187
|
+
rescue StandardError
|
|
188
|
+
nil
|
|
189
|
+
end
|
|
190
|
+
rollback_failed_start(target_ctx, nil, e)
|
|
191
|
+
raise e
|
|
192
|
+
end
|
|
165
193
|
|
|
166
194
|
task.collector_threads = []
|
|
167
195
|
|
|
@@ -241,19 +269,30 @@ module Rubik
|
|
|
241
269
|
task&.ring_buffer&.tail(tail)
|
|
242
270
|
end
|
|
243
271
|
|
|
272
|
+
# Resolve the stdin pipe under the monitor, but write OUTSIDE it: a
|
|
273
|
+
# pipe write blocks once the OS pipe buffer fills, and blocking while
|
|
274
|
+
# holding @monitor would deadlock kill/status/logs/kill_all and the
|
|
275
|
+
# at_exit sweep for the whole manager. The deadline-bounded write
|
|
276
|
+
# (Rubik::Platform.write_until, single-homed with the other process
|
|
277
|
+
# IO plumbing) converts a never-reading child into a false return
|
|
278
|
+
# instead of a hang.
|
|
279
|
+
SEND_INPUT_TIMEOUT = 5.0
|
|
280
|
+
|
|
244
281
|
def send_input(task_id, input)
|
|
245
|
-
@monitor.synchronize do
|
|
282
|
+
io = @monitor.synchronize do
|
|
246
283
|
task = @tasks[task_id.to_s]
|
|
247
284
|
return false unless task && task.status == :running && task.stdin_io && !task.stdin_io.closed?
|
|
248
285
|
|
|
249
|
-
|
|
250
|
-
text += "\n" unless text.end_with?("\n")
|
|
251
|
-
task.stdin_io.write(text)
|
|
252
|
-
task.stdin_io.flush
|
|
253
|
-
true
|
|
254
|
-
rescue IOError, Errno::EPIPE
|
|
255
|
-
false
|
|
286
|
+
task.stdin_io
|
|
256
287
|
end
|
|
288
|
+
|
|
289
|
+
text = input.to_s
|
|
290
|
+
text += "\n" unless text.end_with?("\n")
|
|
291
|
+
Rubik::Platform.write_until(io, text, timeout: SEND_INPUT_TIMEOUT)
|
|
292
|
+
io.flush
|
|
293
|
+
true
|
|
294
|
+
rescue IOError, Errno::EPIPE, Errno::EINTR
|
|
295
|
+
false
|
|
257
296
|
end
|
|
258
297
|
|
|
259
298
|
def kill(task_id)
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative "base"
|
|
4
4
|
require_relative "../../platform"
|
|
5
|
+
require_relative "../../log"
|
|
5
6
|
require "open3"
|
|
6
7
|
require "timeout"
|
|
7
8
|
|
|
@@ -48,7 +49,10 @@ module Rubik
|
|
|
48
49
|
error_message: "Docker sandbox error"
|
|
49
50
|
)
|
|
50
51
|
ensure
|
|
51
|
-
|
|
52
|
+
# Best-effort cleanup, but a failed `docker rm -f` leaves an
|
|
53
|
+
# orphaned container — check the result and leave a trace.
|
|
54
|
+
removed = system("docker", "rm", "-f", container_name, out: File::NULL, err: File::NULL)
|
|
55
|
+
Rubik::Log.warn(:sandbox, "docker container '#{container_name}' could not be removed") unless removed
|
|
52
56
|
end
|
|
53
57
|
|
|
54
58
|
{
|