Skip to content

fix(plugins): avoid doctor crash on legacy interactive state - #70135

Merged
ngutman merged 2 commits into
mainfrom
fix/70006-doctor-clear-crash
Apr 22, 2026
Merged

ngutman merged 2 commits into
mainfrom
fix/70006-doctor-clear-crash

Conversation

@ngutman

@ngutman ngutman commented Apr 22, 2026

Copy link
Copy Markdown
Member

Summary

  • Problem: openclaw doctor could crash with Cannot read properties of undefined (reading 'clear') while clearing plugin interactive handler state.
  • Why it matters: openclaw update runs doctor post-install, so this turned a successful package update into an error path for affected installs.
  • What changed: hydrate legacy/partial openclaw.pluginInteractiveState shapes before clearing state, and always rebind callback dedupe from the dedicated global dedupe cache symbol.
  • What did NOT change (scope boundary): no changes to doctor UX, bundled channel warning behavior, or update flow policy.

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)

  • Root cause: plugin interactive state was treated as a live split-chunk global, but the state loader trusted any existing globalThis[Symbol.for("openclaw.pluginInteractiveState")] object shape. Legacy/partial state objects could therefore be missing callbackDedupe or inflightCallbackDedupe, and clearPluginInteractiveHandlersState() then called .clear() on undefined.
  • Missing detection / guardrail: no regression test covered clearing a legacy partial interactive state shape.
  • Contributing context (if known): doctor/plugin reload paths clear interactive handlers during plugin registry reload, which surfaced the stale global state shape.

Regression Test Plan (if applicable)

  • 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/plugins/interactive.test.ts
  • Scenario the test should lock in: a legacy openclaw.pluginInteractiveState object that only has interactiveHandlers should be hydrated before clear, and interactive callback dedupe should still work afterward.
  • Why this is the smallest reliable guardrail: the crash is in the shared interactive state helper, so a focused state hydration test catches the exact failure mode without needing full doctor/update orchestration.
  • Existing test that already covers this (if any): none.
  • If no new test is added, why not: N/A

User-visible / Behavior Changes

  • openclaw doctor and openclaw update no longer crash on this legacy partial interactive-state shape.

Diagram (if applicable)

N/A

Security Impact (required)

  • New permissions/capabilities? (Yes/No) No
  • Secrets/tokens handling changed? (Yes/No) No
  • New/changed network calls? (Yes/No) No
  • Command/tool execution surface changed? (Yes/No) No
  • Data access scope changed? (Yes/No) No
  • If any Yes, explain risk + mitigation:

Repro + Verification

Environment

  • OS: macOS
  • Runtime/container: local Node 24 repo checkout
  • Model/provider: N/A
  • Integration/channel (if any): plugin interactive state helper
  • Relevant config (redacted): N/A

Steps

  1. Seed globalThis[Symbol.for("openclaw.pluginInteractiveState")] with a legacy partial object that only contains interactiveHandlers.
  2. Clear plugin interactive handlers via clearPluginInteractiveHandlers().
  3. Re-register a handler and dispatch the same callback twice.

Expected

  • Clear should not throw.
  • Missing dedupe state should be recreated.
  • Duplicate callback dispatch should still dedupe correctly.

Actual

  • Before the fix, clear could throw Cannot read properties of undefined (reading 'clear').

Evidence

Attach at least one:

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

Human Verification (required)

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

  • Verified scenarios:
    • Reproduced the exact .clear() crash locally by seeding a legacy partial interactive state object before the fix.
    • Ran pnpm test src/plugins/interactive.test.ts after the fix.
  • Edge cases checked:
    • clearing a partial legacy state
    • interactive handler registration after hydration
    • duplicate callback dedupe still works after hydration
  • What you did not verify:
    • full openclaw update / openclaw doctor end-to-end in a Debian container
    • unrelated repo-wide typecheck failures outside this change

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/No) Yes
  • Config/env changes? (Yes/No) No
  • Migration needed? (Yes/No) No
  • If yes, exact upgrade steps:

