Skip to content

fix(core): return Invalid Request for a null JSON-RPC body - #1143

Merged
xyos merged 1 commit into
aws:mainfrom
xyos:fix/core-rpc-null-body
Nov 6, 2026
Merged

xyos merged 1 commit into
aws:mainfrom
xyos:fix/core-rpc-null-body

Conversation

@xyos

@xyos xyos commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Problem

parseRpcRequest in packages/core/src/rpc.ts reads parsed.id straight after JSON.parse. A request body of the JSON literal null parses to null, so this throws TypeError: Cannot read properties of null (reading 'id') outside any try block. The Lambda handler then rejects, which API Gateway turns into a 502. In the dev server the rejection comes from an async req.on('end') callback, where nothing handles it, so the process crashes.

Other non-object bodies (5, "x", [], true) already get a JSON-RPC Invalid Request (-32600) response. A null body should get the same.

Issue #, if available:

Changes

  • packages/core/src/rpc.ts: parseRpcRequest now checks for a null parse result before reading id and returns the existing Invalid Request error (data.name: 'InvalidRequest') with id null. Arrays and other primitives behave as before.
  • packages/core/src/rpc.test.ts: new test that parseRpcRequest('null') returns ok: false with code -32600 and id null.
  • packages/core/src/lambda-handler.test.ts: new test that a Lambda event with body: 'null' gets a 200 JSON-RPC -32600 error response instead of a rejection.
  • .changeset/core-rpc-null-body.md: patch changeset for @aws-blocks/core.

Validation

  • The new tests failed before the fix with TypeError: Cannot read properties of null (reading 'id') and pass after it.
  • npm run build, @aws-blocks/core npm test, npm run lint, npm run lint:deps: pass.
  • npm test (all workspaces) and npm run test:e2e:local from the repo root: pass.

Checklist

  • PR description included
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@xyos
xyos requested a review from a team as a code owner October 29, 2026 22:14
@changeset-bot

changeset-bot Bot commented Oct 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7e961e1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@aws-blocks/core Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@belaboe belaboe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM. Confirmed that null throws at the base and returns -32600 with id: null here. Optional follow-ups:

  1. The same crash class remains for a deeply nested id (e.g. {"jsonrpc":"2.0","method":"x","id":[[[...20k levels...]]]}, about 40 KB): errorResponse's JSON.stringify throws RangeError, giving a Lambda 502 or a dev-server crash. Keeping id only when it's a string, number or null (else null) fixes it and also stops echoing object or boolean ids (JSON-RPC sections 4/5). Fine as a separate PR.
  2. Consider a body: 'null' case in dev-server-rpc.test.ts.
  3. Nit: const id = parsed?.id ?? null; would let null reuse the existing Invalid Request message.

@xyos
xyos merged commit b7c262c into aws:main Nov 6, 2026
42 checks passed
harsh62 added a commit that referenced this pull request Nov 6, 2026
Brings in 14 commits from main, among them "fix(core): return Invalid
Request for a null JSON-RPC body" (#1143), "fix(core): reject non-JSON-RPC
response bodies with ApiError" (#1171), "feat(bb-logger,core): backend Lambda
log lines carry the invocation's requestId" (#1325), #1131, #1132, #1142,
#1167, #1173, #1178, #1214, #494 and CI changes (#946, #970, #971).

packages/core/src/client/index.test.ts and lambda-handler.test.ts: keep both
sides' tests (this branch's httpErrorStatus, namespace identity and per-call
fetch suites; main's non-JSON-RPC body and request id suites).

packages/core/src/api.ts: the httpErrorStatus docs now say what the client
does since #1171 when a Hosting error page replaces a 403 / 404: it rejects
with a bare ApiError carrying the HTTP status, instead of response.json()
throwing.

# Conflicts:
#	packages/core/src/client/index.test.ts
#	packages/core/src/lambda-handler.test.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants