Skip to content

feat: add edge model proxy so the child never holds the raw provider key - #5

Merged
steipete merged 6 commits into
mainfrom
feat/edge-model-proxy
Jul 5, 2026
Merged

steipete merged 6 commits into
mainfrom
feat/edge-model-proxy

Conversation

@steipete

@steipete steipete commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Draft. The edge side is complete and fully tested, but cutting a claw over to the proxy reconfigures its OpenClaw provider and cannot be verified without a live Crabbox workspace + real inference. It ships default-off (CRABHELM_MODEL_PROXY=off) so it's safe to merge; enabling it needs a staging canary. See "Before enabling" below.

What & why

Today the child VM is handed the raw org-wide OPENAI_API_KEY in credentials.env and calls the provider directly. That key is unrevocable per-claw and identical across the fleet — the single biggest credential-exposure gap, and the one thing redhat-et/claw-operator solves with its credential proxy (the agent pod never holds a real provider key).

This adds the Crabhelm equivalent — an edge model proxy — behind a flag:

When CRABHELM_MODEL_PROXY=on:

  • credentials.env delivers a per-claw, audience-bound model token (typ:"model", aud:"crabhelm-model", clawId, TTL = substrate lifetime) as OPENAI_API_KEY, plus CRABHELM_MODEL_BASE_URL, instead of the raw key.
  • bootstrap-child.sh sets models.providers.openai.baseUrl to https://crabhelm-runtime.openclaw.ai/model/v1, rerouting the built-in openai/* provider through the Worker. (Verified against OpenClaw source: for a built-in provider the OPENAI_BASE_URL env is ignored — the config override is required.)
  • The proxy (worker/model-proxy.ts, mounted at /model/* on the runtime host) verifies the model token, strips the caller's Authorization, injects the real OPENAI_API_KEY, and forwards to a single fixed upstream (api.openai.com) over an endpoint allowlist (/v1/chat/completions, /v1/responses, /v1/responses/compact, /v1/embeddings, GET /v1/models). Streaming (SSE) passes straight through.

Net: the raw provider key never leaves Cloudflare, and each claw receives independently scoped access bounded to its substrate lifetime instead of the same org-wide key on every VM.

When off (default): new credential delivery returns exactly OPENAI_API_KEY=<raw> and appliance bootstrap clears a previously managed proxy base URL. The edge route keeps accepting already-issued tokens during rollback while the signing secret remains configured, avoiding an immediate outage.

Security properties (tested)

  • No token / bad token / wrong-secret token → 401. Not configured → 503.
  • Route + method allowlist (404 off-list, 405 wrong method, 400 on query string, path traversal normalized to a 404).
  • Upstream host is always api.openai.com — never caller-influenced (SSRF-safe); client auth stripped, real key injected, upstream auth/cookies never echoed.
  • Request bodies stream without buffering and stop at 100 MiB for declared or chunked input; upstream calls have a 15-minute deadline for the pinned runtime's 840-second turn window.
  • The stable internal claw identifier is verified from the token but never disclosed to the external provider.
  • Fails closed: enabling the flag without MODEL_SIGNING_SECRET throws at credential delivery.

Testing

pnpm check green (120 tests). New tests/model-proxy.test.ts (16 tests) covers readiness gating, auth, route/method/SSRF allowlisting, Worker-first deployment routing, response compaction, key injection + client-auth stripping, internal-identity filtering, bounded streaming, 502 on upstream failure, and credential-delivery on/off + fail-closed. tests/bootstrap.test.ts executes both proxy setup and missing-override rollback paths.

Before enabling (why draft)

  1. wrangler secret put MODEL_SIGNING_SECRET (≥32 bytes).
  2. Rebuild + re-pin the appliance (guest profile changed: bootstrap-child.sh).
  3. Set CRABHELM_MODEL_PROXY=on on staging, provision one claw, and confirm the existing live inference probe passes end-to-end through the proxy before enabling in production.

For rollback, set the flag off and rebuild + re-pin affected claws so bootstrap clears the managed base URL and restores the raw key. Keep MODEL_SIGNING_SECRET configured through the longest issued token TTL (4 hours by default, 24 hours maximum) so in-flight old claws remain usable during the transition.

Follow-ups (noted, not in scope)

  • Per-request revocation check (disabled/removed claw) on the hot path — currently the token is valid until expiry.
  • Composes with the VM egress lockdown (feat: isolate agent runtime and restrict egress #3): once model traffic is proxied, the allowlist can tighten from "any TCP 443" toward the control-plane + model hosts only.
  • A dedicated upstream/model-catalog per provider if crabhelm grows beyond OpenAI-compatible.

@steipete
steipete marked this pull request as ready for review July 5, 2026 19:22
@steipete
steipete merged commit 5f96beb into main Jul 5, 2026
@steipete
steipete deleted the feat/edge-model-proxy branch July 5, 2026 19:23

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 43cd6f82ee

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread worker/model-proxy.ts
Comment on lines +53 to +55
if (typeof env.MODEL_SIGNING_SECRET !== "string" || new TextEncoder().encode(env.MODEL_SIGNING_SECRET).byteLength < 32) {
throw new Error("model proxy is enabled but MODEL_SIGNING_SECRET is not configured");
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Fail closed when the upstream key is absent

When CRABHELM_MODEL_PROXY=on and MODEL_SIGNING_SECRET is present but OPENAI_API_KEY is unset or blank, this path still mints and installs a model token even though /model/* later returns 503 because modelProxyConfigured() requires the upstream key. The admission check blocks new claws in that configuration, but already-issued bootstrap tokens can still hit credentials.env during an existing install or credential rotation and leave the child with unusable credentials; include the raw provider key in this fail-closed check before signing.

Useful? React with 👍 / 👎.

aitrawog added a commit to opencharly/crabhelm that referenced this pull request Oct 16, 2026
… head 879812f)

The validator's surviving block is body-truthfulness, not code: the "How tested"
section asserts a run count that does not reconcile with the runs it lists. No
tree change is owed -- `git status` in the worktree is clean and
`.github/workflows/tag-on-merge.yml` is byte-identical to its source, as the
validator's own prior rounds confirmed.

Measured, repo-wide, on 2026-10-15 (the filter the body names,
`name == charly/pr-validator`):

  14:50:01Z  completed/failure
  14:12:33Z  completed/failure
  13:06:51Z  completed/failure
  12:48:02Z  completed/failure
  11:56:03Z  completed/failure

Five runs exist. The corrected body enumerates all five so the count and the
list agree; it also sources the digest it cites with the command that produces
it, or drops it.

Missed control: I read a validator verdict as stale after grepping for a
specific string and not finding it -- promoting a negative result about one
string into a positive claim about the verdict's relevance. A re-run replays a
workflow run's stored event payload and does not re-read the current PR body,
so the corrected body can only reach the validator through a new head. This
commit exists to create that head; it carries the round's RCA rather than a
tree change, because the tree owes none.

Assisted-by: dsh DeepSeek deepseek-v4.1-flash (analysed on a live system)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant