Security Audit
December 3, 2024
Version 1.0.0
Presented by 0xMacro
This document includes the results of the security audit for Towns's smart contract code as found in the section titled ‘Source Code’. The security audit was performed by the Macro security team from October 24 to 31, 2024.
The purpose of this audit is to review the source code of certain Towns Solidity contracts, and provide feedback on the design, architecture, and quality of the source code with an emphasis on validating the correctness and security of the software in its entirety.
Disclaimer: While Macro’s review is comprehensive and has surfaced some changes that should be made to the source code, this audit should not solely be relied upon for security, as no single audit is guaranteed to catch all possible bugs.
The following is an aggregation of issues found by the Macro Audit team:
| Severity | Count | Acknowledged | Won't Do | Addressed |
|---|---|---|---|---|
| Medium | 2 | - | - | 2 |
| Low | 1 | - | - | 1 |
| Code Quality | 6 | 1 | 2 | 3 |
Towns was quick to respond to these issues.
Our understanding of the specification was based on the following sources:
The following source code was reviewed during the audit:
a3fb570e4bdf9e3c889ea2fc5da2d883a4f6dc4e
883b14f8279ebab454076e3c94bf8b8c3abfb4e5
b3626867eba5c43049f88132aa040732d3ee53c5
Specifically, we audited the following contracts within this repository.
Token Changes (a3fb570e4bdf9e3c889ea2fc5da2d883a4f6dc4e):
| Source Code | SHA256 |
|---|---|
| ./src/tokens/lock/ILock.sol |
|
| ./src/tokens/lock/LockBase.sol |
|
| ./src/tokens/lock/LockFacet.sol |
|
| ./src/tokens/lock/LockStorage.sol |
|
| ./src/tokens/river/base/IOptimismMintableERC20.sol |
|
| ./src/tokens/river/base/ISemver.sol |
|
| ./src/tokens/river/base/River.sol |
|
| ./src/tokens/river/base/delegation/IMainnetDelegation.sol |
|
| ./src/tokens/river/base/delegation/MainnetDelegation.sol |
|
| ./src/tokens/river/base/delegation/MainnetDelegationBase.sol |
|
| ./src/tokens/river/base/delegation/MainnetDelegationStorage.sol |
|
Merkle Drop (883b14f8279ebab454076e3c94bf8b8c3abfb4e5):
| Source Code | SHA256 |
|---|---|
| ./src/tokens/drop/DropFacet.sol |
|
| ./src/tokens/drop/DropFacetBase.sol |
|
| ./src/tokens/drop/DropStorage.sol |
|
| ./src/tokens/drop/IDropFacet.sol |
|
Rewards Distribution V2 (b3626867eba5c43049f88132aa040732d3ee53c5):
| Source Code | SHA256 |
|---|---|
| ./src/base/registry/facets/distribution/v2/DelegationProxy.sol |
|
| ./src/base/registry/facets/distribution/v2/IRewardsDistribution.sol |
|
| ./src/base/registry/facets/distribution/v2/RewardsDistribution.sol |
|
| ./src/base/registry/facets/distribution/v2/RewardsDistributionBase.sol |
|
| ./src/base/registry/facets/distribution/v2/RewardsDistributionStorage.sol |
|
| ./src/base/registry/facets/distribution/v2/StakingRewards.sol |
|
Note: This document contains an audit solely of the Solidity contracts listed above. Specifically, the audit pertains only to the contracts themselves, and does not pertain to any other programs or scripts, including deployment scripts.
Click on an issue to jump to it, or scroll down to see them all.
increaseStake operations lack zero amount check
initiateWithdrawal lacks distinct event for mainnet delegation withdrawals
We quantify issues in three parts:
This third part – the severity level – is a summary of how much consideration the client should give to fixing the issue. We assign severity according to the table of guidelines below:
| Severity | Description |
|---|---|
|
(C-x) Critical |
We recommend the client must fix the issue, no matter what, because not fixing would mean significant funds/assets WILL be lost. |
|
(H-x) High |
We recommend the client must address the issue, no matter what, because not fixing would be very bad, or some funds/assets will be lost, or the code’s behavior is against the provided spec. |
|
(M-x) Medium |
We recommend the client to seriously consider fixing the issue, as the implications of not fixing the issue are severe enough to impact the project significantly, albiet not in an existential manner. |
|
(L-x) Low |
The risk is small, unlikely, or may not relevant to the project in a meaningful way. Whether or not the project wants to develop a fix is up to the goals and needs of the project. |
|
(Q-x) Code Quality |
The issue identified does not pose any obvious risk, but fixing could improve overall code quality, on-chain composability, developer ergonomics, or even certain aspects of protocol design. |
|
(I-x) Informational |
Warnings and things to keep in mind when operating the protocol. No immediate action required. |
|
(G-x) Gas Optimizations |
The presented optimization suggestion would save an amount of gas significant enough, in our opinion, to be worth the development cost of implementing it. |
The DropFacet contract allows the owner to set an array of conditions by calling setClaimConditions(). By setting the resetElegibility boolean, the previously suppliedClaimedByWallet will not prevent an account from claiming the set conditions, as a new, unused ID will be used.
However, combining mixed eligibility configurations can cause the contract to improperly clear the previous condition IDs, leading to unintended preservation of claim history and inconsistent behavior when resetting eligibility. Consider the following potential order of configurations being set and their final state:
conditionById[0] = Condition A
conditionById[1] = Condition B
conditionById[2] = Condition C
conditionCount = 3
conditionStartId = 0
resetElegibility false:conditionById[0] = New Condition X
conditionById[1] = New Condition Y
conditionById[2] = (deleted)
conditionCount = 2
conditionStartId = 0
resetElegibility true:conditionById[0] = (deleted)
conditionById[1] = (deleted)
conditionById[2] = New Condition 1 (With previous condition C claim history)
conditionById[3] = New Condition 2
conditionById[4] = New Condition 3
conditionCount = 3
conditionStartId = 2
This renders a scenario where the conditionById[2] is re-used even if resetElegibility was true for the latest configuration, disallowing users to claim this condition.
Remediations to Consider:
Consider keeping track of the highest condition ID used and starting from this index when resetting eligibility.
The RewardsDistribution contract accrues rewards to users who stake and delegate their voting power to spaces or valid operators. If users delegate to a space, the delegation points to the space’s delegation. However, if a space stops delegating or an operator becomes inactive, users who initially staked and delegated to these will continue accruing rewards. This occurs because the earning power is updated at the stake or delegation execution, through functions stake(), permitAndStake(), increaseStake(), changeBeneficiary(), and redelegate(). Ultimately, calling into the StakingRewards library’s internal functions that update the earning power.
Additionally, the _sweepSpaceRewardsIfNecessary() function transfers accrued rewards from spaces treasure to their delegated operators. However, when a space redelegates to a new operator, the sweep function uses the current operator mapping. This means all accumulated rewards (including those earned by the previous operator) will be sent to the new operator, resulting in incorrect reward attribution.
This issue could lead to unintended reward distribution and potential economic imbalances within the protocol. It may also indirectly incentivize users to maintain outdated delegations.
Remediations to Consider:
In the DropFacet contract, users can execute a claimWithPenalty() call to receive their allocated tokens according to the parameters of their leaf in the claim data and the configured conditions. After the logic verifies the claim arguments, the function deducts the penaltyBps configured for that specific condition ID:
function claimWithPenalty(
Claim calldata claim
) external returns (uint256 amount) {
DropStorage.Layout storage ds = DropStorage.layout();
_verifyClaim(ds, claim);
ClaimCondition storage condition = ds.getClaimConditionById(
claim.conditionId
);
**amount = claim.quantity;
uint256 penaltyBps = condition.penaltyBps;
if (penaltyBps > 0) {
unchecked {
uint256 penaltyAmount = BasisPoints.calculate(
claim.quantity,
penaltyBps
);
amount = claim.quantity - penaltyAmount;
}
}**
...
}
Reference: DropFacet.sol#L34-74
However, users can not ensure the current penaltyBps will be used as there is no expected output or slippage to ensure this. Although highly unlikely, users might end up with a different quantity of tokens than initially expected if the condition they’re attempting to claim modifies this penalty fee before their transactions are included in a validated block.
Remediations to Consider:
Consider adding an expected penaltyBps to the input arguments and comparing the used value to match the user value.
The DropFacet contract lacks an external view function to fetch all existing configured conditions. Consider adding a new view function to complement the existing getActiveCondition(), enabling retrieval of all present conditions.
The DropFacet contract’s current implementation only allows setting or resetting all claim conditions at once through the setClaimConditions() function. While this approach works, it may be less efficient and more prone to errors when only a single condition needs to be updated or a new one added. A more granular approach to managing claim conditions could improve the contract's flexibility and reduce potential mistakes when modifying conditions.
Consider implementing additional explicit functions that allow for more targeted modifications of claim conditions, such as:
In RewardsDistribution, the current implementation of stakeOnBehalf() wraps the permit call to the token with a try-catch statement and empty error handling to prevent any denial of service issues by a potential third-party actor using the passed signature. It continues the function call if the allowance is present. However, there is no internal verification on the deadline that can filter non-valid permit signatures to prevent this call. Consider checking the deadline internally to prevent expired signatures from being used.
This concerns the permitAndStake function which follows UniStaker. The intention is to delegate signature verification to the EIP-2612 compatible ERC-20 token and attempt to stake even if the permit call fails.
increaseStake operations lack zero amount check
In RewardsDistribution, increaseStake() doesn't validate the input amount. A user could call this with zero amount, causing unnecessary state updates, like reward calculations, and emitting an event without any actual stake increase.
This is intentional for poking state updates.
Currently, if users desire to compound their rewards, they’d need to execute a reward claim and then increase stake or stake separately. To reduce friction in this potential user case, consider having a claimAndStake() function that allows users to claim and stake atomically.
initiateWithdrawal lacks distinct event for mainnet delegation withdrawals
In RewardsDistribution, staking delegations use a two-step process for deposit withdrawals. In contrast, mainnet delegations use a single step to remove the delegated amount directly. However, the current event structure does not provide any arguments to differentiate between local delegation withdrawals and mainnet delegation withdrawals. To enhance transparency and improve data tracking, consider adding a separate event for mainnet delegation withdrawals or including an identifier parameter in the existing event.
Macro makes no warranties, either express, implied, statutory, or otherwise, with respect to the services or deliverables provided in this report, and Macro specifically disclaims all implied warranties of merchantability, fitness for a particular purpose, noninfringement and those arising from a course of dealing, usage or trade with respect thereto, and all such warranties are hereby excluded to the fullest extent permitted by law.
Macro will not be liable for any lost profits, business, contracts, revenue, goodwill, production, anticipated savings, loss of data, or costs of procurement of substitute goods or services or for any claim or demand by any other party. In no event will Macro be liable for consequential, incidental, special, indirect, or exemplary damages arising out of this agreement or any work statement, however caused and (to the fullest extent permitted by law) under any theory of liability (including negligence), even if Macro has been advised of the possibility of such damages.
The scope of this report and review is limited to a review of only the code presented by the Towns team and only the source code Macro notes as being within the scope of Macro’s review within this report. This report does not include an audit of the deployment scripts used to deploy the Solidity contracts in the repository corresponding to this audit. Specifically, for the avoidance of doubt, this report does not constitute investment advice, is not intended to be relied upon as investment advice, is not an endorsement of this project or team, and it is not a guarantee as to the absolute security of the project. In this report you may through hypertext or other computer links, gain access to websites operated by persons other than Macro. Such hyperlinks are provided for your reference and convenience only, and are the exclusive responsibility of such websites’ owners. You agree that Macro is not responsible for the content or operation of such websites, and that Macro shall have no liability to your or any other person or entity for the use of third party websites. Macro assumes no responsibility for the use of third party software and shall have no liability whatsoever to any person or entity for the accuracy or completeness of any outcome generated by such software.