Use a safe transfer helper library for ERC20 transfers
The JBERC20PaymentTerminal contract calls ERC20 transfer and transferFrom without checking return values, so tokens that return false instead of reverting may silently fail, and tokens without return values (like USDT) cause reverts. An attacker could receive project tokens without their payment actually succeeding, and the terminal cannot support popular nonstandard ERC20s.
Originally disclosed on Code4rena
Submitted by horsefacts, also found by 0x1f8b, 0x29A, 0x52, 0xf15ers, AlleyCat, apostle0x01, berndartmueller, cccz, Ch_301, Chom, cloudjunky, codexploder, cryptphi, delfin454000, durianSausage, fatherOfBlocks, Franfran, hake, hansfriese, hyh, IllIllI, jonatascm, Kaiziron, Limbooo, m_Rassska, Meera, oyc_109, peritoflores, rajatbeladiya, rbserver, Ruhum, Sm4rty, svskaushik, and zzzitron
JBERC20PaymentTerminal#_transferFrom calls IERC20#transfer and transferFrom directly. There are two issues with using this interface directly:
JBERC20PaymentTerminal#_transferFromfunction does not check the return value of these calls. Tokens that returnfalserather than revert to indicate failed transfers may silently fail rather than reverting as expected.- Since the IERC20 interface requires a boolean return value, attempting to transfer ERC20s with missing return values will revert. This means Juicebox payment terminals cannot support a number of popular ERC20s, including USDT and BNB.
JBERC20PaymentTerminal#_transferFrom:
function _transferFrom(
address _from,
address payable _to,
uint256 _amount
) internal override {
_from == address(this)
? IERC20(token).transfer(_to, _amount)
: IERC20(token).transferFrom(_from, _to, _amount);
}
Impact
Juicebox payment terminals may issue project tokens to users even though their incoming token transfer failed. Juicebox payment terminals cannot support USDT, BNB, and other popular (but nonstandard) ERC20s.
Recommended Mitigation Steps
Use a safe transfer library like OpenZeppelin SafeERC20 to ensure consistent handling of ERC20 return values and abstract over inconsistent ERC20 implementations.
Additionally, since payment terminals are meant to support a variety of ERC20s, consider writing simulation tests that make token transfers using payment terminals for the most popular and most unusual ERC20s.
(Note also that the out of scope JBETHERC20ProjectPayer and JBETHERC20SplitsPayer contracts also call IERC20#transfer and transferFrom without a helper!)
See the following Forge test, which simulates an attempted USDT transfer. (Run this in fork mode using the --fork-url flag).
// SPDX-License-Identifier: MIT
pragma solidity 0.8.6;
import './helpers/TestBaseWorkflow.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
address constant USDT_ADDRESS = address(0xdAC17F958D2ee523a2206206994597C13D831ec7);
contract TestWeirdERC20 is TestBaseWorkflow {
using SafeERC20 for IERC20Metadata;
JBController controller;
JBProjectMetadata _projectMetadata;
JBFundingCycleData _data;
JBFundingCycleMetadata _metadata;
JBGroupedSplits[] _groupedSplits;
JBFundAccessConstraints[] _fundAccessConstraints;
IJBPaymentTerminal[] _terminals;
JBTokenStore _tokenStore;
JBERC20PaymentTerminal _tetherTerminal;
IERC20Metadata usdt = IERC20Metadata(USDT_ADDRESS);
address _projectOwner;
uint256 WEIGHT = 1000 * 10**18;
function setUp() public override {
super.setUp();
_projectOwner = multisig();
_tokenStore = jbTokenStore();
controller = jbController();
_projectMetadata = JBProjectMetadata({content: 'myIPFSHash', domain: 1});
_data = JBFundingCycleData({
duration: 14,
weight: WEIGHT,
discountRate: 450000000,
ballot: IJBFundingCycleBallot(address(0))
});
_metadata = JBFundingCycleMetadata({
global: JBGlobalFundingCycleMetadata({allowSetTerminals: false, allowSetController: false}),
reservedRate: 5000, //50%
redemptionRate: 5000, //50%
ballotRedemptionRate: 0,
pausePay: false,
pauseDistributions: false,
pauseRedeem: false,
pauseBurn: false,
allowMinting: false,
allowChangeToken: false,
allowTerminalMigration: false,
allowControllerMigration: false,
holdFees: false,
useTotalOverflowForRedemptions: false,
useDataSourceForPay: false,
useDataSourceForRedeem: false,
dataSource: address(0)
});
_tetherTerminal = new JBERC20PaymentTerminal(
usdt,
jbLibraries().ETH(), // currency
jbLibraries().ETH(), // base weight currency
1, // JBSplitsGroupe
jbOperatorStore(),
jbProjects(),
jbDirectory(),
jbSplitsStore(),
jbPrices(),
jbPaymentTerminalStore(),
multisig()
);
evm.label(address(_tetherTerminal), 'TetherTerminal');
_terminals.push(_tetherTerminal);
}
function testTetherPaymentsRevert() public {
JBERC20PaymentTerminal terminal = _tetherTerminal;
_fundAccessConstraints.push(
JBFundAccessConstraints({
terminal: terminal,
token: address(USDT_ADDRESS),
distributionLimit: 10 * 10**18,
overflowAllowance: 5 * 10**18,
distributionLimitCurrency: jbLibraries().ETH(),
overflowAllowanceCurrency: jbLibraries().ETH()
})
);
uint256 projectId = controller.launchProjectFor(
_projectOwner,
_projectMetadata,
_data,
_metadata,
block.timestamp,
_groupedSplits,
_fundAccessConstraints,
_terminals,
''
);
address caller = msg.sender;
evm.label(caller, 'caller');
deal(address(usdt), caller, 20 * 10**18);
evm.prank(caller);
usdt.safeApprove(address(terminal), 20 * 10**18);
evm.prank(caller);
terminal.pay(
projectId,
20 * 10**18,
address(usdt),
msg.sender,
0,
false,
'Forge test',
new bytes(0)
);
}
}
mejango (Juicebox) resolved:
PR with fix: PR #1
berndartmueller (warden) reviewed mitigation:
OpenZeppelins’
SafeERC20library is now used to ensure consistent handling of ERC20 token transfers.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The contract uses the standard IERC20 interface directly without checking return values or handling tokens with missing return values.
Impact
Payment terminals may issue project tokens despite failed incoming transfers, and they cannot support USDT, BNB, and other nonstandard ERC20s.
What the attacker needed
None
The fix
The project fixed it by using OpenZeppelin's SafeERC20 library to handle ERC20 return values consistently.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Some tokens report success or failure by returning true or false, while others just revert on failure. This code ignores the returned value, so a failed transfer might go unnoticed and the user still gets project tokens. Also, tokens that do not return anything (like USDT) break because the code expects a return value.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.