Skip to content

fix(taskstate): reject unknown task state values on unmarshal and REST status param - #404

Merged
nahapetyan-serob merged 1 commit into
a2aproject:mainfrom
ez-lbz:fix/taskstate-enum-validation
Sep 30, 2026
Merged

nahapetyan-serob merged 1 commit into
a2aproject:mainfrom
ez-lbz:fix/taskstate-enum-validation

Conversation

@ez-lbz

@ez-lbz ez-lbz commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

What changed

1. Reject unknown TaskState values on unmarshal

Problem:

TaskState.UnmarshalJSON accepted any string as an opaque enum value, so malformed or unknown states (e.g. "BOGUS_STATE", lowercase values) passed validation silently.

Fix (a2a/core.go, a2a/json_test.go):

  • Restrict unmarshal to the known enum values (plus TASK_STATE_UNSPECIFIED); anything else returns invalid task state %q.
  • Added TestTaskStateUnmarshalJSON covering all valid states and invalid inputs.

2. Validate the REST ListTasks status query parameter

Problem:

handleListTasks built the request from a2a.TaskState(query.Get("status")) with no validation, so an invalid status reached the handler instead of producing a client error.

Fix (a2asrv/rest.go, a2asrv/rest_test.go):

  • Added parseTaskStateOpt, which reuses TaskState's own validation (quoted unmarshal) and rejects unknown values with HTTP 400.
  • Added TestREST_ListTasks_InvalidStatus; updated TestREST_ListTasks_Success to use TASK_STATE_WORKING instead of the previously silently-accepted running.

Testing

  • Full suite passes (go test ./a2asrv/... ./internal/... ./a2agrpc/... ./a2apb/... ./a2acompat/... ./a2a/ ./a2aclient/...).
  • New TestTaskStateUnmarshalJSON and TestREST_ListTasks_InvalidStatus pass.

Behavior change: unknown task state values are now rejected during JSON unmarshaling and in the REST status query parameter.

BEGIN_COMMIT_OVERRIDE
fix(taskstate): reject unknown task state values on unmarshal and REST status param (#404)
END_COMMIT_OVERRIDE

@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 tracking this down.

Comment thread a2a/core.go Resolved
Comment thread a2a/json_test.go Outdated
Comment on lines +371 to +373
// TestTaskStateUnmarshalJSON is a regression test for task state validation: unknown task
// state values must be rejected during JSON unmarshaling instead of being
// silently accepted as an opaque enum value.

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.

After the changes, we would need to add couple of cases where current invalid cases just default to TaskStateUnspecified. The test speaks pretty clear for itself, so no comment needed.

Comment thread a2asrv/rest.go Outdated
Comment on lines +577 to +579
if val == "" {
return a2a.TaskStateUnspecified, nil
}

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.

This check will become redundant, as Unmarshal needs to default "" to TaskStateUnspecified. Comment bellow is redundant.

Comment thread a2asrv/rest_test.go Outdated
// TestREST_ListTasks_InvalidStatus is a regression test for task state validation: an
// invalid status query parameter must be rejected with HTTP 400 instead of
// being silently passed through as an opaque TaskState.
func TestREST_ListTasks_InvalidStatus(t *testing.T) {

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.

this test won't be necessary as invalid status should be defaulted to TaskStateUnspecified

@ez-lbz
ez-lbz force-pushed the fix/taskstate-enum-validation branch from 3b6fc34 to 829db31 Compare September 26, 2026 08:20
@ez-lbz

ez-lbz commented Sep 26, 2026

Copy link
Copy Markdown
Contributor Author

Reworked to your reading of §4.1.3 — unknown values now map to TaskStateUnspecified instead of being rejected. Pushed as 829db31, rebased onto current main (the dirty state is gone).

Point by point:

  • a2a/core.go — TaskStateFromString holds the mapping: every known token resolves to its state, everything else (empty string, unknown token, lowercase, truncated) resolves to TaskStateUnspecified. UnmarshalJSON delegates to it.
  • a2a/json_test.go — the invalid cases are now table entries expecting TaskStateUnspecified rather than errors, alongside all eight known tokens.
  • a2asrv/rest.go — you were right that the separate check was redundant, but the parameter was reaching the handler through a bare a2a.TaskState(...) cast that never went through decoding at all, so dropping the check alone would have left unknown values on the request. It now goes through TaskStateFromString, the same mapping as UnmarshalJSON, and the parse helper is gone.
  • a2asrv/rest_test.go — the old rejection test is replaced by one asserting the status parameter arrives as TaskStateUnspecified for unknown input and still resolves for known input.

go test ./... passes (31/31 packages), plus -race -count=1 -shuffle=on on the touched packages. The only golangci-lint finding is a pre-existing modernize note in a2asrv/jsonrpc.go, which this change does not touch.

Comment thread a2a/json_test.go Outdated
{name: "working", json: `"TASK_STATE_WORKING"`, want: TaskStateWorking},

{name: "empty string", json: `""`, want: TaskStateUnspecified},
{name: "empty string as unspecified", json: `"TASK_STATE_UNSPECIFIED"`, want: TaskStateUnspecified},

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.

let's rename to just unspecified

Comment thread a2asrv/rest_test.go Outdated
}
}

func TestREST_ListTasks_UnknownStatusDefaultsToUnspecified(t *testing.T) {

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.

let's rename to TestREST_ListTasks_StatusMapping as it doesn't only cover the Unknown case.

Comment thread a2asrv/rest_test.go Resolved
`TaskState.UnmarshalJSON` accepted any string as an opaque enum value, so an
unknown token survived decoding and reached the handler as a state outside
any known set. The REST `status` query parameter had the same gap: it was
converted with a bare `a2a.TaskState(...)` cast that bypassed decoding
entirely.

Per review, unknown values now map to `TaskStateUnspecified` rather than
being rejected, which is what the spec defines that value for: "the task is
in an unknown or indeterminate state" (§4.1.3). `TaskStateFromString` holds
the mapping and is used by both `UnmarshalJSON` and the REST handler, so a
known token resolves to its state and everything else — including the empty
string and lowercase variants — resolves to `TaskStateUnspecified`.

This also makes the earlier REST-side validation redundant: the handler no
longer needs its own parse step, since the status parameter goes through the
same mapping.

Tests: table-driven coverage for every known token plus empty, unknown,
lowercase, and truncated values, and a REST test asserting the status
parameter reaches the handler as `TaskStateUnspecified` for unknown input
while known input still resolves.

Signed-off-by: meraklbz <[email protected]>
@ez-lbz
ez-lbz force-pushed the fix/taskstate-enum-validation branch from 829db31 to 9c8df98 Compare September 30, 2026 02:19
@ez-lbz

ez-lbz commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

All three are in as 9c8df98.

  • a2a/json_test.go — the case at :541 was labelled "empty string as unspecified" but its json was "TASK_STATE_UNSPECIFIED", byte-identical to the unspecified case at :530, so it was a duplicate rather than a misnamed case. I dropped it instead of renaming, which leaves "TASK_STATE_UNSPECIFIED" covered once, under unspecified, as you asked. Also aligned the sibling failure message to the same convention (error = %v, want nil).
  • a2asrv/rest_test.go — renamed to TestREST_ListTasks_StatusMapping.
  • Failure messages — both now use the X() = %v, want %v form: status = %d, want %d. body: %s and capturedReq = nil, want non-nil.

gofmt, go vet, and go test ./... are clean (31/31 packages), plus -race -count=1 -shuffle=on on a2a and a2asrv.

@nahapetyan-serob
nahapetyan-serob merged commit 534a60f into a2aproject:main Sep 30, 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.

2 participants