P2: CanvasWindow WebView2 event handlers leak and capture stale auth tokens
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
- Store the
WebResourceRequestedlambda in a field so it can be unsubscribed (-=). - Unsubscribe all handlers in
OnWindowClosedorDispose. - Null out
_gatewayTokenafter disposal to avoid keeping it rooted by a closure. - Consider reusing a single
CanvasWindowinstance rather than recreating it, which would make lifecycle management simpler.
Files
src/OpenClaw.Tray.WinUI/Windows/CanvasWindow.xaml.cs
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:308The 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:
- Repository policy read: AGENTS.md was found and read fully; its gateway credential lifecycle guidance is relevant because this issue involves gateway token handling. (
AGENTS.md:1, bf21e7b216aa) - Current source still captures token in anonymous handler: InitializeWebViewAsync adds a WebResourceRequested lambda after copying _trustedGatewayOrigin and _gatewayToken into locals, so the handler has no field identity for -= and keeps the captured token value. (
src/OpenClaw.Tray.WinUI/Windows/CanvasWindow.xaml.cs:302, bf21e7b216aa) - Close path misses WebResourceRequested cleanup: OnWindowClosed unsubscribes _webMessageReceivedHandler and OnNavigationCompleted, but there is no WebResourceRequested handler field or unsubscribe in the same cleanup block. (
src/OpenClaw.Tray.WinUI/Windows/CanvasWindow.xaml.cs:438, bf21e7b216aa) - Window lifecycle can reuse or recreate CanvasWindow: NodeService creates CanvasWindow with SetTrustedGatewayOrigin on first use, reuses it while open, and closes/recreates it in hide/teardown paths, matching the lifecycle described by the report. (
src/OpenClaw.Tray.WinUI/Services/NodeService.cs:843, bf21e7b216aa) - Existing test only checks one unsubscribe: The markup test asserts WebMessageReceived is unsubscribed but does not cover WebResourceRequested or token-clearing behavior. (
tests/OpenClaw.Tray.Tests/TrayMenuWindowMarkupTests.cs:24, bf21e7b216aa) - Credential lifecycle invariant: Connection architecture documents strict credential precedence and warns not to downgrade paired devices, so stale token injection is within the repository's credential-safety boundary. (
docs/CONNECTION_ARCHITECTURE.md:133, bf21e7b216aa)
Likely related people:
- ranjeshj: git blame and -S history tie the current CanvasWindow WebView2 handler wiring, token injection, and cleanup path to commit 6d8541e57008345c9d459059c820b1f5fb2bafa7. (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 8add87d10c544aa0595774ec8d5417f6d55306c6, 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-reviewcomments, 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-reviewor@clawsweeper re-runon an open PR or issue to request a fresh review only. - Maintainers can also comment
@clawsweeper reviewto 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 explainto ask for more context, or@clawsweeper stopto stop active automation.
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.