kobako 0.30.0-aarch64-linux → 0.31.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 (94) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +335 -0
  4. data/README.md +43 -3
  5. data/ROADMAP.md +1 -1
  6. data/crates/kobako-runtime/CHANGELOG.md +219 -0
  7. data/crates/kobako-runtime/README.md +7 -4
  8. data/crates/kobako-transport/CHANGELOG.md +31 -0
  9. data/crates/kobako-transport/README.md +3 -4
  10. data/crates/kobako-wasmtime/CHANGELOG.md +161 -0
  11. data/crates/kobako-wasmtime/README.md +3 -2
  12. data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
  13. data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
  14. data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
  15. data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
  16. data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
  17. data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
  18. data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
  19. data/data/kobako.wasm +0 -0
  20. data/lib/kobako/3.3/kobako.so +0 -0
  21. data/lib/kobako/3.4/kobako.so +0 -0
  22. data/lib/kobako/4.0/kobako.so +0 -0
  23. data/lib/kobako/catalog/extensions.rb +14 -2
  24. data/lib/kobako/catalog/flags.rb +3 -5
  25. data/lib/kobako/catalog/grants.rb +5 -4
  26. data/lib/kobako/catalog/handles.rb +5 -27
  27. data/lib/kobako/catalog/mount.rb +11 -3
  28. data/lib/kobako/catalog/network.rb +35 -0
  29. data/lib/kobako/catalog/replacements.rb +28 -0
  30. data/lib/kobako/catalog/services.rb +5 -19
  31. data/lib/kobako/catalog/snippets.rb +3 -18
  32. data/lib/kobako/catalog.rb +3 -0
  33. data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
  34. data/lib/kobako/codec/decoder.rb +4 -4
  35. data/lib/kobako/codec/ext_types.rb +3 -3
  36. data/lib/kobako/codec/handle_walk.rb +3 -43
  37. data/lib/kobako/codec/value_budget.rb +132 -40
  38. data/lib/kobako/codec.rb +1 -1
  39. data/lib/kobako/context.rb +24 -11
  40. data/lib/kobako/errors.rb +15 -12
  41. data/lib/kobako/extension.rb +3 -2
  42. data/lib/kobako/filesystem.rb +9 -2
  43. data/lib/kobako/handle.rb +2 -2
  44. data/lib/kobako/names.rb +28 -0
  45. data/lib/kobako/network.rb +210 -0
  46. data/lib/kobako/outcome.rb +7 -7
  47. data/lib/kobako/pool.rb +45 -23
  48. data/lib/kobako/runtime.rb +3 -2
  49. data/lib/kobako/sandbox.rb +92 -35
  50. data/lib/kobako/sandbox_options.rb +41 -106
  51. data/lib/kobako/transport/argument_wrap.rb +64 -0
  52. data/lib/kobako/transport/block_failure.rb +1 -1
  53. data/lib/kobako/transport/dispatcher.rb +6 -6
  54. data/lib/kobako/transport/exposure.rb +26 -15
  55. data/lib/kobako/transport/reflection.rb +21 -4
  56. data/lib/kobako/transport/run.rb +8 -23
  57. data/lib/kobako/transport/yielder.rb +2 -9
  58. data/lib/kobako/transport.rb +1 -0
  59. data/lib/kobako/version.rb +1 -1
  60. data/lib/kobako.rb +6 -2
  61. data/release-please-config.json +15 -0
  62. data/sig/core/thread.rbs +6 -0
  63. data/sig/kobako/catalog/extensions.rbs +2 -0
  64. data/sig/kobako/catalog/grants.rbs +7 -4
  65. data/sig/kobako/catalog/handles.rbs +0 -2
  66. data/sig/kobako/catalog/mount.rbs +3 -1
  67. data/sig/kobako/catalog/network.rbs +20 -0
  68. data/sig/kobako/catalog/replacements.rbs +14 -0
  69. data/sig/kobako/catalog/services.rbs +0 -4
  70. data/sig/kobako/catalog/snippets.rbs +0 -2
  71. data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
  72. data/sig/kobako/codec/handle_walk.rbs +0 -6
  73. data/sig/kobako/codec/value_budget.rbs +47 -4
  74. data/sig/kobako/context.rbs +3 -1
  75. data/sig/kobako/errors.rbs +4 -4
  76. data/sig/kobako/filesystem.rbs +21 -3
  77. data/sig/kobako/network.rbs +115 -0
  78. data/sig/kobako/outcome.rbs +1 -1
  79. data/sig/kobako/pool.rbs +7 -3
  80. data/sig/kobako/runtime.rbs +32 -3
  81. data/sig/kobako/sandbox.rbs +19 -2
  82. data/sig/kobako/sandbox_options.rbs +29 -38
  83. data/sig/kobako/transport/argument_wrap.rbs +15 -0
  84. data/sig/kobako/transport/error.rbs +1 -1
  85. data/sig/kobako/transport/exposure.rbs +2 -4
  86. data/sig/kobako/transport/reflection.rbs +10 -0
  87. data/sig/kobako/transport/run.rbs +0 -2
  88. data/sig/kobako/transport/yielder.rbs +0 -3
  89. data/sig/kobako/transport.rbs +1 -1
  90. data/sig/kobako.rbs +13 -0
  91. metadata +23 -9
  92. data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
  93. data/lib/kobako/constant_name.rb +0 -8
  94. data/sig/kobako/constant_name.rbs +0 -3
data/data/kobako.wasm CHANGED
Binary file
Binary file
Binary file
Binary file
@@ -66,13 +66,25 @@ module Kobako
66
66
 
67
67
  def validate(extension)
68
68
  source = extension.source
69
- raise ArgumentError, "Extension #source must be a String, got #{source.class}" unless source.is_a?(String)
69
+ raise TypeError, "Extension #source must be a String, got #{source.class}" unless source.is_a?(String)
70
70
 
71
+ validate_dependencies(extension.depends_on)
71
72
  backend = extension.backend
72
73
  return if backend.nil?
73
74
  return if backend.respond_to?(:path) && backend.respond_to?(:object) && backend.respond_to?(:provider)
74
75
 
75
- raise ArgumentError, "Extension #backend must expose #path, #object, and #provider"
76
+ raise TypeError, "Extension #backend must expose #path, #object, and #provider"
77
+ end
78
+
79
+ # Each dependency names an Extension as its +name+ does.
80
+ def validate_dependencies(dependencies)
81
+ return if dependencies.nil?
82
+ unless dependencies.is_a?(Array)
83
+ raise TypeError,
84
+ "Extension #depends_on must be an Array, got #{dependencies.class}"
85
+ end
86
+
87
+ dependencies.each { |dependency| Kobako.to_name(dependency, "Extension #depends_on entry") }
76
88
  end
77
89
 
78
90
  def unmet_dependency
@@ -13,12 +13,10 @@ module Kobako
13
13
  end
14
14
 
15
15
  def register(name, value, advisory:)
16
- unless name.is_a?(Symbol) || name.is_a?(String)
17
- raise ArgumentError, "flag name must be a Symbol or String, got #{name.class}"
18
- end
19
- raise ArgumentError, "flag value must be a String, got #{value.class}" unless value.is_a?(String)
16
+ name = Kobako.to_name(name, "flag name")
17
+ raise TypeError, "flag value must be a String, got #{value.class}" unless value.is_a?(String)
20
18
 
21
- key = Catalog.to_utf8(name.to_s, "flag name")
19
+ key = Catalog.to_utf8(name, "flag name")
22
20
  raise ArgumentError, "flag #{key.inspect} already set" if @entries.key?(key)
23
21
 
24
22
  @entries[key] = [Catalog.to_utf8(value, "flag value"), !advisory]
@@ -4,7 +4,7 @@ module Kobako
4
4
  module Catalog
5
5
  class Grants # :nodoc:
6
6
  def initialize
7
- @entries = {} # : Hash[String, Mount]
7
+ @entries = {} # : Hash[key, Mount | Network]
8
8
  end
9
9
 
10
10
  # Each Grant decides what it cannot stand beside.
@@ -20,10 +20,11 @@ module Kobako
20
20
  @entries.key?(grant.key)
21
21
  end
22
22
 
23
- # Each Grant as the native side reads it, an override standing in for
24
- # the Grant it shares a key with.
23
+ # The Mounts, then the network entries, each as the native side reads
24
+ # it, an override standing in for the Grant it shares a key with.
25
25
  def entries(overrides = {})
26
- @entries.merge(overrides).values.map(&:to_native)
26
+ grants = @entries.merge(overrides).values
27
+ [grants.grep(Mount).map(&:to_native), grants.grep(Network).map(&:to_native)]
27
28
  end
28
29
  end
29
30
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "delegate"
4
-
5
3
  require_relative "../handle"
6
4
  require_relative "../transport/exposure"
7
5
 
@@ -21,9 +19,12 @@ module Kobako
21
19
 
22
20
  # The entry records the object's Exposure as it stands at mint, so a
23
21
  # call made through this Handle is authorized against the reference
24
- # the guest was given.
22
+ # the guest was given. The mint point asks the refusal itself, so it
23
+ # holds on every path a Handle is issued.
25
24
  def alloc(object)
26
- reject_unwrappable(object)
25
+ refused = Kobako::Transport::Exposure.refused_class_of(object)
26
+ raise SandboxError, "a #{refused} cannot cross as a Capability Handle" if refused
27
+
27
28
  ensure_capacity
28
29
  id = @next_id
29
30
  @entries[id] = Kobako::Transport::Exposure.of(object, @surfaces)
@@ -48,29 +49,6 @@ module Kobako
48
49
 
49
50
  private
50
51
 
51
- # Refuse to mint a Capability Handle for an object whose reachable
52
- # surface is not Service behaviour: a +Binding+ / +Method+ /
53
- # +UnboundMethod+ hands the guest a callable proxy onto host
54
- # reflection (a returned +Binding+ reaches +Binding#eval+); a +Class+
55
- # or +Module+ hands over its class-level API (+File.popen+ / +read+,
56
- # +Kernel.system+), which the owner-based dispatch floor cannot see
57
- # because a singleton-class owner matches no core-module list; a
58
- # +Delegator+ (+SimpleDelegator+, +DelegateClass+, +WeakRef+,
59
- # +Tempfile+) is a transparent forwarder whose public +method_missing+
60
- # binds and calls the private method the guest names (+Kernel#system+),
61
- # a surface the floor reads as ordinary Service behaviour. Raising here
62
- # keeps the rule at the single mint point, so it holds on both the
63
- # Service-return and the +#run+ host→guest auto-wrap paths.
64
- def reject_unwrappable(object)
65
- case object
66
- when Binding, Method, UnboundMethod, Module, Delegator
67
- # Delegator < BasicObject exposes no static +#class+, so name the
68
- # rejected object's class through Object's own.
69
- kind = Object.instance_method(:class).bind_call(object)
70
- raise SandboxError, "a #{kind} cannot cross as a Capability Handle"
71
- end
72
- end
73
-
74
52
  def ensure_capacity
75
53
  cap = Kobako::Handle::MAX_ID
76
54
  return unless @next_id > cap
@@ -3,13 +3,21 @@
3
3
  module Kobako
4
4
  module Catalog
5
5
  class Mount < Data.define(:at, :access, :filesystem, :policy) # :nodoc:
6
- # The path a Context replaces this Mount at.
7
- def key = at
6
+ # The Mount at +at+, refused by the rules an invocation builds it with,
7
+ # so a mistake raises where it was written.
8
+ def initialize(at:, access:, filesystem:, policy:)
9
+ Kobako::Filesystem.validate_mount(at, access, filesystem)
10
+ super
11
+ end
12
+
13
+ # The kind and path a Context replaces this Mount at, so a Grant of
14
+ # another kind never meets it.
15
+ def key = [:mount, at]
8
16
 
9
17
  # Why +other+ cannot be mounted beside this Mount, or +nil+: a path
10
18
  # means one Mount, as Docker refuses a duplicate mount point.
11
19
  def conflict(other)
12
- "path #{at.inspect} is already mounted" if other.at == at
20
+ "path #{at.inspect} is already mounted" if other.key == key
13
21
  end
14
22
 
