Repository navigation
fix(agents): guard null response body in proxy stream reader - #107325
Conversation
The proxy transport used response.body! (non-null assertion) to enter the SSE read loop without checking whether the body is present. response.ok does not guarantee response.body is non-null — empty 200 responses and prematurely-closed connections leave body=null while ok=true. Sibling transports in the same repository already guard this case: - provider-transport-fetch.ts:148,385,541 - anthropic-transport-stream.ts:847 - openai-completions-transport.ts:272 - mcp-http-fetch.ts:60,76 (added in openclaw#98143) Add the same guard before getReader() so the caller receives a business- level diagnostic ("Proxy returned an empty response body") instead of the misleading JS runtime error "Cannot read properties of null (reading 'getReader')". The try-catch in streamProxy already catches the error and pushes it as a stream event, so this does not introduce a new crash — it replaces an internal TypeError with a meaningful error.
|
Codex review: needs maintainer review before merge. Reviewed July 15, 2026, 10:13 PM ET / July 16, 2026, 02:13 UTC. Summary PR surface: Source +4, Tests +29. Total +33 across 2 files. Reproducibility: yes. A real local HTTP 204 response reproduces ok=true with body=null, and the focused negative control against current main records the existing getReader TypeError. Review metrics: none identified. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Next step before merge
Security Review detailsBest possible solution: Land the narrow null-body guard with its focused regression test, preserving the existing streaming and error-event behavior for all responses that provide a readable body. Do we have a high-confidence way to reproduce the issue? Yes. A real local HTTP 204 response reproduces ok=true with body=null, and the focused negative control against current main records the existing getReader TypeError. Is this the best way to solve the issue? Yes. Checking the body at the single ownership point immediately before getReader() is the narrowest maintainable fix and matches established sibling transport handling without adding fallback behavior or new API surface. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 08ecf63bb608. Label changesLabel changes:
Label justifications:
Evidence reviewedPR surface: Source +4, Tests +29. Total +33 across 2 files. View PR surface stats
What I checked:
Likely related people:
What the crustacean ranks mean
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. How this review workflow works
Review history (1 earlier review cycle)
|
|
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
Merged via squash.
|
…w#107325) * fix(agents): guard null response body in proxy stream reader The proxy transport used response.body! (non-null assertion) to enter the SSE read loop without checking whether the body is present. response.ok does not guarantee response.body is non-null — empty 200 responses and prematurely-closed connections leave body=null while ok=true. Sibling transports in the same repository already guard this case: - provider-transport-fetch.ts:148,385,541 - anthropic-transport-stream.ts:847 - openai-completions-transport.ts:272 - mcp-http-fetch.ts:60,76 (added in openclaw#98143) Add the same guard before getReader() so the caller receives a business- level diagnostic ("Proxy returned an empty response body") instead of the misleading JS runtime error "Cannot read properties of null (reading 'getReader')". The try-catch in streamProxy already catches the error and pushes it as a stream event, so this does not introduce a new crash — it replaces an internal TypeError with a meaningful error. * test(agents): cover null response body in proxy stream reader
…w#107325) * fix(agents): guard null response body in proxy stream reader The proxy transport used response.body! (non-null assertion) to enter the SSE read loop without checking whether the body is present. response.ok does not guarantee response.body is non-null — empty 200 responses and prematurely-closed connections leave body=null while ok=true. Sibling transports in the same repository already guard this case: - provider-transport-fetch.ts:148,385,541 - anthropic-transport-stream.ts:847 - openai-completions-transport.ts:272 - mcp-http-fetch.ts:60,76 (added in openclaw#98143) Add the same guard before getReader() so the caller receives a business- level diagnostic ("Proxy returned an empty response body") instead of the misleading JS runtime error "Cannot read properties of null (reading 'getReader')". The try-catch in streamProxy already catches the error and pushes it as a stream event, so this does not introduce a new crash — it replaces an internal TypeError with a meaningful error. * test(agents): cover null response body in proxy stream reader
What Problem This Solves
streamProxy()insrc/agents/runtime/proxy.tsaccessesresponse.body!with a non-null assertion after checkingresponse.ok, butresponse.ok === truedoes not guaranteeresponse.body !== null. A real HTTP 204 response (or a misconfigured reverse proxy that returns an empty 200 envelope) producesok=true, body=nullthrough the realnode:http→undicifetch stack.When triggered, the caller receives
"Cannot read properties of null (reading 'getReader')"— a JavaScript TypeError that describes an implementation detail (null reference ongetReader) rather than the actual problem (the upstream proxy returned no body). A developer seeing this would debug proxy.ts internals rather than the proxy server configuration.Sibling transports in this repository already guard this case:
provider-transport-fetch.ts:148if (!response.body) return response;provider-transport-fetch.ts:385if (!params.response.ok || !params.response.body)provider-transport-fetch.ts:541if (!response.body)anthropic-transport-stream.ts:847if (!response.body)openai-completions-transport.ts:272if (!response.body || !response.ok)mcp-http-fetch.ts:60if (response.body != null)mcp-http-fetch.ts:76if (!response.body)Three merged PRs applied this same pattern to transports that previously lacked it:
fix(agents): bound body-less MCP HTTP text responses— added!response.bodyguard tomcp-http-fetch.tsfix(mattermost): bound null-body error response reads— removed unbounded!res.bodyfallback in mattermost clientfix(infra): enforce maxBytes in body-less HTTP error snippet path— same body=null guard patternproxy.ts:343was the last remaining transport without this guard.Why This Change Was Made
A 4-line null guard is added before
getReader()so the caller receives a business-level error ("Proxy returned an empty response body") instead of a JS internal TypeError. The existing try-catch instreamProxyalready converts thrown errors to stream error events — no new error propagation path is introduced.streamProxyis a public SDK API:src/agents/runtime/proxy.ts→src/plugin-sdk/agent-core.ts(re-export) →packages/plugin-sdk/dist/(compiled type declarations). A public API should not leak implementation-level error messages.A committed regression test (
proxy.test.ts) ensures the business-level error is preserved through refactoring.User Impact
Operators debugging proxy connection failures see
"Proxy returned an empty response body"— a message that identifies the upstream proxy as the problem. Previously they saw"Cannot read properties of null (reading 'getReader')"— a message that suggests a code bug. Normal streaming paths are unaffected.Evidence
Real HTTP server proof —
ok=true, body=nullthrough real fetchA real
node:httpserver returns204 No Contentand the response is fetched through the real network stack (no stub, no mock):This proves that a real HTTP exchange through the full Node.js fetch → undici → TCP stack produces the
ok=true, body=nullshape. The guard replaces an internal TypeError with a business-level diagnostic.Negative control — upstream main source + new regression test
The new test applied to
upstream/mainsource (before the fix):Upstream main leaks the JS runtime TypeError — the caller sees
"Cannot read properties of null"instead of identifying the upstream proxy as the cause.After fix — full test suite
The new test (
"emits a business-level error when the proxy returns a null response body") is included in the 14. The existing 13 tests pass unchanged.Lint
Delta
Source: +4 lines (guard). Test: +29 lines (regression test).
Observed result after fix: Null-body proxy responses produce
"Proxy returned an empty response body". Normal streaming paths are unchanged. The regression test locks the business-level error message.What was not tested: A live misconfigured proxy server in production returning empty 200 envelopes. The 204 No Content response from a real
node:httpserver proves the exactok=true, body=nullshape through the real fetch/undici/TCP stack — the same code path thatstreamProxy'sfetch()call exercises.Regression Test Plan
Root Cause
In
streamProxy(),response.okis checked (line 328) before entering the SSE read loop. Whenokis true, the code callsresponse.body!.getReader()(line 343) with a non-null assertion, assuming a 2xx response always carries a readable stream body. The Fetch Standard does not guarantee this —response.bodyis null for 204 No Content, 304 Not Modified, responses with an empty payload, and responses from reverse proxies that return header-only status envelopes.The 9 other
response.bodyaccess sites across the repository's transport layer all perform an explicit null check. PRs #98143, #97851, and #99340 applied this pattern to files that previously lacked it.proxy.ts:343inherits the identical risk but was not updated in those rounds.AI Assistance
Implementation, test, real-server proof, and PR body were drafted with agent assistance. All commands were run locally.