Repository navigation
fix: add resource limit in zen-proxy.mjs (CWE-400) - #6
Conversation
Automated security fix generated by OrbisAI Security
|
Heads-up: this is already covered (and extended) in PR #9 — mid-stream body-limit enforcement with early abort exists there as |
…n routes readBody() already aborts mid-stream once a request exceeds MAX_BODY (see previous commit), but the existing oversized-body test only checks the final status, so it can't distinguish a true early abort from the old buffer-everything-then-check pattern. Add a chunk-tracking request mock (mockStreamingReq) and tests that assert the handler stops consuming chunks before the stream is drained, across handleChat, handleApiConfig (PUT), and handleTest. Also give handleApiConfig (PUT) and handleTest the same precise 413 response handleChat already has for oversized bodies, instead of a generic 400 — all three share the same readBody() primitive, so the memory-safety property was already in place; this just makes the response shape consistent.
|
Thanks for flagging #9 — I checked its actual diff rather than taking the summary at face value, and the coverage claim doesn't hold up for the three primary API routes. Citing exact lines from the current PR #9 branch (
So I'd rather not close this one in favor of #9 as-is, since #9 doesn't actually resolve the finding for the primary request path. A couple reasons I think this PR's approach is also preferable on the merits, not just on coverage:
Suggested path: merge this fix first (it's small and now has test coverage), and have #9 rebase onto it — dropping the now-redundant Happy to rebase this PR onto #9 once that's settled either way. |
The readBody function reads the entire request body into memory without enforcing a size limit during streaming. While handleChat checks raw.length > MAX_BODY after reading, the entire payload has already been buffered in memory, allowing memory exhaustion before the size check can reject it. The affected code is
zen-proxy.mjs:530. This change is the fix I would apply.Reference: CWE-400
What changed
zen-proxy.mjs:readBodynow takes alimit(defaultMAX_BODY) and throws mid-stream — as soon as the running total exceeds the limit — instead of buffering the whole body and checking its length afterward. SincereadBodyis shared byhandleChat,handleApiConfig(PUT), andhandleTest, all three are protected by this one change.handleApiConfig(PUT) andhandleTestnow also return a precise413for the oversized-body case (previously a generic400), matchinghandleChat.test/zen-proxy.test.mjs: added a chunk-tracking request mock (mockStreamingReq) and regression tests proving the request is aborted mid-stream (the handler stops pulling chunks before the generator is drained) rather than after full buffering, acrosshandleChat,handleApiConfig(PUT), andhandleTest. Also addedMAX_BODYboundary tests (exactly at the limit, one byte over, one byte under).Verification
npm test— 130 passed, 1 skipped (unrelated:pwshnot available on this host for an install.ps1 test). As a sanity check on the new tests themselves, I temporarily revertedreadBodyto the original unbounded form and confirmed the new mid-stream tests fail specifically on the "stream not fully drained" assertion (not just the status code) — confirming they exercise the actual security property, not just the final response.Automated security fix by OrbisAI Security