Skip to content

txnbuild: validate payload length in contract address decoding - #5943

Merged
tamirms merged 1 commit into
stellar:mainfrom
tamirms:txnbuild-validate-contract-length
May 16, 2026
Merged

tamirms merged 1 commit into
stellar:mainfrom
tamirms:txnbuild-validate-contract-length

Conversation

@tamirms

@tamirms tamirms commented May 15, 2026

Copy link
Copy Markdown
Contributor

Summary

  • 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 an explicit 32-byte length check in NewPaymentToContract and NewAssetBalanceRestoration to reject malformed contract addresses early.
  • Matches the pattern already in place in keypair/from_address.go:27 and xdr/account_id.go:110.

Test plan

  • go test ./txnbuild/ passes
  • Extended TestPaymentToContract to cover an oversized destination payload
  • Extended TestRestoreAssetBalance to cover an oversized contract payload

🤖 Generated with Claude Code

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]>
Copilot AI review requested due to automatic review settings May 15, 2026 20:30

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

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) == 32 checks after strkey.Decode(strkey.VersionByteContract, ...) in NewPaymentToContract and NewAssetBalanceRestoration.
  • 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.

@tamirms
tamirms enabled auto-merge (squash) May 16, 2026 06:07
@tamirms
tamirms merged commit a89c554 into stellar:main May 16, 2026
15 of 19 checks passed
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.

3 participants