Skip to content

fix(agent): Add robust string coercion for numeric literals in transaction payloads#23659

Open
HarshalPatel1972 wants to merge 1 commit into
hashicorp:mainfrom
HarshalPatel1972:fix/kv-string-coercion
Open

fix(agent): Add robust string coercion for numeric literals in transaction payloads#23659
HarshalPatel1972 wants to merge 1 commit into
hashicorp:mainfrom
HarshalPatel1972:fix/kv-string-coercion

Conversation

@HarshalPatel1972

Copy link
Copy Markdown

Description

When API consumers submit transactions to the Consul KV endpoints, fields like tokens and IDs are structurally expected to be strings. However, if a client inadvertently maps data dynamically and passes an unquoted numerical literal (e.g., {"Token": 999888} instead of "999888"), the default encoding/json library unmarshals the numeric literal as a float64. This mismatch causes downstream string type-assertions to fail, potentially dropping the data before it crosses the RPC serialization boundaries.

This PR introduces a defensive UnmarshalJSON safety hook in the HTTP transaction handlers to gracefully intercept and coerce these numerical literals back into valid string blocks.

Changes Made

  • Architected a localized wrapper alias (TxnOp) around api.TxnOp in agent/txn_endpoint.go to intercept JSON mappings.
  • Implemented a custom UnmarshalJSON hook using the json.RawMessage pattern to safely read dynamic types on the Token field and correctly coerce float64 values into strings.
  • Added a targeted deserialization regression test (TestTxnEndpoint_StringCoercion) in agent/txn_endpoint_test.go to assert the boundary's safety against unquoted numerals.

Testing

@HarshalPatel1972 HarshalPatel1972 requested a review from a team as a code owner June 12, 2026 20:42
Copilot AI review requested due to automatic review settings June 12, 2026 20:42
@HarshalPatel1972 HarshalPatel1972 requested a review from a team as a code owner June 12, 2026 20:42
@hashicorp-cla-app

hashicorp-cla-app Bot commented Jun 12, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@hashicorp-cla-app

Copy link
Copy Markdown

CLA assistant check

Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement

Learn more about why HashiCorp requires a CLA and what the CLA includes

Have you signed the CLA already but the status is still pending? Recheck it.

Copilot AI 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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds robust JSON unmarshaling to the txn endpoint so that Token values provided as numeric literals can be coerced into strings without failing request parsing.

Changes:

  • Introduced a local TxnOp wrapper type with a custom UnmarshalJSON to coerce Token from number → string.
  • Switched txn op decoding in convertOps from api.TxnOps to the new local TxnOps type.
  • Added a regression test that submits a numeric Token in the raw JSON payload and asserts the endpoint still processes the transaction.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
agent/txn_endpoint.go Adds a wrapper txn op type with custom JSON unmarshaling and updates endpoint decoding to use it.
agent/txn_endpoint_test.go Adds a test covering numeric-to-string coercion for the Token field in txn payloads.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread agent/txn_endpoint.go
Comment on lines +68 to +73
// TxnOp wraps api.TxnOp to implement robust JSON unmarshaling
// with dynamic string coercion for tokens that might slip in as numeric literals.
type TxnOp struct {
api.TxnOp
Token string `json:"Token,omitempty"`
}
Comment thread agent/txn_endpoint.go Outdated
Comment thread agent/txn_endpoint.go
Comment thread agent/txn_endpoint_test.go Outdated
payload := `[{"KV": {"Verb": "set", "Key": "foo/bar", "Value": "dGVzdA=="}, "Token": 999888}]`

buf := bytes.NewBuffer([]byte(payload))
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
@HarshalPatel1972 HarshalPatel1972 force-pushed the fix/kv-string-coercion branch from f3065fd to 1a1fc8f Compare June 12, 2026 20:52
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