Liquid Staking

Stakelink
DeFiHardhatOracle
50,000 USDC
View results
Submission Details
Severity: medium
Invalid

test/core/wrapped-sd-token.test.ts

The provided Solidity test suite is testing the WrappedSDToken contract, which seems to involve wrapping and unwrapping tokens related to a staking pool mechanism. Here’s an analysis of potential vulnerabilities and proposed improvements, including detailed solutions.

1. Reentrancy Vulnerability

Vulnerability: The wrap and unwrap functions may be vulnerable to reentrancy attacks if they involve external calls. If the wrap or unwrap functions make a call to an external contract (like transferring tokens), malicious actors could exploit this by re-entering the function before the first call has completed.

Improvement: Implement the Checks-Effects-Interactions pattern. Ensure that all state changes are made before any external calls. If applicable, use a reentrancy guard (like OpenZeppelin's ReentrancyGuard).

Solution Example:

import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract WrappedSDToken is ReentrancyGuard {
// Example wrap function
function wrap(uint256 amount) external nonReentrant {
// Check conditions (validity, balance, etc.)
// Effects: Update balances and state before transferring
_updateState(msg.sender, amount);
// Interactions: Make external calls after state is updated
stakingPool.deposit(msg.sender, amount, ...);
}
}

2. Insufficient Input Validation

Vulnerability: There is little to no validation on the amount of tokens being wrapped or unwrapped, which could allow users to attempt to wrap or unwrap zero or negative amounts.

Improvement: Implement checks to validate the input amounts in the wrap and unwrap functions.

Solution Example:

function wrap(uint256 amount) external {
require(amount > 0, "Amount must be greater than zero");
// Proceed with wrapping logic...
}
function unwrap(uint256 amount) external {
require(amount > 0, "Amount must be greater than zero");
// Proceed with unwrapping logic...
}

3. Arithmetic Underflow/Overflow

Vulnerability: Although Solidity 0.8.x includes built-in overflow/underflow protection, if the codebase uses older versions of Solidity or interacts with external libraries without checks, there could be risks.

Improvement: Ensure the code is using Solidity version 0.8.x or higher. If using external libraries, prefer those that include safe math checks.

4. Access Control

Vulnerability: Functions like setPriorityPool and setRebaseController should have access control mechanisms to prevent unauthorized access.

Improvement: Use the Ownable or AccessControl pattern from OpenZeppelin to manage permissions effectively.

Solution Example:

import "@openzeppelin/contracts/access/Ownable.sol";
contract StakingPool is Ownable {
function setPriorityPool(address account) external onlyOwner {
// Logic for setting priority pool
}
function setRebaseController(address account) external onlyOwner {
// Logic for setting rebase controller
}
}

5. Gas Limit and Block Gas Limit

Vulnerability: Some operations could potentially exceed the block gas limit, especially in loops or during mass operations.

Improvement: Optimize contract functions to minimize gas usage and avoid loops that iterate over large data sets.

6. Event Emissions

Vulnerability: The code does not emit events for critical operations, which could hinder debugging and auditing.

Improvement: Ensure that all state-changing operations emit corresponding events for tracking and auditing.

Solution Example:

event TokensWrapped(address indexed user, uint256 amount);
event TokensUnwrapped(address indexed user, uint256 amount);
function wrap(uint256 amount) external {
// Logic...
emit TokensWrapped(msg.sender, amount);
}
function unwrap(uint256 amount) external {
// Logic...
emit TokensUnwrapped(msg.sender, amount);
}

7. Testing Coverage

Improvement: Ensure comprehensive test coverage by adding tests for edge cases, like:

  • Trying to wrap or unwrap zero or negative amounts.

  • Attempting to wrap or unwrap more tokens than available.

  • Accessing restricted functions without proper permissions.

  • Verifying the correct state of contracts after operations.

Solution Example:

it('should revert on wrapping zero tokens', async () => {
const { wsdToken } = await loadFixture(deployFixture);
await assert.isRejected(wsdToken.wrap(0), "Amount must be greater than zero");
});
it('should revert on unwrapping more than balance', async () => {
const { wsdToken } = await loadFixture(deployFixture);
await assert.isRejected(wsdToken.unwrap(1000), "Insufficient balance");
});

Summary

By implementing these improvements and addressing the identified vulnerabilities, the security and robustness of the WrappedSDToken contract can be significantly enhanced. Testing thoroughly and applying best practices will further help ensure a secure deployment in production environments.

Updates

Lead Judging Commences

inallhonesty Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Lack of quality

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.