Reach out for an audit or to learn more about Macro
or Message on Telegram

thirdweb A-10

Security Audit

March 1st, 2023

Version 1.0.0

Presented by 0xMacro

Table of Contents

Introduction

This document includes the results of the security audit for thirdweb's smart contract code as found in the section titled ‘Source Code’. The security audit was performed by the Macro security team from Feburary 21, 2022 to Feburary 27, 2023.

The purpose of this audit is to review the source code of certain thirdweb 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.

Overall Assessment

The following is an aggregation of issues found by the Macro Audit team:

Severity Count Acknowledged Won't Do Addressed
High 2 - - 2
Medium 2 - - 2
Low 1 - - 1
Code Quality 2 - 2 -
Informational 1 - 1 -
Gas Optimization 1 - - 1

thirdweb was quick to respond to these issues.

Specification

Our understanding of the specification was based on the following sources:

Source Code

The following source code was reviewed during the audit:

We audited the following contracts within this repository:

Contract SHA256
eip/ERC721AUpgradeable.sol

bdb2c0116b8db66fc5f5dd27c688a352e390653e066663552d4d0fe8fd8ead18

extension/ContractMetadata.sol

a814ab43a9725a15f900a4f395766306acf15b11adaee6e9ffeba22dd9282395

extension/ERC2771Context.sol

5dbae66e99833dd12c31cd500a32fe3421d22800b439cd1e1e060d51e0942dd4

extension/ERC2771ContextUpgradeable.sol

6296294d3c5ab588f222e6b8a813fad390f618e75cfbbc9ea9c3182272672166

extension/OperatorFilterToggle.sol

9db51112d4e6e2bbd174a9014c226c84a93bdb520651b5406158763a8f27d0d8

extension/OperatorFilterer.sol

ac64ab713fc340391cfddc90655442c7269a8d911fb334b62f5077f4972b73d0

extension/OperatorFiltererUpgradeable.sol

2d4c31ec5c4477e24a7e3df27ecc6b285666b48741060e382b3f225eb06e8e40

extension/Ownable.sol

0c92c9a5db8d10dddaa4907e672373e18da9c9fe345da407e6311bc82b7b9d40

extension/Permissions.sol

1bb91fdcd9342217aa66a70130a5ad679af3cd674fce41fcc9a524de8bd602a2

extension/PermissionsEnumerable.sol

95f5691cc5bf7c97363771f026f46704ee41ae3fbf0c54e76bb31d199f47e754

extension/PrimarySale.sol

89c929fc668c6576063f6291d713a014c67b051268649b7f38973a3c1c620c6f

extension/Royalty.sol

b93af7a5166167858d6a884cb6b9659072777fc2a84c47445684274f1969b316

extension/SignatureActionUpgradeable.sol

f56741fc6d8907b6fd6a132126c5a6697c7449cfecb384ac4861c4e8430ebabc

extension/init/ContractMetadataInit.sol

439d385d4820540114856ca9297b7c05e1a14025b5622c6dfcc54c8a3670eeb4

extension/init/ERC2771ContextInit.sol

ac8902008f61436c416a0901127a6ec56d77a3dc168752bdbabb2a058208cabe

extension/init/ERC721AInit.sol

046f64b7a91b9b84735ac4b63c4d768ef59d93576d7afd8d0d6f641d3e051cc7

extension/init/OwnableInit.sol

00f42698954f001c4e8174b8cbccbce0e550bb6f904abf94421f818e86f4d719

extension/init/PermissionsInit.sol

75b6da527e546772665d027c704ecf79bd0d6752cd68aee174f59ade104683a2

extension/init/PrimarySaleInit.sol

5a64382050165a1f59f157e7b60cded0cb6b446de8e8cdd512570781dc53d646

extension/init/RoyaltyInit.sol

20c513099d53c551d711eeded10dbc6f7fa2384d65b47d902ccfa2dcc7767178

extension/init/SignatureActionInit.sol

ac5586ab52de3172b761b1e2716cf85aab56b1d6488582bec9f26dd7c1779d53

extension/interface/IERC2771Context.sol

65462f90d17fcf1befeccedd6f78a372bd0160de16daf3507ffcd639aac38948

openzeppelin-presets/utils/cryptography/EIP712Upgradeable.sol

077140792d5cc238e26dd08c3060ae08a6bc110d86c07b19447ea048e71aa30f

plugin/BaseRouter.sol

339ce9415f2bf581f81a8d5e1cd01037d1ffad773178589b7a5a5e5507c225a9

plugin/PluginState.sol

b38f6724ce79f1dc1a9e263395cdb90e9c299aa4d4998623904944bf3a02d876

plugin/Router.sol

3bebc90e3cbdcb6a1da513cc60199699c4084256ff92b48954d5544eb37887c3

tiered-drop/TieredDrop.sol

ca637f57186d3b73a65d8175d4ff2b41bf23b0487d7ac504f8441455d7d100e6

tiered-drop/plugin/TieredDropLogic.sol

1805043b20f3dd7cd36057c593d24c34075e41c142ffa970b24299f052c64526

tiered-drop/plugin/TieredDropStorage.sol

3a06285322800233833b091280ac543ee1f1e89ee842850be4c688e1c1587a25

extension/DelayedReveal.sol

4c290cdff808979789dcfab5117dcceaecf14e7eab13663228b2689bf99dc3da

extension/LazyMintWithTier.sol

bb8bea4780240116750a5e64e03bb00872b67e975a93774cd251f0538578c859

extension/DefaultOperatorFiltererUpgradeable.sol

a8894404c22cd603956b7b485b5197ea2e44daa1112186d81c780e4d36c1e2cf

extension/BatchMintMetadata.sol

3323f6df6053af4642bb75e33b4b825c098b4893336c896867d974465c82a459

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.

Issue Descriptions and Recommendations

Click on an issue to jump to it, or scroll down to see them all.

Security Level Reference

We quantify issues in three parts:

  1. The high/medium/low/spec-breaking impact of the issue:
    • How bad things can get (for a vulnerability)
    • The significance of an improvement (for a code quality issue)
    • The amount of gas saved (for a gas optimization)
  2. The high/medium/low likelihood of the issue:
    • How likely is the issue to occur (for a vulnerability)
  3. The overall critical/high/medium/low severity of the issue.

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.

Issue Details

H-1

Operator filter is never initialized

Topic
Spec
Status
Impact
High
Likelihood
High

TieredDropLogic inherits from DefaultOperatorFiltererUpgradeable and overrides NFT transfers to ensure the operator is not blacklisted by the filter registry, provided the operator restriction is set.

However, __OperatorFilterer_init is required to be called to register/subscribe the contract to a filter registry, and it is never called. This causes onlyAllowedOperator and onlyAllowedOperatorApproval modifiers to always return true, no matter the operator. It may also prevent OpenSea from honouring royalties accrued from any collection that implements this, as there is no registration/subscription to their registry.

Remediations to Consider

Call __OperatorFilterer_init in TieredDrop's initializer function, registering the contract to the filter registry.

H-2

Not all contracts use the "storage struct" pattern - causing storage collisions

Topic
Data Model
Status
Impact
High
Likelihood
Medium

TieredDropLogic inherits from DelayedReveal, LazyMintWithTier, and BatchMintMetadata which don’t follow the storage struct pattern, as it is expected for contracts using the extension pattern. This causes the potential for storage collisions to occur, since the contracts use the proxy pattern and use delegate calls to read and write to storage. Storage collisions can allow data to be set and/or read from that is not intended, causing many potential issues and vulnerabilities.

Remediations to Consider

Ensure all contracts storage follow the storage struct pattern, to prevent storage collisions.

M-1

Initial permissions are not enumerable

Topic
Spec
Status
Impact
Medium
Likelihood
High

In TieredDrop.sol’s initializer function, it initializes permissions using the PermissionsInit’s _setupRole(), which allows TieredDrop to set the permission without inheriting from the entire permissions contract.

However, the extension TieredDropLogic inherits from PermissionsEnumerable, with is compatible with permissions, but stores extra data on permission update to allow retrieval of all addresses with a given permission. Since TieredDrop initializes using Permission’s _setupRole(), rather than PermissionsEnumerable's _setupRole(), permissions initialized cannot be retrieved as it is expected to, and the count received by getRoleMemberCount() can be off.

Remediations to Consider

Create a PermissionsEnumerableInit contract that uses enumerable permissions to initialize TieredDrop, allowing accurate information regarding permissions.

M-2

Meta transactions are not supported for extension management operations

Topic
Use Cases
Status
Impact
Medium
Likelihood
Medium

TieredDrop.sol::_canSetExtension() uses msg.sender instead of _msgSender():

function _canSetExtension() internal view virtual override returns (bool) {
        bytes32 defaultAdminRole = 0x00; 
        return IPermissions(address(this)).hasRole(defaultAdminRole, msg.sender);          // @audit use of _msgSender
}

As a consequence, all calls relying on _canSetExtension() for authorization don’t work with meta transactions. In particular, the following calls are affected:

  • BaseRouter.sol::addExtension
  • BaseRouter.sol::updateExtension
  • BaseRouter.sol::removeExtension

Remediations to Consider

Consider changing TieredDrop.sol to inherit from ERC2771ContextUpgradeable and use _msgSender() instead of msg.sender in _canSetExtension().

L-1

