Skip to content

fix: reject trailing data after the JSON body - #453

Merged
nahapetyan-serob merged 3 commits into
a2aproject:mainfrom
vbcherepanov:fix/json-trailing-data
Oct 12, 2026
Merged

nahapetyan-serob merged 3 commits into
a2aproject:mainfrom
vbcherepanov:fix/json-trailing-data

Conversation

@vbcherepanov

Copy link
Copy Markdown
Contributor

Fixes #446

json.NewDecoder(body).Decode(v) stops after the first JSON value, so a body like {...}TRAILING_GARBAGE or two concatenated objects was accepted and processed.

Changes:

  • internal/utils.DecodeJSON decodes one value and then requires EOF, the same rule as json.Unmarshal. Trailing whitespace is still accepted.
  • Used for request bodies in a2asrv (JSON-RPC and the three REST handlers that read a body) and for response bodies in a2aclient (JSON-RPC and REST), plus the same two paths in a2acompat/a2av0.
  • A server now answers such a request with -32700 Parse error (JSON-RPC) or 400 with a parse error (REST), as a2a-python and a2a-js do.

Tests:

  • internal/utils/json_test.go: single value, trailing whitespace, trailing garbage, concatenated objects, an extra }, empty and invalid input.
  • TestJSONRPC_Validations: trailing data and concatenated requests give a parse error, trailing whitespace still succeeds.
  • TestREST_InvalidPayloads: trailing data and concatenated payloads on message:send and pushNotificationConfigs.

The new handler cases fail on main and pass with the change. go test ./... passes.

@nahapetyan-serob nahapetyan-serob 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 for the fix. General comments here, I have also left a few inline comments.

  • FromRESTError in internal/rest/rest.go still decodes REST error bodies with json.NewDecoder(resp.Body).Decode
  • a2acrypto/cannonical.go uses the same pattern, as it has decoder option, we might add an inline check in this case.

Comment thread internal/utils/json_test.go Resolved
Comment thread internal/utils/json.go Resolved
Comment thread a2asrv/rest_test.go Resolved
Comment thread a2aclient/jsonrpc.go Resolved
Wrap the decoder error into ErrTrailingData so callers keep the original cause, and apply the same check to FromRESTError and agent card canonicalization, which still accepted trailing bytes. Add client transport tests (JSON-RPC, REST and both v0.3 compat transports) because clients now reject responses they used to accept, plus the /message:stream server case.
@vbcherepanov
vbcherepanov force-pushed the fix/json-trailing-data branch from 7f2a519 to e4c0a91 Compare October 10, 2026 08:01
@vbcherepanov

Copy link
Copy Markdown
Contributor Author

Thanks for the review, all points are addressed. The helper now wraps the decoder error (%w: %w) and the unit test uses io.EOF / io.ErrUnexpectedEOF with t.Parallel on the test and subtests. Added the /message:stream server case, and trailing-data tests for the JSON-RPC and REST client transports plus both v0.3 compat transports (whitespace accepted, garbage and concatenated bodies rejected). FromRESTError now uses the strict helper, and canonicalizeJSON keeps its UseNumber decoder but checks for trailing data via a small ExpectEOF helper, with tests for both. Rebased on main to pick up #448

Comment thread a2aclient/jsonrpc_test.go Resolved
Comment thread a2aclient/jsonrpc_test.go Resolved
Comment thread a2acompat/a2av0/jsonrpc_compat_test.go Resolved
Comment thread a2acrypto/sign_test.go Resolved
Comment thread internal/utils/json_test.go Resolved
Merge the success and failure error checks into a single errors.Is
(or wantErr bool) check, and compare the decoded task with cmp.Diff
against a full expected value instead of checking the ID alone, so a
partly decoded task fails the test.

Fold the trailing-whitespace case of the canonicalization test into
the table with per-row canonicalize and verify errors.

Decode JSON-RPC requests in the client test fakes with
utils.DecodeJSON, so the fakes reject trailing data the same way the
transports now do.
@vbcherepanov

Copy link
Copy Markdown
Contributor Author

Thanks for the review, all five points are addressed in the latest push.

  1. Merged the error checks into if !errors.Is(err, tc.wantErr) { ... }; if tc.wantErr != nil { return } in all four client transport tests (a2aclient JSON-RPC and REST, a2av0 JSON-RPC and REST) and in TestDecodeJSON. The a2av0 REST test keeps wantErr bool with (err != nil) != tc.wantErr, because that client decodes the body with protojson, which returns its own error type and does not wrap utils.ErrTrailingData.

  2. Each of those four tests now declares want at the top and checks the whole task with cmp.Diff(want, got) (go-cmp is already a module dependency), so a partly decoded task fails the test.

  3. Good catch. The production path already uses it: the a2av0 JSON-RPC client and server both call utils.DecodeJSON in this PR, and the a2av0 REST client reads the whole body and decodes it with protojson, which rejects trailing data on its own. Line 126 was only the test's fake server reading the incoming request, and I had copied the plain decoder from the existing mustDecodeJSONRPC helper in a2aclient. There was no reason to keep it, so both the a2av0 fake and mustDecodeJSONRPC now use utils.DecodeJSON. That also means every client test checks that the transports send exactly one JSON value.

  4. Folded the trailing whitespace row into the table with wantCanonicalizeErr and wantVerifyErr (both nil for that row), and removed the separate block after the loop.

  5. Merged in both TestDecodeJSON and TestExpectEOF. In TestExpectEOF it is now a single if err := ExpectEOF(dec); !errors.Is(err, tc.wantErr) check.

go vet ./..., go test -race ./... and golangci-lint v2.11.4 (same version as CI) are all green.

@nahapetyan-serob
nahapetyan-serob merged commit e85760e into a2aproject:main Oct 12, 2026
5 checks passed
nahapetyan-serob pushed a commit that referenced this pull request Oct 21, 2026
🤖 I have created a release *beep* *boop*
---


## [2.7.0](v2.6.0...v2.7.0)
(2026-10-21)


### Features

* add input mode validation
([#456](#456))
([8c814be](8c814be))
* extension point for snapshot materialization logic
([#449](#449))
([4899045](4899045))
* reject requests with unsupported A2A-Version
([#457](#457))
([4226e74](4226e74))
* use application/a2a+json with Accept-based negotiation
([#465](#465))
([a8caf47](a8caf47))


### Bug Fixes

* **a2a:** serialize task status timestamps in UTC
([#452](#452))
([b657f33](b657f33))
* decode application/a2a+json error responses in the REST client
([#448](#448))
([3e1aa7f](3e1aa7f))
* **errordetails:** handle nil Value in Typed.MarshalJSON
([#474](#474))
([73c49ac](73c49ac))
* **grpc:** forward next_page_token in push config list responses
([#455](#455))
([7210014](7210014))
* **grpc:** keep caller metadata when attaching service params
([#476](#476))
([f01e895](f01e895))
* honor a2a+json Content Type when Accept is */* or missing
([#468](#468))
([2169266](2169266))
* reject trailing data after the JSON body
([#453](#453))
([e85760e](e85760e))
* **rest:** map non-A2A HTTP error responses by status code
([#471](#471))
([4baba5f](4baba5f))
* return plain error when streaming fails before the first event
([#463](#463))
([cf62533](cf62533))
* **taskstate:** reject unknown task state values on unmarshal and REST
status param ([#404](#404))
([534a60f](534a60f))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
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.

[Bug]: HTTP body decoding silently ignores trailing characters / concatenated JSON

2 participants