Download audit report
This document presents the security review performed by Nethermind on the State Bridge Contracts Upgrade containing 344 lines of code. The Worldcoin team implemented a significant change in the state bridge mechanism. Instead of pushing the root hash atomically to all chains when inserting identity commitments, they have reorganized the process. Now, distinct tasks handle the data transfer to each supported blockchain independently, without involving the identity manager smart contract, and they do it at various time intervals. Along the audit of this contract, we report 11 points of attention, where 1 is classified as Medium, 1 is classified as Info, and 9 are classified as Best Practices.
| Audit assessment | |
| Audit Type | Security Review |
| Initial Report | Sep. 6, 2023 |
| Response from Client | Sep. 7, 2023 |
| Final Report | Sep. 8, 2023 |
| Methods | Manual Review, Automated Analysis |
| Repository | world-id-state-bridge |
| Commit Hash (Audit) | 9edc22a94d |
| Documentation | README.md |
| Documentation Assessment | High |
| Test Suite Assessment | High |
Findings by severity
| # | Contract | LoC | Comments | Ratio | Blank | Total |
| 1 | src/PolygonStateBridge.sol | 38 | 58 | 152.6% | 22 | 118 |
| 2 | src/OpStateBridge.sol | 82 | 83 | 101.2% | 38 | 203 |
| 3 | src/OpWorldID.sol | 14 | 37 | 264.3% | 8 | 59 |
| 4 | src/PolygonWorldID.sol | 45 | 69 | 153.3% | 17 | 131 |
| 5 | src/abstract/WorldIDBridge.sol | 77 | 104 | 135.1% | 43 | 224 |
| 6 | src/utils/BytesUtils.sol | 72 | 69 | 95.8% | 19 | 160 |
| 7 | src/interfaces/IOpStateBridgeTransferOwnership.sol | 4 | 8 | 200.0% | 1 | 13 |
| 8 | src/interfaces/IWorldIDIdentityManager.sol | 4 | 5 | 125.0% | 1 | 10 |
| 9 | src/interfaces/IPolygonWorldID.sol | 4 | 15 | 375.0% | 2 | 21 |
| 10 | src/interfaces/IOpWorldID.sol | 4 | 19 | 475.0% | 2 | 25 |
| Total | 344 | 467 | 135.8% | 153 | 964 |
| # | Finding | Severity | Update |
| 1 | Wrong state variable is set in the function setGasLimitSendRoot(...) | Medium | Fixed |
| 2 | Transaction ordering in L2 may leave _latestRoot in the wrong state after root hash expiration | Info | Acknowledged |
| 3 | Avoid using magic numbers | Best Practices | Fixed |
| 4 | Inconsistent usage of custom errors | Best Practices | Fixed |
| 5 | Incorrect NatSpec description in _receiveRoot(...) function | Best Practices | Fixed |
| 6 | Lack of event emission when updating state variables | Best Practices | Fixed |
| 7 | Missing input validation in some functions | Best Practices | Fixed |
| 8 | Optimization of WorldIdBridge | Best Practices | Fixed |
| 9 | Remove the testing library from the code base | Best Practices | Fixed |
| 10 | Remove unused imports | Best Practices | Fixed |
| 11 | Typos in the code base | Best Practices | Fixed |
No system overview is included in this report.
The risk rating methodology used by Nethermind Security follows the principles established by the OWASP Foundation. The severity of each finding is determined by two factors: Likelihood and Impact.
Likelihood measures how likely the finding is to be uncovered and exploited by an attacker. This factor will be one of the following values:
When defining the likelihood of a finding, other factors are also considered. These can include but are not limited to motive, opportunity, exploit accessibility, ease of discovery, and ease of exploit.
Impact is a measure of the damage that may be caused if an attacker exploits the finding. This factor will be one of the following values:
When defining the impact of a finding, other factors are also considered. These can include but are not limited to data and state integrity, loss of availability, financial loss, and reputation damage. After defining the likelihood and impact of an issue, the severity is determined according to the table below.
| Impact / Likelihood | Low | Medium | High |
| High | Medium | High | Critical |
| Medium | Low | Medium | High |
| Low | Info / Best Practices | Low | Medium |
| Undetermined | Undetermined | Undetermined | Undetermined |
To address issues that do not fit a High, Medium or Low severity, Nethermind Security also uses three more finding severities: Informational, Best Practices, and Undetermined.
setGasLimitSendRoot(...)| File | Fix commit | Type | Status |
OpStateBridge.sol | — | Medium | Fixed |
The OpStateBridge contract allows the owner to customize the gas limit for different methods. The value of _gasLimitSendRoot can be set by calling the function setGasLimitSendRoot(...). However, the current codebase instead sets the value for the wrong variable _gasLimitSetRootHistoryExpiry.
function setGasLimitSendRoot(uint32 _opGasLimit) external onlyOwner {
// @audit Should set _gasLimitSendRoot instead of _gasLimitSetRootHistoryExpiry
_gasLimitSetRootHistoryExpiry = _opGasLimit;
emit SetGasLimitSendRoot(_opGasLimit);
}Recommendation
In the function setGasLimitSendRoot(...), set the value for _gasLimitSendRoot instead of _gasLimitSetRootHistoryExpiry.
Resolution
Fixed, current state.
_latestRoot in the wrong state after root hash expiration| File | Fix commit | Type | Status |
WorldIDBridge.sol | — | Info | Acknowledged |
The propagation of latestRoot between L1 and L2 is done via messaging. The message data contains the new root hash but does not include the timestamp indicating the emission time from L1. Depending on the different L1-L2 messaging mechanisms, the ordering of transactions cannot be ensured, and it could be possible that two messages sent from L1 to L2 to propagate root get validated in the reverse order on L2.
This scenario has a limited impact since the user on L2 would just need to specify the root hash that contains its identity. However, there is an edge case when the root hash is not updated and expiration time (ROOT_HISTORY_EXPIRY) is reached for all root hashes. The only valid root hash is the latest one (_latestRoot), which could be the second to last one sent from L1.
function requireValidRoot(uint256 root) internal view {
// The latest root is always valid.
if (root == _latestRoot) {
return;
}All users registered between these two root hash propagations cannot create valid proofs on L2 until a new root hash is propagated.
Recommendation
Consider sending the timestamp from L1 to ensure the latest root hash is correctly updated.
Resolution
Acknowledged. Transaction ordering doesn’t impact the correct functioning of bridged World IDs. The signup-sequencer is the one that takes care of providing roots against the latest root on the L2, which solves the edge case mentioned above.
| File | Fix commit | Type | Status |
OpStateBridge.solPolygonWorldID.sol | PR #106 PR #106 | Best Practices | Fixed |
Enhancing code readability can be achieved by replacing magic numbers with meaningful constants. The code snippet below shows an example:
constructor(
address _worldIDIdentityManager,
address _opWorldIDAddress,
address _crossDomainMessenger
) {
opWorldIDAddress = _opWorldIDAddress;
worldIDAddress = _worldIDIdentityManager;
crossDomainMessengerAddress = _crossDomainMessenger;
// @audit Avoid using magic number
_gasLimitSendRoot = 100000;
_gasLimitSetRootHistoryExpiry = 100000;
_gasLimitTransferOwnership = 100000;
}Recommendation
Consider replacing these magic numbers with named constants or variables that provide meaningful names and context for these values.
Resolution
Fixed in commits 364826209, 33df6b53
| File | Fix commit | Type | Status |
PolygonStateBridge.solPolygonWorldID.sol | PR #108 | Best Practices | Fixed |
Within these contract, both require()/revert() and custom errors are employed. Adopting a consistent approach and using only one error-handling method within each file is recommended.
Recommendation
Consider adopting a uniform error-handling method throughout the codebase.
Resolution
Fixed in commit 52f2c77
_receiveRoot(...) function| File | Fix commit | Type | Status |
WorldIDBridge.sol | PR #106 | Best Practices | Fixed |
The _receiveRoot(...) function contains incorrect custom NatSpec. It specifies that the function may revert if the caller is not the owner. However, the function does not have this logic.
/// @custom: reverts string If the caller is not the owner.
function _receiveRoot(uint256 newRoot) internal {
[...]
}Recommendation
Consider correcting the NatSpec documentation to reflect the function’s implementation.
Resolution
Fixed in commit 844da90
| File | Fix commit | Type | Status |
PolygonStateBridge.solPolygonWorldID.sol | PR #106 PR #106 | Best Practices | Fixed |
It is best practice to emit an event when changing important state variables. The code snippet below shows an example where the function sets the address for fxRootTunnel, but does not emit an event.
function setFxRootTunnel(address _fxRootTunnel) external virtual override onlyOwner {
require(fxRootTunnel == address(0x0), "FxBaseChildTunnel: ROOT_TUNNEL_ALREADY_SET");
// @audit Should emit event
fxRootTunnel = _fxRootTunnel;
}Recommendation
Consider emitting a suitable event when updating state variables.
Resolution
Fixed in commits 33df6b53, c3c25679
| File | Fix commit | Type | Status |
OpStateBridge.solPolygonStateBridge.solPolygonWorldID.sol | PR #106 PR #106 | Best Practices | Fixed |
Some functions do not check for the validity of their input before utilizing them:
OpStateBridge.transferOwnershipOp(...) does not check if _owner is different than address(0). Note that a check is implemented in transferOwnership function of CrossDomainOwnable3 contract, but it can be endorsed at the protocol contract level;setGasLimitSendRoot(...), setGasLimitSetRootHistoryExpiry(...), setGasLimitTransferOwnershipOp(...).. ) does not check for the validity of the gas limit (i.e., whether it differs from 0);PolygonStateBridge constructor function lacks validation for inputs _checkpointManager _fxRoot and _worldIDIdentityManager, allowing them to be set as address(0);PolygonWorldID contract constructor lacks validation of _fxChild address;Including input validation to enhance code robustness and prevent unexpected errors is a good practice.
Recommendation
We recommend implementing a check for inputs in different functions.
Resolution
Fixed in commits c3c25679, 0c08228
WorldIdBridge| File | Fix commit | Type | Status |
WorldIDBridge.sol | PR #106 PR #106 | Best Practices | Fixed |
The treeDepth state variable from the WorldIDBridge abstract contract is never updated. This value is only set within the constructor.
/// @notice Constructs a new instance of the state bridge.
///
/// @param _treeDepth The depth of the identities Merkle tree.
constructor(uint8 _treeDepth) {
if (!SemaphoreTreeDepthValidator.validate(_treeDepth)) {
revert UnsupportedTreeDepth(_treeDepth);
}
treeDepth = _treeDepth;
}Recommendation
Gas optimization can be done following these steps:
treedepth to an immutable variable (initialization in constructor);getTreeDepth() function to pure;Resolution
getTreeDepth() needs to remain a view function for the contract to compile. The solidity compiler considers immutable variables to be non-pure because it is possible to initialize them with some state link. Fixed in commits 12d09df, 8e0e5b4
| File | Fix commit | Type | Status |
WorldIDBridge.sol | PR #106 | Best Practices | Fixed |
The WorldIdBridge abstract contract imports the forge-std console library.
import "forge-std/console.sol";Recommendation
Consider removing the testing library in the production code base.
Resolution
Fixed in commit 88beb2e
| File | Fix commit | Type | Status |
PolygonWorldID.sol | PR #106 | Best Practices | Fixed |
PolygonWorldID contract has the following imports that are not being used.
import {SemaphoreTreeDepthValidator} from "./utils/SemaphoreTreeDepthValidator.sol";
import {SemaphoreVerifier} from "semaphore/base/SemaphoreVerifier.sol";Recommendation
Consider removing unused imports from the contract.
Resolution
Fixed in commit 33df6b5
| File | Fix commit | Type | Status |
WorldIDBridge.solOpStateBridge.sol | PR #106 PR #106 PR #107 | Best Practices | Fixed |
The code base contains the following typos:
verifyProof(...) function of the WorldIDBridge contract;/// @param root The of the Merkle treeOpStateBridge contract:;/// @notice Emitted when the the StateBridge sets the gas limit for SetRootHistoryExpiryt
/// @param _opGasLimit The new opGasLimit for SetRootHistoryExpirytimismRecommendation
Consider correcting the typos to improve code quality.
Resolution
Software documentation refers to the written or visual information that describes the functionality, architecture, design, and implementation of software. It provides a comprehensive overview of the software system and helps users, developers, and stakeholders understand how the software works, how to use it, and how to maintain it. Software documentation can take different forms, such as user manuals, system manuals, technical specifications, requirements documents, design documents, and code comments. Software documentation is critical in software development, enabling effective communication between developers, testers, users, and other stakeholders. It helps to ensure that everyone involved in the development process has a shared understanding of the software system and its functionality. Moreover, software documentation can improve software maintenance by providing a clear and complete understanding of the software system, making it easier for developers to maintain, modify, and update the software over time. Smart contracts can use various types of software documentation. Some of the most common types include:
These types of documentation are essential for smart contract development and maintenance. They help ensure that the contract is properly designed, implemented, and tested, and they provide a reference for developers who need to modify or maintain the contract in the future.
The documentation is presented in README.md. The documentation is sufficient for this audit.
The relevant output is presented below.
| 44Tests passed | 0Failed |
The complete test output is in the full report.
As auditors, we recommend that any changes or updates made to the audited codebase undergo a re-audit or security review to address potential vulnerabilities or risks introduced by the modifications. By conducting a re-audit or security review of the modified codebase, you can significantly enhance the overall security of your system and reduce the likelihood of exploitation. However, we do not possess the authority or right to impose obligations or restrictions on our clients regarding codebase updates, modifications, or subsequent audits. Accordingly, the decision to seek a re-audit or security review lies solely with you.
This report is based on the scope of materials and documentation provided by you to Nethermind in order that Nethermind could conduct the security review outlined in 1. Executive Summary and 2. Audited Files. The results set out in this report may not be complete nor inclusive of all vulnerabilities. Nethermind has provided the review and this report on an as-is, where-is, and as-available basis. You agree that your access and/or use, including but not limited to any associated services, products, protocols, platforms, content, and materials, will be at your sole risk. Blockchain technology remains under development and is subject to unknown risks and flaws. The review does not extend to the compiler layer, or any other areas beyond the programming language, or other programming aspects that could present security risks. This report does not indicate the endorsement of any particular project or team, nor guarantee its security. No third party should rely on this report in any way, including for the purpose of making any decisions to buy or sell a product, service or any other asset. To the fullest extent permitted by law, Nethermind disclaims any liability in connection with this report, its content, and any related services and products and your use thereof, including, without limitation, the implied warranties of merchantability, fitness for a particular purpose, and non-infringement. Nethermind does not warrant, endorse, guarantee, or assume responsibility for any product or service advertised or offered by a third party through the product, any open source or third-party software, code, libraries, materials, or information linked to, called by, referenced by or accessible through the report, its content, and the related services and products, any hyperlinked websites, any websites or mobile applications appearing on any advertising, and Nethermind will not be a party to or in any way be responsible for monitoring any transaction between you and any third-party providers of products or services. As with the purchase or use of a product or service through any medium or in any environment, you should use your best judgment and exercise caution where appropriate. FOR AVOIDANCE OF DOUBT, THE REPORT, ITS CONTENT, ACCESS, AND/OR USAGE THEREOF, INCLUDING ANY ASSOCIATED SERVICES OR MATERIALS, SHALL NOT BE CONSIDERED OR RELIED UPON AS ANY FORM OF FINANCIAL, INVESTMENT, TAX, LEGAL, REGULATORY, OR OTHER ADVICE.
The Leading Engineers of Blockchain Infrastructure