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.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +127 -0
  4. data/README.md +96 -44
  5. data/ROADMAP.md +1 -1
  6. data/crates/kobako-runtime/CHANGELOG.md +44 -0
  7. data/crates/kobako-runtime/README.md +1 -1
  8. data/crates/kobako-transport/CHANGELOG.md +31 -0
  9. data/crates/kobako-transport/README.md +43 -0
  10. data/crates/kobako-wasmtime/CHANGELOG.md +30 -0
  11. data/crates/kobako-wasmtime/README.md +1 -1
  12. data/data/kobako.wasm +0 -0
  13. data/lib/kobako/3.3/kobako.so +0 -0
  14. data/lib/kobako/3.4/kobako.so +0 -0
  15. data/lib/kobako/4.0/kobako.so +0 -0
  16. data/lib/kobako/catalog/extensions.rb +50 -43
  17. data/lib/kobako/catalog/handles.rb +7 -16
  18. data/lib/kobako/catalog/services.rb +24 -46
  19. data/lib/kobako/catalog/snippets.rb +16 -33
  20. data/lib/kobako/codec/decoder.rb +10 -13
  21. data/lib/kobako/codec/encoder.rb +7 -7
  22. data/lib/kobako/codec/error.rb +6 -6
  23. data/lib/kobako/codec/ext_types.rb +12 -72
  24. data/lib/kobako/codec/handle_walk.rb +6 -6
  25. data/lib/kobako/codec/state.rb +4 -56
  26. data/lib/kobako/codec/utils.rb +4 -4
  27. data/lib/kobako/codec.rb +11 -21
  28. data/lib/kobako/context.rb +208 -0
  29. data/lib/kobako/errors.rb +59 -16
  30. data/lib/kobako/execution.rb +52 -0
  31. data/lib/kobako/extension.rb +24 -10
  32. data/lib/kobako/outcome.rb +71 -128
  33. data/lib/kobako/payload/arguments.rb +59 -0
  34. data/lib/kobako/payload.rb +19 -0
  35. data/lib/kobako/pool.rb +1 -7
  36. data/lib/kobako/sandbox.rb +51 -191
  37. data/lib/kobako/sandbox_options.rb +24 -3
  38. data/lib/kobako/snippet/binary.rb +3 -4
  39. data/lib/kobako/snippet/source.rb +5 -6
  40. data/lib/kobako/snippet.rb +2 -2
  41. data/lib/kobako/transport/call.rb +21 -0
  42. data/lib/kobako/transport/dispatcher.rb +94 -142
  43. data/lib/kobako/transport/error.rb +21 -5
  44. data/lib/kobako/transport/reflection.rb +82 -0
  45. data/lib/kobako/transport/run.rb +25 -33
  46. data/lib/kobako/transport/yielder.rb +27 -36
  47. data/lib/kobako/transport.rb +11 -8
  48. data/lib/kobako/unresolved.rb +17 -0
  49. data/lib/kobako/usage.rb +7 -7
  50. data/lib/kobako/version.rb +1 -1
  51. data/lib/kobako.rb +1 -0
  52. data/release-please-config.json +47 -2
  53. data/sig/kobako/catalog/extensions.rbs +4 -6
  54. data/sig/kobako/catalog/handles.rbs +0 -2
  55. data/sig/kobako/catalog/services.rbs +4 -4
  56. data/sig/kobako/catalog/snippets.rbs +4 -2
  57. data/sig/kobako/codec/error.rbs +4 -4
  58. data/sig/kobako/codec/ext_types.rbs +0 -7
  59. data/sig/kobako/codec/state.rbs +0 -4
  60. data/sig/kobako/codec.rbs +4 -5
  61. data/sig/kobako/context.rbs +36 -0
  62. data/sig/kobako/errors.rbs +27 -6
  63. data/sig/kobako/execution.rbs +24 -0
  64. data/sig/kobako/extension.rbs +4 -2
  65. data/sig/kobako/outcome.rbs +12 -13
  66. data/sig/kobako/payload/arguments.rbs +26 -0
  67. data/sig/kobako/payload.rbs +4 -0
  68. data/sig/kobako/pool.rbs +0 -2
  69. data/sig/kobako/runtime.rbs +44 -14
  70. data/sig/kobako/sandbox.rbs +5 -24
  71. data/sig/kobako/sandbox_options.rbs +9 -2
  72. data/sig/kobako/snippet/binary.rbs +1 -1
  73. data/sig/kobako/snippet/source.rbs +1 -1
  74. data/sig/kobako/transport/call.rbs +21 -0
  75. data/sig/kobako/transport/dispatcher.rbs +9 -11
  76. data/sig/kobako/transport/error.rbs +11 -0
  77. data/sig/kobako/transport/reflection.rbs +15 -0
  78. data/sig/kobako/transport/run.rbs +1 -1
  79. data/sig/kobako/transport/yielder.rbs +2 -2
  80. data/sig/kobako/transport.rbs +8 -2
  81. data/sig/kobako/unresolved.rbs +4 -0
  82. metadata +18 -12
  83. data/lib/kobako/fault.rb +0 -38
  84. data/lib/kobako/outcome/panic.rb +0 -42
  85. data/lib/kobako/transport/request.rb +0 -76
  86. data/lib/kobako/transport/response.rb +0 -83
  87. data/lib/kobako/transport/yield.rb +0 -94
  88. data/sig/kobako/fault.rbs +0 -19
  89. data/sig/kobako/outcome/panic.rbs +0 -34
  90. data/sig/kobako/transport/request.rbs +0 -29
  91. data/sig/kobako/transport/response.rbs +0 -33
  92. 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 "request"