15
23
  # The Mount as the native side reads it.
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kobako
4
+ module Catalog
5
+ class Network < Data.define(:name, :entry) # :nodoc:
6
+ # The entry named +name+: +entry+ as it stands, or a Builder adding what
7
+ # the block declares to it. Raises TypeError for a name of another class,
8
+ # and ArgumentError for an empty name, which no Context could replace it
9
+ # by.
10
+ def initialize(name:, entry:)
11
+ name = Kobako.to_name(name, "network name").freeze
12
+ Kobako::Network.validate_name(name)
13
+
14
+ if block_given?
15
+ entry = Kobako::Network::Builder.new(entry)
16
+ yield entry
17
+ end
18
+ super
19
+ end
20
+
21
+ # The kind and name a Context replaces this entry by, so a Grant of
22
+ # another kind never meets it.
23
+ def key = [:network, name]
24
+
25
+ # Why +other+ cannot be given beside this entry, or +nil+: a name means
26
+ # one entry, as a path means one Mount.
27
+ def conflict(other)
28
+ "network entry #{name.inspect} is already given" if other.key == key
29
+ end
30
+
31
+ # The entry as the native side reads it.
32
+ def to_native = [name, entry]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kobako
4
+ module Catalog
5
+ # The Grants one invocation's Context replaced, each standing in for the
6
+ # Sandbox's Grant at its key.
7
+ class Replacements # :nodoc:
8
+ def initialize(grants)
9
+ @grants = grants
10
+ @replaced = {} # : Hash[Grants::key, Grants::grant]
11
+ end
12
+
13
+ # Have +grant+ stand in for the Grant at its key, raising ArgumentError
14
+ # with +refusal+ when the Sandbox holds none there, so a replacement
15
+ # never adds a Grant.
16
+ def replace(grant, refusal)
17
+ raise ArgumentError, refusal unless @grants.include?(grant)
18
+
19
+ @replaced[grant.key] = grant
20
+ nil
21
+ end
22
+
23
+ # The Sandbox's Grants with each replacement in place, as Grants#entries
24
+ # gives them.
25
+ def entries = @grants.entries(@replaced)
26
+ end
27
+ end
28
+ end
@@ -1,15 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../constant_name"
4
3
  require_relative "../errors"
5
4
  require_relative "../transport/exposure"
6
5
 
7
6
  module Kobako
8
7
  module Catalog
9
8
  class Services # :nodoc:
10
- # The Guest Binary keeps this namespace for constants it may add later.
11
- RESERVED_NAMESPACE = "Kobako"
12
-
13
9
  def initialize
14
10
  @bindings = {} # : Hash[String, Kobako::Transport::Exposure]
15
11
  end
@@ -17,9 +13,8 @@ module Kobako
17
13
  # The binding records +object+'s Exposure as it stands now.
18
14
  def bind(path, object)
19
15
  path_str = validate_path(path)
20
- if reserved?(path_str)
21
- raise ArgumentError,
22
- "Service path #{path_str} is in the reserved #{RESERVED_NAMESPACE} namespace"
16
+ if Kobako.reserved_path?(path_str)
17
+ raise ArgumentError, "Service path #{path_str} is in the reserved Kobako namespace"
23
18
  end
24
19
  raise ArgumentError, "Service path #{path_str} conflicts with an existing binding" if collision?(path_str)
25
20
 
@@ -47,25 +42,16 @@ module Kobako
47
42
  private
48
43
 
49
44
  def validate_path(path)
50
- path_str = path.to_s
51
- segments = path_str.split("::", -1)
52
- return path_str if !segments.empty? && segments.all? { |seg| CONSTANT_NAME.match?(seg) }
45
+ path_str = Kobako.to_name(path, "bind path")
46
+ return path_str if Kobako.constant_path?(path_str)
53
47
 
54
48
  raise ArgumentError,
55
49
  "bind path must be constant-form segments joined by '::' (got #{path.inspect})"
56
50
  end
57
51
 
58
- def reserved?(path)
59
- path.split("::").first == RESERVED_NAMESPACE
60
- end
61
-
62
52
  # Keeps a name from being both a bound Service and a grouping prefix.
63
53
  def collision?(path)
64
- @bindings.each_key.any? do |existing|
65
- existing == path ||
66
- existing.start_with?("#{path}::") ||
67
- path.start_with?("#{existing}::")
68
- end
54
+ @bindings.each_key.any? { |bound| Kobako.paths_collide?(path, bound) }
69
55
  end
70
56
  end
71
57
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../constant_name"
4
3
  require_relative "../snippet"
5
4
 
6
5
  module Kobako
@@ -52,7 +51,7 @@ module Kobako
52
51
  code, name = ensure_source_args(code, name)
53
52
  body = Catalog.to_utf8(code, "code")
54
53
 
55
- name_sym = normalize_name(name)
54
+ name_sym = Kobako.to_constant_name(name, "snippet name")
56
55
  if @entries.any? { |e| e.is_a?(Snippet::Source) && e.name == name_sym }
57
56
  raise ArgumentError, "snippet #{name_sym.inspect} already preloaded"
58
57
  end
@@ -65,7 +64,7 @@ module Kobako
65
64
  # as a type error rather than a missing keyword.
66
65
  def ensure_source_args(code, name)
67
66
  raise ArgumentError, "missing keyword: code: + name:, or binary:" if code.nil? && name.nil?
68
- raise ArgumentError, "code must be a String, got #{code.class}" unless code.is_a?(String)
67
+ raise TypeError, "code must be a String, got #{code.class}" unless code.is_a?(String)
69
68
  raise ArgumentError, "missing keyword: name:" if name.nil?
70
69
 
71
70
  [code, name]
@@ -74,7 +73,7 @@ module Kobako
74
73
  # Kept as bytes whatever encoding the caller tagged them with, since
75
74
  # the guest reads them as bytecode rather than text.
76
75
  def register_binary(bytes)
77
- raise ArgumentError, "binary must be a String, got #{bytes.class}" unless bytes.is_a?(String)
76
+ raise TypeError, "binary must be a String, got #{bytes.class}" unless bytes.is_a?(String)
78
77
 
79
78
  @entries << Snippet::Bytecode.new(body: bytes.dup.force_encoding(Encoding::ASCII_8BIT))
80
79
  nil
@@ -91,20 +90,6 @@ module Kobako
91
90
  [Snippet::Bytecode::KIND, nil, entry.body]
92
91
  end
93
92
  end
94
-
95
- def normalize_name(name)
96
- unless name.is_a?(Symbol) || name.is_a?(String)
97
- raise ArgumentError, "snippet name must be a Symbol or String, got #{name.class}"
98
- end
99
-
100
- name_str = name.to_s
101
- unless CONSTANT_NAME.match?(name_str)
102
- raise ArgumentError,
103
- "snippet name must match #{CONSTANT_NAME.inspect} (got #{name.inspect})"
104
- end
105
-
106
- name_str.to_sym
107
- end
108
93
  end
109
94
  end
110
95
  end
@@ -1,12 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "names"
3
4
  require_relative "catalog/handles"
4
5
  require_relative "catalog/services"
5
6
  require_relative "catalog/snippets"
6
7
  require_relative "catalog/flags"
7
8
  require_relative "catalog/extensions"
8
9
  require_relative "catalog/mount"
10
+ require_relative "catalog/network"
9
11
  require_relative "catalog/grants"
12
+ require_relative "catalog/replacements"
10
13
 
11
14
  module Kobako
12
15
  module Catalog # :nodoc:
@@ -4,7 +4,9 @@ require_relative "error"
4
4
 
5
5
  module Kobako
6
6
  module Codec
7
- module Utils # :nodoc:
7
+ # What the codec refuses as it reads a value off the wire, each refusal
8
+ # raised as a Codec error.
9
+ module Boundary # :nodoc:
8
10
  def self.assert_utf8(string, label)
9
11
  return if string.valid_encoding?
10
12
 
@@ -14,7 +16,7 @@ module Kobako
14
16
  # Only for a value object built outside a Decoder.decode block, whose
15
17
  # rescue already maps the same way; a host-layer +ArgumentError+
16
18
  # elsewhere should propagate unchanged.
17
- def self.with_boundary
19
+ def self.within
18
20
  yield
19
21
  rescue ::ArgumentError => e
20
22
  raise InvalidTypeError, e.message
@@ -4,14 +4,14 @@ require "msgpack"
4
4
 
5
5
  require_relative "error"
6
6
  require_relative "ext_types"
7
- require_relative "utils"
7
+ require_relative "boundary"
8
8
 
9
9
  module Kobako
10
10
  module Codec
11
11
  module Decoder # :nodoc:
12
12
  # The block runs inside this method's rescue, so a Value Object built
13
13
  # from the decoded payload reports a broken invariant as
14
- # InvalidTypeError without its own Utils.with_boundary.
14
+ # InvalidTypeError without its own Boundary.within.
15
15
  def self.decode(bytes)
16
16
  ValueBudget.assert_within(bytes, State.current.value_budget)
17
17
  value = FACTORY.load(bytes.b)
@@ -35,13 +35,13 @@ module Kobako
35
35
  # returns UTF-8-tagged Strings for
36
36
  # str family but does not validate the bytes; +bin+ family decodes
37
37
  # to ASCII-8BIT. Walk the tree once and reject invalid UTF-8 in any
38
- # str-typed leaf via Utils.assert_utf8.
38
+ # str-typed leaf via Boundary.assert_utf8.
39
39
  class << self
40
40
  private
41
41
 
