Skip to content

fix(sdk-coin-trx): fail closed on unvalidated contract types in verifyTransaction - #10170

Merged
harman-bitgo merged 1 commit into
masterfrom
CHALO-2126
Nov 12, 2026
Merged

harman-bitgo merged 1 commit into
masterfrom
CHALO-2126

Conversation

@harman-bitgo

Copy link
Copy Markdown
Contributor

Summary

Fixes CHALO-2126: Trx.verifyTransaction had an inverted allowlist — it validated only TransferContract (and TriggerSmartContract on TSS) but silently returned true for every other contract type, including dangerous ones like AccountPermissionUpdateContract.

Changes

  • TSS path: Replace return true fall-through with explicit owner_address validation for all 9 known non-transfer contract types (staking, voting, resource delegation, account setup, permission update), and throw on unknown types
  • Multisig path: Add TriggerSmartContract validation (was completely unverified), add owner_address validation for the same 9 contract types, and throw on unknown types
  • New validation methods: validateDecodedContractOwnerAddress (TSS/protobuf path) and validateMultisigContractOwnerAddress (multisig/JSON path) verify the contract's owner_address matches the wallet's base address

Impact analysis

Queried Snowflake production data for the last 12 months — all TRX transaction types in production are covered by the BitGoJS ContractType enum. No unknown types exist, so the fail-closed behavior will not break any existing flows.

Contract Type 12-month Count Validation
TransferContract 895,384 recipient + amount (existing)
TriggerSmartContract 684,064 recipient + amount (existing TSS, new multisig)
DelegateResourceContract 227,191 new: owner_address
UnDelegateResourceContract 227,030 new: owner_address
AccountPermissionUpdateContract 266 new: owner_address
AccountCreateContract 165 new: owner_address
FreezeBalanceV2Contract 50 new: owner_address
VoteWitnessContract 14 new: owner_address
WithdrawBalanceContract 14 new: owner_address
UnfreezeBalanceV2Contract 12 new: owner_address
WithdrawExpireUnfreezeContract 9 new: owner_address

Test plan

  • 51 verifyTransaction unit tests pass (14 new for owner_address validation, 3 new for multisig path)
  • All 174 sdk-coin-trx unit tests pass
  • Verify no regressions on TRX transfer, TRC20 transfer, staking, and wallet init flows in staging

TICKET: CHALO-2126

🤖 Generated with Claude Code

@harman-bitgo
harman-bitgo requested a review from a team as a code owner November 12, 2026 06:07

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@linear-code

linear-code Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

CHALO-2126

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Compatibility checks are failing on Node.js 27-nightly (experimental, non-blocking). This does not block merge, but it signals an incompatibility to fix before this release becomes supported.

View run

@harman-bitgo

Copy link
Copy Markdown
Contributor Author

@claude review

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @harman-bitgo's task in 43s —— View job


Review of #10170 (fail closed on unvalidated contract types)

  • Read the diff and surrounding code
  • Post review

I did not run the tests. This is a read-through only.

Summary: The approach is right. Replacing the return true fall-through with an allowlist plus a throw closes the CHALO-2126 hole. Extracting validateAddressMatchesWallet is a clean refactor, and the TSS decoding looks correct. decodeAccountPermissionUpdateContract returns a single object with ownerAddress, while the other decoders return arrays with parameter.value.owner_address, and the code handles both. I have a few concerns, mostly about the multisig path.

