Metadata-Version: 2.5
Name: glovebox-core
Version: 0.1.9
Summary: Glovebox's security decisions as pure functions: the host rules, the JSON reader, the egress verdict and the monitor's fail-closed rules
Project-URL: Homepage, https://github.com/AlexanderMattTurner/agent-glovebox
Project-URL: Source, https://github.com/AlexanderMattTurner/agent-glovebox
Author: AlexanderMattTurner
License-Expression: Apache-2.0
Keywords: egress,glovebox,hostname,policy,sandbox
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Security
Requires-Python: >=3.9
Requires-Dist: graphql-core==3.2.12
Description-Content-Type: text/markdown

# glovebox-core

Glovebox's security decisions as pure functions: the host rules, the JSON reader, the egress verdict and the monitor's fail-closed rules. No module here reads a file, a socket, a subprocess, the clock, a thread or the environment. An input that needs one arrives as an argument: a `Protocol` the caller implements, or a value the caller read. `tests/core/test_purity.py` refuses any other import.

## Host rules

`glovebox_core.host_rules` is the one answer to "which host is this" that every part of glovebox uses: the egress proxy's verdict service, the launcher, the session driver and the evals. A host is compared in one spelling: ASCII only, surrounding whitespace removed, lowercased, and an IP literal in its standard form. A name that is not ASCII, or that starts or ends with a dot, is refused rather than folded, so two readers can never disagree about it.

`pod_grants` is the one reader of a Kata pod's `glovebox.dev/egress-grants` annotation, and the node calls it. It returns the granted hosts at their tiers, the address grants, the port grants and the request grants. It raises `GrantRefused` with the node's own message for the first entry the node refuses. How wide an address grant may be is each node's own setting, so it is not checked here.

A host with a port, such as `{"host": "git.example.com:22", "rw": true}`, is a port grant. The node relays a raw stream to that port on the name's public addresses, so it needs `"rw": true`. A host without a port keeps its plain host meaning.

A grant may not name a `*.` wildcard over a public suffix, or over a name with one below it. `pod_grants` takes the Public Suffix List as an argument: `glovebox_hosts.BUNDLED_SUFFIXES` is the list `tldextract` bundles. Where the list cannot be read, every name is refused.

```python
>>> from glovebox_core.host_rules import pod_grants
>>> from glovebox_hosts import BUNDLED_SUFFIXES
>>> granted = pod_grants('["httpbin.org", {"host": "git.example.com:22", "rw": true}]', BUNDLED_SUFFIXES)
>>> granted.hosts
{'httpbin.org': 'ro'}
>>> granted.ports
[PortGrant(host='git.example.com', port=22)]
>>> pod_grants('["*.amazonaws.com"]', BUNDLED_SUFFIXES)
Traceback (most recent call last):
...
glovebox_core.host_rules.GrantRefused: glovebox.dev/egress-grants: '*.amazonaws.com' is a wildcard over a name with the public suffix ...
```

`write_pod_grants(grants, suffixes)` is the writer for the same annotation. It takes a `PodGrants` and returns one canonical line: entries sorted and once, no optional whitespace, an `ro` host as a bare string, a request grant as `{"host":...,"method":...,"path":...}` with no bare entry for its host, and every other entry as `{"host":...,"rw":true}`. It reads its text back with `pod_grants` and raises that reader's `GrantRefused` for any entry the node would refuse. `encode_pod_grants` writes host names alone at the read-only tier, and raises for an address or `HOST:PORT`, which opens a raw tunnel that only a person grants.

### Request grants

`{"host": "api.example.com", "method": "POST", "path": "/v1/jobs"}` grants the host read-only, plus that one method on exactly that path, with any query. The method is `POST`, `PUT`, `PATCH` or `DELETE`. The path is absolute, and each segment holds only letters, digits and `._~-`. Envoy normalizes the path and forwards that form, and the verdict admits only a path equal to the grant. A sibling, child or trailing-slash path stays read-only. A grant on a path that ends in a carve-out's segment, such as `/v1/messages` or `/git-upload-pack`, refuses the whole pod, since that carve-out keeps its own rules. A body that names `_method` as a form or JSON key, whatever its media type, is refused, and so are a multipart body and an `X-Original-URL` or `X-Rewrite-URL` header. The host is one plain name. A wildcard, a port, an address, `"rw": true` or an unknown key refuses the whole pod. An `rw` grant of the same host admits every request anyway. The node writes the grant as a `METHOD:PATH` capability word on the host's row of that pod's table alone. The forbidden-host bar and the GitHub write rules still apply.

A runner that writes the annotation checks each entry before it asks for a pod. `grant_host_refusal(host, suffixes)` gives the node's reason for refusing a host name, or None. `address_grant(entry)` reads an address entry as an `AddressGrant`, returns None for a host name, and raises `GrantRefused` for a malformed one.

The host rules and `glovebox_core.json_object` import only the standard library, so they run under Python 3.9. The egress modules need Python 3.11.

## Egress verdict

`glovebox_core.verdict.decide` rules where one request Envoy asks about may go. The caller passes what it read as `Facts`, such as the setup window, and every address screen as a `Screen`. The verdict resolves no name and writes no record.

## Monitor fail-closed rules

`glovebox_core.monitor` gives the answer for a tool call that no monitor model judged. An outage takes `outage_decision`: the configured fail mode, or ASK for a high-risk call. A reply with no readable verdict, an open circuit breaker and a missing API key each take their own `fixed_decision`. No fail-closed answer is ALLOW. The monitor package makes the model call and keeps the breaker and the records.