Risks and Mitigations

  • Risk: rebinding callbackDedupe during hydration could discard an unexpected nonstandard object stored under the legacy state key.
    • Mitigation: the real dedupe cache already has its own dedicated global symbol, and this change deliberately reuses that canonical cache instead of trusting ad hoc embedded state.
  • Risk: repo smart gate is currently red for unrelated files.
    • Mitigation: scoped verification passed with pnpm test src/plugins/interactive.test.ts; attempted pnpm check:changed failed only in untouched files src/agents/pi-embedded-runner/compact.ts and src/agents/pi-embedded-runner/run/attempt.ts.

@aisle-research-bot

aisle-research-bot Bot commented Apr 22, 2026 •

Copy link
Copy Markdown

🔒 Aisle Security Analysis

We found 1 potential security issue(s) in this PR:

# Severity Title
1 🟠 High Global mutable plugin interactive state exposed via globalThis[Symbol.for(...)] enables cross-plugin tampering
1. 🟠 Global mutable plugin interactive state exposed via globalThis[Symbol.for(...)] enables cross-plugin tampering
Property Value
Severity High
CWE CWE-284
Location src/plugins/interactive-state.ts:17-67

Description

src/plugins/interactive-state.ts stores live mutable routing and replay-protection state on globalThis under a well-known Symbol.for(...) key.

Because Symbol.for() uses the global symbol registry, any code running in-process (including third-party/untrusted plugins) can obtain the same symbol and directly read/modify the state object:

  • Routing hijack/DoS: mutate interactiveHandlers (Map) to add/remove/replace handlers, causing callbacks to be routed to attacker-controlled handlers or break routing.
  • Replay protection bypass: clear/replace callbackDedupe and/or inflightCallbackDedupe to allow repeated processing of the same callback id (double-processing security-sensitive actions).
  • State corruption DoS: overwrite the stored value with a non-object (the hydration path will recreate some pieces but keeps any provided interactiveHandlers Map/inflightCallbackDedupe Set if shaped correctly), allowing an attacker to preserve a malicious Map/Set instance.

Even though the code attempts to “hydrate” legacy shapes, it still trusts any Map/Set instances found on the global store and provides no access control boundaries between plugins.

Vulnerable behavior example (attacker plugin code):

const k = Symbol.for('openclaw.pluginInteractiveState');
const st = (globalThis as any)[k];
st.interactiveHandlers.set('telegram:payments', { /* attacker handler */ });
st.callbackDedupe.clear();

Recommendation

Avoid exposing mutable security-relevant state via the global symbol registry.

Options (choose based on architecture constraints):

  1. Prefer module-private state when possible (no globalThis):
let state: InteractiveState | undefined;
function getState(): InteractiveState {
  state ??= createInteractiveState();
  return state;
}
  1. If cross-runtime-chunk persistence is required, use a non-discoverable key and encapsulate mutation:
  • Use a module-private const key = Symbol('...') (not Symbol.for) and ensure all chunks share the module instance (or a secure registry you control).
  • Store only an opaque object and do not expose the raw Map/Set; instead expose methods that enforce invariants.
  1. If untrusted plugins share the same JS realm, enforce isolation:
  • run plugins in separate VMs/workers/realms, or
  • provide a restricted API surface and prevent direct access to globalThis.

Additionally:

  • Do not accept attacker-provided Map/Set instances from globalThis during hydration; always create fresh instances and copy validated entries.

Analyzed PR: #70135 at commit bdcda8c

Last updated on: 2026-04-22T11:19:03Z

@openclaw-barnacle openclaw-barnacle Bot added size: S maintainer Maintainer-authored PR labels Apr 22, 2026
@greptile-apps

greptile-apps Bot commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a Cannot read properties of undefined (reading 'clear') crash in clearPluginInteractiveHandlersState() when globalThis[Symbol.for("openclaw.pluginInteractiveState")] held a legacy partial-state object missing callbackDedupe or inflightCallbackDedupe. The fix introduces hydrateInteractiveState, which rebuilds any missing fields before clearing, and binds callbackDedupe to its own dedicated global symbol so it is always the canonical dedupe cache regardless of the incoming shape. The new regression test covers the exact failure mode.

Confidence Score: 5/5

Safe to merge; the crash fix is correct and all data structures are preserved through hydration.

No P0 or P1 issues. The single P2 (unconditional re-hydration allocating a new wrapper object on every getState() call) does not affect correctness — Maps, Sets, and the dedupe cache are all preserved by reference across hydration calls.

