Security model
The gateway sits between an AI agent and a game economy. The main threats are untrusted text steering the agent, a backend reached where it should not be, one studio touching another’s data, and runaway agents.
| Tier | Examples | Controls | v1 |
|---|---|---|---|
| read | state, inventory, prices | rate limit | enabled |
| safe | collect, repair, plant | rate limit, cooldown | enabled |
| medium | build, buy, sell | rate limit, cooldown, daily caps | enabled |
| sensitive | gifts, paid currency, deletion | human confirmation | disabled |
- Same rules as a player. The backend stays authoritative for energy, cooldowns and the economy. The agent never gets a side door.
- Every attempt counts. Rate limits are charged before validation.
- Reserve, then settle. Caps are claimed before the call and released if it fails.
- Fail closed. If the policy store is unavailable, calls are refused.
- Static tool descriptions. They come from the uploaded manifest, never from runtime data.
- Untrusted results. Backend output and player-written text are data, size-limited and never copied into instructions.
- Signed requests. The backend only trusts
X-AG-Playerafter verifying the signature. - Hashed audit. Events store a SHA-256 of the arguments, not the arguments.
Implemented in M0
Section titled “Implemented in M0”Rules 1 to 4, 7 and 8 are implemented and tested. Rules 5 and 6 need the MCP layer (M1).
Kill switches (M2)
Section titled “Kill switches (M2)”A policy.Switch turns off everything matching a tenant, tool and player (empty fields are
wildcards). It is checked first in Admit and again in Reserve, answers switched_off, and
is not charged to the rate limit. A switch blocks every tier it matches, reads included. If the
switch store errors, Admit fails closed. Denials are audited like any other. The studio sets
switches through /v1/studio/switches, signed with the admin key (never the player key). Every
change is an audit event with the acting admin, and a failed store write is a 503, not a
silent success.
OAuth and grants (M3)
Section titled “OAuth and grants (M3)”The gateway is an OAuth 2.1 resource server with an embedded authorization server (decisions in
docs/DECISIONS.md, “Auth (M3)”). Without --dev-auth, /mcp accepts only bearer tokens and the
header-trusting dev API is not mounted.
- Tokens are opaque and stored as SHA-256. Access tokens last 15 minutes, refresh tokens rotate, and replaying a rotated refresh token revokes the whole grant.
- PKCE S256 is mandatory. Codes are single use, last 60 seconds, and are bound to client, redirect URI and challenge. A failed exchange still burns the code.
- Audience binding:
resourcemust equal the gateway’s MCP URL at authorize and token time, and the resource server rejects any token whose grant has another resource or tenant. - Revocation is per grant and immediate, because every request looks the token up.
- Redirect URIs: https, or http on literal loopback (any port). Errors before the client and
redirect URI are verified are shown to the player and never redirected. Later errors redirect
with
stateandiss(RFC 9207, mix-up defence). - Consent page: the client name is HTML escaped, the page sets
X-Frame-Options: DENY,frame-ancestors 'none', a CSP with no scripts,form-actionlimited to the page and the client’s redirect origin,no-storeandno-referrer. A missing or unknown decision is a denial. - Link tickets (
XXXX-XXXX, 40 bits, 5 minutes, single use) are minted by the studio backend with a signed request (agentsig, replays refused). Guessing is limited to 10 attempts a minute per address, and every failure looks the same. - Studio-signed requests (link tickets, grants, confirmations) share one verifier
(
internal/studiosig). It checks the signature, requires a player, and accepts each (key id, idempotency key) pair once for twice the skew window, on every method and every endpoint. A pair is recorded only after the signature verified, so an attacker without the key cannot fill or poison the cache. The in-memory cache is capped; at the cap it fails closed (503) and never evicts early. With--redis-addrthe pairs live in Redis, shared by every replica, and a Redis error fails closed (503). A captured approve, deny or revoke cannot be replayed, on any replica. - Known limits: the authorization server state is in memory (lost on restart) unless it uses
Postgres, and dynamic client registration is unauthenticated (rate limited and capped, see
below). The link code limiter and the registration rate limit are per client address; with
--redis-addrthey are counted in Redis, so every replica shares one budget. The client cap (--max-clients) counts live clients in the auth store (an active grant, or registered less than--client-graceago), so with Postgres every replica shares it and a restart does not reset it; the hourly prune frees clients that never linked. It bounds registration only: a pruned but old client that a player links again can take the live count over the cap until clients are pruned. With the memory store the cap is per process.
Manifest uploads (M4)
Section titled “Manifest uploads (M4)”- Manifest upload, list and activate are signed with a separate admin key, never the player-call
key, and go through their own replay checked verifier. Without
GAMEWARDEN_ADMIN_SECRETthey are not mounted. - Uploads are validated before they are stored. Rollback validates again. A failure changes nothing.
- Every version records the signed actor and a hash, so a change to what agents can call is attributable.
- Studio and admin keys rotate without downtime: the verifier accepts a set of keys, loaded from
key files and reloaded on SIGHUP. A failed reload keeps the keys in use. Secrets are never
logged; a reload logs the key ids added and removed. The replay cache survives a rotation.
With
--studio-keys-onlyand--admin-keys-onlythe key from the environment is retired too, and--backend-key-filerotates the key that signs calls to the backend, so no key needs a restart.
Activity feed (M4)
Section titled “Activity feed (M4)”- The player feed is signed with the studio key and filtered to the signed player and the gateway tenant on the server. Query parameters cannot change either. The admin feed needs the admin key, so a leaked studio key cannot read other players’ activity.
- Rows hold the argument hash, never the arguments, and no tenant. Replays are refused and
responses are
no-store. - Events are deleted after
--audit-retention. The in memory store holds only the newest 10000.
Backend calls and argument injection (M6)
Section titled “Backend calls and argument injection (M6)”The connector sends signed requests to the studio’s backend. Arguments come from an agent, so they are untrusted. What the gateway does:
- SSRF-safe dialer. The connector refuses to connect to loopback, private (RFC 1918, unique
local), link-local (including the 169.254.169.254 metadata address), multicast, unspecified,
carrier-grade NAT and other special-purpose ranges. IPv4-mapped IPv6 forms are judged as the
IPv4 address. The check runs on the address being connected to, after DNS resolution, so DNS
rebinding cannot slip past it. The environment proxy is never used.
--backend-allow-privateturns the check off for a backend on a private network (the compose demo does). Keep it off in production unless the backend host is yours. - No redirects. A 3xx from the backend is not followed, so a signed request cannot be bounced to another host. The call fails as a backend error.
- Path placeholders. Values are percent encoded as one path segment. Empty,
.and..values are refused, because encoding leaves dots alone and they would move the request. - Query and body. Values are encoded or JSON encoded, never concatenated.
- Signed headers. The player and call identifiers must be printable ASCII, 1 to 128 bytes. Anything else is refused before signing, so CR, LF and NUL cannot reach a header.
- Argument checks. Exactly one JSON object (extra data after it is refused), known parameters only, integers compared as integers (no float rounding at the bounds), numbers that do not fit are refused, strings are exact matches against enums and at most 4096 bytes. A duplicate key resolves to the last value, and that value is the one validated and forwarded.
| Threat | Control | Test |
|---|---|---|
| Agent makes the gateway call an internal service or the cloud metadata address | Dialer refuses blocked ranges, no redirects, no proxy | internal/connector/ssrf_test.go |
Argument rewrites the request path (../, %2f, .) |
Segment encoding, dot and empty values refused | internal/connector/inject_test.go |
| Argument adds query parameters or headers | Query encoding, identifier check before signing | internal/connector/inject_test.go |
| Oversized, nested or odd JSON | Strict decode, string limit, integer range in int64 | internal/gateway/validate_inject_test.go |
| Duplicate or trailing JSON that shows the validator one thing and the backend another | Extra data refused, last duplicate wins for both | internal/gateway/validate_inject_test.go |
| Oversized request body | Front door cap before any handler reads (1 MiB, 64 KiB for OAuth and studio, manifest upload its own), clean 413 | internal/bodylimit tests |
| A pilot that must not act yet | --max-scope coach caps every grant at read only, at the consent page, token issue and call time | internal/auth/maxscope_test.go, internal/mcpserver |
Not covered here: a circuit breaker per backend.
Planned
Section titled “Planned”Tenant isolation with row-level security, envelope encryption for signing secrets, circuit breaker per backend.
Reporting
Section titled “Reporting”See SECURITY.md.
Public OAuth endpoint hardening (M3)
Section titled “Public OAuth endpoint hardening (M3)”POST /registeris limited to 10 registrations per hour per address (every attempt counts), and the process registers at most 10000 clients. The cap is process local: it resets on restart and is not shared between replicas. Redirect URIs must be unique and at most 2048 bytes, andclient_namemust be printable text.- Client address behind a proxy. The ticket limiter and the registration limiter key on the
connection’s address by default, so behind a reverse proxy every player shares one budget. The
ClientIPHeadersetting makes them use the first value of a header such asX-Forwarded-For. Set it ONLY when the proxy overwrites that header on every request and clients cannot reach the gateway directly. Otherwise any client can choose its own address and evade the limits entirely. A missing or unparsable header falls back to the connection address. - CORS (
Access-Control-Allow-Origin: *, no credentials) exists only on the discovery metadata,/registerand/token./authorize,/v1/studio/*and/mcpsend no CORS headers.