fix(daemon): reject non-object JSON request bodies#982
Conversation
📝 WalkthroughWalkthroughThe HTTP server factory's JSON parsing middleware now validates request body shape, ensuring parsed JSON is a non-null object rather than null or array. A test verifies this validation by sending invalid payloads and confirming the expected 400 error response and route handler bypass. ChangesJSON Body Validation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
apps/daemon/__tests__/unit/http-server-factory.test.ts (1)
170-171: ⚡ Quick winConsider adding a primitive test case for comprehensive coverage.
The test currently validates
nulland array payloads. The implementation also rejects primitives (strings, numbers, booleans) via thetypeof data !== 'object'check on line 72 ofhttp-server-factory.ts. Adding at least one primitive case would ensure all validation branches are tested.📋 Example: Add a string primitive test
try { const nullBody = await makeRequest(port, '/ping', 'POST', 'null', 'secret-token'); const arrayBody = await makeRequest(port, '/ping', 'POST', '[]', 'secret-token'); + const stringBody = await makeRequest(port, '/ping', 'POST', '"hello"', 'secret-token'); expect(nullBody.statusCode).toBe(400); expect(JSON.parse(nullBody.body).error).toBe('Request body must be a JSON object'); expect(arrayBody.statusCode).toBe(400); expect(JSON.parse(arrayBody.body).error).toBe('Request body must be a JSON object'); + expect(stringBody.statusCode).toBe(400); + expect(JSON.parse(stringBody.body).error).toBe('Request body must be a JSON object'); expect(routeHandler).not.toHaveBeenCalled();🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@apps/daemon/__tests__/unit/http-server-factory.test.ts` around lines 170 - 171, Add a primitive payload test to cover the branch in http-server-factory.ts that rejects non-object bodies: extend the existing test that calls makeRequest for '/ping' to include a primitive case (e.g., send a string like '"hello"' or a number/boolean) using makeRequest(port, '/ping', 'POST', '<primitive>', 'secret-token') and assert the expected rejection (HTTP 400 or the same response as the null/array cases). This will exercise the typeof data !== 'object' validation path in http-server-factory.ts and ensure primitives are tested alongside null and arrays.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@apps/daemon/__tests__/unit/http-server-factory.test.ts`:
- Around line 170-171: Add a primitive payload test to cover the branch in
http-server-factory.ts that rejects non-object bodies: extend the existing test
that calls makeRequest for '/ping' to include a primitive case (e.g., send a
string like '"hello"' or a number/boolean) using makeRequest(port, '/ping',
'POST', '<primitive>', 'secret-token') and assert the expected rejection (HTTP
400 or the same response as the null/array cases). This will exercise the typeof
data !== 'object' validation path in http-server-factory.ts and ensure
primitives are tested alongside null and arrays.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: c57d02fc-aa62-4873-9f16-bf49533a273f
📒 Files selected for processing (2)
apps/daemon/__tests__/unit/http-server-factory.test.tsapps/daemon/src/http-server-factory.ts
Summary
400fornulland array payloads instead of lettingnullrequests hang before reaching the handlerVerification
pnpm -F @accomplish/daemon test:unit -- http-server-factorypnpm -F @accomplish/daemon typecheckpnpm exec prettier --check apps/daemon/src/http-server-factory.ts apps/daemon/__tests__/unit/http-server-factory.test.tspnpm lint:eslint(passes with existing upstream warnings inapps/web/src/client/pages/execution/useExecutionEffects.ts, addressed separately in fix(web): simplify execution effect dependencies #981)pnpm format:checkSummary by CodeRabbit
Bug Fixes
Tests