Skip to content

P2: CanvasWindow WebView2 event handlers leak and capture stale auth tokens #556

Closed

Description

@QQSHI13

Problem

CanvasWindow.xaml.cs subscribes to CoreWebView2 events but never unsubscribes them. If the window is recreated (e.g. during node teardown → recreate), handlers accumulate on the old WebView2 instance. Additionally, an anonymous lambda captures _trustedGatewayOrigin and _gatewayToken, which can become stale if tokens rotate.

Relevant code:

CanvasWebView.CoreWebView2.WebMessageReceived += _webMessageReceivedHandler;
CanvasWebView.CoreWebView2.WebResourceRequested += (s, args) => { ... };
CanvasWebView.CoreWebView2.NavigationCompleted += OnNavigationCompleted;

Impact

  • Memory leaks: handlers and captured variables are kept alive by the WebView2 event list.
  • Security: stale auth tokens may be injected into requests after a token refresh or gateway switch.
  • Hard to diagnose because the leak is in native COM interop, not managed GC.

Suggested fix

  1. Store the WebResourceRequested lambda in a field so it can be unsubscribed (-=).
  2. Unsubscribe all handlers in OnWindowClosed or Dispose.
  3. Null out _gatewayToken after disposal to avoid keeping it rooted by a closure.
  4. Consider reusing a single CanvasWindow instance rather than recreating it, which would make lifecycle management simpler.

Files

  • src/OpenClaw.Tray.WinUI/Windows/CanvasWindow.xaml.cs

Activity

  1. clawsweeper commented on May 27, 2026

    @clawsweeper

    Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed May 27, 2026, 9:00 AM ET / 13:00 UTC.

    Summary
    Keep open: current main still wires an anonymous WebResourceRequested handler that captures the trusted origin and gateway token, while the close path only unsubscribes WebMessageReceived and NavigationCompleted.

    Reproducibility: yes. Source inspection on current main shows the WebResourceRequested lambda captures trustedOrigin/token locals and has no corresponding unsubscribe in OnWindowClosed; I did not run a live WebView2 repro.

    Next step
    A narrow fix is likely, but the token-injection path is security-sensitive enough that a maintainer should review the lifecycle approach before assigning automated repair.

    Security
    Needs attention: The issue is security-sensitive because current main can retain and inject a stale bearer token through a WebView2 request handler.

    Review details

    Best possible solution:

    Store the WebResourceRequested handler in a field, remove the matching filter and unsubscribe all CoreWebView2 handlers on close, clear gateway auth fields on disposal, and add focused regression coverage plus a tray/WebView2 smoke.

    Do we have a high-confidence way to reproduce the issue?

    Yes. Source inspection on current main shows the WebResourceRequested lambda captures trustedOrigin/token locals and has no corresponding unsubscribe in OnWindowClosed; I did not run a live WebView2 repro.

    Is this the best way to solve the issue?

    Yes. The suggested fix is the narrow maintainable direction: give the WebResourceRequested handler a field identity, unsubscribe it during close/disposal, avoid captured stale token state, and cover the lifecycle behavior.

    AGENTS.md: found and applied where relevant.

    Remaining risk / open question:

    • Runtime leak magnitude was not measured in a real WebView2 session during this read-only review; the source-level lifecycle and stale-token gap is still clear enough to keep open.

    Codex review notes: model gpt-5.5, reasoning high; reviewed against bf21e7b216aa.

    Label changes

    Label changes:

    • add P2: The report describes a bounded CanvasWindow lifecycle and credential-injection bug with limited blast radius.
    • add impact:security: The issue is about stale gateway bearer tokens being retained and injected after token refresh or gateway switch.
    • add issue-rating: 🦞 diamond lobster: Current issue advisory state selects this label.
    • add clawsweeper:source-repro: Current issue advisory state selects this label.
    • add clawsweeper:no-new-fix-pr: Current issue advisory state selects this label.
    • add clawsweeper:needs-maintainer-review: Current issue advisory state selects this label.
    • add clawsweeper:needs-security-review: Current issue advisory state selects this label.

    Label justifications:

    • P2: The report describes a bounded CanvasWindow lifecycle and credential-injection bug with limited blast radius.
    • impact:security: The issue is about stale gateway bearer tokens being retained and injected after token refresh or gateway switch.
    Evidence reviewed

    Security concerns:

    • [medium] Stale bearer token can remain captured in WebView2 handler — src/OpenClaw.Tray.WinUI/Windows/CanvasWindow.xaml.cs:308
      The WebResourceRequested lambda copies _gatewayToken into a local token and is never unsubscribed, so token rotation or gateway switching can leave an old bearer value attached to the WebView2 request pipeline.
      Confidence: 0.86

    What I checked:

    Likely related people:

    • ranjeshj: git blame and -S history tie the current CanvasWindow WebView2 handler wiring, token injection, and cleanup path to commit 6d8541e. (role: introduced behavior; confidence: high; commits: 6d8541e57008; files: src/OpenClaw.Tray.WinUI/Windows/CanvasWindow.xaml.cs, src/OpenClaw.Tray.WinUI/Services/NodeService.cs)
    • kenehong: git log --follow shows the current CanvasWindow file was most recently re-added/touched in commit 8add87d, though blame for the affected lines still points to the earlier CanvasWindow work. (role: recent area contributor; confidence: medium; commits: 8add87d10c54; files: src/OpenClaw.Tray.WinUI/Windows/CanvasWindow.xaml.cs)
    How this review workflow works
    • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
    • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
    • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
    • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
    • Maintainers can also comment @clawsweeper review to request a fresh review only.
    • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
    • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
    • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.
  2. added
    P2
    impact:security
    issue-rating: 🦞 diamond lobster
    clawsweeper:source-repro
    clawsweeper:no-new-fix-pr
    clawsweeper:needs-maintainer-review
    clawsweeper:needs-security-review
    on May 27, 2026
  3. shanselman commented on May 28, 2026

    @shanselman
    Collaborator

    Addressed by merged PR #564. CanvasWindow now stores and removes the WebView2 auth handler/filter, avoids the anonymous stale-token capture, and clears gateway auth state on close.

  4. locked as resolved and limited conversation to collaborators on May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2clawsweeper:needs-maintainer-reviewclawsweeper:needs-security-reviewclawsweeper:no-new-fix-prclawsweeper:source-reproimpact:securityissue-rating: 🦞 diamond lobster

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions