Skip to content

Commit

Permalink
Address finding 7
Browse files Browse the repository at this point in the history
  • Loading branch information
lastperson committed Sep 9, 2024
1 parent a94faf9 commit 9114518
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions contracts/handlers/ERC1155Handler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import "@openzeppelin/contracts/token/ERC1155/extensions/IERC1155MetadataURI.sol";

contract ERC1155Handler is IHandler, ERCHandlerHelpers, ERC1155Safe, ERC1155Holder {
using SanityChecks for *;
using ERC165Checker for address;

bytes private constant EMPTY_BYTES = "";
Expand Down Expand Up @@ -101,6 +102,7 @@ contract ERC1155Handler is IHandler, ERCHandlerHelpers, ERC1155Safe, ERC1155Hold

(tokenAddress, recipient, tokenIDs, amounts, transferData) = abi.decode(data, (address, address, uint[], uint[], bytes));

recipient.mustNotBeZero();
releaseBatchERC1155(tokenAddress, address(this), recipient, tokenIDs, amounts, transferData);
}

Expand Down
3 changes: 3 additions & 0 deletions contracts/handlers/ERC20Handler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import "../utils/ExcessivelySafeCall.sol";
@notice This contract is intended to be used with the Bridge contract.
*/
contract ERC20Handler is IHandler, ERCHandlerHelpers, DepositDataHelper, ERC20Safe {
using SanityChecks for *;
using ExcessivelySafeCall for address;

error OptionalMessageCallFailed();
Expand Down Expand Up @@ -117,6 +118,7 @@ contract ERC20Handler is IHandler, ERCHandlerHelpers, DepositDataHelper, ERC20Sa

(tokenAddress, recipient, amount) = abi.decode(data, (address, address, uint));

recipient.mustNotBeZero();
releaseERC20(tokenAddress, recipient, amount);
}

Expand All @@ -131,6 +133,7 @@ contract ERC20Handler is IHandler, ERCHandlerHelpers, DepositDataHelper, ERC20Sa
or has a first byte set to the uint8 decimals value of the token contract.
*/
function setResource(bytes32 resourceID, address contractAddress, bytes calldata args) external onlyBridge {
contractAddress.mustNotBeZero();
_setResource(resourceID, contractAddress);

if (args.length > 0) {
Expand Down
3 changes: 3 additions & 0 deletions contracts/handlers/ERC721Handler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Metadata.sol";
@notice This contract is intended to be used with the Bridge contract.
*/
contract ERC721Handler is IHandler, ERCHandlerHelpers, ERC721Safe {
using SanityChecks for *;
using ERC165Checker for address;

bytes4 private constant _INTERFACE_ERC721_METADATA = 0x5b5e139f;
Expand Down Expand Up @@ -128,6 +129,7 @@ contract ERC721Handler is IHandler, ERCHandlerHelpers, ERC721Safe {

(tokenAddress, recipient, tokenID) = abi.decode(data, (address, address, uint));

recipient.mustNotBeZero();
releaseERC721(tokenAddress, address(this), recipient, tokenID);
}

Expand All @@ -140,6 +142,7 @@ contract ERC721Handler is IHandler, ERCHandlerHelpers, ERC721Safe {
@param args Additional data to be passed to specified handler.
*/
function setResource(bytes32 resourceID, address contractAddress, bytes calldata args) external onlyBridge {
contractAddress.mustNotBeZero();
_setResource(resourceID, contractAddress);
}
}
1 change: 1 addition & 0 deletions contracts/handlers/ERCHandlerHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pragma solidity 0.8.11;

import "../interfaces/IERCHandler.sol";
import "../utils/SanityChecks.sol";

/**
@title Function used across handler contracts.
Expand Down
2 changes: 2 additions & 0 deletions contracts/handlers/NativeTokenHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import "../utils/ExcessivelySafeCall.sol";
@notice This contract is intended to be used with the Bridge contract.
*/
contract NativeTokenHandler is IHandler, ERCHandlerHelpers, DepositDataHelper {
using SanityChecks for *;
using ExcessivelySafeCall for address;

uint256 internal constant defaultGas = 50000;
Expand Down Expand Up @@ -123,6 +124,7 @@ contract NativeTokenHandler is IHandler, ERCHandlerHelpers, DepositDataHelper {
if (address(this).balance <= amount) revert InsufficientBalance();
(, recipient, amount) = abi.decode(data, (address, address, uint));

recipient.mustNotBeZero();
(bool success, ) = address(recipient).call{value: amount}("");
if(!success) revert FailedFundsTransfer();
emit Withdrawal(recipient, amount);
Expand Down
6 changes: 5 additions & 1 deletion contracts/handlers/XC20Handler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import "../XC20Safe.sol";
@notice This contract is intended to be used with the Bridge contract.
*/
contract XC20Handler is IHandler, ERCHandlerHelpers, XC20Safe {
/**
using SanityChecks for *;

/**
@param bridgeAddress Contract address of previously deployed Bridge.
*/
constructor(
Expand Down Expand Up @@ -104,6 +106,7 @@ contract XC20Handler is IHandler, ERCHandlerHelpers, XC20Safe {

(tokenAddress, recipient, amount) = abi.decode(data, (address, address, uint));

recipient.mustNotBeZero();
releaseERC20(tokenAddress, recipient, amount);
}

Expand All @@ -116,6 +119,7 @@ contract XC20Handler is IHandler, ERCHandlerHelpers, XC20Safe {
@param args Additional data to be passed to specified handler.
*/
function setResource(bytes32 resourceID, address contractAddress, bytes calldata args) external onlyBridge {
contractAddress.mustNotBeZero();
_setResource(resourceID, contractAddress);

uint8 externalTokenDecimals = uint8(bytes1(args));
Expand Down
4 changes: 4 additions & 0 deletions contracts/handlers/fee/BasicFeeHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.11;

import "../../interfaces/IFeeHandler.sol";
import "../../utils/AccessControl.sol";
import "../../utils/SanityChecks.sol";
import "../FeeHandlerRouter.sol";

/**
Expand All @@ -12,6 +13,8 @@ import "../FeeHandlerRouter.sol";
@notice This contract is intended to be used with the Bridge contract.
*/
contract BasicFeeHandler is IFeeHandler, AccessControl {
using SanityChecks for *;

address public immutable _bridgeAddress;
address public immutable _feeHandlerRouterAddress;
mapping (uint8 => mapping(bytes32 => uint256)) public _domainResourceIDToFee;
Expand Down Expand Up @@ -121,6 +124,7 @@ contract BasicFeeHandler is IFeeHandler, AccessControl {
function transferFee(address payable[] calldata addrs, uint[] calldata amounts) external onlyAdmin {
require(addrs.length == amounts.length, "addrs[], amounts[]: diff length");
for (uint256 i = 0; i < addrs.length; i++) {
addrs[i].mustNotBeZero();
(bool success,) = addrs[i].call{value: amounts[i]}("");
require(success, "Fee ether transfer failed");
emit FeeDistributed(address(0), addrs[i], amounts[i]);
Expand Down
4 changes: 4 additions & 0 deletions contracts/handlers/fee/PercentageERC20FeeHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity 0.8.11;
import "../../interfaces/IBridge.sol";
import "../../interfaces/IERCHandler.sol";
import "../../ERC20Safe.sol";
import "../../utils/SanityChecks.sol";
import { BasicFeeHandler } from "./BasicFeeHandler.sol";

/**
Expand All @@ -13,6 +14,8 @@ import { BasicFeeHandler } from "./BasicFeeHandler.sol";
@notice This contract is intended to be used with the Bridge contract.
*/
contract PercentageERC20FeeHandler is BasicFeeHandler, ERC20Safe {
using SanityChecks for *;

uint32 public constant HUNDRED_PERCENT = 1e8;

/**
Expand Down Expand Up @@ -133,6 +136,7 @@ contract PercentageERC20FeeHandler is BasicFeeHandler, ERC20Safe {
address tokenHandler = IBridge(_bridgeAddress)._resourceIDToHandlerAddress(resourceID);
address tokenAddress = IERCHandler(tokenHandler)._resourceIDToTokenContractAddress(resourceID);
for (uint256 i = 0; i < addrs.length; i++) {
addrs[i].mustNotBeZero();
releaseERC20(tokenAddress, addrs[i], amounts[i]);
emit FeeDistributed(tokenAddress, addrs[i], amounts[i]);
}
Expand Down
4 changes: 4 additions & 0 deletions contracts/handlers/fee/dynamic/TwapFeeHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "../../../interfaces/IFeeHandler.sol";
import "../../../interfaces/IERCHandler.sol";
import "../../../interfaces/IBridge.sol";
import "../../../utils/SanityChecks.sol";
import "./TwapOracle.sol";

/**
Expand All @@ -16,6 +17,8 @@ import "./TwapOracle.sol";
@notice This contract is intended to be used with the Bridge contract.
*/
abstract contract TwapFeeHandler is IFeeHandler, AccessControl {
using SanityChecks for *;

address public immutable _bridgeAddress;
address public immutable _feeHandlerRouterAddress;

Expand Down Expand Up @@ -191,6 +194,7 @@ abstract contract TwapFeeHandler is IFeeHandler, AccessControl {
function transferFee(address payable[] calldata addrs, uint[] calldata amounts) external onlyAdmin {
require(addrs.length == amounts.length, "addrs[], amounts[]: diff length");
for (uint256 i = 0; i < addrs.length; i++) {
addrs[i].mustNotBeZero();
(bool success,) = addrs[i].call{value: amounts[i]}("");
require(success, "Fee ether transfer failed");
emit FeeDistributed(address(0), addrs[i], amounts[i]);
Expand Down
16 changes: 16 additions & 0 deletions contracts/utils/SanityChecks.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// The Licensed Work is (c) 2024 Sygma
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity 0.8.11;

/**
@title Helps validate input parameters.
@author ChainSafe Systems.
*/
library SanityChecks {
error ZeroAddress();

function mustNotBeZero(address addr) internal pure returns(address) {
if (addr == address(0)) revert ZeroAddress();
return addr;
}
}

0 comments on commit 9114518

Please sign in to comment.