Concerns

  1. Multisig: JSON and raw_data_hex can disagree (trx.ts ~L466-510).

    • On the multisig path, contract.type and owner_address come from tx.toJson(), and the new TriggerSmartContract branch decodes raw_data_hex separately.
    • validateMultisigContractOwnerAddress therefore checks the JSON raw_data. The signature is over raw_data_hex/txID.
    • If the builder's from() doesn't confirm that the JSON raw_data matches raw_data_hex and txID, an attacker could pass a benign-looking JSON with a malicious raw_data_hex. This is the same weakness TransferContract already has, so it isn't new. Because this PR extends the same trust to 10 more contract types, I'd confirm the builder checks this, or decode raw_data_hex on the multisig path too.
  2. Missing wallet now hard-fails the previously permissive types.

    • validateAddressMatchesWallet throws missing wallet base address… when wallet is undefined or has no baseAddress/rootAddress.
    • This is correct for failing closed, but callers that omit wallet for staking, permission-update, or wallet-init flows will start failing. The ECDSA/MPCv2 paths pass this.wallet, which is fine.
    • Please confirm AccountCreate and AccountPermissionUpdate flows always pass a wallet with coinSpecific.baseAddress. The staging box in your test plan is still unchecked, so it's worth checking those flows there.
  3. Owner check is stricter than "belongs to this wallet."

    • It only accepts the wallet's base address. FreezeBalanceV2 and DelegateResourceContract also carry receiver_address, which isn't checked.
    • Delegating resources to an arbitrary receiver doesn't move funds, so this is probably acceptable. It's still worth a code comment saying it's intentional.
    • VoteWitness votes aren't validated either, and that is likewise by design.
  4. Error-message change is a public-behavior change.

    • The message consolidation destination address does not match wallet base address became consolidation destination does not match wallet base address (trxToken.ts test updated).
    • Any downstream code or tests that match on the old string will break. A repo-wide grep would confirm nothing else depends on it.
  5. Type safety.

    • (decodedTx.contract as any[]) and contract: any go against the repo's "avoid any" guideline.
    • Consider a minimal { parameter?: { value?: { owner_address?: string } } } type. Use unknown plus a type guard for the decoded contract.
  6. Minor.

    • The two allowlists (numeric enum in TSS, strings in multisig) can drift apart. A single Record<string, ContractType> mapping would keep them in sync.
    • Unsupported contract type for TSS verification: ${decodedTx.contractType} prints a number. Mapping it to the enum name would be more useful when debugging.
    • The multisig TriggerSmartContract branch on a native Trx instance now enforces the consolidation-destination rule when no recipients are given. That is intended per CHALO-2125, but it is a behavior change for multisig token flows and deserves a line in the PR description.

Tests

Coverage looks thorough: 14 new owner_address cases and 3 multisig cases, plus the unknown-type throw. I'd add two cases:

  • A multisig tx whose JSON owner_address differs from the owner in raw_data_hex, once point 1 is settled.
  • A wallet with rootAddress only, to cover that fallback.

The Node 27-nightly failure is marked experimental and non-blocking, and it looks unrelated to this change.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread modules/sdk-coin-trx/src/trx.ts Resolved
Comment thread modules/sdk-coin-trx/src/trx.ts Resolved
Comment thread modules/sdk-coin-trx/src/trx.ts Resolved
…yTransaction

Previously, verifyTransaction only validated TransferContract (and
TriggerSmartContract on TSS), letting all other contract types pass
with `return true`. This meant dangerous operations like
AccountPermissionUpdate could be signed without any verification.

Changes:
- TSS path: validate owner_address for all 9 non-transfer contract types,
  throw on unknown types instead of returning true
- Multisig path: add TriggerSmartContract recipient/amount validation
  (was completely unverified), validate owner_address for all 9 non-transfer
  contract types, throw on unknown types
- Refactor error messages: context-aware messages distinguish consolidation
  destination vs contract owner_address validation
- Extract shared allowlists (OWNER_VALIDATED_CONTRACT_TYPES and
  OWNER_VALIDATED_MULTISIG_CONTRACT_TYPES) to keep TSS/multisig in sync
- Replace `any` types with minimal typed interfaces
- Comprehensive test coverage: 65 verifyTransaction tests covering all
  contract types on both TSS and multisig paths, including rootAddress fallback

Note: only owner_address is validated for non-transfer contracts. Fields like
receiver_address (DelegateResource), votes (VoteWitness), or permission keys
(AccountPermissionUpdate) are not checked because txParams does not yet carry
structured intent for these operations. This is intentional and documented.

TICKET: CHALO-2126

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@harman-bitgo

Copy link
Copy Markdown
Contributor Author

Addressed all review feedback in the latest push (a6b637a):

  • any types removed — replaced with typed interfaces for contract parameter access
  • Shared allowlists — extracted OWNER_VALIDATED_CONTRACT_TYPES and OWNER_VALIDATED_MULTISIG_CONTRACT_TYPES as module-level constants to prevent drift
  • Error message prints enum name — now shows TypeName (number) instead of just the numeric value
  • Code comments added — documenting that only owner_address is validated and why (txParams lacks structured intent for non-transfer ops)
  • rootAddress fallback test — added test for wallets with only rootAddress
  • 188 tests passing (65 verifyTransaction + 123 others)

Pre-existing gaps (AccountPermissionUpdate key validation, multisig JSON-vs-hex contract field reconciliation) acknowledged in inline replies — both are follow-up scope.

@MohammedRyaan786 MohammedRyaan786 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.

lg

@harman-bitgo
harman-bitgo merged commit ce5509c into master Nov 12, 2026
25 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.

2 participants