Skip to content

fix(agents): reject invalid UTF-8 LSP responses - #117271

Merged
steipete merged 1 commit into
openclaw:mainfrom
zhangguiping-xydt:fix/problem-lsp-invalid-utf8-framing
Aug 1, 2026
Merged

steipete merged 1 commit into
openclaw:mainfrom
zhangguiping-xydt:fix/problem-lsp-invalid-utf8-framing

Conversation

@zhangguiping-xydt

Copy link
Copy Markdown
Contributor
Additional instructions

MUST: Keep Allow edits from maintainers enabled for this PR so maintainers
can help update the branch when needed.

What Problem This Solves

Fixes an issue where agent bundle LSP requests could accept a response containing malformed UTF-8 when the replacement character still left the body as valid JSON. The corrupted payload could then be returned to the caller with � instead of failing the transport.

Why This Change Was Made

LSP response bodies are now decoded with a fatal UTF-8 TextDecoder before JSON parsing. Decode failures enter the existing framing-error path, which rejects pending requests, terminates the language-server process, and latches the error for later requests; ignoreBOM: true preserves the previous JSON BOM rejection behavior.

User Impact

Malformed language-server responses can no longer be silently altered and treated as valid results. Callers receive a clear framing error, and the compromised LSP process is shut down instead of continuing with corrupted data.

Evidence

Fresh red on the pinned main baseline:

$ node scripts/run-vitest.mjs src/agents/agent-bundle-lsp-runtime.test.ts -t 'rejects invalid UTF-8 in LSP JSON bodies'
AssertionError: promise resolved instead of rejecting
"contents": "�"
Test Files  2 failed (2)

Focused regression suite after the fix:

$ node scripts/run-vitest.mjs src/agents/agent-bundle-lsp-runtime.test.ts
Test Files  1 passed (1)
Tests  20 passed (20)

Strong live E2E used the production createBundleLspToolRuntime, a real spawned Node child process, and real stdio Content-Length frames. Gateway is not applicable because this runtime talks directly to its language-server child.

entrypoint=createBundleLspToolRuntime
transport=real_spawned_child_process_stdio
scenario=valid
trigger=hover response body is valid UTF-8 JSON
outcome=resolved
tool_text="{\n  \"contents\": \"live-ok-✓\"\n}"
child_close=observed_after_dispose
scenario=invalid
trigger=hover response body contains raw byte 0xff inside a JSON string
outcome=rejected
error=LSP framing error: body is not valid UTF-8
child_close=observed_after_framing_error
future_request=rejected_with_latched_framing_error
verdict=PASS

Formatting and whitespace checks also passed (oxfmt --check and git diff --check).

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Aug 1, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P2 Normal backlog priority with limited blast radius. labels Aug 1, 2026
@clawsweeper

clawsweeper Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 1, 2026, 3:11 AM ET / 07:11 UTC.

ClawSweeper review

What this changes

The PR makes agent-bundle language-server JSON-RPC response decoding reject malformed UTF-8 and adds regression coverage for a raw invalid byte in an otherwise valid response.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

This PR addresses a real current-main transport defect: malformed LSP body bytes are replacement-decoded before JSON parsing, so they can become a different valid response. The branch makes that boundary strict and routes failure through the existing session-failure and process-termination path; no blocking patch defect was found.

Priority: P2
Reviewed head: 98c2c69bf95f6d89008cdd7b0042b6bd1f289df9

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) This is a focused, source-proven repair with strong runtime-oriented proof and no correctness findings.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The PR body provides an after-fix spawned-child stdio transcript for both valid and malformed frames, showing rejection, process closure, and rejection of a later request; no private values are exposed.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR body provides an after-fix spawned-child stdio transcript for both valid and malformed frames, showing rejection, process closure, and rejection of a later request; no private values are exposed.
Evidence reviewed 5 items Current-main defect: Current main decodes each complete LSP response body with Buffer.toString("utf8") before JSON.parse; Node replacement-decodes malformed byte sequences, allowing an otherwise valid JSON payload to be altered rather than rejected.
Proposed strict boundary: The PR replaces replacement decoding with TextDecoder("utf-8", { fatal: true, ignoreBOM: true }) and maps decoding failures to the existing framing-error result; the live Node probe confirmed fatal decoding throws for 0xff and ignoreBOM:true preserves a leading BOM for the existing JSON rejection path.
Existing failure owner: A failed parse already clears the receive buffer, rejects pending work through failLspSession, and terminates the child-process tree, so the proposed decode failure uses the canonical transport-failure lifecycle instead of adding a fallback path.
Findings None None.
Security None None.

How this fits together

Agent bundles spawn configured language servers and exchange JSON-RPC frames over child-process stdio. The LSP framing parser converts response bytes into tool results; malformed frames reject outstanding requests and terminate the affected language-server session.

flowchart LR
  A[Agent bundle request] --> B[Language server child process]
  B --> C[Stdio response bytes]
  C --> D[LSP framing parser]
  D --> E{Valid UTF-8 and JSON?}
  E -->|Yes| F[Tool result]
  E -->|No| G[Reject requests and stop session]
Loading

Before merge

  • Complete next step (P2) - No repair lane is needed because this PR already contains the narrow implementation and focused regression coverage; proceed through normal merge checks.
Agent review details

Security

None.

PR surface

Source +6, Tests +35. Total +41 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 7 1 +6
Tests 1 35 0 +35
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 42 1 +41

Review metrics

None.

Technical review

Best possible solution:

Merge the narrow strict-decoding repair after the normal checks finish, retaining the existing framing-error lifecycle for every malformed LSP response body.

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

Yes. Current main visibly replacement-decodes the declared response body before JSON parsing, and the proposed regression constructs a raw invalid byte inside otherwise valid JSON; the PR body also provides an after-fix spawned-child stdio transcript.

Is this the best way to solve the issue?

Yes. Fatal UTF-8 decoding at the byte-to-text boundary is the narrowest maintainable repair and deliberately reuses the existing parser failure, pending-request rejection, and child-process cleanup path.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 1f130ce61d62.

Labels

Label changes:

  • add P2: This is a bounded agent-runtime correctness repair that prevents corrupted language-server results without changing configuration or user defaults.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR body provides an after-fix spawned-child stdio transcript for both valid and malformed frames, showing rejection, process closure, and rejection of a later request; no private values are exposed.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body provides an after-fix spawned-child stdio transcript for both valid and malformed frames, showing rejection, process closure, and rejection of a later request; no private values are exposed.

Label justifications:

  • P2: This is a bounded agent-runtime correctness repair that prevents corrupted language-server results without changing configuration or user defaults.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body provides an after-fix spawned-child stdio transcript for both valid and malformed frames, showing rejection, process closure, and rejection of a later request; no private values are exposed.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body provides an after-fix spawned-child stdio transcript for both valid and malformed frames, showing rejection, process closure, and rejection of a later request; no private values are exposed.

Evidence

What I checked:

  • Current-main defect: Current main decodes each complete LSP response body with Buffer.toString("utf8") before JSON.parse; Node replacement-decodes malformed byte sequences, allowing an otherwise valid JSON payload to be altered rather than rejected. (src/agents/agent-bundle-lsp-runtime.ts:244, 1f130ce61d62)
  • Proposed strict boundary: The PR replaces replacement decoding with TextDecoder("utf-8", { fatal: true, ignoreBOM: true }) and maps decoding failures to the existing framing-error result; the live Node probe confirmed fatal decoding throws for 0xff and ignoreBOM:true preserves a leading BOM for the existing JSON rejection path. (src/agents/agent-bundle-lsp-runtime.ts:169, 98c2c69bf95f)
  • Existing failure owner: A failed parse already clears the receive buffer, rejects pending work through failLspSession, and terminates the child-process tree, so the proposed decode failure uses the canonical transport-failure lifecycle instead of adding a fallback path. (src/agents/agent-bundle-lsp-runtime.ts:306, 1f130ce61d62)
  • Focused regression coverage: The added test writes a Content-Length frame whose JSON string contains raw byte 0xff, expects the framing error, and verifies process-tree termination. (src/agents/agent-bundle-lsp-runtime.test.ts:370, 98c2c69bf95f)
  • Feature provenance: The current LSP runtime and its test suite were introduced together by the recent lifecycle refactor, making that commit the clearest current-main ownership signal for this parser surface. (src/agents/agent-bundle-lsp-runtime.ts:218, 408abcd46f9e)

Likely related people:

  • Peter Steinberger: Current-main blame and file history attribute the LSP framing parser and its initial focused suite to the lifecycle refactor that introduced this runtime. (role: feature introducer and recent area contributor; confidence: high; commits: 408abcd46f9e; files: src/agents/agent-bundle-lsp-runtime.ts, src/agents/agent-bundle-lsp-runtime.test.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • 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.

@steipete

steipete commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Maintainer verification for original contributor head 98c2c69bf95f6d89008cdd7b0042b6bd1f289df9:

  • Independently proved the installed Node contract: the existing Buffer decoder silently changes raw 0xff into U+FFFD, while fatal UTF-8 decoding raises ERR_ENCODING_INVALID_ENCODED_DATA; ignoreBOM: true preserves existing JSON BOM rejection.
  • Executed the actual production createBundleLspToolRuntime and hover tool against two real spawned Node child processes using real Content-Length stdio frames: valid Unicode returned live-ok-✓; raw 0xff inside a JSON result rejected with LSP framing error: body is not valid UTF-8 and terminated the compromised child.
  • node scripts/run-vitest.mjs src/agents/agent-bundle-lsp-runtime.test.ts passed 20/20, including malformed process-frame rejection and process cleanup.
  • Fresh gpt-5.6-sol structured review with --mode branch --base origin/main --engine codex --thinking xhigh reported zero accepted/actionable findings.
  • Exact-head full hosted CI is green: https://github.com/openclaw/openclaw/actions/runs/30689057165
  • Preserving the original contributor commit, branch, and author.

@steipete
steipete merged commit 0cec56d into openclaw:main Aug 1, 2026
129 of 137 checks passed
@steipete

steipete commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Merged via squash.

vincentkoc added a commit that referenced this pull request Aug 1, 2026
* origin/main: (41 commits)
  test(ci): update memory helper routing expectation (#117295)
  fix(canvas): emit successful control results in JSON mode (#117297)
  fix(imessage): settle native inbound reply delivery (#117282)
  fix(agents): reject invalid UTF-8 LSP responses (#117271)
  fix(line): centralize card and template message alt text (#117280)
  fix(release): validate candidates before Docker publication (#117252)
  fix(media): validate provider-owned generated media downloads (#117281)
  fix(feishu): restore custom HTTPS API domains (#117278)
  fix(models): reuse prepared plugin metadata (#117131)
  fix(agents): stop fallback after delivered-but-failed attempts (#114628)
  fix(memory): reuse targeted session corpus snapshot (#117247)
  fix(matrix): reconcile durable sends after response loss
  test(release): repair full validation regressions (#116931)
  fix(linux-canvas): report socket watcher fallback (#109502)
  fix(codex): preserve authoritative turn completion (#117185)
  fix(canvas): preserve widgets across derived-name collisions (#117132)
  fix(channels): config validation rejects documented channels.<id>.configWrites on 13 channels (#117206)
  fix(msteams): prevent duplicate final replies (#116398)
  fix(doctor): session SQLite restore installs an empty session index over the valid archive (#117221)
  fix(plugins): preserve workspace plugins across CLI and control UI (#117265)
  ...
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 2, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 2, 2026
* origin/main: (41 commits)
  test(ci): update memory helper routing expectation (openclaw#117295)
  fix(canvas): emit successful control results in JSON mode (openclaw#117297)
  fix(imessage): settle native inbound reply delivery (openclaw#117282)
  fix(agents): reject invalid UTF-8 LSP responses (openclaw#117271)
  fix(line): centralize card and template message alt text (openclaw#117280)
  fix(release): validate candidates before Docker publication (openclaw#117252)
  fix(media): validate provider-owned generated media downloads (openclaw#117281)
  fix(feishu): restore custom HTTPS API domains (openclaw#117278)
  fix(models): reuse prepared plugin metadata (openclaw#117131)
  fix(agents): stop fallback after delivered-but-failed attempts (openclaw#114628)
  fix(memory): reuse targeted session corpus snapshot (openclaw#117247)
  fix(matrix): reconcile durable sends after response loss
  test(release): repair full validation regressions (openclaw#116931)
  fix(linux-canvas): report socket watcher fallback (openclaw#109502)
  fix(codex): preserve authoritative turn completion (openclaw#117185)
  fix(canvas): preserve widgets across derived-name collisions (openclaw#117132)
  fix(channels): config validation rejects documented channels.<id>.configWrites on 13 channels (openclaw#117206)
  fix(msteams): prevent duplicate final replies (openclaw#116398)
  fix(doctor): session SQLite restore installs an empty session index over the valid archive (openclaw#117221)
  fix(plugins): preserve workspace plugins across CLI and control UI (openclaw#117265)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XS status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants