Skip to content

fix(gateway): derive loopback owner context from token - #69796

Merged
drobison00 merged 4 commits into
openclaw:mainfrom
drobison00:fix-mcp-owner-auth
Apr 21, 2026
Merged

drobison00 merged 4 commits into
openclaw:mainfrom
drobison00:fix-mcp-owner-auth

Conversation

@drobison00

Copy link
Copy Markdown
Contributor

fix(gateway): derive loopback owner context from token

Summary

Describe the problem and fix in 2–5 bullets:

  • Problem: MCP loopback requests accepted owner context from a child-process-controlled header, so a non-owner bearer could claim owner status.
  • Why it matters: owner-only tools such as gateway, cron, and nodes could be exposed to non-owner ACP child processes.
  • What changed: the loopback server now issues separate owner and non-owner bearer tokens, derives senderIsOwner from the authenticated bearer, and stops exporting the mutable owner header.
  • What did NOT change (scope boundary): no gateway tool policy changes, no new network surfaces, and no CI/workflow/config automation changes.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

Root Cause (if applicable)

For bug fixes or regressions, explain why this happened, not just what changed. Otherwise write N/A. If the cause is unclear, write Unknown.

  • Root cause: resolveMcpRequestContext trusted x-openclaw-sender-is-owner, and the CLI runner exported that value into child-process environment state, so authorization depended on attacker-mutable client input instead of server-side auth state.
  • Missing detection / guardrail: loopback auth only validated a shared bearer token and never bound owner/non-owner authorization to that bearer.
  • Contributing context (if known): the loopback config and live helper path both forwarded the owner header as normal request metadata.

Regression Test Plan (if applicable)

For bug fixes or regressions, name the smallest reliable test coverage that should catch this. Otherwise write N/A.

  • Coverage level that should have caught this:
    • Unit test
    • Seam / integration test
    • End-to-end test
    • Existing coverage already sufficient
  • Target test or file: src/gateway/mcp-http.test.ts and src/agents/cli-runner/bundle-mcp.test.ts
  • Scenario the test should lock in: a non-owner loopback bearer must stay non-owner even if the request spoofs an owner header, and generated MCP config must not emit the mutable owner header.
  • Why this is the smallest reliable guardrail: the bug sits at the loopback auth boundary plus CLI bundle generation, so one loopback request test and one config-generation test cover the vulnerable seam directly.
  • Existing test that already covers this (if any): none found for bearer-derived owner context before this patch.
  • If no new test is added, why not: N/A

User-visible / Behavior Changes

  • Non-owner ACP child processes can no longer recover owner-only loopback tools by spoofing owner metadata.
  • Owner and non-owner loopback callers now use distinct bearer tokens behind the same MCP loopback surface.

Diagram (if applicable)

Before:
[non-owner child process] -> [shared bearer + spoofed owner header] -> [server trusts header] -> [owner-only tools visible]

After:
[non-owner child process] -> [non-owner bearer] -> [server derives owner=false from bearer] -> [owner-only tools hidden]

Security Impact (required)

  • New permissions/capabilities? (No)
  • Secrets/tokens handling changed? (Yes)
  • New/changed network calls? (No)
  • Command/tool execution surface changed? (Yes)
  • Data access scope changed? (No)
  • If any Yes, explain risk + mitigation: loopback token handling now splits owner and non-owner callers into separate bearer scopes so request headers can no longer elevate tool visibility; the exposed tool surface is reduced for non-owner callers rather than expanded.

Repro + Verification

Environment

  • OS: Linux 6.8.0-110-generic #110-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 19 15:09:20 UTC 2026 x86_64 GNU/Linux
  • Runtime/container: Codex workspace container
  • Model/provider: <operator to fill>
  • Integration/channel (if any): MCP loopback / ACP child-process path
  • Relevant config (redacted): default loopback MCP test harness; branch fix-mcp-owner-auth

Steps

  1. Start the MCP loopback server and capture the active runtime tokens.
  2. Send a tools/list request with the non-owner bearer while spoofing x-openclaw-sender-is-owner: true.
  3. Generate CLI bundle MCP config and verify only session/account/channel headers are forwarded.

Expected

  • Non-owner bearer requests remain non-owner regardless of spoofed owner headers.
  • Generated loopback MCP config does not include the mutable owner header.

Actual

  • The focused loopback test path derives senderIsOwner from the bearer token and ignores spoofed owner headers.
  • The focused bundle test path no longer exports or forwards OPENCLAW_MCP_SENDER_IS_OWNER.

Evidence

Attach at least one:

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)
Validation run:
- node scripts/run-vitest.mjs run --config test/vitest/vitest.gateway.config.ts src/gateway/mcp-http.test.ts
- node scripts/run-vitest.mjs run --config test/vitest/vitest.unit.config.ts src/agents/cli-runner/bundle-mcp.test.ts

Additional check attempted:
- node ../repo/node_modules/@typescript/native-preview/bin/tsgo.js -p tsconfig.core.test.json --incremental --tsBuildInfoFile /tmp/openclaw-mcp-owner.tsbuildinfo --declaration false
  Result: blocked by pre-existing environment/type-resolution errors around `ws` declarations in this workspace install; no fix-specific type error was surfaced first.

Human Verification (required)

What you personally verified (not just CI), and how:

  • Verified scenarios: inspected the final diff, confirmed the server now maps owner state from distinct loopback bearers, confirmed loopback config/header generation no longer forwards owner state, and confirmed the branch pushed to the operator fork.
  • Edge cases checked: spoofed owner header with a non-owner bearer, runtime bookkeeping for owner/non-owner tokens, and loopback config generation for bundled MCP clients.
  • What you did not verify: a full end-to-end ACP exploit reproduction outside the focused tests, and a clean full-project typecheck in this sandboxed worktree environment.

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

