# 1. Introduction
iosiro was commissioned by Nexus Mutual to conduct a smart contract audit on their Emergency Response contract upgrade. The audit was performed by one auditor in six working days, between 3 and 11 August 2021. A review of changes made to address findings was conducted on 17 August 2021.
This report is organized into the following sections.
* **[Section 2 - Executive summary:](#section-2)** A high-level description of the findings of the audit.
* **[Section 3 - Audit details:](#section-3)** A description of the scope and methodology of the audit.
* **[Section 4 - Design specification:](#section-4)** An outline of the intended functionality of the smart contracts.
* **[Section 5 - Detailed findings:](#section-5)** Detailed descriptions of the findings of the audit.
The information in this report should be used to better understand the risk exposure of the smart contracts, and as a guide to improving the security posture of the smart contracts by remediating issues identified. The results of this audit are only a reflection of the source code reviewed at the time of the audit and of the source code that was determined to be in-scope.
The purpose of this audit was to achieve the following:
* Identify potential security flaws.
* Ensure that the smart contracts functioned according to the documentation provided.
Assessing the off-chain functionality associated with the contracts, for example, backend web application code, was out of scope of this audit.
Due to the unregulated nature and ease of transfer of cryptocurrencies, operations that store or interact with these assets are considered very high risk with regards to cyber attacks. As such, the highest level of security should be observed when interacting with these assets. This requires a forward-thinking approach, which takes into account the new and experimental nature of blockchain technologies. Strategies that should be used to encourage secure code development include:
* Security should be integrated into the development lifecycle and the level of perceived security should not be limited to a single code audit.
* Defensive programming should be employed to account for unforeseen circumstances.
* Current best practices should be followed where possible.
# 2. Executive summary
This report presents the findings of an audit performed by iosiro on Nexus Mutual's Emergency Response contract upgrade. This upgrade included the following changes:
* Redesign of the contract pause mechanism.
* Refactor of contract upgrade functionality.
* Minor refactoring and code clean-up.
#### Audit findings
iosiro made a number of design comments intended to improve code readability and efficiency, a number of which were implemented. No security risks were identified. Overall, the code was found to be of a high standard and accorded with the specification provided.
# 3. Audit details
## 3.1 Scope
The source code considered in-scope for the assessment is described below. Code from all other files was considered to be out-of-scope. Out-of-scope code that interacts with in-scope code was assumed to function as intended and not introduce any functional or security vulnerabilities.
### 3.1.1 Smart contracts
* **Project name:** Nexus Mutual Smart Contracts
* **Initial audit commit:** [06d9e12](https://github.com/NexusMutual/smart-contracts/commit/06d9e12e2e7462359be16f8b78ce02518b92238b)
* **Final review commit:** [c13ddea](https://github.com/NexusMutual/smart-contracts/commit/c13ddea14cb8300f49148738e7fe8dad74259eb7)
* **Files:** MCR.sol, ClaimsReward.sol, Incidents.sol, Governance.sol, NXMaster.sol
## 3.2 Methodology
A variety of techniques, described below, were used to conduct the audit.
### 3.2.1 Code review
The source code was manually inspected to identify potential security flaws. Code review is a useful approach for detecting security flaws, discrepancies between the specification and implementation, design improvements, and high risk areas of the system.
### 3.2.2 Dynamic analysis
The contracts were compiled, deployed, and tested in a test environment, both manually and through the test suite provided. Manual analysis was used to confirm that the code was functional and to discover whether any potential security issues identified could be exploited.
### 3.2.3 Automated analysis
Tools were used to automatically detect the presence of several types of security vulnerabilities, including reentrancy, timestamp dependency bugs, and transaction-ordering dependency bugs. The static analysis results were manually reviewed and any false positives were removed from the results. Any true positive results were included in this report.
Static analysis tools commonly used include Slither, Securify, and MythX. Tools such as the Remix IDE, compilation output, and linters could also be used to identify potential areas of concern.
## 3.3 Risk ratings
Each issue identified during the audit has been assigned a risk rating. The rating is determined based on the criteria outlined below.
* **High risk** - The issue could result in a loss of funds for the contract owner or system users.
* **Medium risk** - The issue resulted in the code specification being implemented incorrectly.
* **Low risk** - A best practice or design issue that could affect the security of the contract.
* **Informational** - A lapse in best practice or a suboptimal design pattern that has a minimal risk of affecting the security of the contract.
* **Closed** - The issue was identified during the audit and has since been addressed to a satisfactory level to remove the risk that it posed.
# 4. Design specification
Nexus Mutual's Emergency Response release consists of three components, detailed below.
## Contract pause mechanism
Previously, the Nexus Mutual contracts used a time-based pause mechanism which could be triggered by a successful governance proposal. To enable rapid emergency response, this has now been changed to a role-based mechanism. An `emergencyAdmin` address, appointed by a successful governance proposal, will have the power to pause and unpause the system at will, by calling `NXMaster.setEmergencyPause(bool)` with the appropriate parameters. The role does not have any additional abilities. Governance can replace the existing `emergencyAdmin` through an additional successful proposal.
When the system is paused, the following actions will not be possible:
* Locking tokens for claim assessment.
* Adding additional tokens to an existing claim assessment lock.
* Submitting and casting claim assessment votes.
* Changing a claim status.
* Claiming rewards.
* Delegating and undelegating votes.
* Membership functions, such as joining and leaving the mutual, or changing addresses.
* Purchasing cover.
* Transferring assets to and from the Pool and SwapOperator.
* Buying and selling NXM tokens.
* Depositing, staking, unstaking and withdrawing NXM tokens.
* Withdrawing staking rewards.
* Processing pending burn and reward actions.
The following previously unrestricted actions have been included with the above in this release:
* Updating the MCR.
* Redeeming an incident payout.
## Contract upgrade functionality
Contract upgrade functionality has been streamlined and augmented with new functions, and contract upgrade terminology has been clarified. Contracts have one of two upgrade types:
* **Proxy** contracts are upgraded by defining a new implementation in an `OwnedUpgradabilityProxy`.
* **Replaceable** contracts are upgraded by deploying a new contract and updating the relevant dependency references.
As before, contracts are denoted by contract codes. The main upgrade functions added are:
* `upgradeMultipleContracts(...)`: This can be called to upgrade a mixed set of replaceable and proxy-upgradeable contracts.
* `addNewInternalContracts(...)`: This can be called to add multiple new contracts to the system at once.
* `removeContracts(...)`: This can be called to remove contracts from the system.
As before, contract upgrades are handled through governance votes.
## Refactoring and code cleanup
* State variables have been renamed for clarity and made public.
* The `closeClaim(...)` function has been moved from `NXMaster` to `ClaimsReward`.
* Unused variables in proxy-upgradable contracts have been renamed `_unusedX` (where X is a count from 0) to preserve memory layout.
# 5. Detailed findings
The following section details the findings of the audit.
## 5.1 High risk
No high risk issues were present at the conclusion of the audit.
## 5.2 Medium risk
No medium risk issues were present at the conclusion of the audit.
## 5.3 Low risk
No low risk issues were present at the conclusion of the audit.
## 5.4 Informational
No informational issues were present at the conclusion of the audit.
### 5.4.1 Design comments
Actions to improve the functionality and readability of the codebase are outlined below.
#### Refactoring suggestions
1. [NXMaster.sol:22](https://github.com/NexusMutual/smart-contracts/blob/06d9e12e2e7462359be16f8b78ce02518b92238b/contracts/modules/governance/NXMaster.sol#L22),(https://github.com/NexusMutual/smart-contracts/blob/06d9e12e2e7462359be16f8b78ce02518b92238b/contracts/modules/governance/NXMaster.sol#L34): Unused variables defined to preserve storage layout should be given `private` visibility to prevent Solidity from creating unnecessary getter methods. This will reduce contract size and improve interface hygiene.
2. [NXMaster.sol:233](https://github.com/NexusMutual/smart-contracts/blob/06d9e12e2e7462359be16f8b78ce02518b92238b/contracts/modules/governance/NXMaster.sol#L233): As the `paused` state variable has `public` visibility, Solidity will [automatically create a getter for it](https://docs.soliditylang.org/en/v0.5.3/contracts.html#getter-functions). Thus, the `isPause()` function could be removed and this getter used instead, reducing contract size. This would, however, require an upgrade of contracts using `isPause()` directly, which may be undesirable at this stage.
#### Function naming conventions
The following functions should be named with a leading underscore (`_`) as they have `internal` visibility:
1. [NXMaster.sol:77](https://github.com/NexusMutual/smart-contracts/blob/06d9e12e2e7462359be16f8b78ce02518b92238b/contracts/modules/governance/NXMaster.sol#L77): `addNewInternalContract`
2. [NXMaster.sol:147](https://github.com/NexusMutual/smart-contracts/blob/06d9e12e2e7462359be16f8b78ce02518b92238b/contracts/modules/governance/NXMaster.sol#L147): `replaceContract`
3. [NXMaster.sol:206](https://github.com/NexusMutual/smart-contracts/blob/06d9e12e2e7462359be16f8b78ce02518b92238b/contracts/modules/governance/NXMaster.sol#L206): `updateAllDependencies`
## 5.5 Closed
### 5.5.1 Design comments
Actions to improve the functionality and readability of the codebase are outlined below.
#### Refactoring suggestions
1. [ClaimsReward.sol:71](https://github.com/NexusMutual/smart-contracts/blob/06d9e12e2e7462359be16f8b78ce02518b92238b/contracts/modules/claims/ClaimsReward.sol#L71): [In Solidity version 0.7.0, the `now` keyword was deprecated.](https://solidity.readthedocs.io/en/v0.7.0/070-breaking-changes.html) Developers are encouraged to use `block.timestamp` instead to ensure forward compatibility.
1. Implemented in [7223f80](https://github.com/NexusMutual/smart-contracts/commit/7223f801cfc17981fc14d6907b9f5656cb1cdc80).
#### Comment clarity
1. [NXMaster.sol:76](https://github.com/NexusMutual/smart-contracts/blob/06d9e12e2e7462359be16f8b78ce02518b92238b/contracts/modules/governance/NXMaster.sol#L76): The word "upgradable" should be replaced with "replaceable" in this comment, to align with the new terminology.
2. [NXMaster.sol:232](https://github.com/NexusMutual/smart-contracts/blob/06d9e12e2e7462359be16f8b78ce02518b92238b/contracts/modules/governance/NXMaster.sol#L232): "id" should be "is".
Both suggestions were implemented in [ce5d818](https://github.com/NexusMutual/smart-contracts/commit/ce5d8182e8cd7dd80c35adc180ecc2d843d04cd5).