src/plugins/interactive-state.ts — the getState() short-circuit optimization is worth a follow-up but is not blocking.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/plugins/interactive-state.ts
Line: 56-68

Comment:
**`getState()` hydrates on every call, not just for legacy shapes**

The current implementation calls `hydrateInteractiveState` whenever `existing !== undefined`, including for already-valid `InteractiveState` objects. This means every invocation of `getState()` allocates a new wrapper object and writes it back to `globalStore` — even on paths like `claimPluginInteractiveCallbackDedupe` that call `getState()` on every interactive callback. `clearPluginInteractiveHandlersState()` alone triggers three separate hydrations since it calls `getState()` three times.

A short-circuit that skips re-hydration when the stored value is already fully shaped would avoid the repeated allocation:

```ts
function getState() {
  const globalStore = globalThis as Record<PropertyKey, unknown>;
  const existing = globalStore[PLUGIN_INTERACTIVE_STATE_KEY];
  if (existing !== undefined) {
    const s = existing as Partial<InteractiveState>;
    if (s.interactiveHandlers instanceof Map && s.inflightCallbackDedupe instanceof Set && s.callbackDedupe) {
      return existing as InteractiveState;
    }
    const hydrated = hydrateInteractiveState(existing);
    globalStore[PLUGIN_INTERACTIVE_STATE_KEY] = hydrated;
    return hydrated;
  }

  const created = createInteractiveState();
  globalStore[PLUGIN_INTERACTIVE_STATE_KEY] = created;
  return created;
}
```

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

Reviews (1): Last reviewed commit: "fix(plugins): hydrate legacy interactive..." | Re-trigger Greptile