5
- require_relative "response"
6
- require_relative "yield"
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 decodes guest-initiated
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 transport calls.
15
- # Decodes a msgpack-encoded Request envelope, resolves the target
16
- # object through the Catalog::Services (path lookup) or
17
- # Catalog::Handles (Handle lookup), invokes the method, and returns
18
- # a msgpack-encoded Response envelope.
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. Mapped to
39
- # Response.error with type="undefined". Contained at the wire boundary —
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
- # Modules whose instance methods are ambient Ruby reflection /
44
- # metaprogramming surface (+send+, +public_send+, +instance_eval+,
45
- # +method+, +tap+, +instance_variable_get+, ...) rather than Service
46
- # behaviour. A guest-supplied method name resolving to one of these is
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 registry doesn't
73
- # need to publish accessors for the Sandbox-owned +Catalog::Handles+
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 Request carries +block_given: true+. Always
77
- # returns a binary String — every failure path is reified as a
78
- # Response.error envelope so the guest sees a transport error rather
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(request_bytes, services, handler, yield_to_guest)
85
- request, carried_handle = Kobako::Codec.track_handles { Kobako::Transport::Request.decode(request_bytes) }
86
- target = resolve_target(request.target, services, handler)
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 Response.error, while a
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
- encode_caught_error(e)
64
+ [false, *caught_fault(e)] # : [bool, String, String?]
97
65
  ensure
98
66
  yielder&.invalidate!
99
67
  end
100
68
 
101
- # Resolve positional and keyword arguments off +request+ in one step.
102
- # +carried_handle+ reports whether the decode carried any Capability
103
- # Handle; when it did not, every argument resolves to itself, so the
104
- # decoded values pass straight through and the walk is skipped entirely.
105
- # Otherwise both go through #resolve_arg so Handles round-trip back to
106
- # the host-side Ruby object before the call reaches +public_send+.
107
- def resolve_call_args(request, handler, carried_handle)
108
- return [request.args, request.kwargs] unless carried_handle
109
-
110
- [request.args.map { |v| resolve_arg(v, handler) },
111
- request.kwargs.transform_values { |v| resolve_arg(v, handler) }]
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 a +Response.error+
115
- # envelope (binary msgpack). +error+ is the +StandardError+ caught by
116
- # #dispatch's rescue; the +type+ field tells the guest which kind
117
- # of failure it was so it can raise the matching proxy-side error.
118
- def encode_caught_error(error)
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 encode_error("runtime",
121
- "Sandbox received a malformed request: #{error.message}")
122
- when UndefinedTargetError then encode_error("undefined", error.message)
123
- when ArgumentError then encode_error("argument", error.message)
124
- else encode_error("runtime", "#{error.class}: #{error.message}")
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 +Request+ invariant pins it). The empty-kwargs branch omits
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
- reject_meta_method!(target, name)
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 against ambient reflection methods.
152
- # A public method whose owner is a META_OWNERS or GADGET_OWNERS module is
153
- # rejected, except CALLABLE_ALLOW on a gadget target (a bound lambda
154
- # stays invocable). A name with no concrete public method is allowed
155
- # only when the target opts into it via +respond_to?+ (dynamic
156
- # +method_missing+ Services), since the dangerous methods are all
157
- # concretely defined and therefore never reach that branch.
158
- def reject_meta_method!(target, name)
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 Request target to the Ruby object the registry (or
196
- # Catalog::Handles) holds. String targets go through the registry;
197
- # Handle targets (ext 0x01) go through the Catalog::Handles.
198
- #
199
- # Target type is already validated by +Transport::Request.decode+
200
- # before this method is reached, so no else-branch is needed here —
201
- # the wire layer is the system boundary that enforces the invariant.
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, services)
206
- when Kobako::Handle
207
- require_live_object!(target.id, handler)
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, services)
212
- services.lookup(path)
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 +Response.ok+ envelope. When the value is not
226
- # wire-representable per the codec's type mapping, the
227
- # +UnsupportedType+ rescue routes it through the
228
- # Catalog::Handles via #wrap_as_handle and re-encodes with the Capability
229
- # Handle in place. The happy path encodes exactly once.
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
- response = Kobako::Transport::Response.ok(value)
232
- response.encode
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 codec would ride those as bin — a shape the guest refuses.
247
- def encode_error(type, message)
248
- message = message.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
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 decoding bytes
9
- # produced by the guest (a malformed Outcome envelope, a result body
10
- # that fails msgpack decode, a Panic envelope missing required
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; end
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
@@ -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 the +Run+ envelope value object — the host→guest request shape
9
- # consumed by +__kobako_run+.
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 encode and ship to the guest.
20
+ # +Run.new+ is safe to ship to the guest.
20
21
  #
21
- # Run is the host→guest entrypoint dispatch envelope (the +#run+
22
- # request shape), the symmetric counterpart to the guest→host
23
- # +Request+ envelope. +#encode+ takes the Sandbox's
24
- # +Catalog::Handles+ and routes any non-wire-representable +args+ /
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
- # through error fields, so a Handle reaching the call site is by
31
- # definition smuggled in. The +#encode+ output is the "Run envelope"
32
- # that ships through the +__kobako_run+ command buffer.
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 +lib/kobako/outcome/panic.rb+).
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 to the msgpack bytes the guest's +__kobako_run+
52
- # entry point consumes as its command-buffer payload
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
- # +handler+ argument is the Sandbox's table, sharing the same
58
- # allocator the guest→host return path uses.
59
- #
60
- # Layout: msgpack map with string keys +"entrypoint"+ (Symbol via
61
- # ext 0x00), +"args"+ (Array), +"kwargs"+ (Map with Symbol keys);
62
- # any wrapped leaf rides as ext 0x01 in its original position
63
- # (docs/wire-codec.md § ext 0x01 position rules).
64
- def encode(handler)
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 +#encode+ — the reject path is reserved
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)