If a bot review conversation is addressed by this PR, resolve that conversation yourself. Do not leave bot review conversation cleanup for maintainers.

Compatibility / Migration

  • Backward compatible? (Yes)
  • Config/env changes? (No)
  • Migration needed? (No)
  • If yes, exact upgrade steps:

Risks and Mitigations

  • Risk:
    • If any loopback client incorrectly reuses an owner bearer for non-owner traffic, tool visibility will still follow the bearer.
  • Mitigation:
    • The CLI runner and loopback helpers now choose the bearer from senderIsOwner instead of exporting a mutable owner header.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels Apr 21, 2026
@greptile-apps

greptile-apps Bot commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR closes a privilege-escalation vulnerability where a non-owner ACP child process could forge x-openclaw-sender-is-owner: true on a loopback request and gain access to owner-only tools (gateway, cron, nodes). The fix splits the single shared loopback bearer into separate ownerToken / nonOwnerToken secrets, derives senderIsOwner server-side from the authenticated bearer, and strips the mutable owner header from all config generation and request paths. The approach is sound: tokens are generated with crypto.randomBytes(32), compared with safeEqualSecret (constant-time), and the correct token is selected before the child process env is populated.

Confidence Score: 5/5

Safe to merge — the security fix is correctly implemented with no P0/P1 findings.

All remaining findings are P2 style/documentation suggestions. The core security change is sound: server-side token derivation replaces header trust, tokens are cryptographically random and constant-time compared, and the mutable owner header is removed from all outbound config and request paths.

No files require special attention beyond the two minor P2 style notes in mcp-http.loopback-runtime.ts and prepare.ts.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/gateway/mcp-http.loopback-runtime.ts
Line: 24-28

Comment:
**`clearActiveMcpLoopbackRuntime` silently accepts either token**

The function now matches on both `ownerToken` and `nonOwnerToken`, but the only call-site always passes `ownerToken`. The `|| activeRuntime?.nonOwnerToken === token` branch is therefore dead code today. More importantly, if a future caller mistakenly passes `nonOwnerToken`, it would still clear the runtime — which is probably intentional, but the function name and signature give no hint that either token is a valid handle. Consider documenting the intent or tightening the parameter type to prevent misuse.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/agents/cli-runner/prepare.ts
Line: 167-179

Comment:
**Minor indentation inconsistency in ternary**

The `?` operator was moved from 6-space to 8-space indent while the object body and the closing `}: undefined` stayed at 6 spaces, producing a misaligned ternary. Not a correctness issue, but worth a quick autoformat pass to keep the diff clean.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(gateway): derive loopback owner cont..." | Re-trigger Greptile

Comment thread src/agents/cli-runner/prepare.ts Resolved
@drobison00
drobison00 merged commit 3cb1a56 into openclaw:main Apr 21, 2026
92 checks passed
amittell pushed a commit to amittell/openclaw that referenced this pull request Apr 23, 2026
* fix(gateway): derive loopback owner context from token

* docs(changelog): note loopback owner token hardening

* refactor(gateway): clarify loopback runtime cleanup

* fix(gateway): compare both loopback bearer classes

(cherry picked from commit 3cb1a56)
medikoo pushed a commit to medikoo/openclaw that referenced this pull request Apr 24, 2026
* fix(gateway): derive loopback owner context from token

* docs(changelog): note loopback owner token hardening

* refactor(gateway): clarify loopback runtime cleanup

* fix(gateway): compare both loopback bearer classes
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
* fix(gateway): derive loopback owner context from token

* docs(changelog): note loopback owner token hardening

* refactor(gateway): clarify loopback runtime cleanup

* fix(gateway): compare both loopback bearer classes
zhonghe0615 pushed a commit to zhonghe0615/openclaw that referenced this pull request May 7, 2026
* fix(gateway): derive loopback owner context from token

* docs(changelog): note loopback owner token hardening

* refactor(gateway): clarify loopback runtime cleanup

* fix(gateway): compare both loopback bearer classes
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request May 13, 2026
* fix(gateway): derive loopback owner context from token

* docs(changelog): note loopback owner token hardening

* refactor(gateway): clarify loopback runtime cleanup

* fix(gateway): compare both loopback bearer classes
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
* fix(gateway): derive loopback owner context from token

* docs(changelog): note loopback owner token hardening

* refactor(gateway): clarify loopback runtime cleanup

* fix(gateway): compare both loopback bearer classes
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
* fix(gateway): derive loopback owner context from token

* docs(changelog): note loopback owner token hardening

* refactor(gateway): clarify loopback runtime cleanup

* fix(gateway): compare both loopback bearer classes
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
* fix(gateway): derive loopback owner context from token

* docs(changelog): note loopback owner token hardening

* refactor(gateway): clarify loopback runtime cleanup

* fix(gateway): compare both loopback bearer classes
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
* fix(gateway): derive loopback owner context from token

* docs(changelog): note loopback owner token hardening

* refactor(gateway): clarify loopback runtime cleanup

* fix(gateway): compare both loopback bearer classes
Heetisk pushed a commit to Heetisk/openclaw that referenced this pull request Sep 28, 2026
* fix(gateway): derive loopback owner context from token

* docs(changelog): note loopback owner token hardening

* refactor(gateway): clarify loopback runtime cleanup

* fix(gateway): compare both loopback bearer classes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling gateway Gateway runtime maintainer Maintainer-authored PR size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant