---
title: "Kobako: From Default Allow to Deny — Balancing Convenience and Security"
date: 2026-11-18T00:00:00+08:00
publishDate: 2026-11-18T00:00:00+08:00
lastmod: 2026-11-16T21:43:57+08:00
tags: ["LLM","AI","Experience","Ruby","WebAssembly","Gem","mruby","Rust"]
series: "kobako"
toc: true
aiTranslated: true
cover: "https://blog.aotoki.me/posts/2026/11/18/kobako-from-default-allow-to-deny/cover_hu_c57a47befc618b56.webp"
permalink: "https://blog.aotoki.me/en/posts/2026/11/18/kobako-from-default-allow-to-deny/"
language: "en"
---

From the design stage, Kobako aimed to let the Host and the Guest communicate easily, which is why the [Bind](https://blog.aotoki.me/en/posts/2026/11/11/kobako-why-sandbox-has-no-time/) service mechanism was designed in the first place.

To make it as easy as possible to use, you could pass basically any object to `Sandbox#bind` and call that object’s methods from the Guest. That convenience brought a huge risk, and in the end the only option was to change the design.

## Escape

Most security problems come from incomplete “boundary” design. Many recent security incidents in Taiwan also started with a weak point being breached, and became severe because the attacker could keep moving to other places from there.

The Kobako Guest is, by definition, a place that “can run arbitrary code,” which makes it the weak point. And the “Bind mechanism” can reach from the Guest through to the Host, satisfying the condition for moving around, so together they form an attack path.

Under normal circumstances, we would hardly think of such a path, but once we bound an ordinary object, Ruby’s language features exposed the vulnerability.

```ruby
class UserRepository
  def initialize(tenant:) = @tenant = tenant
  def find(email:) = "found #{email}"
end

sandbox.bind("UserRepository", UserRepository.new(tenant: "acme"))

sandbox.eval(<<~RUBY)
  UserRepository.send(:eval, "1 + 1")
RUBY
```

We expected users to call it only as `UserRepository.find(...)`, but we didn’t anticipate a user writing `UserRepository.send(:eval, "1 + 1")`. At this stage, Kobako had no idea what kind of object the user had actually bound, so it used `public_send` to forward the call.

```ruby
def dispatch(target, method, *args)
  target.public_send(method, *args)
end
```

As a result, `UserRepository.send(...)` was translated into `target.public_send(:send, :eval, "1 + 1")`.

Even though we used `#public_send`, `#send` is still allowed to be called through `#public_send`, so the private method `#eval`, which should not be callable, was released and became something to exploit.

With `#eval`, you can do almost anything CRuby itself can do, and the risk goes up dramatically.

## Defense

What always makes defense harder is this: if users have a legitimate reason, we shouldn’t block them, but when a malicious user exploits it, how many restrictions can we add before users find it inconvenient?

First, for the `#send` problem, every object gets this capability through “inheritance,” so the early version enumerated the objects carrying high-risk capabilities and refused to use those capabilities through inheritance.

```ruby
BLOCKED = [BasicObject, Kernel, Object, Module, Class]

def dispatch(target, method, *args)
  owner = target.public_method(method).owner
  raise NoMethodError, "#{method} is not a Service method" if BLOCKED.include?(owner)
  
  target.public_send(method, *args)
end
```

In this case, the `#send` method that could be exploited before now belongs to a method defined in `BLOCKED`, so it becomes a `NoMethodError` and cannot be used. That looked like enough to fix the flaw.

However, there were more boundary problems. In later security reports, we saw exploits like this.

```ruby
class Store
  def initialize = @io = File.open("/etc/hostname")
  def backend_type = @io.class
end

sandbox.bind("App::Store", Store.new)

sandbox.eval(<<~RUBY)
  klass = App::Store.backend_type
  klass.read("/etc/hostname")
RUBY
```

Or exploits like this

```ruby
class UserRepositoryDecorator < SimpleDelegator; end

sandbox.bind("UserRepository", UserRepositoryDecorator.new(UserRepository.new(tenant: "acme")))

sandbox.eval(<<~RUBY)
  UserRepository.method_missing(:eval, "1 + 1")
RUBY
```

The first one works because `#backend_type` returns a `Class` object, which gets wrapped by the Handle mechanism. The attacker only needs to guess that the object outside is the `File` class, and can then escape with `#read` to read system files.

The second one works because `SimpleDelegator` defines its own `#method_missing`, which can still call into classes covered by `BLOCKED`, escaping the original `BLOCKED` scope once again. Even though we had designed `#respond_to_guest?` so users could control which operations are safe, people usually don’t set it up for convenience, so exploitable options kept appearing.

To keep these problems from happening again, Kobako now wraps every Host object by default and carefully checks each call path before letting it through. In effect, when `#respond_to_guest?` isn’t defined, Kobako defines a default version for everyone.

```ruby
def exposed_methods(target)
  case target
  when Module, Delegator then []
  else target.class.public_instance_methods(false)
  end
end

def dispatch(target, method, *args)
  allowed =
    if target.respond_to?(:respond_to_guest?, true)
      target.__send__(:respond_to_guest?, method)
    else
      exposed_methods(target).include?(method)
    end
  raise NoMethodError, "#{method} is not exposed to the guest" unless allowed

  target.public_send(method, *args)
end
```

After the change, it first checks whether `#respond_to_guest?` is defined to decide whether to allow the call. If not, it falls back to a generic check

- Exclude known system objects
- List only the object’s own public methods; private and inherited ones are not exposed

This way, unless an object itself defines a method that calls the inherited `#send`, these capabilities are hard to exploit again. Users do need to redefine methods like `#each` themselves, but this default-deny mechanism is what fully closes off this class of security problems.

## Trade-off

The default-allow design made binding objects easy. Strengthening security here makes things run “a little slower” and makes the API less convenient to use.

For example, in the example that emulates Cloudflare Workers, we used to be able to bind `Rack::Request` directly, but now we need to rewrite it like this.

```ruby
class GuestRequest < Rack::Request
  private

  def respond_to_guest?(name) = %i[request_method path params].include?(name)
end
```

Now `respond_to_guest?` has to be written out explicitly, but as a side effect methods like `#env` are blocked as well, so Host information doesn’t leak, and users have a clearer picture of what they are doing.

Even when deliberately loosened, Kobako’s current mechanism still blocks it well.

```ruby
class Profile
  def initialize = @secret = "TOPSECRET"

  private

  def respond_to_guest?(_name) = true
end

sandbox.bind("Profile", Profile.new)

sandbox.eval(<<~RUBY)
  Profile.pretty_print_instance_variables
RUBY
```

In the example above, the Host has loaded the PrettyPrint library, which brings in methods like `#pretty_print_instance_variables` that can print `@secret`. Here, the check that evolved from the earlier `BLOCKED` list refuses any capability obtained through inheritance first, and `#respond_to_guest?` can only narrow the scope, never reopen it, so `@secret` isn’t printed by mistake.

Overall, the pursuit of the best possible developer experience made the security problem serious, and in the end the only option was to ask users for a little inconvenience and extra work, in exchange for a design they can use with confidence.
