kobako 0.19.0-aarch64-linux → 0.21.0-aarch64-linux
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/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +127 -0
- data/README.md +96 -44
- data/ROADMAP.md +1 -1
- data/crates/kobako-runtime/CHANGELOG.md +44 -0
- data/crates/kobako-runtime/README.md +1 -1
- data/crates/kobako-transport/CHANGELOG.md +31 -0
- data/crates/kobako-transport/README.md +43 -0
- data/crates/kobako-wasmtime/CHANGELOG.md +30 -0
- data/crates/kobako-wasmtime/README.md +1 -1
- data/data/kobako.wasm +0 -0
- data/lib/kobako/3.3/kobako.so +0 -0
- data/lib/kobako/3.4/kobako.so +0 -0
- data/lib/kobako/4.0/kobako.so +0 -0
- data/lib/kobako/catalog/extensions.rb +50 -43
- data/lib/kobako/catalog/handles.rb +7 -16
- data/lib/kobako/catalog/services.rb +24 -46
- data/lib/kobako/catalog/snippets.rb +16 -33
- data/lib/kobako/codec/decoder.rb +10 -13
- data/lib/kobako/codec/encoder.rb +7 -7
- data/lib/kobako/codec/error.rb +6 -6
- data/lib/kobako/codec/ext_types.rb +12 -72
- data/lib/kobako/codec/handle_walk.rb +6 -6
- data/lib/kobako/codec/state.rb +4 -56
- data/lib/kobako/codec/utils.rb +4 -4
- data/lib/kobako/codec.rb +11 -21
- data/lib/kobako/context.rb +208 -0
- data/lib/kobako/errors.rb +59 -16
- data/lib/kobako/execution.rb +52 -0
- data/lib/kobako/extension.rb +24 -10
- data/lib/kobako/outcome.rb +71 -128
- data/lib/kobako/payload/arguments.rb +59 -0
- data/lib/kobako/payload.rb +19 -0
- data/lib/kobako/pool.rb +1 -7
- data/lib/kobako/sandbox.rb +51 -191
- data/lib/kobako/sandbox_options.rb +24 -3
- data/lib/kobako/snippet/binary.rb +3 -4
- data/lib/kobako/snippet/source.rb +5 -6
- data/lib/kobako/snippet.rb +2 -2
- data/lib/kobako/transport/call.rb +21 -0
- data/lib/kobako/transport/dispatcher.rb +94 -142
- data/lib/kobako/transport/error.rb +21 -5
- data/lib/kobako/transport/reflection.rb +82 -0
- data/lib/kobako/transport/run.rb +25 -33
- data/lib/kobako/transport/yielder.rb +27 -36
- data/lib/kobako/transport.rb +11 -8
- data/lib/kobako/unresolved.rb +17 -0
- data/lib/kobako/usage.rb +7 -7
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +1 -0
- data/release-please-config.json +47 -2
- data/sig/kobako/catalog/extensions.rbs +4 -6
- data/sig/kobako/catalog/handles.rbs +0 -2
- data/sig/kobako/catalog/services.rbs +4 -4
- data/sig/kobako/catalog/snippets.rbs +4 -2
- data/sig/kobako/codec/error.rbs +4 -4
- data/sig/kobako/codec/ext_types.rbs +0 -7
- data/sig/kobako/codec/state.rbs +0 -4
- data/sig/kobako/codec.rbs +4 -5
- data/sig/kobako/context.rbs +36 -0
- data/sig/kobako/errors.rbs +27 -6
- data/sig/kobako/execution.rbs +24 -0
- data/sig/kobako/extension.rbs +4 -2
- data/sig/kobako/outcome.rbs +12 -13
- data/sig/kobako/payload/arguments.rbs +26 -0
- data/sig/kobako/payload.rbs +4 -0
- data/sig/kobako/pool.rbs +0 -2
- data/sig/kobako/runtime.rbs +44 -14
- data/sig/kobako/sandbox.rbs +5 -24
- data/sig/kobako/sandbox_options.rbs +9 -2
- data/sig/kobako/snippet/binary.rbs +1 -1
- data/sig/kobako/snippet/source.rbs +1 -1
- data/sig/kobako/transport/call.rbs +21 -0
- data/sig/kobako/transport/dispatcher.rbs +9 -11
- data/sig/kobako/transport/error.rbs +11 -0
- data/sig/kobako/transport/reflection.rbs +15 -0
- data/sig/kobako/transport/run.rbs +1 -1
- data/sig/kobako/transport/yielder.rbs +2 -2
- data/sig/kobako/transport.rbs +8 -2
- data/sig/kobako/unresolved.rbs +4 -0
- metadata +18 -12
- data/lib/kobako/fault.rb +0 -38
- data/lib/kobako/outcome/panic.rb +0 -42
- data/lib/kobako/transport/request.rb +0 -76
- data/lib/kobako/transport/response.rb +0 -83
- data/lib/kobako/transport/yield.rb +0 -94
- data/sig/kobako/fault.rbs +0 -19
- data/sig/kobako/outcome/panic.rbs +0 -34
- data/sig/kobako/transport/request.rbs +0 -29
- data/sig/kobako/transport/response.rbs +0 -33
- data/sig/kobako/transport/yield.rbs +0 -34
|
@@ -1,31 +1,27 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "../codec"
|
|
4
|
-
require_relative "
|
|
5
|
-
require_relative "
|
|
6
|
-
require_relative "
|
|
4
|
+
require_relative "../payload"
|
|
5
|
+
require_relative "call"
|
|
6
|
+
require_relative "reflection"
|
|
7
7
|
require_relative "yielder"
|
|
8
8
|
|
|
9
9
|
module Kobako
|
|
10
10
|
# See lib/kobako/transport.rb for the umbrella module doc; this file
|
|
11
|
-
# owns the pure-function dispatcher that
|
|
12
|
-
# Requests and produces encoded Responses.
|
|
11
|
+
# owns the pure-function dispatcher that answers a routed Call.
|
|
13
12
|
module Transport
|
|
14
|
-
# Pure-function dispatcher for guest-initiated
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
13
|
+
# Pure-function dispatcher for guest-initiated Calls. The native side
|
|
14
|
+
# has already decoded the core envelope, so this resolves the target
|
|
15
|
+
# through the per-invocation path +resolver+ (the +Context+, whose
|
|
16
|
+
# +#lookup+ layers per-invocation providers over the static bindings)
|
|
17
|
+
# or Catalog::Handles, decodes only the payload, invokes the method,
|
|
18
|
+
# and answers +[ok, bytes]+ — which the native side puts on the
|
|
19
|
+
# Reply's ok or fault arm. It never raises.
|
|
19
20
|
#
|
|
20
21
|
# The module is stateless — all mutable state is threaded through
|
|
21
22
|
# arguments so Dispatcher has no instance variables and no side
|
|
22
23
|
# effects beyond mutating the Catalog::Handles via +alloc+ when a
|
|
23
24
|
# non-wire-representable return value must be wrapped.
|
|
24
|
-
#
|
|
25
|
-
# Entry point:
|
|
26
|
-
#
|
|
27
|
-
# Kobako::Transport::Dispatcher.dispatch(request_bytes, services, handler, yield_to_guest)
|
|
28
|
-
# # => msgpack-encoded Response bytes (never raises)
|
|
29
25
|
module Dispatcher
|
|
30
26
|
# Throw tag for the Yielder's break unwind back to the
|
|
31
27
|
# dispatcher's +catch+ frame. +private_constant+ is a
|
|
@@ -35,98 +31,82 @@ module Kobako
|
|
|
35
31
|
|
|
36
32
|
module_function
|
|
37
33
|
|
|
38
|
-
# Internal sentinel raised when target resolution fails.
|
|
39
|
-
#
|
|
34
|
+
# Internal sentinel raised when target resolution fails. Becomes a
|
|
35
|
+
# Fault with type="undefined". Contained at the wire boundary —
|
|
40
36
|
# not part of the public Kobako error taxonomy.
|
|
41
37
|
class UndefinedTargetError < StandardError; end
|
|
42
38
|
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
-
#
|
|
46
|
-
#
|
|
47
|
-
# rejected: only methods the bound object itself exposes as Service behaviour are
|
|
48
|
-
# reachable, and +public_send(:send, ...)+ would otherwise let a guest
|
|
49
|
-
# pivot through +send+ into the private +Kernel#eval+ / +#system+
|
|
50
|
-
# surface (host RCE).
|
|
51
|
-
META_OWNERS = [BasicObject, Kernel, Object, Module, Class].freeze
|
|
52
|
-
private_constant :META_OWNERS
|
|
53
|
-
|
|
54
|
-
# Callable gadget types whose own public methods are reflection surface
|
|
55
|
-
# (+Proc#binding+ reaches +Binding#eval+, +Method#receiver+ / +#unbind+
|
|
56
|
-
# hand back the underlying object) rather than Service behaviour. Only
|
|
57
|
-
# CALLABLE_ALLOW is reachable on a target of these types; a bound
|
|
58
|
-
# lambda stays invocable, its reflective surface does not.
|
|
59
|
-
GADGET_OWNERS = [Proc, Method, UnboundMethod, Binding].freeze
|
|
60
|
-
private_constant :GADGET_OWNERS
|
|
61
|
-
|
|
62
|
-
# The sole methods reachable on a GADGET_OWNERS target: invoking it
|
|
63
|
-
# (+call+ / +[]+ / +yield+) and the harmless +arity+ / +lambda?+
|
|
64
|
-
# describers that aid guest-side debugging.
|
|
65
|
-
CALLABLE_ALLOW = %i[call [] yield arity lambda?].freeze
|
|
66
|
-
private_constant :CALLABLE_ALLOW
|
|
67
|
-
|
|
68
|
-
# Dispatch a single transport request and return the encoded
|
|
69
|
-
# Response bytes. Invoked from the +Runtime#on_dispatch+ Proc that
|
|
70
|
-
# +Kobako::Sandbox#initialize+ installs on the ext side; +services+,
|
|
39
|
+
# Answer a single routed Call with +[ok, bytes]+, which the native
|
|
40
|
+
# side puts on the Reply's ok or fault arm. Invoked from the
|
|
41
|
+
# per-invocation dispatch Proc that
|
|
42
|
+
# +Kobako::Context+ hands to +Runtime#eval+ / +#run+; +resolver+,
|
|
71
43
|
# +handler+, and +yield_to_guest+ are captured in that Proc's
|
|
72
|
-
# closure so the Dispatcher stays stateless and the
|
|
73
|
-
#
|
|
74
|
-
# or +Runtime+. +yield_to_guest+ is a +String → String+ callable
|
|
44
|
+
# closure so the Dispatcher stays stateless and neither the resolver
|
|
45
|
+
# nor the Context needs to publish accessors for the per-invocation
|
|
46
|
+
# +Catalog::Handles+ or +Runtime+. +yield_to_guest+ is a +String → String+ callable
|
|
75
47
|
# (the ext's per-dispatch +Kobako::Runtime::GuestYielder+) used only
|
|
76
|
-
# when the
|
|
77
|
-
#
|
|
78
|
-
#
|
|
79
|
-
# than a wasm trap.
|
|
48
|
+
# when the Call carries +block_given: true+. Never raises — every
|
|
49
|
+
# failure path takes the fault arm instead, so the guest sees a
|
|
50
|
+
# transport error rather than a wasm trap.
|
|
80
51
|
#
|
|
81
52
|
# The decode runs inside +Codec.track_handles+ so #resolve_call_args
|
|
82
53
|
# can skip the argument walk when no Capability Handle crossed the
|
|
83
54
|
# wire.
|
|
84
|
-
def dispatch(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
args, kwargs = resolve_call_args(request, handler, carried_handle)
|
|
88
|
-
yielder = Yielder.new(yield_to_guest, BREAK_THROW, handler) if request.block_given
|
|
89
|
-
encode_ok(catch(BREAK_THROW) { invoke(target, request.method_name, args, kwargs, yielder) }, handler)
|
|
55
|
+
def dispatch(call, resolver, handler, yield_to_guest)
|
|
56
|
+
yielder = Yielder.new(yield_to_guest, BREAK_THROW, handler) if call.block_given
|
|
57
|
+
[true, encode_ok(run(call, resolver, handler, yielder), handler), nil] # : [bool, String, String?]
|
|
90
58
|
# StandardError is the boundary by intent: a Service method's
|
|
91
|
-
# application fault folds into a guest-rescuable
|
|
59
|
+
# application fault folds into a guest-rescuable fault, while a
|
|
92
60
|
# host-process failure (NoMemoryError, SignalException, a bare Exception)
|
|
93
61
|
# stays uncaught and traps the invocation rather than being masked as a
|
|
94
62
|
# rescuable fault.
|
|
95
63
|
rescue StandardError => e
|
|
96
|
-
|
|
64
|
+
[false, *caught_fault(e)] # : [bool, String, String?]
|
|
97
65
|
ensure
|
|
98
66
|
yielder&.invalidate!
|
|
99
67
|
end
|
|
100
68
|
|
|
101
|
-
#
|
|
102
|
-
# +
|
|
103
|
-
#
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
69
|
+
# Decode the payload, resolve the receiver, and run the method inside
|
|
70
|
+
# the +catch+ frame a guest +break+ unwinds to. Split from #dispatch
|
|
71
|
+
# so the reply-shaping and the failure boundary stay one glance wide.
|
|
72
|
+
def run(call, resolver, handler, yielder)
|
|
73
|
+
arguments, carried_handle = Kobako::Codec.track_handles { Payload::Arguments.decode(call.payload) }
|
|
74
|
+
receiver = resolve_target(call.target, resolver, handler)
|
|
75
|
+
args, kwargs = resolve_call_args(arguments, handler, carried_handle)
|
|
76
|
+
catch(BREAK_THROW) { invoke(receiver, call.method_name, args, kwargs, yielder) }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Resolve positional and keyword arguments off the decoded payload in
|
|
80
|
+
# one step. +carried_handle+ reports whether the decode carried any
|
|
81
|
+
# Capability Handle; when it did not, every argument resolves to
|
|
82
|
+
# itself, so the decoded values pass straight through and the walk is
|
|
83
|
+
# skipped entirely. Otherwise both go through #resolve_arg so Handles
|
|
84
|
+
# round-trip back to the host-side Ruby object before the call reaches
|
|
85
|
+
# +public_send+.
|
|
86
|
+
def resolve_call_args(arguments, handler, carried_handle)
|
|
87
|
+
return [arguments.args, arguments.kwargs] unless carried_handle
|
|
88
|
+
|
|
89
|
+
[arguments.args.map { |v| resolve_arg(v, handler) },
|
|
90
|
+
arguments.kwargs.transform_values { |v| resolve_arg(v, handler) }]
|
|
112
91
|
end
|
|
113
92
|
|
|
114
|
-
# Map an error caught at the dispatch boundary to
|
|
115
|
-
#
|
|
116
|
-
#
|
|
117
|
-
# of failure it was so it can raise the
|
|
118
|
-
|
|
93
|
+
# Map an error caught at the dispatch boundary to the message and the
|
|
94
|
+
# category the native side frames into the Reply's fault arm. +error+
|
|
95
|
+
# is the +StandardError+ caught by #dispatch's rescue; the category
|
|
96
|
+
# tells the guest which kind of failure it was so it can raise the
|
|
97
|
+
# matching proxy-side error.
|
|
98
|
+
def caught_fault(error)
|
|
119
99
|
case error
|
|
120
|
-
when Kobako::Codec::Error then
|
|
121
|
-
|
|
122
|
-
when UndefinedTargetError then
|
|
123
|
-
when ArgumentError then
|
|
124
|
-
else
|
|
100
|
+
when Kobako::Codec::Error then fault("runtime",
|
|
101
|
+
"Sandbox received a malformed request: #{error.message}")
|
|
102
|
+
when UndefinedTargetError then fault("undefined", error.message)
|
|
103
|
+
when ArgumentError then fault("argument", error.message)
|
|
104
|
+
else fault("runtime", "#{error.class}: #{error.message}")
|
|
125
105
|
end
|
|
126
106
|
end
|
|
127
107
|
|
|
128
108
|
# Dispatch +method+ on +target+. +kwargs+ is already Symbol-keyed
|
|
129
|
-
# (the +
|
|
109
|
+
# (the +Payload::Arguments+ invariant pins it). The empty-kwargs branch omits
|
|
130
110
|
# the +**+ splat so Ruby 3.x's strict kwargs separation does not
|
|
131
111
|
# reject calls to no-kwarg methods when the wire carries the
|
|
132
112
|
# uniform empty-map shape.
|
|
@@ -138,8 +118,7 @@ module Kobako
|
|
|
138
118
|
# conditional.
|
|
139
119
|
def invoke(target, method, args, kwargs, yielder = nil)
|
|
140
120
|
name = method.to_sym
|
|
141
|
-
|
|
142
|
-
reject_unexposed!(target, name)
|
|
121
|
+
reject_unreachable!(target, name)
|
|
143
122
|
block = yielder&.to_proc
|
|
144
123
|
if kwargs.empty?
|
|
145
124
|
target.public_send(name, *args, &block)
|
|
@@ -148,38 +127,14 @@ module Kobako
|
|
|
148
127
|
end
|
|
149
128
|
end
|
|
150
129
|
|
|
151
|
-
# Guard the +public_send+ below
|
|
152
|
-
#
|
|
153
|
-
#
|
|
154
|
-
#
|
|
155
|
-
#
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
owner = target.public_method(name).owner
|
|
160
|
-
gadget = GADGET_OWNERS.include?(owner)
|
|
161
|
-
return unless META_OWNERS.include?(owner) || gadget
|
|
162
|
-
return if gadget && CALLABLE_ALLOW.include?(name)
|
|
163
|
-
|
|
164
|
-
raise UndefinedTargetError, "method #{name.inspect} is not a Service method"
|
|
165
|
-
rescue NameError
|
|
166
|
-
return if target.respond_to?(name)
|
|
167
|
-
|
|
168
|
-
raise UndefinedTargetError, "no public method #{name.inspect} on target"
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
# Consult the target's opt-in narrowing predicate. A bound object
|
|
172
|
-
# may define a private +respond_to_guest?(name)+ to restrict which of its
|
|
173
|
-
# methods the guest reaches; a falsy answer rejects the dispatch.
|
|
174
|
-
# The predicate composes beneath #reject_meta_method! — it only narrows,
|
|
175
|
-
# never re-opening the reflection surface the floor rejects — and is
|
|
176
|
-
# consulted with the private surface included so the guest's +public_send+
|
|
177
|
-
# dispatch can never reach +respond_to_guest?+ itself.
|
|
178
|
-
def reject_unexposed!(target, name)
|
|
179
|
-
return unless target.respond_to?(:respond_to_guest?, true)
|
|
180
|
-
return if target.__send__(:respond_to_guest?, name)
|
|
181
|
-
|
|
182
|
-
raise UndefinedTargetError, "method #{name.inspect} is not exposed to the guest"
|
|
130
|
+
# Guard the +public_send+ below: Reflection decides what counts as
|
|
131
|
+
# Service behaviour on this target, and its refusal reason becomes
|
|
132
|
+
# the guest's +undefined+ fault. Both the ambient-surface floor and
|
|
133
|
+
# the target's own narrowing predicate answer through it, so a
|
|
134
|
+
# rejected name discloses nothing about which of the two refused.
|
|
135
|
+
def reject_unreachable!(target, name)
|
|
136
|
+
reason = Reflection.refusal(target, name)
|
|
137
|
+
raise UndefinedTargetError, reason if reason
|
|
183
138
|
end
|
|
184
139
|
|
|
185
140
|
# Resolve every Kobako::Handle in an argument — bare or nested in an
|
|
@@ -192,24 +147,23 @@ module Kobako
|
|
|
192
147
|
raise UndefinedTargetError, e.message
|
|
193
148
|
end
|
|
194
149
|
|
|
195
|
-
# Resolve a
|
|
196
|
-
# Catalog::Handles) holds.
|
|
197
|
-
#
|
|
198
|
-
#
|
|
199
|
-
#
|
|
200
|
-
#
|
|
201
|
-
|
|
202
|
-
def resolve_target(target, services, handler)
|
|
150
|
+
# Resolve a Call target to the Ruby object the path +resolver+ (or
|
|
151
|
+
# Catalog::Handles) holds. The native side already discriminated the
|
|
152
|
+
# two forms off the core envelope's +kind+ tag: a String is a bound
|
|
153
|
+
# constant's path, an Integer is a Capability Handle id. No
|
|
154
|
+
# else-branch is needed — the envelope layer is the system boundary
|
|
155
|
+
# that enforces the invariant.
|
|
156
|
+
def resolve_target(target, resolver, handler)
|
|
203
157
|
case target
|
|
204
158
|
when String
|
|
205
|
-
resolve_path(target,
|
|
206
|
-
when
|
|
207
|
-
require_live_object!(target
|
|
159
|
+
resolve_path(target, resolver)
|
|
160
|
+
when Integer
|
|
161
|
+
require_live_object!(target, handler)
|
|
208
162
|
end
|
|
209
163
|
end
|
|
210
164
|
|
|
211
|
-
def resolve_path(path,
|
|
212
|
-
|
|
165
|
+
def resolve_path(path, resolver)
|
|
166
|
+
resolver.lookup(path)
|
|
213
167
|
rescue KeyError => e
|
|
214
168
|
raise UndefinedTargetError, e.message
|
|
215
169
|
end
|
|
@@ -222,15 +176,15 @@ module Kobako
|
|
|
222
176
|
raise UndefinedTargetError, e.message
|
|
223
177
|
end
|
|
224
178
|
|
|
225
|
-
# Encode +value+ as a
|
|
226
|
-
#
|
|
227
|
-
#
|
|
228
|
-
#
|
|
229
|
-
#
|
|
179
|
+
# Encode +value+ as the body of a Reply's ok arm — the value alone,
|
|
180
|
+
# since the envelope's tag already carries the success. A value that
|
|
181
|
+
# is not wire-representable per the codec's type mapping raises
|
|
182
|
+
# +UnsupportedTypeError+; the rescue routes it through the
|
|
183
|
+
# Catalog::Handles via #wrap_as_handle and re-encodes with the
|
|
184
|
+
# Capability Handle in place. The happy path encodes exactly once.
|
|
230
185
|
def encode_ok(value, handler)
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
rescue Kobako::Codec::UnsupportedType
|
|
186
|
+
Kobako::Codec::Encoder.encode(value)
|
|
187
|
+
rescue Kobako::Codec::UnsupportedTypeError
|
|
234
188
|
encode_ok(wrap_as_handle(value, handler), handler)
|
|
235
189
|
end
|
|
236
190
|
|
|
@@ -243,11 +197,9 @@ module Kobako
|
|
|
243
197
|
|
|
244
198
|
# +message+ folds to UTF-8 first: Ruby core builds some exception
|
|
245
199
|
# messages as ASCII-8BIT (the arity ArgumentError, for one), and
|
|
246
|
-
# the
|
|
247
|
-
def
|
|
248
|
-
message
|
|
249
|
-
fault = Kobako::Fault.new(type: type, message: message)
|
|
250
|
-
Kobako::Transport::Response.error(fault).encode
|
|
200
|
+
# the envelope requires UTF-8 of the text fields it frames.
|
|
201
|
+
def fault(type, message)
|
|
202
|
+
[message.encode(Encoding::UTF_8, invalid: :replace, undef: :replace), type] # : [String, String]
|
|
251
203
|
end
|
|
252
204
|
end
|
|
253
205
|
end
|
|
@@ -5,10 +5,9 @@ require_relative "../errors"
|
|
|
5
5
|
module Kobako
|
|
6
6
|
module Transport
|
|
7
7
|
# +Kobako::SandboxError+ subclass raised when the host detects a
|
|
8
|
-
# structural violation of the wire contract while
|
|
9
|
-
# produced
|
|
10
|
-
#
|
|
11
|
-
# fields). Distinct from a Wasm trap (engine signalled the guest
|
|
8
|
+
# structural violation of the wire contract while reading what the
|
|
9
|
+
# guest produced — an invocation value the payload codec cannot
|
|
10
|
+
# decode. Distinct from a Wasm trap (engine signalled the guest
|
|
12
11
|
# runtime is unrecoverable) and from a normal sandbox-layer failure
|
|
13
12
|
# (the script raised but the protocol was respected): a
|
|
14
13
|
# +Transport::Error+ always indicates the guest runtime is corrupted —
|
|
@@ -19,6 +18,23 @@ module Kobako
|
|
|
19
18
|
# +rescue Kobako::SandboxError+ still catches it; callers that want
|
|
20
19
|
# to distinguish wire-violation paths from script failures can
|
|
21
20
|
# +rescue Kobako::Transport::Error+ directly.
|
|
22
|
-
class Error < Kobako::SandboxError
|
|
21
|
+
class Error < Kobako::SandboxError
|
|
22
|
+
def initialize(message, diagnostic: nil, **)
|
|
23
|
+
super(message, **)
|
|
24
|
+
@diagnostic = diagnostic
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# The codec fault behind this violation, appended to Ruby's own
|
|
28
|
+
# rendering. A caller cannot act on the inner "Symbol payload must
|
|
29
|
+
# be …" wording, so it stays out of #message; an operator
|
|
30
|
+
# triaging a corrupted runtime still needs it, and
|
|
31
|
+
# +detailed_message+ is where Ruby puts text of exactly that kind.
|
|
32
|
+
def detailed_message(...)
|
|
33
|
+
rendered = super
|
|
34
|
+
return rendered if @diagnostic.nil?
|
|
35
|
+
|
|
36
|
+
"#{rendered}\n#{@diagnostic}"
|
|
37
|
+
end
|
|
38
|
+
end
|
|
23
39
|
end
|
|
24
40
|
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kobako
|
|
4
|
+
module Transport
|
|
5
|
+
# The reflection floor a guest→host dispatch must clear before the
|
|
6
|
+
# Dispatcher reaches +public_send+: which method names on a resolved
|
|
7
|
+
# target count as Service behaviour, and which are Ruby's ambient
|
|
8
|
+
# metaprogramming surface.
|
|
9
|
+
#
|
|
10
|
+
# Answers with a refusal reason rather than raising, so the error
|
|
11
|
+
# taxonomy stays with the Dispatcher and this module holds only the
|
|
12
|
+
# policy.
|
|
13
|
+
module Reflection
|
|
14
|
+
# Modules whose instance methods are ambient Ruby reflection /
|
|
15
|
+
# metaprogramming surface (+send+, +public_send+, +instance_eval+,
|
|
16
|
+
# +method+, +tap+, +instance_variable_get+, ...) rather than Service
|
|
17
|
+
# behaviour. A guest-supplied method name resolving to one of these is
|
|
18
|
+
# rejected: only methods the bound object itself exposes as Service
|
|
19
|
+
# behaviour are reachable, and +public_send(:send, ...)+ would
|
|
20
|
+
# otherwise let a guest pivot through +send+ into the private
|
|
21
|
+
# +Kernel#eval+ / +#system+ surface (host RCE).
|
|
22
|
+
META_OWNERS = [BasicObject, Kernel, Object, Module, Class].freeze
|
|
23
|
+
private_constant :META_OWNERS
|
|
24
|
+
|
|
25
|
+
# Callable gadget types whose own public methods are reflection surface
|
|
26
|
+
# (+Proc#binding+ reaches +Binding#eval+, +Method#receiver+ / +#unbind+
|
|
27
|
+
# hand back the underlying object) rather than Service behaviour. Only
|
|
28
|
+
# CALLABLE_ALLOW is reachable on a target of these types; a bound
|
|
29
|
+
# lambda stays invocable, its reflective surface does not.
|
|
30
|
+
GADGET_OWNERS = [Proc, Method, UnboundMethod, Binding].freeze
|
|
31
|
+
private_constant :GADGET_OWNERS
|
|
32
|
+
|
|
33
|
+
# The sole methods reachable on a GADGET_OWNERS target: invoking it
|
|
34
|
+
# (+call+ / +[]+ / +yield+) and the harmless +arity+ / +lambda?+
|
|
35
|
+
# describers that aid guest-side debugging.
|
|
36
|
+
CALLABLE_ALLOW = %i[call [] yield arity lambda?].freeze
|
|
37
|
+
private_constant :CALLABLE_ALLOW
|
|
38
|
+
|
|
39
|
+
module_function
|
|
40
|
+
|
|
41
|
+
# The reason +name+ is unreachable on +target+, or +nil+ when the
|
|
42
|
+
# dispatch may proceed. Composes the ambient-surface floor with the
|
|
43
|
+
# target's own opt-in narrowing, in that order: the predicate only
|
|
44
|
+
# narrows and can never re-open what the floor rejects.
|
|
45
|
+
def refusal(target, name)
|
|
46
|
+
ambient_refusal(target, name) || narrowing_refusal(target, name)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Guard against ambient reflection methods. A public method whose
|
|
50
|
+
# owner is a META_OWNERS or GADGET_OWNERS module is rejected, except
|
|
51
|
+
# CALLABLE_ALLOW on a gadget target (a bound lambda stays invocable).
|
|
52
|
+
# A name with no concrete public method is allowed only when the
|
|
53
|
+
# target opts into it via +respond_to?+ (dynamic +method_missing+
|
|
54
|
+
# Services), since the dangerous methods are all concretely defined
|
|
55
|
+
# and therefore never reach that branch.
|
|
56
|
+
def ambient_refusal(target, name)
|
|
57
|
+
owner = target.public_method(name).owner
|
|
58
|
+
gadget = GADGET_OWNERS.include?(owner)
|
|
59
|
+
return nil unless META_OWNERS.include?(owner) || gadget
|
|
60
|
+
return nil if gadget && CALLABLE_ALLOW.include?(name)
|
|
61
|
+
|
|
62
|
+
"method #{name.inspect} is not a Service method"
|
|
63
|
+
rescue NameError
|
|
64
|
+
return nil if target.respond_to?(name)
|
|
65
|
+
|
|
66
|
+
"no public method #{name.inspect} on target"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Consult the target's opt-in narrowing predicate. A bound object may
|
|
70
|
+
# define a private +respond_to_guest?(name)+ to restrict which of its
|
|
71
|
+
# methods the guest reaches; a falsy answer refuses the dispatch. It
|
|
72
|
+
# is consulted with the private surface included so the guest's
|
|
73
|
+
# +public_send+ dispatch can never reach +respond_to_guest?+ itself.
|
|
74
|
+
def narrowing_refusal(target, name)
|
|
75
|
+
return nil unless target.respond_to?(:respond_to_guest?, true)
|
|
76
|
+
return nil if target.__send__(:respond_to_guest?, name)
|
|
77
|
+
|
|
78
|
+
"method #{name.inspect} is not exposed to the guest"
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
data/lib/kobako/transport/run.rb
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative "../handle"
|
|
4
4
|
require_relative "../codec"
|
|
5
|
+
require_relative "../payload"
|
|
5
6
|
|
|
6
7
|
module Kobako
|
|
7
8
|
# See lib/kobako/transport.rb for the umbrella module doc; this file
|
|
8
|
-
# owns
|
|
9
|
-
#
|
|
9
|
+
# owns +Run+, the host-side value object for one +#run+ request — the
|
|
10
|
+
# native side frames it into the envelope +__kobako_run+ consumes.
|
|
10
11
|
module Transport
|
|
11
12
|
# Host-side value object for a single +Sandbox#run+ invocation
|
|
12
13
|
# ({docs/wire-codec.md Invocation channels}[link:../../../docs/wire-codec.md]).
|
|
@@ -16,23 +17,20 @@ module Kobako
|
|
|
16
17
|
# arguments. Host pre-flight (entrypoint type / name pattern, forged
|
|
17
18
|
# Handle, kwargs-key type) is enforced at construction so the Value
|
|
18
19
|
# Object is the single source of truth — anything that passes
|
|
19
|
-
# +Run.new+ is safe to
|
|
20
|
+
# +Run.new+ is safe to ship to the guest.
|
|
20
21
|
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
# +
|
|
24
|
-
#
|
|
25
|
-
# +kwargs+ leaf through it as a +Kobako::Handle+ — the
|
|
26
|
-
# symmetric counterpart of the guest→host wrap path in the
|
|
27
|
-
# dispatcher. A +Kobako::Handle+ that arrives **already
|
|
22
|
+
# +#payload+ takes the invocation's +Catalog::Handles+ and routes any
|
|
23
|
+
# non-wire-representable +args+ / +kwargs+ leaf through it as a
|
|
24
|
+
# +Kobako::Handle+ — the symmetric counterpart of the guest→host wrap
|
|
25
|
+
# path in the dispatcher. A +Kobako::Handle+ that arrives **already
|
|
28
26
|
# constructed** in the caller's +args+ / +kwargs+ is rejected at
|
|
29
|
-
# construction: legitimate Handles only enter Host App code
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
27
|
+
# construction: legitimate Handles only enter Host App code through
|
|
28
|
+
# error fields, so a Handle reaching the call site is by definition
|
|
29
|
+
# smuggled in. +Runtime#run+ frames that payload with the entrypoint
|
|
30
|
+
# into the Run envelope the +__kobako_run+ command buffer carries.
|
|
33
31
|
#
|
|
34
32
|
# Built on the +class X < Data.define(...)+ subclass form (the
|
|
35
|
-
# Steep-friendly shape — see
|
|
33
|
+
# Steep-friendly shape — see +.rubocop.yml+ for the rationale).
|
|
36
34
|
class Run < Data.define(:entrypoint, :args, :kwargs)
|
|
37
35
|
# Ruby constant-name pattern enforced on the +entrypoint+ Symbol.
|
|
38
36
|
# Parallel to
|
|
@@ -48,25 +46,19 @@ module Kobako
|
|
|
48
46
|
super
|
|
49
47
|
end
|
|
50
48
|
|
|
51
|
-
# Encode this Run
|
|
52
|
-
#
|
|
53
|
-
# ({docs/wire-codec.md Invocation channels}[link:../../../docs/wire-codec.md]).
|
|
49
|
+
# Encode this Run's arguments as the codec payload the Run
|
|
50
|
+
# envelope carries — +Runtime#run+ frames it with the entrypoint.
|
|
54
51
|
# Walks +args+ / +kwargs+ through Codec::HandleWalk.deep_wrap so
|
|
55
52
|
# any non-wire-representable leaf is allocated into +handler+ and
|
|
56
|
-
# replaced with a +Kobako::Handle+; the
|
|
57
|
-
#
|
|
58
|
-
#
|
|
59
|
-
#
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
Codec::Encoder.encode(
|
|
66
|
-
"entrypoint" => entrypoint,
|
|
67
|
-
"args" => Codec::HandleWalk.deep_wrap(args, handler),
|
|
68
|
-
"kwargs" => Codec::HandleWalk.deep_wrap(kwargs, handler)
|
|
69
|
-
)
|
|
53
|
+
# replaced with a +Kobako::Handle+; the +handler+ argument is the
|
|
54
|
+
# invocation's table, sharing the same allocator the guest→host
|
|
55
|
+
# return path uses. A wrapped leaf rides as ext 0x01 in its
|
|
56
|
+
# original position (docs/wire/payload-msgpack.md § ext 0x01).
|
|
57
|
+
def payload(handler)
|
|
58
|
+
Payload::Arguments.new(
|
|
59
|
+
args: Codec::HandleWalk.deep_wrap(args, handler),
|
|
60
|
+
kwargs: Codec::HandleWalk.deep_wrap(kwargs, handler)
|
|
61
|
+
).encode
|
|
70
62
|
end
|
|
71
63
|
|
|
72
64
|
private
|
|
@@ -95,7 +87,7 @@ module Kobako
|
|
|
95
87
|
# Handle objects only through raised error fields, so a Handle
|
|
96
88
|
# reaching +args+ is a forged or smuggled token. Non-wire-
|
|
97
89
|
# representable arguments that are not Handles are handled by
|
|
98
|
-
# auto-wrap inside +#
|
|
90
|
+
# auto-wrap inside +#payload+ — the reject path is reserved
|
|
99
91
|
# for Handle objects specifically.
|
|
100
92
|
def validate_args!(args)
|
|
101
93
|
raise ArgumentError, "arguments must be an Array" unless args.is_a?(Array)
|