diff --git a/modules/abstract-eth/src/abstractEthLikeNewCoins.ts b/modules/abstract-eth/src/abstractEthLikeNewCoins.ts index b07629af0f..58b226c17f 100644 --- a/modules/abstract-eth/src/abstractEthLikeNewCoins.ts +++ b/modules/abstract-eth/src/abstractEthLikeNewCoins.ts @@ -94,6 +94,7 @@ import { decodeUnwrapCalldata, decodeWrapCalldata, ERC1155TransferBuilder, + EthTransactionData, ERC721TransferBuilder, getBufferedByteCode, getCommon, @@ -592,6 +593,9 @@ export const optionalDeps = { /** Intent types other than payments that verifyTssTransaction already verifies. */ const TSS_DEDICATED_INTENT_TYPES = new Set(['defiApprove', 'defiDeposit', 'defiWithdraw', 'wrapApprove']); +/** Contract-call / custom-tx intent spellings verified against a single declared recipient. */ +const TSS_CONTRACT_CALL_INTENT_TYPES = new Set(['contractCall', 'contract-call', 'customTx', 'custom-tx']); + type TssDecodedArg = ReturnType[number]; const TSS_ERC20_TRANSFER_TYPES = ['address', 'uint256']; @@ -3641,6 +3645,27 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin implem return { isToken: true, contract: contract.toLowerCase() }; } + /** + * Decode raw serialized EVM transaction bytes directly via `EthTransactionData.fromSerialized`, + * bypassing `TransactionBuilder.from()` / `.build()` reserialization so calldata bytes cannot be + * altered or normalized before verification. + */ + private decodeRawTssTxFields(txHex: string | undefined): { to: string; value: string; data: string } { + if (!txHex) { + throw new Error('missing txHex in txPrebuild'); + } + const common = AbstractEthLikeNewCoins.getCustomChainCommon(this.getChainId()); + if (!this.staticsCoin?.features.includes(CoinFeature.EIP1559)) { + common.setHardfork(optionalDeps.EthCommon.Hardfork.Petersburg); + } + const txJson = EthTransactionData.fromSerialized(txHex, common).toJson(); + return { + to: (txJson.to ?? '').toLowerCase(), + value: txJson.value ?? '0', + data: (txJson.data || '0x').toLowerCase(), + }; + } + /** * Verify a single-recipient payment: explicit calldata must match exactly; otherwise the bytes * must be a plain native send or a canonical ERC-20 transfer of the declared token. @@ -3815,6 +3840,48 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin implem } } + /** + * Verify a `contractCall` / `customTx` (including kebab-case aliases) intent against the decoded + * transaction. Fails closed unless exactly one recipient is declared and decoded `to`, `value` + * (default 0) and `data` match the declared address, amount and calldata exactly. + */ + private async verifyTssContractCall( + txType: string, + recipients: ITransactionRecipient[], + txHex: string | undefined, + throwRecipientMismatch: (message: string, mismatchedRecipients: Recipient[]) => Promise + ): Promise { + if (recipients.length === 0) { + await throwRecipientMismatch(`${txType} transaction requires a declared recipient to verify against`, []); + } + if (recipients.length > 1) { + await throwRecipientMismatch( + `${txType} transaction must declare a single recipient, got ${recipients.length}`, + [] + ); + } + + const recipient = recipients[0]; + const { to, value, data } = this.decodeRawTssTxFields(txHex); + const mismatch = (message: string, address = to, amount = value): Promise => + throwRecipientMismatch(message, [{ address, amount }]); + + if (!recipient.address || to !== recipient.address.toLowerCase()) { + await mismatch('destination address does not match with the recipient address'); + } + const expectedAmount = + recipient.amount === undefined || recipient.amount === null || recipient.amount === '' ? '0' : recipient.amount; + if (!tssValuesEqual(value, expectedAmount)) { + await mismatch('the transaction amount in txPrebuild does not match the value given by client'); + } + const expectedData = isEmptyTssCalldata(recipient.data) + ? '0x' + : addHexPrefix(recipient.data as string).toLowerCase(); + if (data !== expectedData) { + await mismatch('transaction calldata does not match the calldata given by client'); + } + } + /** * @param {VerifyEthTransactionOptions} params * @param {TransactionParams} params.txParams - params object passed to send @@ -3846,7 +3913,10 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin implem txPrebuild?.consolidateId || txParams.stakingRequestId || txParams.prebuildTx?.stakingRequestId || - (txParams.type && NO_RECIPIENT_TX_TYPES.has(txParams.type)) + (txParams.type && NO_RECIPIENT_TX_TYPES.has(txParams.type)) || + // The kebab-case aliases of contractCall/customTx are not in NO_RECIPIENT_TX_TYPES, but they + // are verified fail-closed below, so they must not trip this generic guard either. + (txParams.type && TSS_CONTRACT_CALL_INTENT_TYPES.has(txParams.type)) ) ) { throw new Error('missing txParams'); @@ -3871,6 +3941,8 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin implem } else { await this.verifyTssBatchPayment(txParams, recipients, txPrebuild.txHex, throwRecipientMismatch); } + } else if (txType && TSS_CONTRACT_CALL_INTENT_TYPES.has(txType)) { + await this.verifyTssContractCall(txType, txParams.recipients ?? [], txPrebuild.txHex, throwRecipientMismatch); } else if (txType && !TSS_DEDICATED_INTENT_TYPES.has(txType) && !params.verification?.consolidationToBaseAddress) { this.warnTssVerificationIncomplete( txType, diff --git a/modules/sdk-coin-eth/test/unit/eth.ts b/modules/sdk-coin-eth/test/unit/eth.ts index 9eeb9106d3..2df905b861 100644 --- a/modules/sdk-coin-eth/test/unit/eth.ts +++ b/modules/sdk-coin-eth/test/unit/eth.ts @@ -1626,7 +1626,7 @@ describe('ETH:', function () { }); }; - ['tokenApproval', 'customTx', 'defiApprove'].forEach((type) => { + ['tokenApproval', 'defiApprove'].forEach((type) => { it(`should reject approve(wrapper, amount) on the underlying relabelled as ${type}`, async function () { await expectDefiRejection( verifyAs(type, defiCalldata('approve(address,uint256)', [wrapper, amount])), @@ -1635,23 +1635,44 @@ describe('ETH:', function () { }); }); - it('should reject a mutated-amount wrapper approve relabelled as customTx', async function () { + it('should still reject approve(wrapper, amount) relabelled as customTx, with or without a declared call', async function () { + // customTx is now verified by the contract-call handler before the wrapper guard runs; either check + // may reject, but the relabelled wrapper approve must never sign. + const data = defiCalldata('approve(address,uint256)', [wrapper, amount]); + const coin = bitgo.coin('hteth') as Hteth; + const wallet = new Wallet(bitgo, coin, { coinSpecific: { baseAddress: address1 } }); + const txHex = await buildDefiTxHex(data, underlying); + for (const recipients of [undefined, [{ address: underlying, amount: '0', data }]]) { + await expectDefiRejection( + coin.verifyTransaction({ + txParams: { type: 'customTx', recipients, wallet } as never, + txPrebuild: { txHex, coin: 'hteth', walletId: 'w' } as never, + wallet, + verification: {}, + walletType: 'tss', + }), + recipients ? /must be declared as a wrapApprove intent/ : /requires a declared recipient/ + ); + } + }); + + it('should reject a mutated-amount wrapper approve relabelled as transferToken', async function () { await expectDefiRejection( - verifyAs('customTx', defiCalldata('approve(address,uint256)', [wrapper, '999999999999'])), + verifyAs('transferToken', defiCalldata('approve(address,uint256)', [wrapper, '999999999999'])), /must be declared as a wrapApprove intent/ ); }); it('should reject a wrapper approve with trailing bytes (an ERC-20 ignores them)', async function () { await expectDefiRejection( - verifyAs('customTx', defiCalldata('approve(address,uint256)', [wrapper, amount]) + '00'), + verifyAs('transferToken', defiCalldata('approve(address,uint256)', [wrapper, amount]) + '00'), /must be declared as a wrapApprove intent/ ); }); it('should not affect approvals of the underlying to other spenders', async function () { - (await verifyAs('customTx', defiCalldata('approve(address,uint256)', [address2, amount]))).should.equal(true); - (await verifyAs('tokenApproval', defiCalldata('approve(address,uint256)', [address2, amount]))).should.equal( + // tokenApproval and customTx have their own verifiers; an intent type with no verifier still passes untouched. + (await verifyAs('transferToken', defiCalldata('approve(address,uint256)', [address2, amount]))).should.equal( true ); }); @@ -1668,7 +1689,7 @@ describe('ETH:', function () { ); try { await coin.verifyTransaction({ - txParams: { type: 'customTx', wallet } as any, + txParams: { type: 'transferToken', wallet } as never, txPrebuild: { txHex, coin: 'hteth', walletId: 'w' } as any, wallet, verification: {}, @@ -1688,11 +1709,10 @@ describe('ETH:', function () { // Every type below skips the wrapApprove check and reaches the guard; none of these txs is an approve of a // wrapper, so all must verify exactly as they did before the guard existed. const benign: Array<[string, string, string]> = [ - ['customTx', defiCalldata('transfer(address,uint256)', [address2, amount]), underlying], - ['customTx', defiCalldata('approve(address,uint256)', [address2, amount]), underlying], - ['customTx', defiCalldata('approve(address,uint256)', [wrapper, amount]), address2], - ['tokenApproval', defiCalldata('approve(address,uint256)', [address2, amount]), underlying], - ['tokenApproval', defiCalldata('approve(address,uint256)', [wrapper, amount]), address2], + // tokenApproval and customTx are verified by their own handlers and are not part of this list. + ['transferToken', defiCalldata('transfer(address,uint256)', [address2, amount]), underlying], + ['transferToken', defiCalldata('approve(address,uint256)', [address2, amount]), underlying], + ['transferToken', defiCalldata('approve(address,uint256)', [wrapper, amount]), address2], ['acceleration', defiCalldata('transfer(address,uint256)', [address2, amount]), address2], ['fillNonce', '0x', address2], ['enabletoken', defiCalldata('transfer(address,uint256)', [address2, amount]), address2], @@ -1703,9 +1723,11 @@ describe('ETH:', function () { }); it('should not affect transfers to the underlying or approves of the wrapper on other contracts', async function () { - (await verifyAs('customTx', defiCalldata('transfer(address,uint256)', [wrapper, amount]))).should.equal(true); + (await verifyAs('transferToken', defiCalldata('transfer(address,uint256)', [wrapper, amount]))).should.equal( + true + ); ( - await verifyAs('customTx', defiCalldata('approve(address,uint256)', [wrapper, amount]), address2) + await verifyAs('transferToken', defiCalldata('approve(address,uint256)', [wrapper, amount]), address2) ).should.equal(true); }); diff --git a/modules/sdk-coin-eth/test/unit/tssPaymentVerification.ts b/modules/sdk-coin-eth/test/unit/tssPaymentVerification.ts index 9f0c05234d..90c8f77458 100644 --- a/modules/sdk-coin-eth/test/unit/tssPaymentVerification.ts +++ b/modules/sdk-coin-eth/test/unit/tssPaymentVerification.ts @@ -325,13 +325,102 @@ describe('TSS payment verification', function () { }); }); - describe('other intents', function () { - it('warns for contractCall instead of claiming it was verified', async function () { - const txHex = await buildTx({ to: recipient, value: '1' }); - (await verify({ type: 'contractCall', recipients: [] }, txHex)).should.be.true(); - warned(/contractCall: no verifier is implemented/).should.be.true(); + // CECHO-2680 / INFOSEC-671: contractCall and customTx (both spellings) are verified against a + // single declared recipient instead of returning true unchecked. + describe('contractCall / customTx verification', function () { + const callData = calldata('deposit', ['uint256', 'address'], ['5', baseAddress]); + + for (const type of ['contractCall', 'contract-call', 'customTx', 'custom-tx']) { + it(`verifies a matching ${type} call`, async function () { + const txHex = await buildTx({ to: otherRecipient, value: '0', data: callData }); + const txParams = { type, recipients: [{ address: otherRecipient, amount: '0', data: callData }] }; + (await verify(txParams, txHex)).should.be.true(); + warn.called.should.be.false(); + }); + + it(`rejects a ${type} call to a different destination`, async function () { + const txHex = await buildTx({ to: recipient, value: '0', data: callData }); + const txParams = { type, recipients: [{ address: otherRecipient, amount: '0', data: callData }] }; + await expectMismatch(verify(txParams, txHex), /destination address does not match/); + }); + + it(`rejects a ${type} call with a different native value`, async function () { + const txHex = await buildTx({ to: otherRecipient, value: '1', data: callData }); + const txParams = { type, recipients: [{ address: otherRecipient, amount: '0', data: callData }] }; + await expectMismatch(verify(txParams, txHex), /amount in txPrebuild does not match/); + }); + + it(`rejects a ${type} call with different calldata`, async function () { + const txHex = await buildTx({ to: otherRecipient, value: '0', data: callData }); + const declared = calldata('deposit', ['uint256', 'address'], ['6', baseAddress]); + const txParams = { type, recipients: [{ address: otherRecipient, amount: '0', data: declared }] }; + await expectMismatch(verify(txParams, txHex), /calldata does not match/); + }); + + it(`rejects a ${type} with no recipients`, async function () { + const txHex = await buildTx({ to: otherRecipient, value: '1' }); + await expectMismatch(verify({ type }, txHex), /requires a declared recipient/); + await expectMismatch(verify({ type, recipients: [] }, txHex), /requires a declared recipient/); + }); + + it(`rejects a ${type} with more than one recipient`, async function () { + const txHex = await buildTx({ to: otherRecipient, value: '0', data: callData }); + const txParams = { + type, + recipients: [ + { address: otherRecipient, amount: '0', data: callData }, + { address: recipient, amount: '0', data: callData }, + ], + }; + await expectMismatch(verify(txParams, txHex), /must declare a single recipient/); + }); + } + + it('defaults a contractCall value to 0 and empty calldata when the intent declares neither', async function () { + const txHex = await buildTx({ to: otherRecipient, value: '0' }); + (await verify({ type: 'contractCall', recipients: [{ address: otherRecipient }] }, txHex)).should.be.true(); }); + it('rejects a contractCall whose tx carries value the intent did not declare', async function () { + const txHex = await buildTx({ to: otherRecipient, value: '1' }); + await expectMismatch( + verify({ type: 'contractCall', recipients: [{ address: otherRecipient }] }, txHex), + /amount in txPrebuild does not match/ + ); + }); + + // The builder drops calldata shorter than a 4-byte selector when it reserializes, so the check + // must read the original signed bytes, not a rebuilt copy. + it('rejects a contractCall whose signed bytes carry sub-selector calldata the intent did not declare', async function () { + const common = AbstractEthLikeNewCoins.getCustomChainCommon(coin.getChainId()); + const tx = FeeMarketEIP1559Transaction.fromTxData( + { + nonce: 1, + maxPriorityFeePerGas: 10, + maxFeePerGas: 100, + gasLimit: 100000, + to: otherRecipient, + value: 0, + data: '0x01', + chainId: coin.getChainId(), + }, + { common } + ); + const txHex = '0x' + tx.serialize().toString('hex'); + await expectMismatch( + verify({ type: 'contractCall', recipients: [{ address: otherRecipient, amount: '0' }] }, txHex), + /calldata does not match/ + ); + ( + await verify( + { type: 'contractCall', recipients: [{ address: otherRecipient, amount: '0', data: '0x01' }] }, + txHex + ) + ).should.be.true(); + }); + }); + + describe('other intents', function () { it('warns for an intent type with no verifier', async function () { const txHex = await buildTx({ to: recipient, value: '1' }); (await verify({ type: 'acceleration' }, txHex)).should.be.true();