Skip to content

JSON-RPC parser accepts primitive params instead of returning InvalidParams #179

Description

@ikenyal

Which package(s) are affected?

@aws-blocks/core

Describe the bug

parseRpcRequest() accepts primitive JSON-RPC params values even though JSON-RPC 2.0 requires params, when present, to be a structured value (an array or object).

The parser currently uses:

args: Array.isArray(parsed.params)
  ? parsed.params
  : Object.values(parsed.params ?? {}),

As a result:

  • params: "abc" is accepted as args: ["a", "b", "c"]
  • params: 42 is accepted as args: []
  • params: true is accepted as args: []
  • params: null is accepted as args: []

This can turn a malformed request from a hand-written HTTP or generated native client into a valid method invocation with unexpected arguments. RpcErrorCode.InvalidParams (-32602) is already defined, but no parser path currently emits it.

Expected behavior

  • Array params remain positional arguments.
  • Object params remain named arguments, using the existing value-order behavior.
  • Omitted params continue to mean no arguments.
  • Primitive or null params return a JSON-RPC error response with code -32602 and preserve the request id.

Reproduction steps

On current main (a1de3b8), call:

parseRpcRequest(JSON.stringify({
  jsonrpc: "2.0",
  method: "api.echo",
  params: "abc",
  id: 1,
}));

Actual result:

{
  "ok": true,
  "request": {
    "apiNamespace": "api",
    "method": "echo",
    "args": ["a", "b", "c"],
    "id": 1
  }
}

The same request with params: 42, true, or null is also accepted with an empty args array.

Suggested fix

Validate params before converting it to arguments. If the property is present, require an array or non-null object; otherwise return RpcErrorCode.InvalidParams with a structured InvalidParams error name. Add focused unit tests covering accepted array/object/omitted values and rejected primitive/null values.

Additional context

I searched existing open and closed issues and pull requests for InvalidParams, parseRpcRequest, and JSON-RPC parameter validation and did not find an existing report or implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions