PrincipalToken is not ERC-5095 compliant
The PrincipalToken contract does not follow the ERC-5095 standard in several ways: redeem and withdraw do not allow a third party with token approval to act on behalf of the owner, and maxWithdraw reverts when the contract is paused instead of returning zero, while maxRedeem does not return zero when paused. An attacker could not directly exploit this, but integrators relying on the standard would encounter unexpected failures and incorrect behaviour.
Submitted by jnforja, also found by sl1, dimulski, wangxx2026 (1, 2), JohnSmith, 0xLogos, 14si2o_Flint, erosjohn, Aymen0909, Limbooo, Giorgio (1, 2), smaul, ZanyBonzy, 0xhacksmithh, Brenzee, btk, 0xDemon, lsaudit (1, 2), mrudenko, memforvik, Franklin, Shubham, and nmirchev8
https://github.com/code-423n4/2024-02-spectra/blob/main/src/tokens/PrincipalToken.sol#L483-L485
https://github.com/code-423n4/2024-02-spectra/blob/main/src/tokens/PrincipalToken.sol#L460-L462
https://github.com/code-423n4/2024-02-spectra/blob/main/src/tokens/PrincipalToken.sol#L278-L287
https://github.com/code-423n4/2024-02-spectra/blob/main/src/tokens/PrincipalToken.sol#L229-L237
Protocols that try to integrate with Spectra, expecting PrincipalToken to be ERC-5095 compliant, will face an array of issues that may damage Spectra’s brand and limit Spectra’s growth in the market.
Proof of Concept
All official ERC-5095 requirements are on their official page. Non-compliant methods are listed below along with why they are not compliant and code POCs demonstrating the issues. To run the POCs, copy-paste them into PrincipalToken.t.sol:
PrincipalToken::redeem and PrincipalToken::withdraw
As specified in ERC-5095, both withdraw and redeem must support a flow where msg.sender has approval over the owner’s tokens:
MUST support a redeem flow where the Principal Tokens are burned from holder directly where holder is msg.sender or msg.sender has EIP-20 approval over the principal tokens of holder.
MUST support a withdraw flow where the principal tokens are burned from holder directly where holder is msg.sender or msg.sender has EIP-20 approval over the principal tokens of holder.
However, neither PrincipalToken::redeem nor PrincipalToken::withdraw support this flow type:
//copy-paste into `PrincipalToken.sol`
function testRedeemDoesNotSupportERC20ApprovalFlow() public {
uint256 amountToDeposit = 1e18;
uint256 expected = _testDeposit(amountToDeposit, address(this));
_increaseTimeToExpiry();
principalToken.storeRatesAtExpiry();
principalToken.approve(MOCK_ADDR_5, UINT256_MAX);
assertEq(principalToken.allowance(address(this), MOCK_ADDR_5), UINT256_MAX);
vm.startPrank(MOCK_ADDR_5);
vm.expectRevert();
//Should not revert as MOCK_ADDR_5 has allowance over tokens.
principalToken.redeem(expected, MOCK_ADDR_5, address(this));
vm.stopPrank();
}
function testWithdrawDoesNotSupportERC20ApprovalFlow() public {
uint256 amount = 1e18;
underlying.approve(address(principalToken), amount);
principalToken.deposit(amount, testUser);
principalToken.approve(MOCK_ADDR_5, UINT256_MAX);
assertEq(principalToken.allowance(address(this), MOCK_ADDR_5), UINT256_MAX);
vm.prank(MOCK_ADDR_5);
vm.expectRevert();
//Should not revert as MOCK_ADDR_5 has allowance over tokens.
principalToken.withdraw(amount, MOCK_ADDR_5, testUser);
vm.stopPrank();
}
PrincipalToken::maxWithdraw
According to ERC-5095, maxWithdraw must not revert and must return 0 if withdrawal is disabled.
MUST factor in both global and user-specific limits, like if withdrawals are entirely disabled (even temporarily) it MUST return 0.
MUST NOT revert.
However, PrincipalToken::maxWithdraw reverts if PrincipalToken is paused:
//copy-paste into `PrincipalToken.sol`
function testMaxWithdrawRevertsWhenPausedWhenItShouldNeverRevert() public {
vm.prank(scriptAdmin);
principalToken.pause();
vm.expectRevert();
//Should not revert, should return 0 to comply to ERC-5095.
principalToken.maxWithdraw(address(this));
}
PrincipalToken::maxRedeem
According to ERC-5095, maxRedeem must return 0 if redeem is disabled:
MUST factor in both global and user-specific limits, like if redemption is entirely disabled (even temporarily) it MUST return 0.
However, PrincipalToken::maxRedeem does not return 0 when PrincipalToken is paused:
//copy-paste into `PrincipalToken.sol`
function testMaxRedeemDoesNotReturnZeroWhenPausedEvenThoughItShould() public {
uint256 amountToDeposit = 1e18;
_testDeposit(amountToDeposit, address(this));
vm.prank(scriptAdmin);
principalToken.pause();
//Should return 0 to comply to ERC-5095.
assertNotEq(principalToken.maxRedeem(address(this)), 0);
}
Recommended Mitigation Steps
PrincipalToken::redeemandPrincipalToken::withdrawshould be changed to support a flow wheremsg.senderhas EPI-20 approval over the owner’s principal tokens.PrincipalToken::maxRedeemandPrincipalToken::maxWithdrawshould be changed to return 0 whenPrincipalTokenis paused.
Dravee (Judge) decreased severity to Medium
yanisepfl (sponsor) confirmed and commented:
Mitigated here.
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
Likely the implementation of redeem, withdraw, maxWithdraw and maxRedeem did not account for the ERC-5095 requirements around third-party approvals and paused-state return values.
What the attacker needed
A deployed PrincipalToken contract; a user with PrincipalTokens; the contract is paused for maxWithdraw/maxRedeem issues
The fix
The issues were fixed by updating redeem and withdraw to support third-party approvals and by making maxRedeem and maxWithdraw return zero when the contract is paused.
How it was found
- Deposit underlying tokens to receive PrincipalTokens.
- Approve another address to spend the PrincipalTokens.
- Call redeem or withdraw from the approved address, which reverts unexpectedly.
- Pause the contract and call maxWithdraw, which reverts instead of returning zero.
- Pause the contract and call maxRedeem, which returns a non-zero value instead of zero.
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.
ERC-5095 is a rulebook for a type of token that represents a deposit that can be redeemed later. This token breaks some of those rules: it does not let someone you have approved withdraw or redeem on your behalf, and its helper functions that report the maximum you can withdraw or redeem give wrong answers when the token is paused. These mistakes do not let someone steal funds directly, but they make the token behave unexpectedly for other programs that follow the standard.
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.