txnbuild: validate payload length in contract address decoding - #5943
Merged
Merged
Conversation
strkey.Decode() accepts payloads of any length with valid checksums. Without an explicit length check at the call site, the copy(...) into the fixed-size xdr.ContractId / xdr.Hash truncates oversized payloads to their first 32 bytes. Add explicit 32-byte length check in NewPaymentToContract and NewAssetBalanceRestoration to reject malformed contract addresses early, matching the pattern already in place in keypair/from_address.go and xdr/account_id.go. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens txnbuild contract-address handling by ensuring contract IDs decoded from strkey have the expected 32-byte payload length, preventing silent truncation when copying into fixed-size XDR hash/contract-id types.
Changes:
- Add explicit
len(decoded) == 32checks afterstrkey.Decode(strkey.VersionByteContract, ...)inNewPaymentToContractandNewAssetBalanceRestoration. - Return clear errors when the decoded payload length is not 32 bytes.
- Extend existing tests to cover oversized (checksum-valid) contract-address payloads.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
txnbuild/restore_footprint.go |
Rejects contract addresses whose decoded payload is not exactly 32 bytes before copying into xdr.Hash. |
txnbuild/restore_footprint_test.go |
Adds a test case asserting oversized contract payloads are rejected with the new error. |
txnbuild/invoke_host_function.go |
Rejects destination contract addresses whose decoded payload is not exactly 32 bytes before copying into xdr.ContractId. |
txnbuild/invoke_host_function_test.go |
Adds a test case asserting oversized destination payloads are rejected with the new error. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
karthikiyer56
approved these changes
May 15, 2026
tamirms
enabled auto-merge (squash)
May 16, 2026 06:07
urvisavla
pushed a commit
that referenced
this pull request
May 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
strkey.Decode()accepts payloads of any length with valid checksums. Without an explicit length check at the call site, thecopy(...)into the fixed-sizexdr.ContractId/xdr.Hashtruncates oversized payloads to their first 32 bytes.NewPaymentToContractandNewAssetBalanceRestorationto reject malformed contract addresses early.keypair/from_address.go:27andxdr/account_id.go:110.Test plan
go test ./txnbuild/passesTestPaymentToContractto cover an oversized destination payloadTestRestoreAssetBalanceto cover an oversized contract payload🤖 Generated with Claude Code