42
42
  def validate_utf8(value)
43
43
  case value
44
- when String then Utils.assert_utf8(value, "str payload") if value.encoding == Encoding::UTF_8
44
+ when String then Boundary.assert_utf8(value, "str payload") if value.encoding == Encoding::UTF_8
45
45
  when Array then value.each { |v| validate_utf8(v) }
46
46
  when Hash
47
47
  value.each do |key, val|
@@ -3,7 +3,7 @@
3
3
  require "msgpack"
4
4
 
5
5
  require_relative "error"
6
- require_relative "utils"
6
+ require_relative "boundary"
7
7
  require_relative "state"
8
8
  require_relative "../handle"
9
9
 
@@ -49,7 +49,7 @@ module Kobako
49
49
  # unpacker would otherwise apply to invalid bytes.
50
50
  def unpack_symbol(payload)
51
51
  name = payload.b.force_encoding(Encoding::UTF_8)
52
- Utils.assert_utf8(name, "Symbol payload")
52
+ Boundary.assert_utf8(name, "Symbol payload")
53
53
  name.to_sym
54
54
  end
55
55
 
@@ -66,7 +66,7 @@ module Kobako
66
66
  raise InvalidTypeError, "Handle payload must be 4 bytes, got #{bytes.bytesize}" unless bytes.bytesize == 4
67
67
 
68
68
  id = bytes.unpack1("N") # : Integer
69
- Codec::Utils.with_boundary { Kobako::Handle.restore(id) }
69
+ Codec::Boundary.within { Kobako::Handle.restore(id) }
70
70
  end
71
71
 
72
72
  def register_symbol(factory)
@@ -26,22 +26,9 @@ module Kobako
26
26
  primitive_type?(value) || container_representable?(value, depth)
27
27
  end
28
28
 
29
- # Only Hash values are wrapped: a non-representable key raises rather
30
- # than crossing as a token the guest→host restore walk could not
31
- # round-trip.
32
- def self.deep_wrap(value, handles, depth = 0)
33
- guard_nesting(depth)
34
- case value
35
- when ::Array then value.map { |element| deep_wrap(element, handles, depth + 1) }
36
- when ::Hash then deep_wrap_hash(value, handles, depth)
37
- else
38
- representable?(value) ? value : handles.alloc(value)
39
- end
40
- end
41
-
42
- # The inverse of #deep_wrap. A Handle here was decoded off the wire,
43
- # never forged by the guest, and +handles.fetch+ refuses an id with no
44
- # live binding.
29
+ # Each Handle a decoded value holds, as the object it stands for. A
30
+ # Handle here was decoded off the wire, never forged by the guest, and
31
+ # +handles.fetch+ refuses an id with no live binding.
45
32
  def self.deep_restore(value, handles)
46
33
  case value
47
34
  when ::Array then value.map { |element| deep_restore(element, handles) }
@@ -59,33 +46,6 @@ module Kobako
59
46
  class << self
60
47
  private
61
48
 
62
- # A reference cycle necessarily trips the nesting bound, so a +#run+
63
- # argument fails as a clean +Kobako::SandboxError+ rather than an
64
- # unbounded host recursion.
65
- def guard_nesting(depth)
66
- return unless depth > MAX_NESTING_DEPTH
67
-
68
- raise Kobako::SandboxError,
69
- "a #run argument nests deeper than #{MAX_NESTING_DEPTH} levels within the Run payload and " \
70
- "cannot cross the sandbox boundary (possible reference cycle)"
71
- end
72
-
73
- # A stateful object may cross the boundary as a Hash value but not as
74
- # a key — the one deliberate asymmetry with the guest→host restore
75
- # walk, which resolves Handle keys the guest built.
76
- def deep_wrap_hash(hash, handles, depth)
77
- wrapped = {} # : Hash[untyped, untyped]
78
- hash.each do |key, val|
79
- unless representable?(key, depth + 1)
80
- raise Kobako::SandboxError,
81
- "a Hash passed to #run has a key that cannot cross the sandbox boundary " \
82
- "(#{key.class}); only wire-representable values may be Hash keys"
83
- end
84
- wrapped[key] = deep_wrap(val, handles, depth + 1)
85
- end
86
- wrapped
87
- end
88
-
89
49
  def primitive_type?(value)
90
50
  case value
91
51
  when ::NilClass, ::TrueClass, ::FalseClass, ::Float, ::String, ::Symbol, Kobako::Handle then true