Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/grok-bot-events-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ jobs:
- run: npm test
- run: npm run build && git diff --exit-code -- plugin
- run: node ../../../tools/bundle-notices.mjs . --plugin plugin --entry src/bridge.mjs --bundle dist/bridge.mjs --check
- run: npm ci && npm run typecheck && npm test
working-directory: polyfills/events/grok-bot/worker
3 changes: 3 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Code closes, until no thread has a watch, and wakes a thread with T3 Code's
`t3 thread send`. The turn it starts runs in the thread's own permission
mode. It reads no stored credentials, Codex's or T3 Code's.

For Grok Bot, see [mcp-events for Grok Bot](polyfills/events/grok-bot/SECURITY.md)
(covering both the Cloudflare Worker deployment and the self-hosted Node bridge).

These are third-party plugins and extensions that run with your permissions
in each host.

Expand Down
147 changes: 120 additions & 27 deletions polyfills/events/grok-bot/README.md

Large diffs are not rendered by default.

33 changes: 31 additions & 2 deletions polyfills/events/grok-bot/SECURITY.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
# Security

Keep the profile, event spool, routine URL, routine key and source credentials private. Run only trusted stdio commands. The sidecar sends the routine a batch reference over HTTPS and rejects redirects. The webhook key stays in the sidecar's environment.
This document covers both Grok Bot polyfill implementations in this folder: the **Cloudflare Worker** (`worker/`) and the **self-hosted Node bridge** (`src/`, `plugin/dist/bridge.mjs`).

Server metadata and event contents are untrusted. The routine should validate the batch reference, use a fixed reader command with one argument, and apply only user-approved actions. Source payloads remain in the spool until read by the routine.
## Credential boundaries & storage

### Cloudflare Worker (`worker/`)

- **Separated keys:** `READER_KEY` (installed in Grok Bot's Remote HTTPS connector) authorizes `POST /mcp` only. Operator endpoints (`/oauth/start`, `/admin/servers`, `DELETE /admin/servers/*`, `/admin/delivery/retry`, `/admin/delivery/accept`) require a separate `ADMIN_KEY`. Both are stored as encrypted Cloudflare Worker secrets and mirrored locally in `~/.grok-events-worker.json` (mode `0600`).
- **Routine credentials:** `GROK_ROUTINE_URL` and `GROK_ROUTINE_KEY` are stored as encrypted Cloudflare Worker secrets (never in SQLite or logs).
- **Upstream credentials:** Static servers configured via `add-server` are stored in the encrypted `UPSTREAM_SERVERS` Worker secret. Servers connected via `npm run setup -- login` store their OAuth `access_token`, optional `refresh_token`, `client_id`, `token_endpoint`, and `requested_scope` inside your private Durable Object's SQLite database on your Cloudflare account.
- **No cross-origin credential leaks:** All outbound upstream MCP requests, OAuth discovery/registration/token/revocation requests, and Grok routine wake requests use `redirect: 'manual'` and reject `3xx` responses. All production endpoints (including OAuth metadata URLs and issuers) must use `https://` with no userinfo or fragments (`http://127.0.0.1` / `localhost` is allowed only when `MCP_EVENTS_DEV=1`).
- **OAuth scope enforcement & refresh:** When a scope is requested (e.g. `orchestration:read`), the Worker rejects any broader grant returned at login or refresh, never saves it, attempts revocation of both access and refresh tokens if a revocation endpoint is advertised, and explicitly distinguishes confirmed revocation from unconfirmed revocation in the callback page.
- **Watch endpoint binding & server removal:** Every watch records its upstream URL at creation time. Removing a server (`DELETE /admin/servers/<name>`) or re-authenticating it to a different URL retires existing watches and invalidates their webhook hooks (`410`).

### Self-hosted Node bridge (`src/`, `plugin/dist/bridge.mjs`)

- Keep the profile directory (`GROK_EVENTS_HOME`, mode `0700`), `reader.key` (mode `0600`), event spool, routine URL, routine key, and upstream source credentials private and outside any Git checkout. Run only trusted stdio commands.
- `watch_event` and `unwatch_event` can only subscribe to servers explicitly configured by the operator in `config.json`. The connector cannot add new upstream URLs or stdio commands at runtime.

## Transport, webhooks, and DNS rebinding protection

- **Routine wakes:** Sent strictly over HTTPS with redirects refused (`redirect: 'manual'` on Workers, `redirect: 'error'` on Node) and a 15-second timeout. The webhook body contains only `{ "source": "mcp-events", "batch": "<inbox>.e<first>-e<last>" }` — never untrusted event payloads or secrets.
- **MCP connector (`/mcp`):** Authenticated via timing-safe comparison against `Authorization: Bearer <READER_KEY>`. Keep `READER_KEY`, `ADMIN_KEY`, `GROK_ROUTINE_KEY`, and upstream credentials separate.
- **Browser `Origin` guard:** If `READER_ORIGIN` / `GROK_EVENTS_READER_ORIGIN` is set, only that exact `https://` origin is accepted. If unset, any request carrying a browser `Origin` header is rejected with HTTP `403`, while server-to-server MCP clients (which send no `Origin`) remain reachable.
- **Signed webhooks (`/hooks/<hookId>`):** The Worker streams incoming bodies with a strict 256 KiB cap before allocating, verifies Standard Webhooks HMAC-SHA256 (`v1,`) over the exact received bytes and exact raw `webhook-timestamp` header (5-minute freshness window), deduplicates by `(hook_id, webhook-id)` and event ID after re-reading fresh watch state, and echoes `verification` challenges only after signature verification succeeds.
- **Stand-down (`MCP_EVENTS_OFF=1`):** Empties the MCP tool list, stops all alarms/watchers, and rejects `/hooks/*`, `/oauth/start`, and `/oauth/callback` with HTTP `503`.
- **Health endpoints (`/health`, `/healthz`):** Unauthenticated liveness checks return status/counters only — never secrets, server URLs, or event payloads.

## Untrusted server data, storage limits, and redaction

- Server descriptions, event metadata, and event payloads are untrusted external data returned under `untrustedEvents` (or explicitly labeled as untrusted data). Receiving an event does not grant permission to execute actions.
- **Retention & backpressure (Worker):** Acknowledged events older than 14 days are pruned while retaining at least 1,000 recent records; replay IDs are pruned after 8 days. Unacknowledged backlog is capped at 5,000 events (`MAX_PENDING_EVENTS`), returning HTTP `503` with `Retry-After: 60` on webhooks and pausing polls until drained. `read_batch` bounds reads to one wake batch.
- **Redaction:** Both implementations scrub known `READER_KEY`, `ADMIN_KEY`, routine URL/key, upstream bearer/header credentials, and per-hook signing secrets from errors and tool outputs.

Report vulnerabilities through the [repository security process](../../../SECURITY.md).
Loading