A network plugin gets an approved endpoint—not unrestricted networking.

The application owns the policy. A supervisor permits name resolution for localhost, remembers the selected loopback address and port, and checks the subsequent connection against all of them. A second request to example.com:80 is denied.

The example uses a real loopback server and needs no public service. Its policy is application code, not a static list buried in a container CLI.

  1. Plugin requests resolutionThe supervisor accepts only localhost and the demo port.
  2. Supervisor remembers intentIt records the selected address and port returned by the resolver.
  3. Connection is checkedAddress, port, and socket type must match before the host connects on the plugin’s behalf.

1. Make the test deterministic

The host opens a loopback TCP server on the first available port in 8080–8090. A server fiber accepts one newline-delimited request and replies with a known string.

init.lua—local server setup

local listener = ip.tcp.acceptor.new()
listener:open('v4')
listener:set_option('reuse_address', true)
listener:bind('127.0.0.1', candidate)
listener:listen()

No DNS infrastructure or internet availability is required. The plugin still exercises Emilua’s getaddrinfo and connection mediation path.

2. Create the compatibility channel

libc_service.new() returns a master used by the supervisor and a slave consumed by the sandboxed actor. The worker cannot invoke arbitrary ambient-authority operations merely because this channel exists.

local master, slave = libc_service.new()
local worker = easybox.new{
    module = fs.path.new('/plugin.lua'),
    source_tree_cache = {
        ['plugin.lua'] = util.read_source(root / 'plugin.lua'),
    },
    libc_service = slave,
    stdout = 'share',
    stderr = 'share',
}

The source cache explicitly packages the plugin’s Lua file before filesystem access is removed.

3. Authorize resolution for the intended host and service

The first handler rejects a different hostname or port. For the intended request, it selects the IPv4 loopback result and records the exact address and port for the next step.

init.lua—application-specific name-resolution policy

getaddrinfo = function(master)
    local host, service = master:arguments()
    if host ~= 'localhost' or tostring(service) ~= tostring(port) then
        master:send('noname')
        return
    end

    -- Select entries[1] or the 127.0.0.1 result.
    authorized_address = selected.address
    authorized_port = selected.port
    master:send{selected.address, selected.port}
end

This is the important distinction: the application controls both the decision and the state used by later decisions.

4. Check address, port, and socket type before connecting

The connection handler receives the descriptor and checks all policy inputs. If they do not match, it closes the descriptor and returns EACCES. If they match, the supervisor performs the connection.

init.lua—connection policy

if
    addr ~= authorized_address or service ~= authorized_port or
    not fd:is_socket('inet', 'stream', 'tcp')
then
    fd:close()
    master:send(-1, generic_error.EACCES)
    return
end

local sock = ip.tcp.socket.new()
sock:assign('v4', fd)
local ok, err = pcall(function() sock:connect(addr, service) end)

The IPv6 handler deliberately denies connections in this IPv4-only test rather than leaving an unexamined escape path.

5. Return each outcome through its own pipe

The worker performs one allowed and one denied request. Each request carries a separate output descriptor, so the host reads a bounded response without an additional reply-channel lifecycle.

init.lua—request-specific response capability

local input, output = pipe.pair()
output = output:release()
worker:send{ endpoint = endpoint, text = text, output = output }
output:close()
return util.read_to_eof(input)

The supervisor stops on EOF when the worker exits instead of reporting normal teardown as a fiber failure.

Verified output

Allowed request: policy-approved: hello through the supervisor
Denied request: POLICY_DENIED: Host not found (authoritative)

The test asserts the exact successful response and requires the denied response to carry the POLICY_DENIED marker.

Limits and runtime notes

libc_service is compatibility, not a syscall firewall; combine it with OS enforcement. Its master side documents a no-multiplexing constraint, which can limit parallelism for chatty slaves.

The current native reply reconstruction under-allocates its port-text buffer for five-digit ports. This demonstration intentionally uses 8080–8090. Review or fix that runtime path before substituting arbitrary ephemeral ports.

← Image decoderNext: least-authority pipes →