Reach out for an audit or to learn more about Macro
or Message on Telegram

Nori 2

Security Audit

February 14, 2023

Version 1.0.0

Presented by 0xMacro

Table of Contents

Introduction

This document includes the results of the security audit for Nori's smart contract code as found in the section titled ‘Source Code’. The security audit was performed by the Macro security team from January 9, 2022 to January 13, 2022.

The purpose of this audit is to review the source code of certain Nori 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.

Overall Assessment

The following is an aggregation of issues found by the Macro Audit team:

Severity Count Acknowledged Won't Do Addressed
Medium 1 - - 1
Low 2 - - 2
Code Quality 4 - - 4
Informational 1 - - -

Nori was quick to respond to these issues.

Specification

Our understanding of the specification was based on the following sources:

Source Code

The following source code was reviewed during the audit:

Specifically, we audited the following contracts within this repository:

Source Code SHA256
contracts/Certificate.sol

492957970c6f3fd92f61bf4e8848367bd0825832b334b14992129a1e57d3146b

contracts/Errors.sol

a5cb5dc6126ad841f8982315b57a771e45da7bdac1b9043b9c380f05fcc738a8

contracts/ICertificate.sol

986ee21e44f0453a4196cd599d91542240003029e0dfdd48cd7feb6ba1264bfc

contracts/IMarket.sol

5e83ef1c43c154274d4457d452267a6db61e4184fe831d4edf770011b68b9964

contracts/IRemoval.sol

e7cd030a213ca08354f8780bb9f7d081b0de8b5044648f319cfffe1af44c321f

contracts/IRestrictedNORI.sol

4fbbca2b2b3a7b987211a720253a5acfe517fef298043c373648489efb87e55e

contracts/Market.sol

0cbef7c1a9f4f6bfc48af800a0bfd28df79ebf2451f54fe380ab56ba126ebf0c

contracts/Removal.sol

2a61d55c7f10266c8349b00fe2732075100ccdab4d4bfecf4bf22fa1d73029db

contracts/RestrictedNORI.sol

74e88e19d8b57b098a531714ff34a9f1416f383b0a1781d78bd2189fd116562c

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.

Issue Descriptions and Recommendations

Click on an issue to jump to it, or scroll down to see them all.

Security Level Reference

We quantify issues in three parts:

  1. The high/medium/low/spec-breaking impact of the issue:
    • How bad things can get (for a vulnerability)
    • The significance of an improvement (for a code quality issue)
    • The amount of gas saved (for a gas optimization)
  2. The high/medium/low likelihood of the issue:
    • How likely is the issue to occur (for a vulnerability)
  3. The overall critical/high/medium/low severity of the issue.

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.

Issue Details

M-1

RestrictedNORI cannot be minted for suppliers with non-ERC1155 addresses

Topic
Error Recovery
Impact
High
Likelihood
Low
Only added accounting needed to enable a safe implementaiton in the future. This was done as the likelihood of needing the feature is low.

Market.sol currently allows for RestrictedNORI to fail to be minted for a supplier if their address is not ERC1155 compatible during the Removal purchasing workflow (see the try catch block in the fulfillOrderXXX() functions). Nori’s plan is to allow for suppliers to have the failed-to-be-minted RestrictedNORI minted to a different address for the suppliers.

This plan isn’t currently achievable with the code as written. RestrictedNORI.sol doesn’t allow for RestrictedNORI to be minted to an arbitrary address as the mint() function on line 451 pulls the receiver for the mint from the removalID's encoding:

address supplierAddress = RemovalIdLib.supplierAddress({
    removalId: removalId
  });
super._mint({to: supplierAddress, id: projectId, amount: amount, data: ""});

Remediations to Consider:

  • Adding a function to RestrictedNORI.sol with a parameter for the mint receiver.

    Note: we’d also like to recommend that this function only be able to mint the amount of RestrictedNORI which corresponds to the amount of RestrictedNORI which failed to be minted in the Market.sol workflow. This could be achieved by recording in RestrictedNORI.sol the amount of RestrictedNORI that should’ve been minted during the catch of the try-catch blocks. This would promote safer accounting and prevent Nori from accidentally minting too-much RestrictedNORI for a supplier.

  • Ensuring that suppliers are aware that they should use addresses which are able to handle ERC1155 tokens.

L-1

Certificate is missing accounting to indicate current backing of Removals

Topic
Protocol Design
Status
Impact
Medium
Likelihood
High
Additionally added mentioned replacement logic which 0xMacro audited as a part of this fix. Commit points to this new feature.

Nori’s Certificate contract is intended to show that the Certificate-NFTs are backed by Removals. Currently, there isn’t a way to easily validate on-chain that the Certificate contract is properly backed.

For example, after release events occur which release Removals that were backing the Certificates, it’s hard to show that the Certificate contract is no longer properly backed. This will make it harder to show in the future, once replacement logic is written, that the Certificate contract and its Certificate-NFTs are once again properly backed.

Remediations to consider:

  • Adding accounting variables to show how many Removals are missing from the Certificate’s backing.
L-2

purchasingToken.transferFrom() return value is not checked

Topic
3rd Party Behavior
Status
Impact
High
Likelihood
Low

There is no guarantee that the IERC20WithPermit _purchasingToken will inherit OZ’s ERC20 which reverts on failure to transfer. The transfer could be unsuccessful and return false, and the recipient will still be minted a Certificate token even though the removal was not successfully purchased.

Checking return values is a good practice to prevent unforeseeable situations, like USDC upgrading their contracts to an implementation which returns false instead of reverting.

Remediations to Consider:

  • Consider checking the return value of _purchasingToken.transferFrom() so that suppliers are guaranteed to receive their earned tokens and the recipient is not minted a Certificate token that is unearned.
Q-1

Market.sol’s _fulfillOrder() and _fulfillOrderWithoutFee() could be combined

Topic
Code Duplication
Status
Quality Impact
Low

The only difference between the two functions is the following in the without-fee variant on lines 1326-1330:

_purchasingToken.transferFrom({ 
  from: from, 
  to: _noriFeeWallet, 
  amount: this.calculateNoriFee(removalAmounts[i])  
});

Remediations to consider:

  • Lifting this logic outside of the function to be done once on the total certificateAmount instead of on the individual supplier removal amounts. This would allow the _fulfillOrder() logic to be re-used.
Q-2

Market.sol uses the non-upgradeable version of OpenZeppelin’s Math library for uint256 instead of the upgradable version.

Topic
Upgradeability
Status
Quality Impact
Low

Remediations to consider:

  • Using OpenZeppelin’s MathUpgradeable library instead.
Q-3

Missing comments for purchasingTokenAddress and priceMultiple

Topic
Documentation
Status
Quality Impact
Low

In Certificate.sol, there are 2 functions that are missing comments regarding purchasingTokenAddress and priceMultiple:

  1. _receieveRemovalBatch()
    • This function accepts recipient, certificateAmount, removalsIds, removalAmounts, purchasingTokenAddress, and priceMultiple as parameters. However, there are missing Natspec @param tags for purchasingTokenAddress and priceMultiple.
  2. onERC1155BatchReceived()
    • This function accepts data as a parameter which encodes recipient, certificateAmount, purchasingTokenAddress, and priceMultiple. However, the Natspec comment does not mention the purchasingTokenAddress or priceMultiple, and instead states: @param data The bytes that encode the certificate's recipient address and total amount.

Remediations to consider:

  • Consider expanding the comments to include information on purchasingTokenAddress and priceMultiple.
Q-4

RestrictedNORI documentation out of date

Topic
Documentation
Status
Quality Impact
Low

RestrictedNORI.sol’s documentation states:

It is possible to create more than one schedule per owner address and also to transfer full or partial restricted balances between addresses.

However, restricted Nori token transfers are disabled as of this update.

Remediation to Consider:

  • Update the spec to mention that restricted Nori balances cannot be transferred, yet it might be possible in a future release.
I-1

Market.sol’s _permit() function can have functionality extended

Topic
Use Cases
Impact
Informational

Currently, Market.sol’s _permit() function requires that the transaction initiator also be the signer of the signed message:

_purchasingToken.permit({
    owner: _msgSender(),
    spender: address(this),
    value: amount,
    deadline: deadline,
    v: v,
    r: r,
    s: s
});

This design is slightly different from the typical use cases of signed messages. Many projects allow signed messages to be submitted by accounts other than the message signer to enable other parties to (1) pay for gas costs and (2) allow for other custom business logic.

Nori could use this pattern to enable things like allowing other parties to fund buyers’ gas costs or enabling other accounts besides the MARKET_ADMIN_ROLE to fund the no-fee swap endpoints.

Note, that if this approach is introduced, it should be communicated to users that if they sign messages with the Market contract as the spender of the purchasing tokens, that the user is giving permission for a purchase to be made with the amount of permitted token in Market.sol.

Response by Nori

We took your advice!

commits: 0b859b4f5712525a02decb9ee05bdcc6a628f056, a8a17a90801531f64379f56428b08c28adf32a29

Disclaimer

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 Nori 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.