Unreachable custom error

Topic
Events
Status
Impact
Low
Likelihood
Medium

In OperatorFiltererUpgradeable.sol’s onlyAllowedOperator and onlyAllowedOperatorApproval modifiers, a check is made that will revert with OperatorNotAllowed if OPERATOR_FILTER_REGISTRY.isOperatorAllowed() returns false:

if (!OPERATOR_FILTER_REGISTRY.isOperatorAllowed(address(this), msg.sender)) {
    revert OperatorNotAllowed(msg.sender);
}

However, the operator filter registry’s isOperatorAllowed function never returns false, it either returns true, or reverts with its own custom error AddressFiltered or CodeHashFiltered:

function isOperatorAllowed(address registrant, address operator) external view returns (bool) {

    address registration = _registrations[registrant];

    if (registration != address(0)) {

        EnumerableSet.AddressSet storage filteredOperatorsRef;

        EnumerableSet.Bytes32Set storage filteredCodeHashesRef;
        
        filteredOperatorsRef = _filteredOperators[registration];
        
        filteredCodeHashesRef = _filteredCodeHashes[registration];
        
        if (filteredOperatorsRef.contains(operator)) {
        
            revert AddressFiltered(operator);
        
        }

        if (operator.code.length > 0) {
        
            bytes32 codeHash = operator.codehash;
            
            if (filteredCodeHashesRef.contains(codeHash)) {
            
                revert CodeHashFiltered(operator, codeHash);
            
            }
        
        }

    }

    return true;
}

Remediations to Consider

Remove the check if isOperatorAllowed returns false for both onlyAllowedOperator and onlyAllowedOperatorApproval, and allow it to revert with the relevant error, removing the need for OperatorNotAllowed.

Q-1

Ownable is setup but never used

Topic
Quality
Status
Wont Do
Quality Impact
Low

In TieredDrop.sol OwnableInit is used to setup an owner of the contract in initialize() and TieredDropLogic.sol inherits Ownable and sets up who can change owner.

However, the owner and onlyOwner are never used, and is initialized for no apparent reason.

Remediations to Consider

Remove Ownable from TieredDropLogic.sol, and do not initialize OwnableInit in TieredDrop.sol, in order to save gas on deployments, and remove unnecessary functions.

Response by thirdweb

Not fixing. The owner() functionality of Ownable is necessary for updating the NFT collection’s Opensea storefront metadata (banner image, etc.)

Q-2

Use custom errors

Topic
Quality
Status
Wont Do
Quality Impact
Medium

For solidity versions 0.8.4+ it is advised to revert with custom errors over using require statements. Using them saves bytecode on deployment, a little gas on execution, and allows for more detailed error messages with the ability to pass in parameters.

Remediations to Consider

Replace require statements with custom errors to give more context about errors and save a bit of gas.

Response by thirdweb

Not fixing. thirdweb plans to further spec out the introduction of custom errors to its stack.

G-1

Unnecessary external calls for permissions

Topic
Gas Optimization
Status
Gas Savings
Medium

In TieredDropLogic.sol, there are multiple external calls to check if an address has a given role. Example:

return Permissions(address(this)).hasRole(DEFAULT_ADMIN_ROLE, _msgSender());

However, external calls cost additional gas, and in this case an external call can be avoided by directly retrieving the data using PermissionsStorage:

function _hasRole(bytes32 role, address addr) internal view returns(bool) {
    PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage();      
return data._hasRole[role][addr];
}

Remediations to Consider

Replace external calls to Permissions to query permissions by using PermissionsStorage in order to save gas.

I-1

Admin can add any extension

Topic
Trust Model
Status
Wont Do
Impact
Informational

TieredDrop.sol inherits from BaseRouter.sol, and overrides _canSetExtension() to allow the defaultAdminRole, set in its initializer as the _defaultAdmin address, to add any extension to the contract. However, an extension has complete control over writing to storage, since the calls are done via delegateCall. This allows a malicious admin to add an extension that can do anything to the contract, including steal all NFTs, or steal all tokens that the contract has been approved for.

Remediations to Consider

  • Setup an extension registry, a contract where all valid extension are stored, and have it so only the team can set these extensions. Then when adding an extension, it is retrieved from the registry. This ensures extensions are still opt in, but are limited to vetted and safe extensions in order to prevent malicious functions being set.
  • Create a version of TieredDrop.sol that inherits from RouterImmutable.sol, giving users the option to prevent additional extensions from being added after deployment.
Response by thirdweb

Not fixing. This aspect of particularly the TieredDrop contract is intentional. thirdweb will later introduce an extension registry for the rest of its contracts to ensure only vetted extensions can be added/used by thirdweb contracts.

Disclaimer

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 thirdweb 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.