# Security model What matbot defends, against whom, and where the edges are. It is written for whoever installs matbot, and for anyone writing a plugin that adds a privileged operation. ## The threat model Three principles. A behaviour that breaches none of them is not a defect, however surprising it looks. 1. **Authentication is outside matbot.** A reverse proxy, or a network that only trusted people can reach, decides who may use an installation. Whoever can reach the web UI can reach the tools and files the same server serves, and so can pages served from it (a dashboard calls tools; that is what it is for). 2. **Gates decide what matbot may do.** A privileged operation (installing a plugin, importing a module into a function, sending a request, admitting a Telegram chat) passes through a permission gate. No tool may widen what a gate permits; only a user can, by answering it. 3. **The adversary is a rogue model inside the authentication barrier.** It can call every tool it is given, write code, and reach the network from wherever matbot runs. That is why a gate asks through a channel the model is not party to (the UI, the terminal), and why a gate **refuses when nobody can be asked**: a trigger, a background job or `POST /tools/:name` has no human behind it. Point (3) is the significant one. The LLM operates _within_ the security boundary that (1), your infrastructure, provides. Therefore the security and gating model is designed to constrain what the LLM can do, not as a hard security boundary for a poorly implemented plugin. Hard boundaries might be: - Running matbot in a container (kubernetes, docker) - Using `node`'s `--permission` options to limit access (ensuring, since it applies to all access, that the matbot data directories are writeable) - Running matbot as a user with suitably restricted access rights The main vector for security risks is therefore the LLM itself, especially as matbot encourages the LLM to write and run code, and an over-zealous LLM might well find a way access sensitive data it shouldn't by chaining and retrying tools. For this reason, code generated by matbot (typically tools) are *gated*. Tools shipped as part of the repo, or run from plugins is considered correct by defintion. Plugin installs are gated for that reason - a user must confirm they trust the plugin operates with the same access rights as the matbot process. Plugins that install node modules are also gated. In each case the gate *widens* the default access. An installation can specify a default set of gated permissions using the `default_settings: { "@matatbread/matbot-default-gate": ... }` in the matbot.yaml. Note: many gates are absent/unecessary in the web-build as the file system is inaccessible to the browser. The property this gives is: *a privileged operation is decided somewhere replaceable, and by default that means a human is asked*. It is not "the model cannot change this". The default policy keeps its standing answers in plugin settings, wherever the storage backend keeps them; a tool that can write there can grant itself anything. `docker-bash` mounts them read-only for that reason. Plain `bash` can write them, and everything else, as the user matbot runs as. The exception is an answer given *for this session*: by default the policy keeps it in memory, where no tool can write it, and it ends with the process. A refusal at a door with nobody to ask is logged (once an hour per gate and subject), naming the gate and subject a standing answer would need, so an unattended job that stopped working can be found and fixed. A standing answer ("always allow…") is the user extending authority. It applies at every door, including the ones with no human behind them, and it can be withdrawn with `gate_action`. ## What is gated | Gate | Asked when | |---|---| | `plugin.add`, `plugin.load` | A plugin is installed, or loaded for the first time | | `plugin.reload` | A plugin is reloaded and its code differs from what was loaded (on disk, or fetched upstream) | | `plugin.provision-deps` | A fetched plugin now declares dependencies its cached copy lacks, which would be installed | | `plugin.remove`, `plugin.npm-uninstall`, `plugin.remove-provisioned` | A plugin is removed, and with it what adding it installed or created | | `provider.add`, `provider.update`, `provider.remove` | A provider profile is added, changed or removed | | `provider.add-unverified`, `provider.update-unverified` | The endpoint check failed, and the profile would be saved anyway | | `provider.add-unlisted`, `provider.update-unlisted` | The model is not among those the endpoint lists (asked last: listing sends the profile's credential) | | `mcp_action.add` | An MCP server is connected or spawned; the prompt names any vault keys a local one is given | | `mcp_action.remove` | An MCP server is disconnected | | `mcp_action.secret` | A remote MCP server is connected with a header that sends a vault key to its origin | | `http.request` | The `http` tool reaches an origin (scheme, host and port), including one a redirect leads to | | `http.secret` | The `http` tool sends a vault key to an origin | | `tool_function.import` | A function body imports a module | | `tool_function.connect` | A function body's `fetch` or `WebSocket` reaches an origin, including by redirect | | `tool_function.write-secret` | A function body stores a value in the vault under a key that does not exist yet (`context.writeSecret`) | | `tool_function.replace-secret` | A function body overwrites an existing vault key. Asked every time: no standing answer decides it, so with nobody to ask it is refused | | `tool_function.edit` | A function or package that uses `import(`, `fetch`, `WebSocket` or `writeSecret` is redefined: may the new version keep the grants? | | `function_runner_config.set` | The function runner's time bound is changed | | `telegram_open_door.open` | The Telegram door is opened to new chats | | `tools.overwrite` | A plugin registers a tool another plugin owns | A function's own grants name its **lineage**: the version it descends from by edits the user permitted to keep them. An edit nobody permitted, or one made where nobody can be asked (another process, a job, an HTTP call), starts a lineage of its own, so its grants are asked for again. Permission to keep them is given per function, for this session only (the gate offers no other lifetime for it), while the grants themselves hold wherever the function runs. "Any function" answers name no lineage at all. A provider's credential does not follow its endpoint to another origin: `provider update` points the profile at a fresh, unset vault key instead, for the user to supply with `plugin store-key`. The list is the gates matbot's own plugins raise. A plugin can raise gates of its own, under its own tool's name, and `gate_action get` reports every gate that has a standing answer, whoever raised it. ## How a gate decides A gate is a question with a yes-or-no answer. The plugin doing the work **declares** it: a gate id (`http.request`), a subject (the origin), a sentence naming the act, and what to do if nobody can be asked. The registered `PermissionGate` **decides** it. The default policy (core's default gate, always present unless an embedder supplies another — see [DEFAULT-GATE.md](DEFAULT-GATE.md)) decides in this order: 1. **A standing answer that covers the act** decides it, without asking. Where several do — an allow of a directory and a denial of one file in it — **the most specific wins**: the one nearest the act, then one held for this session before one held everywhere, then a denial should two tie. 2. **Otherwise, if someone can be asked** (the web UI or the terminal of the turn that raised it), it asks, as three choices: - **allow or deny**; - **for how long**: *this time*, *for this session*, or *always*; - **what**: the subject, or one of the broader subjects the plugin offers, narrowest first ("anything in this directory", "any builtin", "any function"), or every subject of the gate. 3. **Otherwise** it does what the plugin declared for nobody being there, which for every gate above is to refuse, and logs the refusal. A refusal tells the model which of these refused, and, where nothing in the turn can change that, says so, so that it does not go looking for a way round. **Standing answers** are kept in the default policy's plugin settings, one key per gate mapping a scope (`'*'` everywhere, a session's id, `#http` for the HTTP tool door) to `true` for every subject of the gate, or a list of subjects. Denials sit beside them under `deny:`, in the same shape. A session answer is kept in memory unless `gate_action session-store` says otherwise. An installation can configure them under `default_settings:` (see [egress controlled outside matbot](#when-egress-is-controlled-outside-matbot) for an example), and [DEFAULT-GATE.md](DEFAULT-GATE.md#configuring-it) has the format. Settings are layered per gate: answering a prompt stores the subjects in effect plus the new one, and from then on the stored list is what applies, so a later edit to that gate's configured value has no effect until the stored one is cleared. `gate_action get` reports the answers in effect (and any `denials`); `list` reports them one entry at a time with an id, optionally those mentioning some text (a host, a function), and `clear` forgets the entries named, a gate (reverting it to its configured value), or everything. Forgetting a denial asks the user first, every time (`gate_action.forget-denial`), since it undoes a refusal they chose; forgetting an allow does not, since it only makes a gate ask again. There is no `set`: the one way to give a standing allow at run time is a person answering the prompt that names the act. `deny` adds a standing denial, also asking the user every time (`gate_action.deny`), for what is never prompted for because a broader allow covers it — one file inside a granted directory. ### What an answer can change Every gate starts closed: asked, or refused where nobody can be asked. The allows a person gives, and those an installation configures, **widen** what matbot may do: the gates are how a user relaxes the defaults. The point is that model-written code does nothing the user has not permitted, not to judge what a user permits: an answer means what it says. "Always allow every `plugin.add`" means anything that reaches the tool endpoint installs any plugin, with full access, unasked. A **denial** narrows what it names, refused from then on without asking, which is what stops a model asking the same question again. It is as broad as the subject chosen — one file, or a directory and everything below it — and, being the more specific answer, it carves what it names out of a broader allow. It does not lift an allow of something narrower inside it. A restriction is also expressed: - **by not loading the plugin**, or the tool, that does the thing; - **as a configured denial**, `deny:` — for a body's imports, `'deny:tool_function.import': { '*': ['* node:child_process'] }` forbids a module to every function, and a body naming it fails its type-check; - **by a replacement `PermissionGate`** with its rules compiled in, which can refuse what the default would ask about, and whose rules are out of reach of anything that can write settings; - **by the deployment**: the OS user matbot runs as, a container, a firewall or egress proxy. ## Settings and the tool registry Two invariants keep a tool — bundled, or one a model drives — from reaching past its own authority without a gate: - **A plugin reaches only its own settings.** `services.settings()` is scoped to the plugin; the raw `settings` store cannot be opened through a plugin's `createStore` (whose store names are letters, digits, `_` and `-`, so no spelling of a path reaches it either), because it holds every plugin's settings, the default gate's standing answers among them. `tool-store`'s `store_action` once *exposed* any namespace, `settings` included, which let a model write the gate answers and grant itself every gate; it now keeps the stores it creates in a namespace of its own and cannot open another's. - **A plugin removes only what it registered.** Removing another plugin's tool through a plugin's machine and registering into the gap would replace it without the `tools.overwrite` gate, and removing another's hooks would take its validators away; the registry refuses both (`tools.remove`, and `removeByPlugin` for tools, hooks and system-context contributors). These keep a plugin from handing a model more than it means to. They are not a sandbox for the plugin itself, which has full Node capability: it can still reach the active `StorageBackend` directly (`services.StorageBackend.createStore('settings')`), or the files under it. ## The web UI's endpoints The UI answers gate prompts over HTTP (`POST /sessions/:id/prompt`). Anything that can reach that endpoint can answer a prompt, which is correct for every browser the user has open and wrong for the model. `frontend-web` therefore: - **refuses every request arriving on a connection matbot's own process opened.** The model reaches the network from inside that process (the `http` tool, a function's `fetch`, an MCP client), and node reports each such connection on every platform; - **answers its own origin only**, unless `cors_origin` names another; - **refuses a POST not sent as `application/json`**, which is what makes a browser ask before a page on another origin may send one; - **sanitises model output**: HTML in a reply is kept, minus what runs script or changes the page (event handlers, `