Download audit report
This document outlines the security review conducted by Nethermind Security for the StarkNet Foundation on the Token Distributor Contract containing 108 lines of code. The distributor contract is designed to distribute tokens utilizing Merkle tree verification. A Merkle tree is a binary tree where leaf nodes represent data, internal nodes represent hashed pairs of child nodes, and the root node represents the total hash of the data set. The tree is managed off-chain, with only the root hash being published to the contract by the owner. Multiple roots can be published, each representing the tree’s state at a different point in time.
When users wish to make a claim, they need to query the off-chain service for their claim data, including the amount and proofs for onchain validation then submit it to the distributor contract. The contract uses this data to calculate the Merkle tree root and then compares it with the published roots in storage. If any one of the roots matches, the claim is valid, and the contract sends the token to the user.
The audit was performed using: (a) manual analysis of the codebase, (b) simulation of the smart contracts and (c) automation analysis of the smart contracts.
| Audit assessment | |
| Audit Type | Security Review |
| Initial Report | Feb 19, 2024 |
| Final Report | Feb 22, 2024 |
| Methods | Manual Review, Automated analysis |
| Repository | defispring |
| Commit Hash | 5ca2536c6c |
| Final Commit Hash | be3ca1f2b5 |
| Documentation | README.md |
| Documentation Assessment | High |
| Test Suite Assessment | High |
Findings by severity
| # | Contract | LoC | Comments | Ratio | Blank | Total |
| 1 | src/contract.cairo | 106 | 10 | 9.4% | 22 | 138 |
| 2 | src/lib.cairo | 2 | 0 | 0.0% | 0 | 2 |
| Total | 108 | 10 | 9.3% | 22 | 140 |
The class hash of the Distributor contract is 0x006a54af2934978ac59b27b91291d3da634f161fd5f22a2993da425893c44c64.
| # | Finding | Severity | Update |
| 1 | Hardcoded token address | Info | Acknowledged |
| 2 | Low trust on Alexandria Merkle Tree library | Info | Acknowledged |
| 3 | Possible hash collision when calculating the hash of leaves | Info | Fixed |
| 4 | Maintainable imports rather single line for every imports | Best Practices | Fixed |
| 5 | Unchecked return value for token transfer | Best Practices | Fixed |
| 6 | Use of Poisedon hashing instead of Pedersen | Best Practices | Fixed |
The Distributor contract facilitates the distribution of the STRK token to eligible participants through the use of a Merkle tree.
A Merkle tree is a binary tree where leaf nodes represent data, internal nodes represent hashed pairs of child nodes, and the root node represents the total hash of the data set. The tree is managed off-chain, with only the root hash being published to the contract by the owner. Multiple roots can be published, each representing the tree’s state at a different point in time.
When users wish to make a claim, they need to query the off-chain service for their claim data, including the amount and proofs for onchain validation then submit it to the distributor contract. The contract uses this data to calculate the Merkle tree root and then compares it with the published roots in storage. If any one of the roots matches, the claim is valid, and the contract sends the token to the user.
This contract comprises a concise set of functions:
1. claim(amount: u128, proof: Span::<felt252>): This enables users to claim a specified amount of tokens by providing a valid Merkle proof.
2. add_root(new_root: felt252): This is reserved for the contract owner to add a new Merkle root, thereby specifying eligibility for token claims.
3. get_root_for(claimee: ContractAddress, amount: u128, proof: Span::<felt252>): This retrieves the Merkle root associated with a particular user’s claim, along with the specified amount and corresponding Merkle proof.
4. amount_already_claimed(claimee: ContractAddress): This returns the amount of tokens already claimed by a specific user.
5. roots(): This returns the list of all Merkle roots currently registered in the contract.
The contract owner is responsible for adding Merkle roots by calling the add_root(...) function, with each root representing eligibility for token claims for individual users.
Users can claim their tokens using the claim(...) function along with valid Merkle proofs. Upon a successful claim, the allocation_claimed mapping is updated to prevent duplicate claims, ensuring each user can claim tokens only once per Merkle root.
The prepared security review is based on the following assumptions:
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.
| File | Fix commit | Type | Status |
contract.cairo | — | Info | Acknowledged |
The $STRK address is currently hardcoded to its address on the Sepolia testnet. This address may differ on the mainnet, which would require manual checks and code changes before deployment.
Recommendation
Consider injecting the $STRK address via the constructor.
Resolution
We have verified that the mainnet address will not differ from the testnet addresses. We want to avoid complicating the constructor to minimize room for error during deployment of the contract.
| File | Fix commit | Type | Status |
contract.cairo | — | Info | Acknowledged |
In the current setup, there exists an external dependency originating from the Alexandria library. This particular external dependency has not undergone an audit, which raises concerns about the overall integrity and reliability of the system.
The following code snippet is where the concern arises:
use alexandria_merkle_tree::merkle_tree::{
Hasher, MerkleTree, pedersen::PedersenHasherImpl, MerkleTreeTrait
};Recommendation
Consider monitoring this library for potential future issues. This can be done by regularly checking updates and changes made to the library. Also, consider conducting an audit of the library to ensure its integrity and reliability.
Resolution
Acknowledged. We will continue monitoring the library for potential future issues.
| File | Fix commit | Type | Status |
contract.cairo | 5f06c14be3ca1f | Info | Fixed |
Presently, the leaf of the Merkle tree is computed using the following code snippet:
let leaf = LegacyHash::hash(claimee.into(), amount);
merkle_tree.compute_root(leaf, proof)The function LegacyHash::hash() preprocesses the input values into felt252 before hashing. Consequently, the output of this function represents the Pedersen hash of two felt252 values. Similarly, the middle node in the Merkle tree also corresponds to the Pedersen hash of two felt252 values. Therefore, in theory, it is conceivable that an attacker could provide claimee as the left middle node and amount as the right middle node, leading to a situation where a valid claim could be made to drain the contract. However, this attack vector is contingent upon two conditions:
u128, as it is provided as the input parameter amount;Recommendation
Consider avoiding using leaf values that are two felt252 prior to hashing or using a different hashing function other than Pedersen for hashing leaves.
Resolution
Fixed in commit 5f06c147b42daf3685d3cb9a5f58c67edc757998 on branch audit-fixes by hashing leaves twice:
leaf = LegacyHash::hash(LegacyHash::hash(claimee.into(), amount), 1);Update from Nethermind Security
Hashing has changed, but the test data has not been updated. As a result, the test cases are failing.
Update from the client
Fixed in commit be3ca1f2b55e1de94399c29da854adb30184bebc on branch audit-fixes by using Poseidon for hashing the leaves.
| File | Fix commit | Type | Status |
contract.cairo | c91d05f | Best Practices | Fixed |
In the current coding style of the contract, individual external libraries are imported one by one, each on a single line. This approach, while functional, does not align with best coding practices. It is generally advised to consolidate these imports into a single line, where possible, to improve the readability and maintainability of the code.
To illustrate, here is a snapshot of how the external libraries are currently imported into the contract:
use openzeppelin::access::ownable::ownable::OwnableComponent::InternalTrait;
use openzeppelin::access::ownable::ownable::OwnableComponent;Recommendation
Instead of having multiple lines for each import, it would be more advantageous and in line with best practices to group them into a single line. This would not only make the code cleaner but also easier to manage and maintain.
The following is an example of how the imports can be grouped:
use openzeppelin::access::ownable::ownable::{OwnableComponent::InternalTrait,OwnableComponent}; // @audit - Best PracticeResolution
Fixed in commit c91d05feb3b5b7ca06bce3593144021bac2ebad9on branch audit-fixes.
| File | Fix commit | Type | Status |
contract.cairo | a20d0be | Best Practices | Fixed |
It is usually good to check the return value unless one is certain the given token will revert in the event of a failure. In this instance, the $STRK token is currently set to the testnet address, and the code can also be upgraded in the future.
token.transfer(claimee, u256 { high: 0, low: left_to_claim });Recommendation
Consider checking the return value of the token transfer call.
Resolution
Fixed in commit a20d0be7dc4ab5441770c3cd9a190a9444f1bb3f on branch audit-fixes
| File | Fix commit | Type | Status |
contract.cairo | 5f06c14 | Best Practices | Fixed |
The current hashing technique implemented in this contract is Pedersen hashing. Pedersen hashing, while effective in certain use cases, can be resource-intensive and may not always be the optimal choice. It is recommended to switch to Poseidon hashing, a different hashing algorithm that is known to be significantly less costly in terms of computational resources. This shift in technique can potentially improve the overall efficiency of the contract.
Recommendation
It is recommended to use Poseidon hashing as it is much more gas-efficient compared to Pedersen hashing.
Resolution
Fixed in commit 5f06c147b42daf3685d3cb9a5f58c67edc757998 on branch audit-fixes
Update from Nethermind Security
Commit 5f06c147 changed the hashing of both leaves and middle nodes to Poseidon in the backend side. However, on the contract side, only middle nodes are calculated using Poseidon, while leaves are still calculated using Pedersen (Legacy::hash() uses Pedersen as can be seen here).
Update from the client
Acknowledged. We will stick with Pedersen hashing for the tree because the backend is already stabilized, but use Poseidon for the leaves.
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 StarkNet Foundation has provided documentation about the distributor contract in the codebase comments and through the README file in the repository. The provided documentation is sufficient for the audit.
| 5Tests 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