Spiko
Starknet Contracts

Spiko issues tokenized shares in a money market fund. We reviewed the Cairo contracts that run those shares on Starknet.
report id
0333
category
Token & RWA
language
Cairo
final report
November 6, 2024
lines of code
413
Content

Download audit report

1

Executive Summary

This document presents the security review performed by Nethermind Security for Spiko smart contracts. The Spiko team has leveraged blockchain technology to issue and distribute securities. As such, they designed a protocol for managing tokenized shares in a money market fund. The security review was exclusively on their Cairo smart contracts to be deployed on Starknet.

This security review focuses exclusively on the smart contracts listed in Section 2 (Audited Files).

The audit was performed using (a) manual analysis of the codebase and (b) creation of test cases.

Audit assessment
Audit TypeSecurity Review
Initial ReportOct 25, 2024
Response from ClientRegular responses during audit engagement
Final ReportNov 6, 2024
Repositorystarknet-contracts
Commit (Audit)ac79ee9985
Commit (Final)ecb9ccb42c
Documentation AssessmentMedium
Test Suite AssessmentMedium
Total findings
4
Fixed
4
Acknowledged
0
Mitigated
0
Unresolved
0

Findings by severity

  • Critical0
  • High0
  • Medium0
  • Low2
  • Informational1
  • Best Practices1

2

Audited Files

#ContractLoCCommentsRatioBlankTotal
1redemption.cairo1992713.6%22248
2lib.cairo1632817.2%26217
3permission_manager.cairo451533.3%1070
4roles.cairo600.0%06
Total4137016.9%58541

4

System Overview

The Spiko protocol introduces a Cairo-based implementation of their original Solidity contracts, designed for managing tokenized shares in a money market fund. Spiko’s system isolates all investment activities outside these contracts, while minting, burning, and transferring shares are managed within the protocol, granting investors control over their fund shares.

The audit examined three primary upgradable contracts, which are described in detail in the following sections. Together, these contracts allow secure management of the fund’s shares.

4.1 Permission Manager

The Permission Manager contract oversees the entire management system of the roles. The protocol enforces a role-based access control mechanism, where each role grants specific privileges. Spiko uses six custom roles and one default administrative role in this contract. Each custom role corresponds to a unique functionality:

  • WHITELISTER_ROLE: Manages the whitelist, adding or removing users from the WHITELISTED_ROLE.
  • WHITELISTED_ROLE: Authorizes users to hold and manage shares.
  • MINTER_ROLE: Grants the ability to mint shares for whitelisted users.
  • BURNER_ROLE: Enables the burning of shares, initially assigned to the Redemption contract.
  • PAUSER_ROLE: Allows pausing the Token contract, preventing any new mints or burns.
  • REDEMPTION_EXECUTOR_ROLE: Manages the redemption process, with privileges to execute or cancel redemption requests.
  • DEFAULT_ADMIN_ROLE: Default admin role, which can grant all of the roles and manage the upgradability of the contract.

4.2 Token Contract and Redemption Contract

The Token contract functions as an ERC20 token representing shares in the money market fund. Only whitelisted users can receive these shares, which are minted by entities with the MINTER_ROLE. Becoming a whitelisted user requires passing a KYC process, supporting the protocol’s centralized design.

After users complete Spiko’s KYC process and any external asset transfers, they are whitelisted, and their shares are minted. Once shares are issued, users can manage them, including transfers and redemptions. Transfers can only occur between whitelisted users, and each transfer represents a legal event that requires user action outside the blockchain.

When users wish to redeem their shares, they initiate the process by calling the redeem(...) function within the Token contract, which is reflected in the Redemption contract where all redemption requests are managed:

fn redeem(ref self: ContractState, amount: u256, salt: felt252)

This function transfers shares to the Redemption contract, where the on_redeem(...) function creates a redemption request:

fn on_redeem(
ref self: ContractState,
token: ContractAddress,
from: ContractAddress,
amount: u256,
salt: felt252
)

The redemption request is stored in a mapping, with the request’s hash as the key and its status as the value. This status mechanism helps protect against replay attacks.

Once the request is created, an off-chain process is initiated, and the status of the redemption request is updated accordingly in the Redemption contract. Only an entity with the REDEMPTION_EXECUTOR_ROLE can update the request status by using one of two functions:

fn execute_redemption(
ref self: ContractState,
token: ContractAddress,
from: ContractAddress,
amount: u256,
salt: felt252
)
fn cancel_redemption(
ref self: ContractState,
token: ContractAddress,
from: ContractAddress,
amount: u256,
salt: felt252
)

The execute_redemption(...) function finalizes the redemption process by burning the shares, as the Redemption contract holds the BURNER_ROLE. This event also corresponds to the off-chain transfer of assets back to the user. Conversely, the cancel_redemption(...) function allows the protocol or the user to cancel the redemption request, transferring.

5

Risk Rating Methodology

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:

  1. High: the issue is trivial to exploit and has no specific conditions that need to be met;
  2. Medium: the issue is moderately complex and may have some conditions that need to be met;
  3. Low: the issue is very complex and requires very specific conditions to be met.

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:

  1. High: the issue can cause significant damage, such as loss of funds or the protocol entering an unrecoverable state;
  2. Medium: the issue can cause moderate damage, such as impacts that only affect a small group of users or only a particular part of the protocol;
  3. Low: the issue can cause little to no damage, such as bugs that are easily recoverable or cause unexpected interactions that cause minor inconveniences.

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 / LikelihoodLowMediumHigh
HighMediumHighCritical
MediumLowMediumHigh
LowInfo / Best PracticesLowMedium
UndeterminedUndeterminedUndeterminedUndetermined

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.

  1. Informational findings do not pose any risk to the application, but they carry some information that the audit team intends to pass to the client formally;
  2. Best Practice findings are used when some piece of code does not conform with smart contract development best practices;
  3. Undetermined findings are used when we cannot predict the impact or likelihood of the issue.

