Security Audit
March 1st, 2023
Version 1.0.0
Presented by 0xMacro
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.
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.
Our understanding of the specification was based on the following sources:
The following source code was reviewed during the audit:
391cf6625b813a0e51c02eab61d179c63d96104f
c03069cedbcad9707c921fbea8a702a6c3fa673a
We audited the following contracts within this repository:
Source Code | SHA256 |
---|---|
eip/ERC721AUpgradeable.sol |
|
extension/ContractMetadata.sol |
|
extension/ERC2771Context.sol |
|
extension/ERC2771ContextUpgradeable.sol |
|
extension/OperatorFilterToggle.sol |
|
extension/OperatorFilterer.sol |
|
extension/OperatorFiltererUpgradeable.sol |
|
extension/Ownable.sol |
|
extension/Permissions.sol |
|
extension/PermissionsEnumerable.sol |
|
extension/PrimarySale.sol |
|
extension/Royalty.sol |
|
extension/SignatureActionUpgradeable.sol |
|
extension/init/ContractMetadataInit.sol |
|
extension/init/ERC2771ContextInit.sol |
|
extension/init/ERC721AInit.sol |
|
extension/init/OwnableInit.sol |
|
extension/init/PermissionsInit.sol |
|
extension/init/PrimarySaleInit.sol |
|
extension/init/RoyaltyInit.sol |
|
extension/init/SignatureActionInit.sol |
|
extension/interface/IERC2771Context.sol |
|
openzeppelin-presets/utils/cryptography/EIP712Upgradeable.sol |
|
plugin/BaseRouter.sol |
|
plugin/PluginState.sol |
|
plugin/Router.sol |
|
tiered-drop/TieredDrop.sol |
|
tiered-drop/plugin/TieredDropLogic.sol |
|
tiered-drop/plugin/TieredDropStorage.sol |
|
extension/DelayedReveal.sol |
|
extension/LazyMintWithTier.sol |
|
extension/DefaultOperatorFiltererUpgradeable.sol |
|
extension/BatchMintMetadata.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.
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. |
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.
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.
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.
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()
.
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
.
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.
Not fixing. The owner() functionality of Ownable is necessary for updating the NFT collection’s Opensea storefront metadata (banner image, etc.)
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.
Not fixing. thirdweb plans to further spec out the introduction of custom errors to its stack.
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.
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
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.
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.