Comment on lines +56 to 68
function getState() {
const globalStore = globalThis as Record<PropertyKey, unknown>;
const existing = globalStore[PLUGIN_INTERACTIVE_STATE_KEY];
if (existing !== undefined) {
const hydrated = hydrateInteractiveState(existing);
globalStore[PLUGIN_INTERACTIVE_STATE_KEY] = hydrated;
return hydrated;
}

const created = createInteractiveState();
globalStore[PLUGIN_INTERACTIVE_STATE_KEY] = created;
return created;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 getState() hydrates on every call, not just for legacy shapes

The current implementation calls hydrateInteractiveState whenever existing !== undefined, including for already-valid InteractiveState objects. This means every invocation of getState() allocates a new wrapper object and writes it back to globalStore — even on paths like claimPluginInteractiveCallbackDedupe that call getState() on every interactive callback. clearPluginInteractiveHandlersState() alone triggers three separate hydrations since it calls getState() three times.

A short-circuit that skips re-hydration when the stored value is already fully shaped would avoid the repeated allocation:

function getState() {
  const globalStore = globalThis as Record<PropertyKey, unknown>;
  const existing = globalStore[PLUGIN_INTERACTIVE_STATE_KEY];
  if (existing !== undefined) {
    const s = existing as Partial<InteractiveState>;
    if (s.interactiveHandlers instanceof Map && s.inflightCallbackDedupe instanceof Set && s.callbackDedupe) {
      return existing as InteractiveState;
    }
    const hydrated = hydrateInteractiveState(existing);
    globalStore[PLUGIN_INTERACTIVE_STATE_KEY] = hydrated;
    return hydrated;
  }

  const created = createInteractiveState();
  globalStore[PLUGIN_INTERACTIVE_STATE_KEY] = created;
  return created;
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/plugins/interactive-state.ts
Line: 56-68

Comment:
**`getState()` hydrates on every call, not just for legacy shapes**

The current implementation calls `hydrateInteractiveState` whenever `existing !== undefined`, including for already-valid `InteractiveState` objects. This means every invocation of `getState()` allocates a new wrapper object and writes it back to `globalStore` — even on paths like `claimPluginInteractiveCallbackDedupe` that call `getState()` on every interactive callback. `clearPluginInteractiveHandlersState()` alone triggers three separate hydrations since it calls `getState()` three times.

A short-circuit that skips re-hydration when the stored value is already fully shaped would avoid the repeated allocation:

```ts
function getState() {
  const globalStore = globalThis as Record<PropertyKey, unknown>;
  const existing = globalStore[PLUGIN_INTERACTIVE_STATE_KEY];
  if (existing !== undefined) {
    const s = existing as Partial<InteractiveState>;
    if (s.interactiveHandlers instanceof Map && s.inflightCallbackDedupe instanceof Set && s.callbackDedupe) {
      return existing as InteractiveState;
    }
    const hydrated = hydrateInteractiveState(existing);
    globalStore[PLUGIN_INTERACTIVE_STATE_KEY] = hydrated;
    return hydrated;
  }

  const created = createInteractiveState();
  globalStore[PLUGIN_INTERACTIVE_STATE_KEY] = created;
  return created;
}
```

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

@ngutman ngutman self-assigned this Apr 22, 2026
@ngutman
ngutman force-pushed the fix/70006-doctor-clear-crash branch from c6fd0a9 to bdcda8c Compare April 22, 2026 11:17
@ngutman
ngutman merged commit 8778521 into main Apr 22, 2026
11 checks passed
@ngutman
ngutman deleted the fix/70006-doctor-clear-crash branch April 22, 2026 11:17
@ngutman

ngutman commented Apr 22, 2026

Copy link
Copy Markdown
Member Author

Landed via temp rebase onto main.\n\n- Gate: pnpm lint && pnpm build && pnpm test\n- Land commit: bdcda8c\n- Merge commit: 8778521\n\nLocal note: repo-wide gate hit existing unrelated type failures in untouched files src/agents/pi-embedded-runner/compact.ts and src/agents/pi-embedded-runner/run/attempt.ts, so I validated the PR with scoped coverage (

[email protected] test /Users/guti/projects/openclaw/copy-1
node scripts/test-projects.mjs src/plugins/interactive.test.ts

RUN v4.1.4 /Users/guti/projects/openclaw/copy-1

Test Files 1 passed (1)
Tests 14 passed (14)
Start at 14:17:34
Duration 393ms (transform 126ms, setup 44ms, import 152ms, tests 127ms, environment 0ms)) before landing.\n\nThanks @ngutman!

medikoo pushed a commit to medikoo/openclaw that referenced this pull request Apr 24, 2026
…w#70135)

* fix(plugins): hydrate legacy interactive state

* fix(plugins): avoid doctor crash on legacy interactive state (openclaw#70135) (thanks @ngutman)
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…w#70135)

* fix(plugins): hydrate legacy interactive state

* fix(plugins): avoid doctor crash on legacy interactive state (openclaw#70135) (thanks @ngutman)
zhonghe0615 pushed a commit to zhonghe0615/openclaw that referenced this pull request May 7, 2026
…w#70135)

* fix(plugins): hydrate legacy interactive state

* fix(plugins): avoid doctor crash on legacy interactive state (openclaw#70135) (thanks @ngutman)
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request May 13, 2026
…w#70135)

* fix(plugins): hydrate legacy interactive state

* fix(plugins): avoid doctor crash on legacy interactive state (openclaw#70135) (thanks @ngutman)
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
…w#70135)

* fix(plugins): hydrate legacy interactive state

* fix(plugins): avoid doctor crash on legacy interactive state (openclaw#70135) (thanks @ngutman)
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
…w#70135)

* fix(plugins): hydrate legacy interactive state

* fix(plugins): avoid doctor crash on legacy interactive state (openclaw#70135) (thanks @ngutman)
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
…w#70135)

* fix(plugins): hydrate legacy interactive state

* fix(plugins): avoid doctor crash on legacy interactive state (openclaw#70135) (thanks @ngutman)
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
…w#70135)

* fix(plugins): hydrate legacy interactive state

* fix(plugins): avoid doctor crash on legacy interactive state (openclaw#70135) (thanks @ngutman)
chovizzz added a commit to chovizzz/openclaw that referenced this pull request Sep 4, 2026
Heetisk pushed a commit to Heetisk/openclaw that referenced this pull request Sep 28, 2026
…w#70135)

* fix(plugins): hydrate legacy interactive state

* fix(plugins): avoid doctor crash on legacy interactive state (openclaw#70135) (thanks @ngutman)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] openclaw doctor crashes with TypeError "Cannot read properties of undefined (reading 'clear')" after update on v2026.4.21

1 participant