6

Issues

6.1 Transfers of shares should be pausable

FileFix commitTypeStatus
lib.cairoc1beaa9LowFixed

The Token contract implements the PausableComponent, which allows the protocol to pause key operations such as minting and burning. These actions are tied to legal events that occur within the Spiko protocol, ensuring that certain operations align with off-chain legal processes. However, while minting and burning can be paused, share transfers are not currently restricted during a pause.

This issue arises because the before_update(...) hook function does not check the paused state of the protocol. As a result, even when the protocol is paused, users can transfer their shares. This creates an inconsistency, as share transfers may trigger legal events without any way to pause them in sync with the protocol’s pause state.

Recommendation

Consider adding a check for the paused state in the before_update(...) hook function.

Resolution

c1beaa92950af6aa4320a14fb765497b087ee640

6.2 Users can renounce their WHITELISTED_ROLE, violating key protocol invariants

FileFix commitTypeStatus
permission_manager.cairoecb9ccbLowFixed

Only whitelisted users are allowed to hold shares represented by the Token contract. This whitelist is enforced by the before_update(...) hook, ensuring that minting and transferring of shares are restricted to users with the WHITELISTED_ROLE. The WHITELISTED_ROLE is managed by the PermissionManager contract, which uses OpenZeppelin’s AccessControlComponent. The role is assigned by users with the WHITELISTER_ROLE.

However, users can renounce their WHITELISTED_ROLE using the public renounce_role(...) function. This can lead to two potential issues:

1. A non-whitelisted user may end up holding shares, violating a key protocol invariant that only whitelisted users can own shares; 2. The REDEMPTION_EXECUTOR_ROLE cannot cancel a redemption request if the user is no longer whitelisted, which may disrupt the redemption process;

These issues can be avoided by preventing users from renouncing their WHITELISTED_ROLE.

Recommendation

Consider disallowing users with the WHITELISTED_ROLE from renouncing their role.

Resolution

ecb9ccb42cb85dd3c0051458da03c8db1e74c6cc

6.3 Zero amount redemption should be forbidden

FileFix commitTypeStatus
lib.cairo538d809InfoFixed

The redemption process for shares occurs in two steps. First, the user calls redeem(...), transferring the specified amount of shares to the Redemption contract. Then, a user with the REDEMPTION_EXECUTOR_ROLE either executes or cancels the redemption request.

However, users can currently initiate a redemption for zero shares, which leads to unnecessary gas costs for the protocol, as the request still requires execution or cancellation (Note: However, the protocol can ignore such a request). Additionally, the Spiko documentation specifies a minimum redemption amount of $1. To align with this requirement, once the Oracle is implemented, it would be beneficial to enforce a minimum share redemption threshold at the smart contract level.

Recommendation

Consider prohibiting redemptions for zero shares to prevent unnecessary requests.

Resolution

538d80942b3131959f8d43a066ab92b7ff54206a

6.4 Implement two-step transfer of ownership

FileFix commitTypeStatus
lib.cairo
redemption.cairo
5cdb0d3Best PracticesFixed

The Token and Redemption contracts use the OwnableMixinImpl component from OpenZeppelin, which handles access control for the contract owner. Given that the protocol is highly centralized, additional safeguards should be in place to prevent accidental or unauthorized transfers of ownership to unintended addresses.

A widely recommended practice is to implement a two-step ownership transfer, as provided by the OwnableTwoStepMixinImpl component. This approach requires the new owner to explicitly accept the ownership before the transfer is finalized, reducing the risk of mistakenly assigning ownership to an incorrect address.

Recommendation

Consider replacing the current OwnableMixinImpl with OwnableTwoStepMixinImpl to introduce a safer, two-step ownership transfer process.

Resolution

5cdb0d324566d83695a6ddc9c58ba232c6d15322

7

Documentation Evaluation

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:

  • Technical whitepaper: A technical whitepaper is a comprehensive document describing the smart contract’s design and technical details. It includes information about the purpose of the contract, its architecture, its components, and how they interact with each other;
  • User manual: A user manual is a document that provides information about how to use the smart contract. It includes step-by-step instructions on how to perform various tasks and explains the different features and functionalities of the contract;
  • Code documentation: Code documentation is a document that provides details about the code of the smart contract. It includes information about the functions, variables, and classes used in the code, as well as explanations of how they work;
  • API documentation: API documentation is a document that provides information about the API (Application Programming Interface) of the smart contract. It includes details about the methods, parameters, and responses that can be used to interact with the contract;
  • Testing documentation: Testing documentation is a document that provides information about how the smart contract was tested. It includes details about the test cases that were used, the results of the tests, and any issues that were identified during testing;
  • Audit documentation: Audit documentation includes reports, notes, and other materials related to the security audit of the smart contract. This type of documentation is critical in ensuring that the smart contract is secure and free from vulnerabilities.

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.

8

Remarks & Test Suite

The Spiko team provided comprehensive documentation that outlines the various user flows within the protocol. Additionally, the Spiko team has effectively addressed concerns and questions raised by the Nethermind Security team during their regular calls.

Remarks about Spiko test suite

The Spiko team has developed a test suite that covers the protocol’s basic flows. However, it currently does not include a variety of flows and execution scenarios. We recommend enhancing the test suite to cover all system specifications thoroughly.

Test suite
23Tests passed0Failed

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.